forked from rocicorp/replicache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.js
More file actions
76 lines (69 loc) · 1.6 KB
/
Copy pathdata.js
File metadata and controls
76 lines (69 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* @typedef {"string"|"object"|"arraybuffer"|"blob"} RandomDataType
*/
/**
* @param {RandomDataType} type
* @param {number} len
* @param {number} datumSize
*/
export function randomData(type, len, datumSize) {
return Array.from({length: len}).map(() => randomDatum(type, datumSize));
}
/**
* @param {RandomDataType} type
* @param {number} len
*/
function randomDatum(type, len) {
if (type == 'string') {
return randomString(len);
} else if (type == 'object') {
return randomObject(len);
} else if (type == 'arraybuffer') {
return randomUint8Array(len).buffer;
} else if (type == 'blob') {
return randomBlob(len);
} else {
throw new Error('unsupported');
}
}
/**
* @param {number} len
* @returns {Record<string, string>}
*/
function randomObject(len) {
const ret = /** @type Record<string, string> */ ({});
for (let i = 0; i < Math.min(100, len); i++) {
ret[`k${i}`] = randomString(Math.ceil(len / 100));
}
return ret;
}
/**
* @param {number} numStrings
* @param {number} strLen
*/
export function makeRandomStrings(numStrings, strLen) {
return Array.from({length: numStrings}, () => randomString(strLen));
}
/**
* @param {number} len
*/
function randomString(len) {
const arr = randomUint8Array(len);
return new TextDecoder('ascii').decode(arr);
}
/**
* @param {number} len
*/
function randomBlob(len) {
return new Blob([randomUint8Array(len)]);
}
/**
* @param {number} len
*/
function randomUint8Array(len) {
const arr = new Uint8Array(len);
for (let i = 0; i < len; i++) {
arr[i] = Math.floor(Math.random() * 254 + 1);
}
return arr;
}