@terrygonguet/utils 0.1.1 → 0.2.1
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/dist/index.d.ts +3 -8
- package/dist/index.js +14 -17
- package/dist/json.d.ts +7 -0
- package/dist/json.js +11 -0
- package/dist/tableau.d.ts +63 -0
- package/dist/tableau.js +125 -0
- package/package.json +12 -7
package/dist/index.d.ts
CHANGED
|
@@ -2,17 +2,12 @@
|
|
|
2
2
|
* Behaviour is undefined when max < min
|
|
3
3
|
*/
|
|
4
4
|
declare function clamp(value: number, min: number, max: number): number;
|
|
5
|
-
type JSONReviver = (key: string, value: any) => any;
|
|
6
|
-
/**
|
|
7
|
-
* This function does no runtime type checking,
|
|
8
|
-
* make sure that the parsed value is valid
|
|
9
|
-
*/
|
|
10
|
-
declare function safeParse<T>(str: string, defaultValue: T, reviver?: JSONReviver): T;
|
|
11
|
-
declare function composeJSONRevivers(...revivers: JSONReviver[]): JSONReviver;
|
|
12
5
|
declare function createNoopProxy<T>(): T;
|
|
13
6
|
declare function noop(): void;
|
|
14
7
|
declare function exhaustive(_: never): never;
|
|
15
8
|
declare function hash(message: string): Promise<ArrayBuffer>;
|
|
16
9
|
declare function range(start: number, end: number, step?: number): Generator<number, void, unknown>;
|
|
10
|
+
declare function safe<T, Err = Error>(f: () => Promise<T>): Promise<[null, T] | [Err, null]>;
|
|
11
|
+
declare function safe<T, Err = Error>(f: () => T): [null, T] | [Err, null];
|
|
17
12
|
|
|
18
|
-
export { clamp,
|
|
13
|
+
export { clamp, createNoopProxy, exhaustive, hash, noop, range, safe };
|
package/dist/index.js
CHANGED
|
@@ -2,21 +2,6 @@
|
|
|
2
2
|
function clamp(value, min, max) {
|
|
3
3
|
return Math.min(Math.max(value, min), max);
|
|
4
4
|
}
|
|
5
|
-
function safeParse(str, defaultValue, reviver) {
|
|
6
|
-
try {
|
|
7
|
-
return JSON.parse(str, reviver);
|
|
8
|
-
} catch (_) {
|
|
9
|
-
return defaultValue;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
function composeJSONRevivers(...revivers) {
|
|
13
|
-
return function(key, value) {
|
|
14
|
-
for (const reviver of revivers) {
|
|
15
|
-
value = reviver(key, value);
|
|
16
|
-
}
|
|
17
|
-
return value;
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
5
|
function createNoopProxy() {
|
|
21
6
|
const noop2 = () => proxy;
|
|
22
7
|
const no = () => false;
|
|
@@ -50,13 +35,25 @@ function* range(start, end, step = 1) {
|
|
|
50
35
|
yield i;
|
|
51
36
|
}
|
|
52
37
|
}
|
|
38
|
+
function safe(f) {
|
|
39
|
+
try {
|
|
40
|
+
const promiseOrResult = f();
|
|
41
|
+
if (promiseOrResult instanceof Promise)
|
|
42
|
+
return promiseOrResult.then(
|
|
43
|
+
(result) => [null, result],
|
|
44
|
+
(error) => [error, null]
|
|
45
|
+
);
|
|
46
|
+
else return [null, promiseOrResult];
|
|
47
|
+
} catch (error) {
|
|
48
|
+
return [error, null];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
53
51
|
export {
|
|
54
52
|
clamp,
|
|
55
|
-
composeJSONRevivers,
|
|
56
53
|
createNoopProxy,
|
|
57
54
|
exhaustive,
|
|
58
55
|
hash,
|
|
59
56
|
noop,
|
|
60
57
|
range,
|
|
61
|
-
|
|
58
|
+
safe
|
|
62
59
|
};
|
package/dist/json.d.ts
ADDED
package/dist/json.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
type Predicate<T> = (value: T, index: number, tableau: Tableau<T>) => boolean;
|
|
2
|
+
type AssertPredicate<T, U extends T> = (value: T, index: number, tableau: Tableau<T>) => value is U;
|
|
3
|
+
type Reducer<T, U> = (previousValue: U, currentValue: T, index: number, tableau: Tableau<T>) => U;
|
|
4
|
+
type Mapper<T, U> = (value: T, index: number, tableau: Tableau<T>) => U;
|
|
5
|
+
type Comparer<T> = (a: T, b: T) => number;
|
|
6
|
+
type FlatTableau<Arr, Depth extends number> = {
|
|
7
|
+
done: Arr;
|
|
8
|
+
recur: Arr extends Tableau<infer InnerArr> ? FlatTableau<InnerArr, [
|
|
9
|
+
-1,
|
|
10
|
+
0,
|
|
11
|
+
1,
|
|
12
|
+
2,
|
|
13
|
+
3,
|
|
14
|
+
4,
|
|
15
|
+
5,
|
|
16
|
+
6,
|
|
17
|
+
7,
|
|
18
|
+
8,
|
|
19
|
+
9,
|
|
20
|
+
10,
|
|
21
|
+
11,
|
|
22
|
+
12,
|
|
23
|
+
13,
|
|
24
|
+
14,
|
|
25
|
+
15,
|
|
26
|
+
16,
|
|
27
|
+
17,
|
|
28
|
+
18,
|
|
29
|
+
19,
|
|
30
|
+
20
|
|
31
|
+
][Depth]> : Arr;
|
|
32
|
+
}[Depth extends -1 ? "done" : "recur"];
|
|
33
|
+
declare class Tableau<T> extends Array<T> {
|
|
34
|
+
constructor(length?: number);
|
|
35
|
+
i(idx: number): T | undefined;
|
|
36
|
+
ientries(): IterableIterator<[number, T]>;
|
|
37
|
+
ievery<U extends T>(predicate: AssertPredicate<T, U>, thisArg?: any): this is Tableau<U>;
|
|
38
|
+
ievery(predicate: Predicate<T>, thisArg?: any): boolean;
|
|
39
|
+
ifilter<U extends T>(predicate: AssertPredicate<T, U>, thisArg?: any): Tableau<U>;
|
|
40
|
+
ifilter(predicate: Predicate<T>, thisArg?: any): Tableau<T>;
|
|
41
|
+
ifind<U extends T>(predicate: AssertPredicate<T, U>, thisArg?: any): U | undefined;
|
|
42
|
+
ifind(predicate: Predicate<T>, thisArg?: any): T | undefined;
|
|
43
|
+
ifindIndex(predicate: Predicate<T>, thisArg?: any): number | undefined;
|
|
44
|
+
ifindLast<U extends T>(predicate: AssertPredicate<T, U>, thisArg?: any): U | undefined;
|
|
45
|
+
ifindLast(predicate: Predicate<T>, thisArg?: any): T | undefined;
|
|
46
|
+
ifindLastIndex(predicate: Predicate<T>, thisArg?: any): number | undefined;
|
|
47
|
+
iflat<Depth extends number = 1>(depth?: Depth): FlatTableau<T, Depth>;
|
|
48
|
+
iflatMap<U>(callback: Mapper<T, U | Tableau<U>>, thisArg?: any): Tableau<U>;
|
|
49
|
+
iindexOf(searchElement: T, fromIndex?: number): number | undefined;
|
|
50
|
+
ikeys(): IterableIterator<number>;
|
|
51
|
+
ilastIndexOf(searchElement: T, fromIndex?: number): number | undefined;
|
|
52
|
+
imap<U>(callbackFn: Mapper<T, U>, thisArg?: any): Tableau<U>;
|
|
53
|
+
ireduce<U>(callbackFn: Reducer<T, U>, initialValue: U): U;
|
|
54
|
+
ireduceRight<U>(callbackFn: Reducer<T, U>, initialValue: U): U;
|
|
55
|
+
isome(predicate: Predicate<T>, thisArg?: any): boolean;
|
|
56
|
+
isort(compareFn: Comparer<T>): Tableau<T>;
|
|
57
|
+
isplice(start: number, deleteCount?: number, ...items: T[]): Tableau<T>;
|
|
58
|
+
iwith(index: number, value: T): Tableau<T>;
|
|
59
|
+
static from<T>(source: Iterable<T> | ArrayLike<T>): Tableau<T>;
|
|
60
|
+
static of<T>(...elements: T[]): Tableau<T>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export { Tableau };
|
package/dist/tableau.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// src/tableau.ts
|
|
2
|
+
var Tableau = class _Tableau extends Array {
|
|
3
|
+
constructor(length) {
|
|
4
|
+
length == void 0 ? super() : super(length);
|
|
5
|
+
}
|
|
6
|
+
i(idx) {
|
|
7
|
+
if (idx == 0) throw new RangeError("Cannot access index 0 in a Tableau");
|
|
8
|
+
return this.at(idx - 1);
|
|
9
|
+
}
|
|
10
|
+
*ientries() {
|
|
11
|
+
for (let i = 0; i < this.length; i++) {
|
|
12
|
+
yield [i + 1, this[i]];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
ievery(predicate, thisArg) {
|
|
16
|
+
return this.every((value, i) => predicate(value, i + 1, this), thisArg);
|
|
17
|
+
}
|
|
18
|
+
ifilter(predicate, thisArg) {
|
|
19
|
+
return this.filter((value, i) => predicate(value, i + 1, this), thisArg);
|
|
20
|
+
}
|
|
21
|
+
ifind(predicate, thisArg) {
|
|
22
|
+
return this.find((value, i) => predicate(value, i + 1, this), thisArg);
|
|
23
|
+
}
|
|
24
|
+
ifindIndex(predicate, thisArg) {
|
|
25
|
+
const idx = this.findIndex(
|
|
26
|
+
(value, i) => predicate(value, i + 1, this),
|
|
27
|
+
thisArg
|
|
28
|
+
);
|
|
29
|
+
return idx == -1 ? void 0 : idx + 1;
|
|
30
|
+
}
|
|
31
|
+
ifindLast(predicate, thisArg) {
|
|
32
|
+
return this.findLast(
|
|
33
|
+
(value, i) => predicate(value, i + 1, this),
|
|
34
|
+
thisArg
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
ifindLastIndex(predicate, thisArg) {
|
|
38
|
+
const idx = this.findLastIndex(
|
|
39
|
+
(value, i) => predicate(value, i + 1, this),
|
|
40
|
+
thisArg
|
|
41
|
+
);
|
|
42
|
+
return idx == -1 ? void 0 : idx + 1;
|
|
43
|
+
}
|
|
44
|
+
iflat(depth) {
|
|
45
|
+
return this.flat(depth);
|
|
46
|
+
}
|
|
47
|
+
iflatMap(callback, thisArg) {
|
|
48
|
+
return this.flatMap(
|
|
49
|
+
(value, i) => callback(value, i + 1, this),
|
|
50
|
+
thisArg
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
iindexOf(searchElement, fromIndex = 1) {
|
|
54
|
+
if (fromIndex == 0)
|
|
55
|
+
throw new RangeError("Cannot access index 0 in a Tableau");
|
|
56
|
+
const idx = this.indexOf(searchElement, fromIndex - 1);
|
|
57
|
+
return idx == -1 ? void 0 : idx + 1;
|
|
58
|
+
}
|
|
59
|
+
*ikeys() {
|
|
60
|
+
for (const key of this.keys()) {
|
|
61
|
+
yield key + 1;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
ilastIndexOf(searchElement, fromIndex = this.length) {
|
|
65
|
+
if (fromIndex == 0)
|
|
66
|
+
throw new RangeError("Cannot access index 0 in a Tableau");
|
|
67
|
+
const idx = this.lastIndexOf(searchElement, fromIndex - 1);
|
|
68
|
+
return idx == -1 ? void 0 : idx + 1;
|
|
69
|
+
}
|
|
70
|
+
imap(callbackFn, thisArg) {
|
|
71
|
+
return this.map(
|
|
72
|
+
(value, i) => callbackFn(value, i + 1, this),
|
|
73
|
+
thisArg
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
ireduce(callbackFn, initialValue) {
|
|
77
|
+
return this.reduce(
|
|
78
|
+
(acc, cur, i) => callbackFn(acc, cur, i + 1, this),
|
|
79
|
+
initialValue
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
ireduceRight(callbackFn, initialValue) {
|
|
83
|
+
return this.reduceRight(
|
|
84
|
+
(acc, cur, i) => callbackFn(acc, cur, i + 1, this),
|
|
85
|
+
initialValue
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
isome(predicate, thisArg) {
|
|
89
|
+
return this.some((value, i) => predicate(value, i + 1, this), thisArg);
|
|
90
|
+
}
|
|
91
|
+
isort(compareFn) {
|
|
92
|
+
return this.toSorted(compareFn);
|
|
93
|
+
}
|
|
94
|
+
isplice(start, deleteCount, ...items) {
|
|
95
|
+
if (start == 0)
|
|
96
|
+
throw new RangeError("Cannot access index 0 in a Tableau");
|
|
97
|
+
return this.toSpliced(start - 1, deleteCount, ...items);
|
|
98
|
+
}
|
|
99
|
+
iwith(index, value) {
|
|
100
|
+
if (index == 0)
|
|
101
|
+
throw new RangeError("Cannot access index 0 in a Tableau");
|
|
102
|
+
return this.with(index - 1, value);
|
|
103
|
+
}
|
|
104
|
+
static from(source) {
|
|
105
|
+
if ("length" in source) {
|
|
106
|
+
const litanie = new _Tableau(source.length);
|
|
107
|
+
for (let i = 0; i < source.length; i++) {
|
|
108
|
+
litanie[i] = source[i];
|
|
109
|
+
}
|
|
110
|
+
return litanie;
|
|
111
|
+
} else {
|
|
112
|
+
const litanie = new _Tableau();
|
|
113
|
+
for (const element of source) {
|
|
114
|
+
litanie.push(element);
|
|
115
|
+
}
|
|
116
|
+
return litanie;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
static of(...elements) {
|
|
120
|
+
return _Tableau.from(elements);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
export {
|
|
124
|
+
Tableau
|
|
125
|
+
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terrygonguet/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"dev": "vite",
|
|
8
8
|
"build": "tsup",
|
|
9
|
-
"test": "vitest test --update"
|
|
10
|
-
"coverage": "vitest run --coverage"
|
|
9
|
+
"test": "vitest test --update"
|
|
11
10
|
},
|
|
12
11
|
"files": [
|
|
13
12
|
"dist"
|
|
@@ -20,16 +19,22 @@
|
|
|
20
19
|
"./async": {
|
|
21
20
|
"types": "./dist/async.d.ts",
|
|
22
21
|
"import": "./dist/async.js"
|
|
22
|
+
},
|
|
23
|
+
"./json": {
|
|
24
|
+
"types": "./dist/json.d.ts",
|
|
25
|
+
"import": "./dist/json.js"
|
|
26
|
+
},
|
|
27
|
+
"./tableau": {
|
|
28
|
+
"types": "./dist/tableau.d.ts",
|
|
29
|
+
"import": "./dist/tableau.js"
|
|
23
30
|
}
|
|
24
31
|
},
|
|
25
32
|
"devDependencies": {
|
|
26
|
-
"@vitest/coverage-v8": "^0.32.0",
|
|
27
|
-
"@vitest/ui": "^0.32.0",
|
|
28
33
|
"prettier": "^2.8.8",
|
|
29
34
|
"tsup": "^8.1.0",
|
|
30
35
|
"typescript": "^5.0.2",
|
|
31
|
-
"vite": "^4.
|
|
32
|
-
"vitest": "^
|
|
36
|
+
"vite": "^5.4.8",
|
|
37
|
+
"vitest": "^2.1.2"
|
|
33
38
|
},
|
|
34
39
|
"author": {
|
|
35
40
|
"email": "terry@gonguet.com",
|