@20syldev/api 3.5.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierrc +7 -0
- package/README.md +32 -33
- package/dist/app.js +36 -0
- package/dist/app.js.map +1 -0
- package/dist/config/env.js +73 -0
- package/dist/config/env.js.map +1 -0
- package/dist/config/versions.js +72 -0
- package/dist/config/versions.js.map +1 -0
- package/dist/constants.js +42 -0
- package/dist/constants.js.map +1 -0
- package/dist/middleware/cors.js +15 -0
- package/dist/middleware/cors.js.map +1 -0
- package/dist/middleware/error.js +6 -0
- package/dist/middleware/error.js.map +1 -0
- package/dist/middleware/json.js +8 -0
- package/dist/middleware/json.js.map +1 -0
- package/dist/middleware/logger.js +26 -0
- package/dist/middleware/logger.js.map +1 -0
- package/dist/middleware/ratelimit.js +58 -0
- package/dist/middleware/ratelimit.js.map +1 -0
- package/dist/middleware/version.js +30 -0
- package/dist/middleware/version.js.map +1 -0
- package/dist/modules/v3/algorithms.js +221 -0
- package/dist/modules/v3/algorithms.js.map +1 -0
- package/{modules → dist/modules}/v3/captcha.js +4 -13
- package/dist/modules/v3/captcha.js.map +1 -0
- package/dist/modules/v3/chat.js +103 -0
- package/dist/modules/v3/chat.js.map +1 -0
- package/{modules → dist/modules}/v3/color.js +12 -13
- package/dist/modules/v3/color.js.map +1 -0
- package/dist/modules/v3/convert.js +41 -0
- package/dist/modules/v3/convert.js.map +1 -0
- package/dist/modules/v3/domain.js +83 -0
- package/dist/modules/v3/domain.js.map +1 -0
- package/dist/modules/v3/hash.js +16 -0
- package/dist/modules/v3/hash.js.map +1 -0
- package/dist/modules/v3/hyperplanning.js +50 -0
- package/dist/modules/v3/hyperplanning.js.map +1 -0
- package/dist/modules/v3/levenshtein.js +38 -0
- package/dist/modules/v3/levenshtein.js.map +1 -0
- package/dist/modules/v3/personal.js +129 -0
- package/dist/modules/v3/personal.js.map +1 -0
- package/dist/modules/v3/qrcode.js +21 -0
- package/dist/modules/v3/qrcode.js.map +1 -0
- package/dist/modules/v3/tic_tac_toe.js +221 -0
- package/dist/modules/v3/tic_tac_toe.js.map +1 -0
- package/dist/modules/v3/time.js +88 -0
- package/dist/modules/v3/time.js.map +1 -0
- package/{modules → dist/modules}/v3/token.js +10 -11
- package/dist/modules/v3/token.js.map +1 -0
- package/dist/modules/v3/username.js +92 -0
- package/dist/modules/v3/username.js.map +1 -0
- package/dist/modules/v3/utils.js +70 -0
- package/dist/modules/v3/utils.js.map +1 -0
- package/dist/modules/v3.js +16 -0
- package/dist/modules/v3.js.map +1 -0
- package/dist/modules/v4/algorithms.js +200 -0
- package/dist/modules/v4/algorithms.js.map +1 -0
- package/dist/modules/v4/captcha.js +35 -0
- package/dist/modules/v4/captcha.js.map +1 -0
- package/dist/modules/v4/chat.js +92 -0
- package/dist/modules/v4/chat.js.map +1 -0
- package/dist/modules/v4/color.js +43 -0
- package/dist/modules/v4/color.js.map +1 -0
- package/dist/modules/v4/convert.js +33 -0
- package/dist/modules/v4/convert.js.map +1 -0
- package/dist/modules/v4/dice.js +30 -0
- package/dist/modules/v4/dice.js.map +1 -0
- package/dist/modules/v4/domain.js +78 -0
- package/dist/modules/v4/domain.js.map +1 -0
- package/dist/modules/v4/encode.js +165 -0
- package/dist/modules/v4/encode.js.map +1 -0
- package/dist/modules/v4/geo.js +41 -0
- package/dist/modules/v4/geo.js.map +1 -0
- package/dist/modules/v4/hash.js +9 -0
- package/dist/modules/v4/hash.js.map +1 -0
- package/dist/modules/v4/hyperplanning.js +42 -0
- package/dist/modules/v4/hyperplanning.js.map +1 -0
- package/dist/modules/v4/levenshtein.js +29 -0
- package/dist/modules/v4/levenshtein.js.map +1 -0
- package/dist/modules/v4/palette.js +89 -0
- package/dist/modules/v4/palette.js.map +1 -0
- package/dist/modules/v4/personal.js +125 -0
- package/dist/modules/v4/personal.js.map +1 -0
- package/dist/modules/v4/placeholder.js +96 -0
- package/dist/modules/v4/placeholder.js.map +1 -0
- package/dist/modules/v4/qrcode.js +12 -0
- package/dist/modules/v4/qrcode.js.map +1 -0
- package/dist/modules/v4/statistics.js +41 -0
- package/dist/modules/v4/statistics.js.map +1 -0
- package/dist/modules/v4/text.js +293 -0
- package/dist/modules/v4/text.js.map +1 -0
- package/dist/modules/v4/tic_tac_toe.js +206 -0
- package/dist/modules/v4/tic_tac_toe.js.map +1 -0
- package/dist/modules/v4/time.js +73 -0
- package/dist/modules/v4/time.js.map +1 -0
- package/dist/modules/v4/token.js +36 -0
- package/dist/modules/v4/token.js.map +1 -0
- package/dist/modules/v4/username.js +87 -0
- package/dist/modules/v4/username.js.map +1 -0
- package/dist/modules/v4/utils.js +26 -0
- package/dist/modules/v4/utils.js.map +1 -0
- package/dist/modules/v4/validate.js +54 -0
- package/dist/modules/v4/validate.js.map +1 -0
- package/dist/modules/v4.js +18 -0
- package/dist/modules/v4.js.map +1 -0
- package/dist/routes/delete.js +65 -0
- package/dist/routes/delete.js.map +1 -0
- package/dist/routes/get.js +441 -0
- package/dist/routes/get.js.map +1 -0
- package/dist/routes/index.js +50 -0
- package/dist/routes/index.js.map +1 -0
- package/dist/routes/patch.js +41 -0
- package/dist/routes/patch.js.map +1 -0
- package/dist/routes/post.js +185 -0
- package/dist/routes/post.js.map +1 -0
- package/dist/storage/index.js +13 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/types/storage.js +2 -0
- package/dist/types/storage.js.map +1 -0
- package/dist/utils/response.js +10 -0
- package/dist/utils/response.js.map +1 -0
- package/dist/utils/version.js +3 -0
- package/dist/utils/version.js.map +1 -0
- package/docs/changelog.md +333 -0
- package/eslint.config.js +20 -0
- package/package.json +31 -15
- package/src/app.ts +48 -0
- package/src/config/env.ts +81 -0
- package/src/config/versions.ts +92 -0
- package/src/constants.ts +47 -0
- package/src/middleware/cors.ts +17 -0
- package/src/middleware/error.ts +7 -0
- package/src/middleware/json.ts +9 -0
- package/src/middleware/logger.ts +31 -0
- package/src/middleware/ratelimit.ts +66 -0
- package/src/middleware/version.ts +39 -0
- package/{modules → src/modules}/v3/algorithms.js +1 -1
- package/src/modules/v3/captcha.js +60 -0
- package/{modules → src/modules}/v3/chat.js +6 -4
- package/src/modules/v3/color.js +60 -0
- package/{modules → src/modules}/v3/convert.js +5 -5
- package/{modules → src/modules}/v3/domain.js +51 -4
- package/{modules → src/modules}/v3/hash.js +1 -1
- package/{modules → src/modules}/v3/hyperplanning.js +11 -10
- package/{modules → src/modules}/v3/levenshtein.js +3 -3
- package/{modules → src/modules}/v3/personal.js +20 -8
- package/{modules → src/modules}/v3/qrcode.js +1 -1
- package/{modules → src/modules}/v3/tic_tac_toe.js +24 -18
- package/{modules → src/modules}/v3/time.js +22 -11
- package/src/modules/v3/token.js +54 -0
- package/src/modules/v3/username.js +94 -0
- package/{modules → src/modules}/v3/utils.js +2 -2
- package/{modules → src/modules}/v3.js +1 -1
- package/src/modules/v4/algorithms.ts +197 -0
- package/src/modules/v4/captcha.ts +49 -0
- package/src/modules/v4/chat.ts +122 -0
- package/src/modules/v4/color.ts +64 -0
- package/src/modules/v4/convert.ts +38 -0
- package/src/modules/v4/dice.ts +39 -0
- package/src/modules/v4/domain.ts +81 -0
- package/src/modules/v4/encode.ts +170 -0
- package/src/modules/v4/geo.ts +53 -0
- package/src/modules/v4/hash.ts +9 -0
- package/src/modules/v4/hyperplanning.ts +58 -0
- package/src/modules/v4/levenshtein.ts +38 -0
- package/src/modules/v4/palette.ts +103 -0
- package/src/modules/v4/personal.ts +147 -0
- package/src/modules/v4/placeholder.ts +122 -0
- package/src/modules/v4/qrcode.ts +11 -0
- package/src/modules/v4/statistics.ts +55 -0
- package/src/modules/v4/text.ts +300 -0
- package/src/modules/v4/tic_tac_toe.ts +255 -0
- package/src/modules/v4/time.ts +92 -0
- package/src/modules/v4/token.ts +43 -0
- package/src/modules/v4/username.ts +89 -0
- package/src/modules/v4/utils.ts +38 -0
- package/src/modules/v4/validate.ts +55 -0
- package/src/modules/v4.ts +17 -0
- package/src/routes/delete.ts +72 -0
- package/src/routes/get.ts +508 -0
- package/src/routes/index.ts +58 -0
- package/src/routes/patch.ts +45 -0
- package/src/routes/post.ts +200 -0
- package/src/storage/index.ts +16 -0
- package/src/types/express.d.ts +17 -0
- package/src/types/storage.ts +48 -0
- package/src/utils/response.ts +11 -0
- package/src/utils/version.ts +5 -0
- package/tests/integration/api.test.ts +395 -0
- package/tests/tsconfig.json +3 -0
- package/tests/unit/algorithms.test.ts +120 -0
- package/tests/unit/color.test.ts +33 -0
- package/tests/unit/convert.test.ts +42 -0
- package/tests/unit/dice.test.ts +57 -0
- package/tests/unit/domain.test.ts +47 -0
- package/tests/unit/encode.test.ts +108 -0
- package/tests/unit/geo.test.ts +45 -0
- package/tests/unit/hash.test.ts +37 -0
- package/tests/unit/hyperplanning.test.ts +77 -0
- package/tests/unit/levenshtein.test.ts +34 -0
- package/tests/unit/palette.test.ts +53 -0
- package/tests/unit/personal.test.ts +65 -0
- package/tests/unit/statistics.test.ts +57 -0
- package/tests/unit/text.test.ts +107 -0
- package/tests/unit/time.test.ts +67 -0
- package/tests/unit/token.test.ts +61 -0
- package/tests/unit/username.test.ts +34 -0
- package/tests/unit/validate.test.ts +71 -0
- package/tsconfig.json +17 -0
- package/tsconfig.test.json +9 -0
- package/app.js +0 -865
- package/modules/v3/username.js +0 -32
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { test, describe } from 'node:test';
|
|
2
|
+
import { strict as assert } from 'node:assert';
|
|
3
|
+
import personal from '../../src/modules/v4/personal.js';
|
|
4
|
+
|
|
5
|
+
describe('personal', () => {
|
|
6
|
+
test('returns expected fields', () => {
|
|
7
|
+
const result = personal();
|
|
8
|
+
const fields = [
|
|
9
|
+
'name',
|
|
10
|
+
'email',
|
|
11
|
+
'phone',
|
|
12
|
+
'job',
|
|
13
|
+
'language',
|
|
14
|
+
'card',
|
|
15
|
+
'cvc',
|
|
16
|
+
'expiration',
|
|
17
|
+
'address',
|
|
18
|
+
'birthday',
|
|
19
|
+
'civil_status',
|
|
20
|
+
'social_profiles',
|
|
21
|
+
'emergency_contacts',
|
|
22
|
+
'subscriptions',
|
|
23
|
+
'pets',
|
|
24
|
+
];
|
|
25
|
+
for (const f of fields) {
|
|
26
|
+
assert.ok(f in result, `missing field: ${f}`);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('card has 4 groups of 4 digits', () => {
|
|
31
|
+
const result = personal();
|
|
32
|
+
assert.match(result.card as string, /^\d{4} \d{4} \d{4} \d{4}$/);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('cvc is 3 digits', () => {
|
|
36
|
+
const result = personal();
|
|
37
|
+
const cvc = result.cvc as number;
|
|
38
|
+
assert.ok(cvc >= 100 && cvc <= 999);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('expiration MM/YY format', () => {
|
|
42
|
+
const result = personal();
|
|
43
|
+
assert.match(result.expiration as string, /^\d{2}\/\d{2}$/);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('phone has international code', () => {
|
|
47
|
+
const result = personal();
|
|
48
|
+
assert.match(result.phone as string, /^\+\d+ /);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('social_profiles has 4 platforms', () => {
|
|
52
|
+
const result = personal();
|
|
53
|
+
const social = result.social_profiles as Record<string, string>;
|
|
54
|
+
assert.ok('twitter' in social);
|
|
55
|
+
assert.ok('facebook' in social);
|
|
56
|
+
assert.ok('linkedin' in social);
|
|
57
|
+
assert.ok('instagram' in social);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('emergency_contacts non-empty', () => {
|
|
61
|
+
const result = personal();
|
|
62
|
+
const contacts = result.emergency_contacts as unknown[];
|
|
63
|
+
assert.ok(contacts.length >= 1);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { test, describe } from 'node:test';
|
|
2
|
+
import { strict as assert } from 'node:assert';
|
|
3
|
+
import statistics from '../../src/modules/v4/statistics.js';
|
|
4
|
+
|
|
5
|
+
describe('statistics', () => {
|
|
6
|
+
test('basic computation on integers', () => {
|
|
7
|
+
const result = statistics('1,2,3,4,5');
|
|
8
|
+
assert.equal(result.count, 5);
|
|
9
|
+
assert.equal(result.sum, 15);
|
|
10
|
+
assert.equal(result.min, 1);
|
|
11
|
+
assert.equal(result.max, 5);
|
|
12
|
+
assert.equal(result.range, 4);
|
|
13
|
+
assert.equal(result.mean, 3);
|
|
14
|
+
assert.equal(result.median, 3);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('median on even count', () => {
|
|
18
|
+
const result = statistics('1,2,3,4');
|
|
19
|
+
assert.equal(result.median, 2.5);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('mode is empty when all values unique', () => {
|
|
23
|
+
const result = statistics('1,2,3,4,5');
|
|
24
|
+
assert.deepEqual(result.mode, []);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('mode detects most frequent value', () => {
|
|
28
|
+
const result = statistics('1,2,2,3,3,3');
|
|
29
|
+
assert.deepEqual(result.mode, [3]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('multiple modes', () => {
|
|
33
|
+
const result = statistics('1,1,2,2,3');
|
|
34
|
+
assert.deepEqual(result.mode.sort(), [1, 2]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('stddev of identical values is 0', () => {
|
|
38
|
+
const result = statistics('5,5,5,5');
|
|
39
|
+
assert.equal(result.stddev, 0);
|
|
40
|
+
assert.equal(result.variance, 0);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('handles negative and decimal values', () => {
|
|
44
|
+
const result = statistics('-1.5,0,1.5');
|
|
45
|
+
assert.equal(result.mean, 0);
|
|
46
|
+
assert.equal(result.min, -1.5);
|
|
47
|
+
assert.equal(result.max, 1.5);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('throws on non-numeric values', () => {
|
|
51
|
+
assert.throws(() => statistics('1,abc,3'), /must contain only numbers/);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('throws on missing values', () => {
|
|
55
|
+
assert.throws(() => statistics(''), /required/);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { test, describe } from 'node:test';
|
|
2
|
+
import { strict as assert } from 'node:assert';
|
|
3
|
+
import { slug, stats, lorem, number } from '../../src/modules/v4/text.js';
|
|
4
|
+
|
|
5
|
+
describe('text', () => {
|
|
6
|
+
describe('slug', () => {
|
|
7
|
+
test('basic slug', () => {
|
|
8
|
+
assert.equal(slug('Hello World'), 'hello-world');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test('strips diacritics', () => {
|
|
12
|
+
assert.equal(slug('Crème Brûlée'), 'creme-brulee');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('strips special chars', () => {
|
|
16
|
+
assert.equal(slug('Mon Article #1 !'), 'mon-article-1');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test('collapses multiple spaces', () => {
|
|
20
|
+
assert.equal(slug('a b c'), 'a-b-c');
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe('stats', () => {
|
|
25
|
+
test('counts characters and words', () => {
|
|
26
|
+
const result = stats('Hello world');
|
|
27
|
+
assert.equal(result.characters, 11);
|
|
28
|
+
assert.equal(result.charactersNoSpaces, 10);
|
|
29
|
+
assert.equal(result.words, 2);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('counts sentences', () => {
|
|
33
|
+
const result = stats('Hi. How are you? Fine!');
|
|
34
|
+
assert.equal(result.sentences, 3);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('reading time format', () => {
|
|
38
|
+
const result = stats('a '.repeat(200).trim());
|
|
39
|
+
assert.match(result.readingTime, /min/);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('short text returns seconds', () => {
|
|
43
|
+
const result = stats('hello world');
|
|
44
|
+
assert.match(result.readingTime, /s$/);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('lorem', () => {
|
|
49
|
+
test('words count', () => {
|
|
50
|
+
const result = lorem('words', '10');
|
|
51
|
+
assert.equal(result.split(' ').length, 10);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('sentences end with period', () => {
|
|
55
|
+
const result = lorem('sentences', '3');
|
|
56
|
+
const sentences = result.split('. ');
|
|
57
|
+
assert.equal(sentences.length, 3);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('paragraphs separated by double newline', () => {
|
|
61
|
+
const result = lorem('paragraphs', '2');
|
|
62
|
+
assert.equal(result.split('\n\n').length, 2);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('throws on invalid type', () => {
|
|
66
|
+
assert.throws(() => lorem('verses', '5'), /Type must be one of/);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe('number', () => {
|
|
71
|
+
test('English: 42', () => {
|
|
72
|
+
assert.equal(number('42', 'en'), 'forty-two');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('English: 100', () => {
|
|
76
|
+
assert.equal(number('100', 'en'), 'one hundred');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('English: 1234', () => {
|
|
80
|
+
assert.equal(number('1234', 'en'), 'one thousand two hundred thirty-four');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('French: 21', () => {
|
|
84
|
+
assert.equal(number('21', 'fr'), 'vingt et un');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('French: 80', () => {
|
|
88
|
+
assert.equal(number('80', 'fr'), 'quatre-vingts');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('French: 0', () => {
|
|
92
|
+
assert.equal(number('0', 'fr'), 'zéro');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('English: negative', () => {
|
|
96
|
+
assert.equal(number('-5', 'en'), 'minus five');
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('throws on invalid lang', () => {
|
|
100
|
+
assert.throws(() => number('42', 'es'), /Lang must be one of/);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('throws on non-integer', () => {
|
|
104
|
+
assert.throws(() => number('3.14', 'en'), /must be an integer/);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { test, describe } from 'node:test';
|
|
2
|
+
import { strict as assert } from 'node:assert';
|
|
3
|
+
import time from '../../src/modules/v4/time.js';
|
|
4
|
+
|
|
5
|
+
describe('time', () => {
|
|
6
|
+
test('live returns full set of formats', () => {
|
|
7
|
+
const result = time('live');
|
|
8
|
+
const fields = [
|
|
9
|
+
'iso',
|
|
10
|
+
'utc',
|
|
11
|
+
'timestamp',
|
|
12
|
+
'locale',
|
|
13
|
+
'date',
|
|
14
|
+
'time',
|
|
15
|
+
'year',
|
|
16
|
+
'month',
|
|
17
|
+
'day',
|
|
18
|
+
'hour',
|
|
19
|
+
'minute',
|
|
20
|
+
'second',
|
|
21
|
+
'ms',
|
|
22
|
+
'dayOfWeek',
|
|
23
|
+
'dayOfYear',
|
|
24
|
+
'weekNumber',
|
|
25
|
+
'timezone',
|
|
26
|
+
'timezoneOffset',
|
|
27
|
+
];
|
|
28
|
+
for (const f of fields) {
|
|
29
|
+
assert.ok(f in result, `missing field: ${f}`);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('iso is parseable', () => {
|
|
34
|
+
const result = time('live');
|
|
35
|
+
assert.ok(!isNaN(Date.parse(result.iso as string)));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('format=year returns only date', () => {
|
|
39
|
+
const result = time('live', undefined, undefined, 'year');
|
|
40
|
+
assert.ok('date' in result);
|
|
41
|
+
assert.equal(typeof result.date, 'number');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('random within range', () => {
|
|
45
|
+
const result = time('random', '2020-01-01', '2020-12-31');
|
|
46
|
+
const ts = result.timestamp as number;
|
|
47
|
+
assert.ok(ts >= new Date('2020-01-01').getTime());
|
|
48
|
+
assert.ok(ts <= new Date('2020-12-31').getTime());
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('throws on invalid type', () => {
|
|
52
|
+
assert.throws(() => time('foo'), /valid type/);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('throws on invalid format', () => {
|
|
56
|
+
assert.throws(() => time('live', undefined, undefined, 'fakeformat'), /valid format/);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('throws on invalid timezone', () => {
|
|
60
|
+
assert.throws(() => time('live', undefined, undefined, undefined, 'Mars/Olympus'), /valid timezone/);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('Europe/Paris timezone', () => {
|
|
64
|
+
const result = time('live', undefined, undefined, undefined, 'Europe/Paris');
|
|
65
|
+
assert.equal(result.timezone, 'Europe/Paris');
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { test, describe } from 'node:test';
|
|
2
|
+
import { strict as assert } from 'node:assert';
|
|
3
|
+
import token from '../../src/modules/v4/token.js';
|
|
4
|
+
|
|
5
|
+
describe('token', () => {
|
|
6
|
+
test('alpha length', () => {
|
|
7
|
+
const t = token(24, 'alpha');
|
|
8
|
+
assert.equal(t.length, 24);
|
|
9
|
+
assert.match(t, /^[a-zA-Z]+$/);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test('alphanum length', () => {
|
|
13
|
+
const t = token(32, 'alphanum');
|
|
14
|
+
assert.equal(t.length, 32);
|
|
15
|
+
assert.match(t, /^[a-zA-Z0-9]+$/);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('hex contains only hex chars', () => {
|
|
19
|
+
const t = token(16, 'hex');
|
|
20
|
+
assert.equal(t.length, 16);
|
|
21
|
+
assert.match(t, /^[0-9a-f]+$/);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('num contains only digits', () => {
|
|
25
|
+
const t = token(20, 'num');
|
|
26
|
+
assert.match(t, /^\d+$/);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('urlsafe chars only', () => {
|
|
30
|
+
const t = token(40, 'urlsafe');
|
|
31
|
+
assert.match(t, /^[a-zA-Z0-9_-]+$/);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('uuid type produces hex', () => {
|
|
35
|
+
const t = token(32, 'uuid');
|
|
36
|
+
assert.equal(t.length, 32);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('default type alphanum', () => {
|
|
40
|
+
const t = token(20);
|
|
41
|
+
assert.match(t, /^[a-zA-Z0-9]+$/);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('throws below minimum length', () => {
|
|
45
|
+
assert.throws(() => token(5, 'alpha'), /greater than or equal to 12/);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('throws above maximum length', () => {
|
|
49
|
+
assert.throws(() => token(5000, 'alpha'), /4096/);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('throws on invalid type', () => {
|
|
53
|
+
assert.throws(() => token(20, 'martian'), /Invalid token type/);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('two tokens are different (entropy check)', () => {
|
|
57
|
+
const a = token(32, 'alphanum');
|
|
58
|
+
const b = token(32, 'alphanum');
|
|
59
|
+
assert.notEqual(a, b);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { test, describe } from 'node:test';
|
|
2
|
+
import { strict as assert } from 'node:assert';
|
|
3
|
+
import username from '../../src/modules/v4/username.js';
|
|
4
|
+
|
|
5
|
+
describe('username', () => {
|
|
6
|
+
test('returns expected fields', () => {
|
|
7
|
+
const result = username();
|
|
8
|
+
assert.ok('username' in result);
|
|
9
|
+
assert.ok('number' in result);
|
|
10
|
+
assert.ok('adjective' in result);
|
|
11
|
+
assert.ok('animal' in result);
|
|
12
|
+
assert.ok('job' in result);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('username is a non-empty string', () => {
|
|
16
|
+
const result = username();
|
|
17
|
+
assert.equal(typeof result.username, 'string');
|
|
18
|
+
assert.ok((result.username as string).length > 0);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('number is between 0 and 99', () => {
|
|
22
|
+
const result = username();
|
|
23
|
+
const n = result.number as number;
|
|
24
|
+
assert.ok(n >= 0 && n <= 99);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('produces varied usernames', () => {
|
|
28
|
+
const seen = new Set<string>();
|
|
29
|
+
for (let i = 0; i < 10; i++) {
|
|
30
|
+
seen.add(username().username as string);
|
|
31
|
+
}
|
|
32
|
+
assert.ok(seen.size > 1);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { test, describe } from 'node:test';
|
|
2
|
+
import { strict as assert } from 'node:assert';
|
|
3
|
+
import { luhn, iban, email } from '../../src/modules/v4/validate.js';
|
|
4
|
+
|
|
5
|
+
describe('validate', () => {
|
|
6
|
+
describe('luhn', () => {
|
|
7
|
+
test('valid Visa test card', () => {
|
|
8
|
+
const result = luhn('4111111111111111');
|
|
9
|
+
assert.equal(result.valid, true);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test('invalid card number', () => {
|
|
13
|
+
const result = luhn('4111111111111112');
|
|
14
|
+
assert.equal(result.valid, false);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('strips spaces and dashes', () => {
|
|
18
|
+
const result = luhn('4111-1111 1111-1111');
|
|
19
|
+
assert.equal(result.valid, true);
|
|
20
|
+
assert.equal(result.value, '4111111111111111');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('throws on non-digit', () => {
|
|
24
|
+
assert.throws(() => luhn('4111ABC11111111'), /only digits/);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('throws on too short', () => {
|
|
28
|
+
assert.throws(() => luhn('411111'), /between 12 and 19/);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('iban', () => {
|
|
33
|
+
test('valid French IBAN', () => {
|
|
34
|
+
const result = iban('FR1420041010050500013M02606');
|
|
35
|
+
assert.equal(result.valid, true);
|
|
36
|
+
assert.equal(result.country, 'FR');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('invalid checksum', () => {
|
|
40
|
+
const result = iban('FR1420041010050500013M02607');
|
|
41
|
+
assert.equal(result.valid, false);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('strips spaces', () => {
|
|
45
|
+
const result = iban('FR14 2004 1010 0505 0001 3M02 606');
|
|
46
|
+
assert.equal(result.valid, true);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('throws on bad format', () => {
|
|
50
|
+
assert.throws(() => iban('1234'), /Invalid IBAN format/);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe('email', () => {
|
|
55
|
+
test('valid email', () => {
|
|
56
|
+
assert.equal(email('hello@example.com').valid, true);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('rejects missing domain', () => {
|
|
60
|
+
assert.equal(email('hello@').valid, false);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('rejects missing @', () => {
|
|
64
|
+
assert.equal(email('helloexample.com').valid, false);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('rejects spaces', () => {
|
|
68
|
+
assert.equal(email('hello world@example.com').valid, false);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"allowJs": true,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"noUncheckedIndexedAccess": true,
|
|
9
|
+
"outDir": "dist",
|
|
10
|
+
"rootDir": "src",
|
|
11
|
+
"declaration": false,
|
|
12
|
+
"sourceMap": true,
|
|
13
|
+
"esModuleInterop": true,
|
|
14
|
+
"skipLibCheck": true
|
|
15
|
+
},
|
|
16
|
+
"include": ["src"]
|
|
17
|
+
}
|