Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92b8af9
commit 99e40c0
Showing
3 changed files
with
108 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2021 Signal Messenger, LLC | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import { assert } from 'chai'; | ||
|
||
import * as Username from '../../types/Username'; | ||
|
||
describe('Username', () => { | ||
describe('getUsernameFromSearch', () => { | ||
const { getUsernameFromSearch } = Username; | ||
|
||
it('matches invalid username searches', () => { | ||
assert.strictEqual(getUsernameFromSearch('username!'), 'username!'); | ||
assert.strictEqual(getUsernameFromSearch('1username'), '1username'); | ||
assert.strictEqual(getUsernameFromSearch('use'), 'use'); | ||
assert.strictEqual( | ||
getUsernameFromSearch('username9012345678901234567'), | ||
'username9012345678901234567' | ||
); | ||
}); | ||
|
||
it('matches valid username searches', () => { | ||
assert.strictEqual(getUsernameFromSearch('username_34'), 'username_34'); | ||
assert.strictEqual(getUsernameFromSearch('u5ername'), 'u5ername'); | ||
assert.strictEqual(getUsernameFromSearch('user'), 'user'); | ||
assert.strictEqual( | ||
getUsernameFromSearch('username901234567890123456'), | ||
'username901234567890123456' | ||
); | ||
}); | ||
|
||
it('matches valid and invalid usernames with @ prefix', () => { | ||
assert.strictEqual(getUsernameFromSearch('@username!'), 'username!'); | ||
assert.strictEqual(getUsernameFromSearch('@1username'), '1username'); | ||
assert.strictEqual(getUsernameFromSearch('@username_34'), 'username_34'); | ||
assert.strictEqual(getUsernameFromSearch('@u5ername'), 'u5ername'); | ||
}); | ||
|
||
it('matches valid and invalid usernames with @ suffix', () => { | ||
assert.strictEqual(getUsernameFromSearch('username!@'), 'username!'); | ||
assert.strictEqual(getUsernameFromSearch('1username@'), '1username'); | ||
assert.strictEqual(getUsernameFromSearch('username_34@'), 'username_34'); | ||
assert.strictEqual(getUsernameFromSearch('u5ername@'), 'u5ername'); | ||
}); | ||
|
||
it('does not match something that looks like a phone number', () => { | ||
assert.isUndefined(getUsernameFromSearch('+')); | ||
assert.isUndefined(getUsernameFromSearch('2223')); | ||
assert.isUndefined(getUsernameFromSearch('+3')); | ||
assert.isUndefined(getUsernameFromSearch('+234234234233')); | ||
}); | ||
}); | ||
|
||
describe('isValidUsername', () => { | ||
const { isValidUsername } = Username; | ||
|
||
it('does not match invalid username searches', () => { | ||
assert.isFalse(isValidUsername('username!')); | ||
assert.isFalse(isValidUsername('1username')); | ||
assert.isFalse(isValidUsername('use')); | ||
assert.isFalse(isValidUsername('username9012345678901234567')); | ||
}); | ||
|
||
it('matches valid usernames', () => { | ||
assert.isTrue(isValidUsername('username_34')); | ||
assert.isTrue(isValidUsername('u5ername')); | ||
assert.isTrue(isValidUsername('_username')); | ||
assert.isTrue(isValidUsername('user')); | ||
assert.isTrue(isValidUsername('username901234567890123456')); | ||
}); | ||
|
||
it('does not match valid and invalid usernames with @ prefix or suffix', () => { | ||
assert.isFalse(isValidUsername('@username_34')); | ||
assert.isFalse(isValidUsername('@1username')); | ||
assert.isFalse(isValidUsername('username_34@')); | ||
assert.isFalse(isValidUsername('1username@')); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2021 Signal Messenger, LLC | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
export const MAX_USERNAME = 26; | ||
export const MIN_USERNAME = 4; | ||
|
||
export function isValidUsername(searchTerm: string): boolean { | ||
return /^[a-z_][0-9a-z_]{3,25}$/.test(searchTerm); | ||
} | ||
|
||
export function getUsernameFromSearch(searchTerm: string): string | undefined { | ||
if (/^[+0-9]+$/.test(searchTerm)) { | ||
return undefined; | ||
} | ||
|
||
const match = /^@?(.*?)@?$/.exec(searchTerm); | ||
|
||
if (match && match[1]) { | ||
return match[1]; | ||
} | ||
|
||
return undefined; | ||
} |