From 1c15708525152d9039e5617f9d637fe6e708d747 Mon Sep 17 00:00:00 2001 From: En-En <39373446+En-En-Code@users.noreply.github.com> Date: Mon, 2 Jan 2023 10:48:02 -0500 Subject: [PATCH 1/2] Support for historical accounts with trailing hyphens. Also added new unit test cases to confirm proper behavior and updated README.md. --- README.md | 8 +++++--- index.js | 2 +- module.js | 2 +- test.js | 15 ++++++++++----- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index b1dd226..bc0911e 100644 --- a/README.md +++ b/README.md @@ -11,14 +11,15 @@ import githubUsernameRegex from 'github-username-regex'; githubUsernameRegex.test('john'); //=> true githubUsernameRegex.test('john-due'); //=> true -githubUsernameRegex.test('john-due-'); //=> false +githubUsernameRegex.test('john--due'); //=> false ``` According to the form validation messages on [*Join Github*](https://github.com/join) page, * Github username may only contain alphanumeric characters or hyphens. * Github username cannot have multiple consecutive hyphens. -* Github username cannot begin or end with a hyphen. +* Github username cannot begin with a hyphen. +** Github previously allowed usernames to end with a hyphen, but does not anymore. * Maximum is 39 characters. ## Installation @@ -50,6 +51,7 @@ Type: [`RegExp`](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Regular githubUsernameRegex.test('a'); githubUsernameRegex.test('0'); githubUsernameRegex.test('a-b'); +githubUsernameRegex.test('a-b-'); githubUsernameRegex.test('a-b-123'); githubUsernameRegex.test('a'.repeat(39)); @@ -57,7 +59,7 @@ githubUsernameRegex.test('a'.repeat(39)); githubUsernameRegex.test(''); githubUsernameRegex.test('a_b'); githubUsernameRegex.test('a--b'); -githubUsernameRegex.test('a-b-'); +githubUsernameRegex.test('a-b--'); githubUsernameRegex.test('-a-b'); githubUsernameRegex.test('a'.repeat(40)); ``` diff --git a/index.js b/index.js index 6e33d24..fa12afe 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,3 @@ -var module$1 = /^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}$/i; +var module$1 = /^[a-z\d](?:[a-z\d]|-(?!-)){0,38}$/i; module.exports = module$1; diff --git a/module.js b/module.js index b79eed6..eeb5ce4 100644 --- a/module.js +++ b/module.js @@ -1 +1 @@ -export default /^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}$/i; +export default /^[a-z\d](?:[a-z\d]|-(?!-)){0,38}$/i; diff --git a/test.js b/test.js index c8546b0..c771853 100644 --- a/test.js +++ b/test.js @@ -5,22 +5,26 @@ const {strictEqual} = require('assert'); for (const validName of [ 'a', + 'A', 'abc', 'DEF', 'Ghi', 'a-z', 'a-b-c', + 'a-', '0', '10', '1-2', + '1-22-333-4444', 'abc123', 'abc-123', - 'x'.repeat(39) + 'x'.repeat(39), + 'a'.concat('b-'.repeat(19)) ]) { strictEqual( githubUsernameRegex.test(validName), true, - `Expected "${validName}" to be considiered as a valid Github username, but it wasn't.` + `Expected "${validName}" to be considered as a valid Github username, but it wasn't.` ); } @@ -31,8 +35,8 @@ for (const invalidName of [ 'a ', ' b', '-', - 'a-', '-b', + 'a--', 'a--b', 'a_b', 'a\nb', @@ -41,12 +45,13 @@ for (const invalidName of [ 'あ', '🍣', String.fromCharCode(15), - 'x'.repeat(40) + 'x'.repeat(40), + 'aa'.concat('b-'.repeat(19)) ]) { strictEqual( githubUsernameRegex.test(invalidName), false, - `Expected ${JSON.stringify(invalidName)} to be considiered as an invalid Github username, but it wasn't.` + `Expected ${JSON.stringify(invalidName)} to be considered as an invalid Github username, but it wasn't.` ); } From da906033b723c3ab555b31d7027e58893ba2a5cc Mon Sep 17 00:00:00 2001 From: En-En <39373446+En-En-Code@users.noreply.github.com> Date: Mon, 2 Jan 2023 10:51:15 -0500 Subject: [PATCH 2/2] Fix README.md indent --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bc0911e..31db9aa 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ According to the form validation messages on [*Join Github*](https://github.com/ * Github username may only contain alphanumeric characters or hyphens. * Github username cannot have multiple consecutive hyphens. * Github username cannot begin with a hyphen. -** Github previously allowed usernames to end with a hyphen, but does not anymore. + * Github previously allowed usernames to end with a hyphen, but does not anymore. * Maximum is 39 characters. ## Installation