@jackens/nnn 2024.3.8 → 2024.3.31
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/nnn.d.ts +14 -18
- package/nnn.js +5 -39
- package/package.json +1 -4
- package/readme.md +16 -58
package/nnn.d.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A helper that checks equality of the given arguments.
|
|
3
|
-
*/
|
|
4
|
-
export declare const eq: (x: any, y: any) => boolean;
|
|
5
|
-
|
|
6
1
|
/**
|
|
7
2
|
* The type of arguments of the `escapeValues` and `escape` helpers.
|
|
8
3
|
*/
|
|
@@ -11,12 +6,12 @@ export type EscapeMap = Map<any, (value?: any) => string>;
|
|
|
11
6
|
/**
|
|
12
7
|
* A generic helper for escaping `values` by given `escapeMap`.
|
|
13
8
|
*/
|
|
14
|
-
export declare const escapeValues: (escapeMap: EscapeMap, values: any
|
|
9
|
+
export declare const escapeValues: (escapeMap: EscapeMap, values: Partial<Array<any>>) => Partial<Array<string>>;
|
|
15
10
|
|
|
16
11
|
/**
|
|
17
12
|
* A generic helper for escaping `values` by given `escapeMap` (in *TemplateStrings* flavor).
|
|
18
13
|
*/
|
|
19
|
-
export declare const escape: (escapeMap: EscapeMap, template: TemplateStringsArray, ...values: any
|
|
14
|
+
export declare const escape: (escapeMap: EscapeMap, template: TemplateStringsArray, ...values: Partial<Array<any>>) => string;
|
|
20
15
|
|
|
21
16
|
/**
|
|
22
17
|
* A helper that implements typographic corrections specific to Polish typography.
|
|
@@ -49,9 +44,9 @@ export type HArgs = [string | Node, ...HArgs1[]];
|
|
|
49
44
|
* - All other arguments of type `HArgs` are passed to `h` and the results are appended to the element being created or modified.
|
|
50
45
|
*/
|
|
51
46
|
export declare const h: {
|
|
52
|
-
<T extends keyof HTMLElementTagNameMap>(tag: T, ...args1: HArgs1
|
|
53
|
-
<N extends Node>(node: N, ...args1: HArgs1
|
|
54
|
-
(tagOrNode: string | Node, ...args1: HArgs1
|
|
47
|
+
<T extends keyof HTMLElementTagNameMap>(tag: T, ...args1: Partial<Array<HArgs1>>): HTMLElementTagNameMap[T];
|
|
48
|
+
<N extends Node>(node: N, ...args1: Partial<Array<HArgs1>>): N;
|
|
49
|
+
(tagOrNode: string | Node, ...args1: Partial<Array<HArgs1>>): Node;
|
|
55
50
|
};
|
|
56
51
|
|
|
57
52
|
/**
|
|
@@ -70,15 +65,15 @@ export declare const h: {
|
|
|
70
65
|
* - All other arguments of type `HArgs` are passed to `s` and the results are appended to the element being created or modified.
|
|
71
66
|
*/
|
|
72
67
|
export declare const s: {
|
|
73
|
-
<T extends keyof SVGElementTagNameMap>(tag: T, ...args1: HArgs1
|
|
74
|
-
<N extends Node>(node: N, ...args1: HArgs1
|
|
75
|
-
(tagOrNode: string | Node, ...args1: HArgs1
|
|
68
|
+
<T extends keyof SVGElementTagNameMap>(tag: T, ...args1: Partial<Array<HArgs1>>): SVGElementTagNameMap[T];
|
|
69
|
+
<N extends Node>(node: N, ...args1: Partial<Array<HArgs1>>): N;
|
|
70
|
+
(tagOrNode: string | Node, ...args1: Partial<Array<HArgs1>>): Node;
|
|
76
71
|
};
|
|
77
72
|
|
|
78
73
|
/**
|
|
79
74
|
* A convenient shortcut for `s('svg', ['use', { 'xlink:href': '#' + id }], ...args)`.
|
|
80
75
|
*/
|
|
81
|
-
export declare const svgUse: (id: string, ...args: HArgs1
|
|
76
|
+
export declare const svgUse: (id: string, ...args: Partial<Array<HArgs1>>) => SVGSVGElement;
|
|
82
77
|
|
|
83
78
|
/**
|
|
84
79
|
* A replacement for the `in` operator (not to be confused with the `for-in` loop) that works properly.
|
|
@@ -89,6 +84,7 @@ export declare const has: (key: any, ref: any) => boolean;
|
|
|
89
84
|
* A helper that checks if the given argument is of a certain type.
|
|
90
85
|
*/
|
|
91
86
|
export declare const is: {
|
|
87
|
+
(type: ArrayConstructor, arg: any): arg is Partial<Array<any>>;
|
|
92
88
|
(type: BigIntConstructor, arg: any): arg is bigint;
|
|
93
89
|
(type: BooleanConstructor, arg: any): arg is boolean;
|
|
94
90
|
(type: NumberConstructor, arg: any): arg is number;
|
|
@@ -96,7 +92,7 @@ export declare const is: {
|
|
|
96
92
|
(type: StringConstructor, arg: any): arg is string;
|
|
97
93
|
(type: SymbolConstructor, arg: any): arg is symbol;
|
|
98
94
|
(type: undefined, arg: any): arg is undefined | null;
|
|
99
|
-
<T extends abstract new (...args: any
|
|
95
|
+
<T extends abstract new (...args: Partial<Array<any>>) => any>(type: T, arg: any): arg is InstanceType<T>;
|
|
100
96
|
};
|
|
101
97
|
|
|
102
98
|
/**
|
|
@@ -159,12 +155,12 @@ export declare const nanolightJs: (code: string) => HArgs1[];
|
|
|
159
155
|
/**
|
|
160
156
|
* A helper that implements TypeScript’s `Pick` utility type.
|
|
161
157
|
*/
|
|
162
|
-
export declare const pick: <T extends Partial<Record<PropertyKey, any>>, K extends (keyof T)[]>(obj: Partial<Record<PropertyKey, any>>, keys: any
|
|
158
|
+
export declare const pick: <T extends Partial<Record<PropertyKey, any>>, K extends (keyof T)[]>(obj: Partial<Record<PropertyKey, any>>, keys: Partial<Array<any>>) => Pick<T, K[number]>;
|
|
163
159
|
|
|
164
160
|
/**
|
|
165
161
|
* A helper that implements TypeScript’s `Omit` utility type.
|
|
166
162
|
*/
|
|
167
|
-
export declare const omit: <T extends Partial<Record<PropertyKey, any>>, K extends (keyof T)[]>(obj: Partial<Record<PropertyKey, any>>, keys: any
|
|
163
|
+
export declare const omit: <T extends Partial<Record<PropertyKey, any>>, K extends (keyof T)[]>(obj: Partial<Record<PropertyKey, any>>, keys: Partial<Array<any>>) => Omit<T, K[number]>;
|
|
168
164
|
|
|
169
165
|
/**
|
|
170
166
|
* A helper for choosing the correct singular and plural.
|
|
@@ -181,7 +177,7 @@ export declare const pro: (ref: any) => any;
|
|
|
181
177
|
*
|
|
182
178
|
* It returns an array of triples: `[«name», «prototype-name», «array-of-own-property-names»]`.
|
|
183
179
|
*/
|
|
184
|
-
export declare const refsInfo: (...refs: any
|
|
180
|
+
export declare const refsInfo: (...refs: Partial<Array<any>>) => [string, string, (string | undefined)[]][];
|
|
185
181
|
|
|
186
182
|
/**
|
|
187
183
|
* A helper that generates a UUID v1 identifier (with a creation timestamp).
|
package/nnn.js
CHANGED
|
@@ -1,38 +1,3 @@
|
|
|
1
|
-
// src/nnn/eq.ts
|
|
2
|
-
var eq = (x, y) => {
|
|
3
|
-
if (x === y) {
|
|
4
|
-
return true;
|
|
5
|
-
}
|
|
6
|
-
const xConstructor = x?.constructor;
|
|
7
|
-
if (xConstructor === y?.constructor) {
|
|
8
|
-
if (xConstructor === Number) {
|
|
9
|
-
return isNaN(x) && isNaN(y) || +x === +y;
|
|
10
|
-
}
|
|
11
|
-
if (xConstructor === Date) {
|
|
12
|
-
return +x === +y;
|
|
13
|
-
}
|
|
14
|
-
if (xConstructor === String || xConstructor === RegExp) {
|
|
15
|
-
return "" + x === "" + y;
|
|
16
|
-
}
|
|
17
|
-
if (xConstructor === Array) {
|
|
18
|
-
return x.length === y.length && x.every((item, index) => eq(item, y[index]));
|
|
19
|
-
}
|
|
20
|
-
if (xConstructor === Object) {
|
|
21
|
-
const keysOfX = Object.keys(x);
|
|
22
|
-
return keysOfX.length === Object.keys(y).length && keysOfX.every((key) => eq(x[key], y[key]));
|
|
23
|
-
}
|
|
24
|
-
if (xConstructor === Set || xConstructor === Map) {
|
|
25
|
-
if (x.size !== y.size) {
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
const xa = [...x];
|
|
29
|
-
const ya = [...y];
|
|
30
|
-
return xa.every((xv) => ya.find((yv) => eq(xv, yv)));
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return false;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
1
|
// src/nnn/escape.ts
|
|
37
2
|
var escapeValues = (escapeMap, values) => values.map((value) => (escapeMap.get(value?.constructor) ?? escapeMap.get(undefined))?.(value) ?? "");
|
|
38
3
|
var escape = (escapeMap, template, ...values) => String.raw(template, ...escapeValues(escapeMap, values));
|
|
@@ -203,8 +168,10 @@ var jsOnParse = (handlers, text) => JSON.parse(text, (key, value) => {
|
|
|
203
168
|
}
|
|
204
169
|
isSecondKey = true;
|
|
205
170
|
}
|
|
206
|
-
|
|
207
|
-
|
|
171
|
+
const handler = handlers[key];
|
|
172
|
+
const params = value[key];
|
|
173
|
+
if (is(Function, handler) && is(Array, params)) {
|
|
174
|
+
return handler(...params);
|
|
208
175
|
}
|
|
209
176
|
}
|
|
210
177
|
return value;
|
|
@@ -305,6 +272,5 @@ export {
|
|
|
305
272
|
h,
|
|
306
273
|
fixTypography,
|
|
307
274
|
escapeValues,
|
|
308
|
-
escape
|
|
309
|
-
eq
|
|
275
|
+
escape
|
|
310
276
|
};
|
package/package.json
CHANGED
|
@@ -3,9 +3,6 @@
|
|
|
3
3
|
"description": "Jackens’ JavaScript helpers.",
|
|
4
4
|
"homepage": "https://jackens.github.io/nnn/doc/",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"chart",
|
|
7
|
-
"chartable",
|
|
8
|
-
"charts",
|
|
9
6
|
"CSS-in-JS",
|
|
10
7
|
"deepEqual",
|
|
11
8
|
"DOM",
|
|
@@ -43,5 +40,5 @@
|
|
|
43
40
|
"types": "nnn.d.ts",
|
|
44
41
|
"name": "@jackens/nnn",
|
|
45
42
|
"type": "module",
|
|
46
|
-
"version": "2024.3.
|
|
43
|
+
"version": "2024.3.31"
|
|
47
44
|
}
|
package/readme.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Jackens’ JavaScript helpers.
|
|
4
4
|
|
|
5
|
-
<sub>Version: <code class="version">2024.3.
|
|
5
|
+
<sub>Version: <code class="version">2024.3.31</code></sub>
|
|
6
6
|
|
|
7
7
|
## Examples
|
|
8
8
|
|
|
@@ -44,7 +44,6 @@ import { «something» } from './node_modules/@jackens/nnn/nnn.js'
|
|
|
44
44
|
- `HArgs1`: The type of arguments of the `h` and `s` helpers.
|
|
45
45
|
- `JcNode`: The type of arguments of the `jc` helper.
|
|
46
46
|
- `JcRoot`: The type of arguments of the `jc` helper.
|
|
47
|
-
- `eq`: A helper that checks equality of the given arguments.
|
|
48
47
|
- `escape`: A generic helper for escaping `values` by given `escapeMap` (in *TemplateStrings* flavor).
|
|
49
48
|
- `escapeValues`: A generic helper for escaping `values` by given `escapeMap`.
|
|
50
49
|
- `fixTypography`: A helper that implements typographic corrections specific to Polish typography.
|
|
@@ -107,52 +106,10 @@ type JcRoot = Partial<Record<PropertyKey, JcNode>>;
|
|
|
107
106
|
|
|
108
107
|
The type of arguments of the `jc` helper.
|
|
109
108
|
|
|
110
|
-
### eq
|
|
111
|
-
|
|
112
|
-
```ts
|
|
113
|
-
const eq: (x: any, y: any) => boolean;
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
A helper that checks equality of the given arguments.
|
|
117
|
-
|
|
118
|
-
#### Usage Examples
|
|
119
|
-
|
|
120
|
-
```js
|
|
121
|
-
expect(eq(true, true)).toBeTrue()
|
|
122
|
-
expect(eq(NaN, NaN)).toBeTrue()
|
|
123
|
-
expect(eq(null, undefined)).toBeFalse()
|
|
124
|
-
expect(eq(42, 42)).toBeTrue()
|
|
125
|
-
expect(eq(42, new Number(42))).toBeTrue()
|
|
126
|
-
expect(eq(42, Number(42))).toBeTrue()
|
|
127
|
-
expect(eq(new Number(42), Number(42))).toBeTrue()
|
|
128
|
-
expect(eq(42, '42')).toBeFalse()
|
|
129
|
-
expect(eq('42', '42')).toBeTrue()
|
|
130
|
-
expect(eq('42', new String('42'))).toBeTrue()
|
|
131
|
-
expect(eq('42', String('42'))).toBeTrue()
|
|
132
|
-
expect(eq(String('42'), new String('42'))).toBeTrue()
|
|
133
|
-
expect(eq(/42/, /42/)).toBeTrue()
|
|
134
|
-
expect(eq(/42/, /42/g)).toBeFalse()
|
|
135
|
-
expect(eq(new Date(42), new Date(42))).toBeTrue()
|
|
136
|
-
expect(eq(new Date(), new Date(42))).toBeFalse()
|
|
137
|
-
expect(eq({ j: '42', c: 42 }, { c: 42, j: '42' })).toBeTrue()
|
|
138
|
-
expect(eq([42, '42'], [42, '42'])).toBeTrue()
|
|
139
|
-
expect(eq(new Set(['42', 42]), new Set([42, '42']))).toBeTrue()
|
|
140
|
-
expect(eq(new Set(['42', 42]), new Set([42]))).toBeFalse()
|
|
141
|
-
expect(eq(new Set([42, undefined]), new Set([42]))).toBeFalse()
|
|
142
|
-
expect(eq(
|
|
143
|
-
new Map([[{ j: 42 }, { J: '42' }], [{ c: 42 }, { C: '42' }]]),
|
|
144
|
-
new Map([[{ c: 42 }, { C: '42' }], [{ j: 42 }, { J: '42' }]])
|
|
145
|
-
)).toBeTrue()
|
|
146
|
-
expect(eq(
|
|
147
|
-
new Map([[{ j: 42 }, { J: '42' }], [{ c: 42 }, { C: '42' }]]),
|
|
148
|
-
new Map([[{ j: '42' }, { J: 42 }], [{ c: '42' }, { C: 42 }]])
|
|
149
|
-
)).toBeFalse()
|
|
150
|
-
```
|
|
151
|
-
|
|
152
109
|
### escape
|
|
153
110
|
|
|
154
111
|
```ts
|
|
155
|
-
const escape: (escapeMap: EscapeMap, template: TemplateStringsArray, ...values: any
|
|
112
|
+
const escape: (escapeMap: EscapeMap, template: TemplateStringsArray, ...values: Partial<Array<any>>) => string;
|
|
156
113
|
```
|
|
157
114
|
|
|
158
115
|
A generic helper for escaping `values` by given `escapeMap` (in *TemplateStrings* flavor).
|
|
@@ -162,7 +119,7 @@ A generic helper for escaping `values` by given `escapeMap` (in *TemplateStrings
|
|
|
162
119
|
```js
|
|
163
120
|
const escapeMap: EscapeMap = new Map([
|
|
164
121
|
[undefined, () => 'NULL'],
|
|
165
|
-
[Array, (values: any
|
|
122
|
+
[Array, (values: Partial<Array<any>>) => escapeValues(escapeMap, values).join(', ')],
|
|
166
123
|
[Boolean, (value: boolean) => `b'${+value}'`],
|
|
167
124
|
[Date, (value: Date) => `'${value.toISOString().replace(/^(.+)T(.+)\..*$/, '$1 $2')}'`],
|
|
168
125
|
[Number, (value: number) => `${value}`],
|
|
@@ -187,7 +144,7 @@ expect(actual).toStrictEqual(expected)
|
|
|
187
144
|
### escapeValues
|
|
188
145
|
|
|
189
146
|
```ts
|
|
190
|
-
const escapeValues: (escapeMap: EscapeMap, values: any
|
|
147
|
+
const escapeValues: (escapeMap: EscapeMap, values: Partial<Array<any>>) => Partial<Array<string>>;
|
|
191
148
|
```
|
|
192
149
|
|
|
193
150
|
A generic helper for escaping `values` by given `escapeMap`.
|
|
@@ -216,9 +173,9 @@ expect(p.innerHTML).toStrictEqual(
|
|
|
216
173
|
|
|
217
174
|
```ts
|
|
218
175
|
const h: {
|
|
219
|
-
<T extends keyof HTMLElementTagNameMap>(tag: T, ...args1: HArgs1
|
|
220
|
-
<N extends Node>(node: N, ...args1: HArgs1
|
|
221
|
-
(tagOrNode: string | Node, ...args1: HArgs1
|
|
176
|
+
<T extends keyof HTMLElementTagNameMap>(tag: T, ...args1: Partial<Array<HArgs1>>): HTMLElementTagNameMap[T];
|
|
177
|
+
<N extends Node>(node: N, ...args1: Partial<Array<HArgs1>>): N;
|
|
178
|
+
(tagOrNode: string | Node, ...args1: Partial<Array<HArgs1>>): Node;
|
|
222
179
|
};
|
|
223
180
|
```
|
|
224
181
|
|
|
@@ -347,6 +304,7 @@ expect(has('key', null)).toBeFalse()
|
|
|
347
304
|
|
|
348
305
|
```ts
|
|
349
306
|
const is: {
|
|
307
|
+
(type: ArrayConstructor, arg: any): arg is Partial<Array<any>>;
|
|
350
308
|
(type: BigIntConstructor, arg: any): arg is bigint;
|
|
351
309
|
(type: BooleanConstructor, arg: any): arg is boolean;
|
|
352
310
|
(type: NumberConstructor, arg: any): arg is number;
|
|
@@ -354,7 +312,7 @@ const is: {
|
|
|
354
312
|
(type: StringConstructor, arg: any): arg is string;
|
|
355
313
|
(type: SymbolConstructor, arg: any): arg is symbol;
|
|
356
314
|
(type: undefined, arg: any): arg is undefined | null;
|
|
357
|
-
<T extends abstract new (...args: any
|
|
315
|
+
<T extends abstract new (...args: Partial<Array<any>>) => any>(type: T, arg: any): arg is InstanceType<T>;
|
|
358
316
|
};
|
|
359
317
|
```
|
|
360
318
|
|
|
@@ -761,7 +719,7 @@ expect(nanolightJs(codeJs)).toStrictEqual([
|
|
|
761
719
|
### omit
|
|
762
720
|
|
|
763
721
|
```ts
|
|
764
|
-
const omit: <T extends Partial<Record<PropertyKey, any>>, K extends (keyof T)[]>(obj: Partial<Record<PropertyKey, any>>, keys: any
|
|
722
|
+
const omit: <T extends Partial<Record<PropertyKey, any>>, K extends (keyof T)[]>(obj: Partial<Record<PropertyKey, any>>, keys: Partial<Array<any>>) => Omit<T, K[number]>;
|
|
765
723
|
```
|
|
766
724
|
|
|
767
725
|
A helper that implements TypeScript’s `Omit` utility type.
|
|
@@ -777,7 +735,7 @@ expect(omit(obj, ['c'])).toStrictEqual({ a: 42, b: '42' })
|
|
|
777
735
|
### pick
|
|
778
736
|
|
|
779
737
|
```ts
|
|
780
|
-
const pick: <T extends Partial<Record<PropertyKey, any>>, K extends (keyof T)[]>(obj: Partial<Record<PropertyKey, any>>, keys: any
|
|
738
|
+
const pick: <T extends Partial<Record<PropertyKey, any>>, K extends (keyof T)[]>(obj: Partial<Record<PropertyKey, any>>, keys: Partial<Array<any>>) => Pick<T, K[number]>;
|
|
781
739
|
```
|
|
782
740
|
|
|
783
741
|
A helper that implements TypeScript’s `Pick` utility type.
|
|
@@ -857,7 +815,7 @@ expect(ref).toStrictEqual({ one: { two: { three: { four: 1234 } } } })
|
|
|
857
815
|
### refsInfo
|
|
858
816
|
|
|
859
817
|
```ts
|
|
860
|
-
const refsInfo: (...refs: any
|
|
818
|
+
const refsInfo: (...refs: Partial<Array<any>>) => [string, string, (string | undefined)[]][];
|
|
861
819
|
```
|
|
862
820
|
|
|
863
821
|
A helper that provides information about the given `refs`.
|
|
@@ -901,9 +859,9 @@ console.log(browserFingerprint())
|
|
|
901
859
|
|
|
902
860
|
```ts
|
|
903
861
|
const s: {
|
|
904
|
-
<T extends keyof SVGElementTagNameMap>(tag: T, ...args1: HArgs1
|
|
905
|
-
<N extends Node>(node: N, ...args1: HArgs1
|
|
906
|
-
(tagOrNode: string | Node, ...args1: HArgs1
|
|
862
|
+
<T extends keyof SVGElementTagNameMap>(tag: T, ...args1: Partial<Array<HArgs1>>): SVGElementTagNameMap[T];
|
|
863
|
+
<N extends Node>(node: N, ...args1: Partial<Array<HArgs1>>): N;
|
|
864
|
+
(tagOrNode: string | Node, ...args1: Partial<Array<HArgs1>>): Node;
|
|
907
865
|
};
|
|
908
866
|
```
|
|
909
867
|
|
|
@@ -924,7 +882,7 @@ A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style help
|
|
|
924
882
|
### svgUse
|
|
925
883
|
|
|
926
884
|
```ts
|
|
927
|
-
const svgUse: (id: string, ...args: HArgs1
|
|
885
|
+
const svgUse: (id: string, ...args: Partial<Array<HArgs1>>) => SVGSVGElement;
|
|
928
886
|
```
|
|
929
887
|
|
|
930
888
|
A convenient shortcut for `s('svg', ['use', { 'xlink:href': '#' + id }], ...args)`.
|