Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.

Commit d22194d

Browse files
authored
Merge pull request #6 from payid-org/misc
Add actual utility functions to use for implementing PayID-specific functions. Context: Both the PayID server and the Xpring SDK have this isAscii function, and it seems like a isValidUrl function would be useful as well.
2 parents b2d5d1b + aa81e2c commit d22194d

File tree

7 files changed

+166
-1
lines changed

7 files changed

+166
-1
lines changed

package-lock.json

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "payid-utils",
2+
"name": "@payid-org/payid-utils",
33
"version": "0.1.0",
44
"description": "A TypeScript library providing PayID utility functions",
55
"homepage": "https://github.com/payid-org/payid-utils#readme",
@@ -26,11 +26,13 @@
2626
"devDependencies": {
2727
"@arkweid/lefthook": "^0.7.2",
2828
"@fintechstudios/eslint-plugin-chai-as-promised": "^3.0.2",
29+
"@types/chai": "^4.2.11",
2930
"@types/mocha": "^7.0.2",
3031
"@types/node": "^14.0.14",
3132
"@typescript-eslint/eslint-plugin": "^3.5.0",
3233
"@typescript-eslint/parser": "^3.5.0",
3334
"@xpring-eng/eslint-config-base": "^0.9.0",
35+
"chai": "^4.2.0",
3436
"eslint": "^7.3.1",
3537
"eslint-plugin-array-func": "^3.1.6",
3638
"eslint-plugin-eslint-comments": "^3.2.0",

src/.gitkeep

Whitespace-only changes.

src/utils.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Validate if the input is ASCII based text.
3+
*
4+
* Shamelessly taken from:
5+
* https://stackoverflow.com/questions/14313183/javascript-regex-how-do-i-check-if-the-string-is-ascii-only.
6+
*
7+
* @param input - The input to check.
8+
*
9+
* @returns A boolean indicating the result.
10+
*/
11+
export function isAscii(input: string): boolean {
12+
// eslint-disable-next-line no-control-regex -- The ASCII regex uses control characters
13+
return /^[\x00-\x7F]*$/u.test(input)
14+
}
15+
16+
/**
17+
* Validate if the given input is a valid URL.
18+
*
19+
* @param input - A string that may or may not be a valid URL.
20+
*
21+
* @returns A boolean indicating whether or not the input string was a valid URL.
22+
*/
23+
export function isValidUrl(input: string): boolean {
24+
// Check that there is at least the possibility of a TLD existing
25+
if (!input.includes('.')) {
26+
return false
27+
}
28+
29+
try {
30+
// eslint-disable-next-line no-new -- Explicitly using new URL to see if a TypeError is thrown.
31+
new URL(input)
32+
} catch {
33+
return false
34+
}
35+
36+
return true
37+
}

test/.gitkeep

Whitespace-only changes.

test/unit/utils/isAscii.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'mocha'
2+
import { assert } from 'chai'
3+
4+
import { isAscii } from '../../../src/utils'
5+
6+
describe('isAscii()', function (): void {
7+
it('Returns true for a string that is all ASCII', function () {
8+
// GIVEN an ASCII string
9+
const asciiString = 'johndoe'
10+
11+
// WHEN the string is determined to be ASCII
12+
const isStringAscii = isAscii(asciiString)
13+
14+
// THEN we expect to get true back
15+
assert.strictEqual(isStringAscii, true)
16+
})
17+
18+
it('Returns false for a string that contains non-ASCII characters', function () {
19+
// GIVEN an non-ASCII string
20+
const nonAsciiString = 'example.网络'
21+
22+
// WHEN we see if the string is ASCII
23+
const isStringAscii = isAscii(nonAsciiString)
24+
25+
// THEN we expect to get false back
26+
assert.strictEqual(isStringAscii, false)
27+
})
28+
})

test/unit/utils/isValidUrl.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import 'mocha'
2+
import { assert } from 'chai'
3+
4+
import { isValidUrl } from '../../../src/utils'
5+
6+
describe('isValidUrl()', function (): void {
7+
it('Returns true for a string that is a valid URL', function () {
8+
// GIVEN a valid URL string
9+
const validUrl = 'https://example.com'
10+
11+
// WHEN we see if the URL string is a valid URL
12+
const isUrlValid = isValidUrl(validUrl)
13+
14+
// THEN we expect to get true back
15+
assert.strictEqual(isUrlValid, true)
16+
})
17+
18+
it('Returns false for a URL without a protocol', async function () {
19+
// GIVEN an URL without a protocol
20+
const invalidUrl = 'example.com'
21+
22+
// WHEN we see if the string is a valid URL
23+
const isUrlValid = isValidUrl(invalidUrl)
24+
25+
// THEN we expect to get false back
26+
assert.strictEqual(isUrlValid, false)
27+
})
28+
29+
it('Returns false for a URL without a TLD', async function () {
30+
// GIVEN an URL without a TLD
31+
const invalidUrl = 'https://example'
32+
33+
// WHEN we see if the string is a valid URL
34+
const isUrlValid = isValidUrl(invalidUrl)
35+
36+
// THEN we expect to get false back
37+
assert.strictEqual(isUrlValid, false)
38+
})
39+
})

0 commit comments

Comments
 (0)