Skip to content

Commit 8de9d93

Browse files
committed
Added cacheId helper functions
1 parent ecc89b0 commit 8de9d93

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

src/helpers.test.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { all, json, anyOrder } from './helpers.js';
2+
3+
describe('all (Args to string)', () => {
4+
test('Without arguments', () => {
5+
expect(all()).toEqual('');
6+
});
7+
8+
test('undefined', () => {
9+
expect(all(undefined)).toEqual('undefined');
10+
});
11+
12+
test('null', () => {
13+
expect(all(null)).toEqual('null');
14+
});
15+
16+
test('Object', () => {
17+
expect(all({ a: 1, b: true, c: null })).toEqual('[object Object]');
18+
});
19+
20+
test('Empty object', () => {
21+
expect(all({})).toEqual('[object Object]');
22+
});
23+
24+
test('Array', () => {
25+
expect(all(['str', 1, undefined, true, null])).toEqual('str,1,,true,');
26+
});
27+
28+
test('Empty array', () => {
29+
expect(all([])).toEqual('');
30+
});
31+
32+
test('RegExp', () => {
33+
expect(all(/^(.*)?/)).toEqual('/^(.*)?/');
34+
});
35+
36+
test('Multiple arguments', () => {
37+
expect(all('str', 100, null, true, undefined, /^(.*)?/)).toEqual(
38+
'str-100-null-true-undefined-/^(.*)?/'
39+
);
40+
});
41+
});
42+
43+
describe('json (Args to JSON string)', () => {
44+
test('Without arguments', () => {
45+
expect(json()).toEqual('[]');
46+
});
47+
48+
test('undefined', () => {
49+
expect(json(undefined)).toEqual('[null]');
50+
});
51+
52+
test('null', () => {
53+
expect(json(null)).toEqual('[null]');
54+
});
55+
56+
test('Object', () => {
57+
expect(json({ a: 1, b: true, c: null })).toEqual('[{"a":1,"b":true,"c":null}]');
58+
});
59+
60+
test('Empty object', () => {
61+
expect(json({})).toEqual('[{}]');
62+
});
63+
64+
test('Array', () => {
65+
expect(json(['str', 1, undefined, true, null])).toEqual('[["str",1,null,true,null]]');
66+
});
67+
68+
test('Empty array', () => {
69+
expect(json([])).toEqual('[[]]');
70+
});
71+
72+
test('RegExp', () => {
73+
expect(json(/^(.*)?/)).toEqual('[{}]');
74+
});
75+
76+
test('Multiple arguments', () => {
77+
expect(json({ a: 1 }, true, undefined, [false, { b: 2 }], /^(.*)?/)).toEqual(
78+
'[{"a":1},true,null,[false,{"b":2}],{}]'
79+
);
80+
});
81+
});
82+
83+
describe('anyOrder (Args in any order to string)', () => {
84+
test('Without arguments', () => {
85+
expect(anyOrder()).toEqual('');
86+
});
87+
88+
test('Strings', () => {
89+
expect(anyOrder('a', 'b', 'c', '0', '1')).toEqual(anyOrder('1', 'b', 'a', 'c', '0'));
90+
});
91+
92+
test('Numbers', () => {
93+
expect(anyOrder(1, 2, 3, 4, 100, 1000)).toEqual(anyOrder(100, 2, 1000, 4, 1, 3));
94+
});
95+
96+
test('Booleans', () => {
97+
expect(anyOrder(true, false, true, false)).toEqual(anyOrder(false, false, true, true));
98+
});
99+
100+
test('null and undefined', () => {
101+
expect(anyOrder(null, undefined)).toEqual(anyOrder(undefined, null));
102+
});
103+
104+
test('Arrays', () => {
105+
const arr1 = ['str', 1, undefined, true, null];
106+
const arr2 = [1, 2, 3, true, null];
107+
const arr3 = [null, 100];
108+
109+
expect(anyOrder(arr1, arr2, arr3)).toEqual(anyOrder(arr3, arr1, arr2));
110+
});
111+
112+
test('RegExp objects', () => {
113+
const r1 = /^(.*)?/;
114+
const r2 = /([A-Z])\w+/;
115+
const r3 = /(\d+(\.\d+)?)/gi;
116+
117+
expect(anyOrder(r1, r2, r3)).toEqual(anyOrder(r2, r3, r1));
118+
});
119+
120+
test('Multiple arguments', () => {
121+
const string = 'str';
122+
const number = 100;
123+
const regexp = /^(.*)?/;
124+
125+
expect(anyOrder(string, number, null, true, undefined, regexp, false)).toEqual(
126+
anyOrder(false, undefined, true, regexp, null, string, number)
127+
);
128+
});
129+
});

src/helpers.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Get an ID from all the arguments casted to a string and joined together.
3+
*
4+
* **Note**: does not work with objects.
5+
*
6+
* @param args - Any number or arguments
7+
* @returns String ID
8+
*/
9+
export function all(...args: readonly any[]): string {
10+
return args.map(String).join('-');
11+
}
12+
13+
/**
14+
* Get a JSON string ID from the arguments.
15+
*
16+
* **Note**: does not work with `RegExp` objects.
17+
*
18+
* @param args - Any number or arguments
19+
* @returns String ID
20+
*/
21+
export function json(...args: readonly any[]): string {
22+
return JSON.stringify(args);
23+
}
24+
25+
/**
26+
* Get the same ID from a set of arguments no matter the order they're passed in.
27+
*
28+
* The passed arguments are sorted and then casted to a string and then joined together.
29+
*
30+
* **Note**: does not work with objects.
31+
*
32+
* @param args - Any number or arguments
33+
* @returns String ID
34+
*/
35+
export function anyOrder(...args: readonly any[]): string {
36+
return args.slice().sort().map(String).join('-');
37+
}

0 commit comments

Comments
 (0)