Skip to content

Commit 74ae57d

Browse files
dguotunnckoCore
authored andcommitted
Handle more edge cases (#3)
1. Disallow underscores 2. Allow single character usernames 3. Allow multiple hyphens, as long as they aren't next to each other
1 parent 141660e commit 74ae57d

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ regex().test('foo-bar'); // => true
2121
regex().test('foobar'); // => true
2222
regex().test('3foobar'); // => true
2323
regex().test('3foo-bar'); // => true
24+
regex().test('foo-bar-baz'); // => true
25+
regex().test('f'); // => true
2426
regex().test('foo-bar-'); // => false
2527
regex().test('-foo-bar'); // => false
2628
regex().test('foo--bar'); // => false
2729
regex().test('~derp@darp---++asdf'); // => false
2830
regex().test('[email protected]'); // => false
31+
regex().test('foo_bar'); // => false
2932
```
3033

3134
## Why?

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
* Expose username regex, following github conventions
33
* like:
44
* _Username may only contain alphanumeric characters
5-
* and only single hyphen, and cannot begin or end with hyphen._
5+
* and only single hyphens, and cannot begin or end with a hyphen._
66
*
77
*
88
* Example input:
99
* foo
1010
* foo-bar
1111
*/
1212
module.exports = function regexUsername () {
13-
return /^\w+-?\w+(?!-)$/;
13+
return /^([a-z\d]+-)*[a-z\d]+$/i;
1414
};

test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@ describe('username regex', function() {
1111
it('should match username input', function() {
1212
assert.equal(regex().test('foobar'), true);
1313
assert.equal(regex().test('foo-bar'), true);
14+
assert.equal(regex().test('FooBar'), true);
15+
assert.equal(regex().test('FOOBAR'), true);
16+
assert.equal(regex().test('foo-bar-baz'), true);
17+
assert.equal(regex().test('f'), true);
1418
});
1519

1620
it('should catch incorrect input', function() {
1721
assert.equal(regex().test('-foo'), false);
1822
assert.equal(regex().test('foo-'), false);
23+
assert.equal(regex().test('-'), false);
1924
assert.equal(regex().test('foo--bar'), false);
2025
assert.equal(regex().test('foo-bar-'), false);
2126
assert.equal(regex().test('3tobi--ferret'), false);
2227
assert.equal(regex().test('~~derp@darp-----++asdfasdf'), false);
28+
assert.equal(regex().test('foo_bar'), false);
29+
assert.equal(regex().test('_foobar'), false);
30+
assert.equal(regex().test('foobar_'), false);
2331
});
2432
});

0 commit comments

Comments
 (0)