@terrygonguet/utils 0.1.0 → 0.2.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/dist/index.d.ts CHANGED
@@ -2,17 +2,11 @@
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 = unknown>(f: () => T): [null, T] | [Err, null];
17
11
 
18
- export { clamp, composeJSONRevivers, createNoopProxy, exhaustive, hash, noop, range, safeParse };
12
+ 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,20 @@ function* range(start, end, step = 1) {
50
35
  yield i;
51
36
  }
52
37
  }
38
+ function safe(f) {
39
+ try {
40
+ const result = f();
41
+ return [null, result];
42
+ } catch (error) {
43
+ return [error, null];
44
+ }
45
+ }
53
46
  export {
54
47
  clamp,
55
- composeJSONRevivers,
56
48
  createNoopProxy,
57
49
  exhaustive,
58
50
  hash,
59
51
  noop,
60
52
  range,
61
- safeParse
53
+ safe
62
54
  };
package/dist/json.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * This function does no runtime type checking,
3
+ * make sure that the parsed value is valid
4
+ */
5
+ declare function safeParse<T>(str: string, defaultValue: T, reviver?: (key: string, value: any) => any): T;
6
+
7
+ export { safeParse };
package/dist/json.js ADDED
@@ -0,0 +1,11 @@
1
+ // src/json.ts
2
+ function safeParse(str, defaultValue, reviver) {
3
+ try {
4
+ return JSON.parse(str, reviver);
5
+ } catch (_) {
6
+ return defaultValue;
7
+ }
8
+ }
9
+ export {
10
+ safeParse
11
+ };
@@ -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 };
@@ -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,7 +1,8 @@
1
1
  {
2
2
  "name": "@terrygonguet/utils",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
+ "license": "MIT",
5
6
  "scripts": {
6
7
  "dev": "vite",
7
8
  "build": "tsup",
@@ -19,6 +20,14 @@
19
20
  "./async": {
20
21
  "types": "./dist/async.d.ts",
21
22
  "import": "./dist/async.js"
23
+ },
24
+ "./json": {
25
+ "types": "./dist/json.d.ts",
26
+ "import": "./dist/json.js"
27
+ },
28
+ "./tableau": {
29
+ "types": "./dist/tableau.d.ts",
30
+ "import": "./dist/tableau.js"
22
31
  }
23
32
  },
24
33
  "devDependencies": {