@jackens/nnn 2024.2.19 → 2024.2.24
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 +220 -21
- package/nnn.js +419 -37
- package/package.json +3 -1
- package/readme.md +193 -186
- package/chartable.d.ts +0 -31
- package/chartable.js +0 -193
- package/eq.d.ts +0 -8
- package/eq.js +0 -83
- package/escape.d.ts +0 -18
- package/escape.js +0 -51
- package/fixTypography.d.ts +0 -8
- package/fixTypography.js +0 -61
- package/h.d.ts +0 -55
- package/h.js +0 -213
- package/has.d.ts +0 -9
- package/has.js +0 -40
- package/is.d.ts +0 -17
- package/is.js +0 -65
- package/jcss.d.ts +0 -33
- package/jcss.js +0 -293
- package/jsOnParse.d.ts +0 -20
- package/jsOnParse.js +0 -85
- package/locale.d.ts +0 -8
- package/locale.js +0 -39
- package/nanolight.d.ts +0 -6
- package/nanolight.js +0 -21
- package/nanolightJs.d.ts +0 -8
- package/nanolightJs.js +0 -29
- package/pick.d.ts +0 -14
- package/pick.js +0 -31
- package/plUral.d.ts +0 -8
- package/plUral.js +0 -39
- package/pro.d.ts +0 -8
- package/pro.js +0 -39
- package/refsInfo.d.ts +0 -11
- package/refsInfo.js +0 -57
- package/uuid1.d.ts +0 -16
- package/uuid1.js +0 -54
package/readme.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Jackens’ JavaScript helpers.
|
|
4
4
|
|
|
5
|
-
<sub>Version: <code class="version">2024.2.
|
|
5
|
+
<sub>Version: <code class="version">2024.2.24</code></sub>
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -61,7 +61,7 @@ import { «something» } from './node_modules/@jackens/nnn/nnn.js'
|
|
|
61
61
|
### EscapeMap
|
|
62
62
|
|
|
63
63
|
```ts
|
|
64
|
-
|
|
64
|
+
type EscapeMap = Map<any, (value?: any) => string>;
|
|
65
65
|
```
|
|
66
66
|
|
|
67
67
|
The type of arguments of the `escapeValues` and `escape` helpers.
|
|
@@ -69,7 +69,7 @@ The type of arguments of the `escapeValues` and `escape` helpers.
|
|
|
69
69
|
### HArgs
|
|
70
70
|
|
|
71
71
|
```ts
|
|
72
|
-
|
|
72
|
+
type HArgs = [string | Node, ...HArgs1[]];
|
|
73
73
|
```
|
|
74
74
|
|
|
75
75
|
The type of arguments of the `h` and `s` helpers.
|
|
@@ -77,7 +77,7 @@ The type of arguments of the `h` and `s` helpers.
|
|
|
77
77
|
### HArgs1
|
|
78
78
|
|
|
79
79
|
```ts
|
|
80
|
-
|
|
80
|
+
type HArgs1 = Partial<Record<PropertyKey, any>> | null | undefined | Node | string | number | HArgs;
|
|
81
81
|
```
|
|
82
82
|
|
|
83
83
|
The type of arguments of the `h` and `s` helpers.
|
|
@@ -85,7 +85,7 @@ The type of arguments of the `h` and `s` helpers.
|
|
|
85
85
|
### JcssNode
|
|
86
86
|
|
|
87
87
|
```ts
|
|
88
|
-
|
|
88
|
+
type JcssNode = {
|
|
89
89
|
[attributeOrSelector: string]: string | number | JcssNode | undefined;
|
|
90
90
|
};
|
|
91
91
|
```
|
|
@@ -95,7 +95,7 @@ The type of arguments of the `jcss` helper.
|
|
|
95
95
|
### JcssRoot
|
|
96
96
|
|
|
97
97
|
```ts
|
|
98
|
-
|
|
98
|
+
type JcssRoot = Partial<Record<PropertyKey, JcssNode>>;
|
|
99
99
|
```
|
|
100
100
|
|
|
101
101
|
The type of arguments of the `jcss` helper.
|
|
@@ -103,19 +103,20 @@ The type of arguments of the `jcss` helper.
|
|
|
103
103
|
### chartable
|
|
104
104
|
|
|
105
105
|
```ts
|
|
106
|
-
|
|
106
|
+
const chartable: ({ table, headerColumn, xGap, xLabelsMinHeight, xLabelsRotate, xReverse, yGap, yLabelsLeftMinWidth, yLabelsRightMinWidth, yMax, zLabelsMinWidth, zyMappings }: {
|
|
107
107
|
table: HTMLTableElement;
|
|
108
|
-
headerColumn?: boolean;
|
|
109
|
-
xGap?: number;
|
|
110
|
-
|
|
111
|
-
xLabelsRotate?: boolean;
|
|
112
|
-
xReverse?: boolean;
|
|
113
|
-
yGap?: number;
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
yMax?: number;
|
|
117
|
-
|
|
118
|
-
|
|
108
|
+
headerColumn?: boolean | undefined;
|
|
109
|
+
xGap?: number | undefined;
|
|
110
|
+
xLabelsMinHeight?: number | undefined;
|
|
111
|
+
xLabelsRotate?: boolean | undefined;
|
|
112
|
+
xReverse?: boolean | undefined;
|
|
113
|
+
yGap?: number | undefined;
|
|
114
|
+
yLabelsLeftMinWidth?: number | undefined;
|
|
115
|
+
yLabelsRightMinWidth?: number | undefined;
|
|
116
|
+
yMax?: number | undefined;
|
|
117
|
+
zLabelsMinWidth?: number | undefined;
|
|
118
|
+
zyMappings?: [(label: string) => number, (value: number) => string][] | undefined;
|
|
119
|
+
}) => SVGSVGElement;
|
|
119
120
|
```
|
|
120
121
|
|
|
121
122
|
A helper for creating a chart based on a table (conf. <https://jackens.github.io/nnn/chartable/>).
|
|
@@ -124,19 +125,20 @@ Options:
|
|
|
124
125
|
- `table`: `HTMLTableElement` to extract data, data series labels and X axis labels
|
|
125
126
|
- `headerColumn`: flag indicating that `table` has a header column with X axis labels
|
|
126
127
|
- `xGap`: X axis spacing
|
|
127
|
-
- `
|
|
128
|
+
- `xLabelsMinHeight`: minimal height of X axis labels
|
|
128
129
|
- `xLabelsRotate`: flag to rotate X axis labels
|
|
129
130
|
- `xReverse`: flag to reverse all data series
|
|
130
131
|
- `yGap`: Y axis spacing
|
|
131
|
-
- `
|
|
132
|
-
- `
|
|
132
|
+
- `yLabelsLeftMinWidth`: minimal width of Y axis left labels
|
|
133
|
+
- `yLabelsRightMinWidth`: minimal width of Y axis right labels
|
|
133
134
|
- `yMax`: number of Y axis lines
|
|
134
|
-
- `
|
|
135
|
+
- `zLabelsMinWidth`: minimal width of data series labels
|
|
136
|
+
- `zyMappings`: mappings per data series
|
|
135
137
|
|
|
136
138
|
### eq
|
|
137
139
|
|
|
138
140
|
```ts
|
|
139
|
-
|
|
141
|
+
const eq: (x: any, y: any) => boolean;
|
|
140
142
|
```
|
|
141
143
|
|
|
142
144
|
A helper that checks equality of the given arguments.
|
|
@@ -144,41 +146,41 @@ A helper that checks equality of the given arguments.
|
|
|
144
146
|
#### Usage Examples
|
|
145
147
|
|
|
146
148
|
```js
|
|
147
|
-
expect(eq(true, true)).
|
|
148
|
-
expect(eq(NaN, NaN)).
|
|
149
|
-
expect(eq(null, undefined)).
|
|
150
|
-
expect(eq(42, 42)).
|
|
151
|
-
expect(eq(42, new Number(42))).
|
|
152
|
-
expect(eq(42, Number(42))).
|
|
153
|
-
expect(eq(new Number(42), Number(42))).
|
|
154
|
-
expect(eq(42, '42')).
|
|
155
|
-
expect(eq('42', '42')).
|
|
156
|
-
expect(eq('42', new String('42'))).
|
|
157
|
-
expect(eq('42', String('42'))).
|
|
158
|
-
expect(eq(String('42'), new String('42'))).
|
|
159
|
-
expect(eq(/42/, /42/)).
|
|
160
|
-
expect(eq(/42/, /42/g)).
|
|
161
|
-
expect(eq(new Date(42), new Date(42))).
|
|
162
|
-
expect(eq(new Date(), new Date(42))).
|
|
163
|
-
expect(eq({ j: '42', c: 42 }, { c: 42, j: '42' })).
|
|
164
|
-
expect(eq([42, '42'], [42, '42'])).
|
|
165
|
-
expect(eq(new Set(['42', 42]), new Set([42, '42']))).
|
|
166
|
-
expect(eq(new Set(['42', 42]), new Set([42]))).
|
|
167
|
-
expect(eq(new Set([42, undefined]), new Set([42]))).
|
|
149
|
+
expect(eq(true, true)).toBeTrue()
|
|
150
|
+
expect(eq(NaN, NaN)).toBeTrue()
|
|
151
|
+
expect(eq(null, undefined)).toBeFalse()
|
|
152
|
+
expect(eq(42, 42)).toBeTrue()
|
|
153
|
+
expect(eq(42, new Number(42))).toBeTrue()
|
|
154
|
+
expect(eq(42, Number(42))).toBeTrue()
|
|
155
|
+
expect(eq(new Number(42), Number(42))).toBeTrue()
|
|
156
|
+
expect(eq(42, '42')).toBeFalse()
|
|
157
|
+
expect(eq('42', '42')).toBeTrue()
|
|
158
|
+
expect(eq('42', new String('42'))).toBeTrue()
|
|
159
|
+
expect(eq('42', String('42'))).toBeTrue()
|
|
160
|
+
expect(eq(String('42'), new String('42'))).toBeTrue()
|
|
161
|
+
expect(eq(/42/, /42/)).toBeTrue()
|
|
162
|
+
expect(eq(/42/, /42/g)).toBeFalse()
|
|
163
|
+
expect(eq(new Date(42), new Date(42))).toBeTrue()
|
|
164
|
+
expect(eq(new Date(), new Date(42))).toBeFalse()
|
|
165
|
+
expect(eq({ j: '42', c: 42 }, { c: 42, j: '42' })).toBeTrue()
|
|
166
|
+
expect(eq([42, '42'], [42, '42'])).toBeTrue()
|
|
167
|
+
expect(eq(new Set(['42', 42]), new Set([42, '42']))).toBeTrue()
|
|
168
|
+
expect(eq(new Set(['42', 42]), new Set([42]))).toBeFalse()
|
|
169
|
+
expect(eq(new Set([42, undefined]), new Set([42]))).toBeFalse()
|
|
168
170
|
expect(eq(
|
|
169
171
|
new Map([[{ j: 42 }, { J: '42' }], [{ c: 42 }, { C: '42' }]]),
|
|
170
172
|
new Map([[{ c: 42 }, { C: '42' }], [{ j: 42 }, { J: '42' }]])
|
|
171
|
-
)).
|
|
173
|
+
)).toBeTrue()
|
|
172
174
|
expect(eq(
|
|
173
175
|
new Map([[{ j: 42 }, { J: '42' }], [{ c: 42 }, { C: '42' }]]),
|
|
174
176
|
new Map([[{ j: '42' }, { J: 42 }], [{ c: '42' }, { C: 42 }]])
|
|
175
|
-
)).
|
|
177
|
+
)).toBeFalse()
|
|
176
178
|
```
|
|
177
179
|
|
|
178
180
|
### escape
|
|
179
181
|
|
|
180
182
|
```ts
|
|
181
|
-
|
|
183
|
+
const escape: (escapeMap: EscapeMap, template: TemplateStringsArray, ...values: any[]) => string;
|
|
182
184
|
```
|
|
183
185
|
|
|
184
186
|
A generic helper for escaping `values` by given `escapeMap` (in *TemplateStrings* flavor).
|
|
@@ -186,13 +188,13 @@ A generic helper for escaping `values` by given `escapeMap` (in *TemplateStrings
|
|
|
186
188
|
#### Usage Examples
|
|
187
189
|
|
|
188
190
|
```js
|
|
189
|
-
const
|
|
191
|
+
const escapeMap: EscapeMap = new Map([
|
|
190
192
|
[undefined, () => 'NULL'],
|
|
191
|
-
[Array, (
|
|
192
|
-
[Boolean, (
|
|
193
|
-
[Date, (
|
|
194
|
-
[Number, (
|
|
195
|
-
[String, (
|
|
193
|
+
[Array, (values: any[]) => escapeValues(escapeMap, values).join(', ')],
|
|
194
|
+
[Boolean, (value: boolean) => `b'${+value}'`],
|
|
195
|
+
[Date, (value: Date) => `'${value.toISOString().replace(/^(.+)T(.+)\..*$/, '$1 $2')}'`],
|
|
196
|
+
[Number, (value: number) => `${value}`],
|
|
197
|
+
[String, (value: string) => `'${value.replace(/'/g, "''")}'`]
|
|
196
198
|
])
|
|
197
199
|
|
|
198
200
|
const sql = escape.bind(null, escapeMap)
|
|
@@ -207,13 +209,13 @@ const expected = `
|
|
|
207
209
|
FROM table_name
|
|
208
210
|
WHERE column_name IN (b'1', NULL, NULL, 42, '42', '4''2', NULL, '1980-03-31 04:30:00')`
|
|
209
211
|
|
|
210
|
-
expect(actual).
|
|
212
|
+
expect(actual).toStrictEqual(expected)
|
|
211
213
|
```
|
|
212
214
|
|
|
213
215
|
### escapeValues
|
|
214
216
|
|
|
215
217
|
```ts
|
|
216
|
-
|
|
218
|
+
const escapeValues: (escapeMap: EscapeMap, values: any[]) => string[];
|
|
217
219
|
```
|
|
218
220
|
|
|
219
221
|
A generic helper for escaping `values` by given `escapeMap`.
|
|
@@ -221,7 +223,7 @@ A generic helper for escaping `values` by given `escapeMap`.
|
|
|
221
223
|
### fixTypography
|
|
222
224
|
|
|
223
225
|
```ts
|
|
224
|
-
|
|
226
|
+
const fixTypography: (node: Node) => void;
|
|
225
227
|
```
|
|
226
228
|
|
|
227
229
|
A helper that implements typographic corrections specific to Polish typography.
|
|
@@ -233,7 +235,7 @@ const p = h('p', 'Pchnąć w tę łódź jeża lub ośm skrzyń fig (zob. https:
|
|
|
233
235
|
|
|
234
236
|
fixTypography(p)
|
|
235
237
|
|
|
236
|
-
expect(p.innerHTML).
|
|
238
|
+
expect(p.innerHTML).toStrictEqual(
|
|
237
239
|
'Pchnąć <span style="white-space:nowrap">w </span>tę łódź jeża lub ośm skrzyń fig ' +
|
|
238
240
|
'(zob. https://\u200Bpl.\u200Bwikipedia.\u200Borg/\u200Bwiki/\u200BPangram).')
|
|
239
241
|
```
|
|
@@ -241,9 +243,11 @@ expect(p.innerHTML).to.deep.equal(
|
|
|
241
243
|
### h
|
|
242
244
|
|
|
243
245
|
```ts
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
246
|
+
const h: {
|
|
247
|
+
<T extends keyof HTMLElementTagNameMap>(tag: T, ...args1: HArgs1[]): HTMLElementTagNameMap[T];
|
|
248
|
+
<N extends Node>(node: N, ...args1: HArgs1[]): N;
|
|
249
|
+
(tagOrNode: string | Node, ...args1: HArgs1[]): Node;
|
|
250
|
+
};
|
|
247
251
|
```
|
|
248
252
|
|
|
249
253
|
A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style helper for creating and modifying `HTMLElement`s (see also `s`).
|
|
@@ -265,73 +269,73 @@ A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style help
|
|
|
265
269
|
```js
|
|
266
270
|
const b = h('b')
|
|
267
271
|
|
|
268
|
-
expect(b.outerHTML).
|
|
272
|
+
expect(b.outerHTML).toStrictEqual('<b></b>')
|
|
269
273
|
|
|
270
274
|
const i = h('i', 'text')
|
|
271
275
|
|
|
272
276
|
h(b, i)
|
|
273
277
|
|
|
274
|
-
expect(i.outerHTML).
|
|
275
|
-
expect(b.outerHTML).
|
|
278
|
+
expect(i.outerHTML).toStrictEqual('<i>text</i>')
|
|
279
|
+
expect(b.outerHTML).toStrictEqual('<b><i>text</i></b>')
|
|
276
280
|
|
|
277
281
|
h(i, { $className: 'some class' })
|
|
278
282
|
|
|
279
|
-
expect(i.outerHTML).
|
|
280
|
-
expect(b.outerHTML).
|
|
283
|
+
expect(i.outerHTML).toStrictEqual('<i class="some class">text</i>')
|
|
284
|
+
expect(b.outerHTML).toStrictEqual('<b><i class="some class">text</i></b>')
|
|
281
285
|
```
|
|
282
286
|
|
|
283
287
|
```js
|
|
284
|
-
expect(h('span', 'text').outerHTML).
|
|
285
|
-
expect(h('span', { $innerText: 'text' }).outerHTML).
|
|
288
|
+
expect(h('span', 'text').outerHTML).toStrictEqual('<span>text</span>')
|
|
289
|
+
expect(h('span', { $innerText: 'text' }).outerHTML).toStrictEqual('<span>text</span>')
|
|
286
290
|
```
|
|
287
291
|
|
|
288
292
|
```js
|
|
289
293
|
expect(h('div', { style: 'margin:0;padding:0' }).outerHTML)
|
|
290
|
-
.
|
|
294
|
+
.toStrictEqual('<div style="margin:0;padding:0"></div>')
|
|
291
295
|
expect(h('div', { $style: 'margin:0;padding:0' }).outerHTML)
|
|
292
|
-
.
|
|
296
|
+
.toStrictEqual('<div style="margin: 0px; padding: 0px;"></div>')
|
|
293
297
|
expect(h('div', { $style: { margin: 0, padding: 0 } }).outerHTML)
|
|
294
|
-
.
|
|
298
|
+
.toStrictEqual('<div style="margin: 0px; padding: 0px;"></div>')
|
|
295
299
|
```
|
|
296
300
|
|
|
297
301
|
```js
|
|
298
302
|
const input1 = h('input', { value: 42 })
|
|
299
303
|
const input2 = h('input', { $value: '42' })
|
|
300
304
|
|
|
301
|
-
expect(input1.value).
|
|
302
|
-
expect(input2.value).
|
|
305
|
+
expect(input1.value).toStrictEqual('42')
|
|
306
|
+
expect(input2.value).toStrictEqual('42')
|
|
303
307
|
|
|
304
|
-
expect(input1.outerHTML).
|
|
305
|
-
expect(input2.outerHTML).
|
|
308
|
+
expect(input1.outerHTML).toStrictEqual('<input value="42">')
|
|
309
|
+
expect(input2.outerHTML).toStrictEqual('<input>')
|
|
306
310
|
|
|
307
311
|
const checkbox1 = h('input', { type: 'checkbox', checked: true })
|
|
308
312
|
const checkbox2 = h('input', { type: 'checkbox', $checked: true })
|
|
309
313
|
|
|
310
|
-
expect(checkbox1.checked).
|
|
311
|
-
expect(checkbox2.checked).
|
|
314
|
+
expect(checkbox1.checked).toBeTrue()
|
|
315
|
+
expect(checkbox2.checked).toBeTrue()
|
|
312
316
|
|
|
313
|
-
expect(checkbox1.outerHTML).
|
|
314
|
-
expect(checkbox2.outerHTML).
|
|
317
|
+
expect(checkbox1.outerHTML).toStrictEqual('<input type="checkbox" checked="">')
|
|
318
|
+
expect(checkbox2.outerHTML).toStrictEqual('<input type="checkbox">')
|
|
315
319
|
```
|
|
316
320
|
|
|
317
321
|
```js
|
|
318
322
|
const div = h('div')
|
|
319
323
|
|
|
320
|
-
expect(div.key).
|
|
324
|
+
expect(div.key).toBeUndefined()
|
|
321
325
|
|
|
322
326
|
h(div, { $key: { one: 1 } })
|
|
323
327
|
|
|
324
|
-
expect(div.key).
|
|
328
|
+
expect(div.key).toStrictEqual({ one: 1 })
|
|
325
329
|
|
|
326
330
|
h(div, { $key: { two: 2 } })
|
|
327
331
|
|
|
328
|
-
expect(div.key).
|
|
332
|
+
expect(div.key).toStrictEqual({ one: 1, two: 2 })
|
|
329
333
|
```
|
|
330
334
|
|
|
331
335
|
### has
|
|
332
336
|
|
|
333
337
|
```ts
|
|
334
|
-
|
|
338
|
+
const has: (key: any, ref: any) => boolean;
|
|
335
339
|
```
|
|
336
340
|
|
|
337
341
|
A replacement for the `in` operator (not to be confused with the `for-in` loop) that works properly.
|
|
@@ -341,17 +345,17 @@ A replacement for the `in` operator (not to be confused with the `for-in` loop)
|
|
|
341
345
|
```js
|
|
342
346
|
const obj = { key: 'K', null: 'N' }
|
|
343
347
|
|
|
344
|
-
expect('key' in obj).
|
|
345
|
-
expect(has('key', obj)).
|
|
348
|
+
expect('key' in obj).toBeTrue()
|
|
349
|
+
expect(has('key', obj)).toBeTrue()
|
|
346
350
|
|
|
347
|
-
expect('null' in obj).
|
|
348
|
-
expect(has('null', obj)).
|
|
351
|
+
expect('null' in obj).toBeTrue()
|
|
352
|
+
expect(has('null', obj)).toBeTrue()
|
|
349
353
|
|
|
350
|
-
expect(null in obj).
|
|
351
|
-
expect(has(null, obj)).
|
|
354
|
+
expect(null in obj).toBeTrue()
|
|
355
|
+
expect(has(null, obj)).toBeFalse()
|
|
352
356
|
|
|
353
|
-
expect('toString' in obj).
|
|
354
|
-
expect(has('toString', obj)).
|
|
357
|
+
expect('toString' in obj).toBeTrue()
|
|
358
|
+
expect(has('toString', obj)).toBeFalse()
|
|
355
359
|
```
|
|
356
360
|
|
|
357
361
|
```js
|
|
@@ -364,21 +368,20 @@ try {
|
|
|
364
368
|
}
|
|
365
369
|
|
|
366
370
|
expect(typeError instanceof TypeError) // Cannot use 'in' operator to search for 'key' in null
|
|
367
|
-
expect(has('key', null)).
|
|
371
|
+
expect(has('key', null)).toBeFalse()
|
|
368
372
|
```
|
|
369
373
|
|
|
370
374
|
### is
|
|
371
375
|
|
|
372
376
|
```ts
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
};
|
|
377
|
+
function is(type: BigIntConstructor, arg: any): arg is bigint;
|
|
378
|
+
function is(type: BooleanConstructor, arg: any): arg is boolean;
|
|
379
|
+
function is(type: NumberConstructor, arg: any): arg is number;
|
|
380
|
+
function is(type: ObjectConstructor, arg: any): arg is Partial<Record<PropertyKey, any>>;
|
|
381
|
+
function is(type: StringConstructor, arg: any): arg is string;
|
|
382
|
+
function is(type: SymbolConstructor, arg: any): arg is symbol;
|
|
383
|
+
function is(type: undefined, arg: any): arg is undefined | null;
|
|
384
|
+
function is<T extends abstract new (...args: any[]) => any>(type: T, arg: any): arg is InstanceType<T>;
|
|
382
385
|
```
|
|
383
386
|
|
|
384
387
|
A helper that checks if the given argument is of a certain type.
|
|
@@ -386,51 +389,51 @@ A helper that checks if the given argument is of a certain type.
|
|
|
386
389
|
#### Usage Examples
|
|
387
390
|
|
|
388
391
|
```js
|
|
389
|
-
expect(is(Number, 42)).
|
|
390
|
-
expect(is(Number, Number(42))).
|
|
391
|
-
expect(is(Number, new Number(42))).
|
|
392
|
-
expect(is(Number, NaN)).
|
|
393
|
-
expect(is(String, '42')).
|
|
394
|
-
expect(is(String, String('42'))).
|
|
395
|
-
expect(is(String, new String('42'))).
|
|
396
|
-
expect(is(Symbol, Symbol('42'))).
|
|
397
|
-
expect(is(Symbol, Object(Symbol('42')))).
|
|
398
|
-
expect(is(undefined, undefined)).
|
|
399
|
-
expect(is(undefined, null)).
|
|
400
|
-
expect(is(Object, {})).
|
|
401
|
-
expect(is(Array, [])).
|
|
402
|
-
expect(is(RegExp, /42/)).
|
|
403
|
-
expect(is(Date, new Date(42))).
|
|
404
|
-
expect(is(Set, new Set(['42', 42]))).
|
|
405
|
-
expect(is(Map, new Map([[{ j: 42 }, { J: '42' }], [{ c: 42 }, { C: '42' }]]))).
|
|
392
|
+
expect(is(Number, 42)).toBeTrue()
|
|
393
|
+
expect(is(Number, Number(42))).toBeTrue()
|
|
394
|
+
expect(is(Number, new Number(42))).toBeTrue()
|
|
395
|
+
expect(is(Number, NaN)).toBeTrue()
|
|
396
|
+
expect(is(String, '42')).toBeTrue()
|
|
397
|
+
expect(is(String, String('42'))).toBeTrue()
|
|
398
|
+
expect(is(String, new String('42'))).toBeTrue()
|
|
399
|
+
expect(is(Symbol, Symbol('42'))).toBeTrue()
|
|
400
|
+
expect(is(Symbol, Object(Symbol('42')))).toBeTrue()
|
|
401
|
+
expect(is(undefined, undefined)).toBeTrue()
|
|
402
|
+
expect(is(undefined, null)).toBeTrue()
|
|
403
|
+
expect(is(Object, {})).toBeTrue()
|
|
404
|
+
expect(is(Array, [])).toBeTrue()
|
|
405
|
+
expect(is(RegExp, /42/)).toBeTrue()
|
|
406
|
+
expect(is(Date, new Date(42))).toBeTrue()
|
|
407
|
+
expect(is(Set, new Set(['42', 42]))).toBeTrue()
|
|
408
|
+
expect(is(Map, new Map([[{ j: 42 }, { J: '42' }], [{ c: 42 }, { C: '42' }]]))).toBeTrue()
|
|
406
409
|
```
|
|
407
410
|
|
|
408
411
|
```js
|
|
409
|
-
const iz = (
|
|
412
|
+
const iz = (type: any, arg: any) => ({}).toString.call(arg).slice(8, -1) === type?.name
|
|
410
413
|
|
|
411
414
|
class FooBar { }
|
|
412
415
|
|
|
413
|
-
expect(is(FooBar, new FooBar())).
|
|
414
|
-
expect(iz(FooBar, new FooBar())).
|
|
416
|
+
expect(is(FooBar, new FooBar())).toBeTrue()
|
|
417
|
+
expect(iz(FooBar, new FooBar())).toBeFalse()
|
|
415
418
|
|
|
416
|
-
expect(is(Object, new FooBar())).
|
|
417
|
-
expect(iz(Object, new FooBar())).
|
|
419
|
+
expect(is(Object, new FooBar())).toBeFalse()
|
|
420
|
+
expect(iz(Object, new FooBar())).toBeTrue()
|
|
418
421
|
|
|
419
422
|
const fakeFooBar = {}
|
|
420
423
|
|
|
421
424
|
fakeFooBar[Symbol.toStringTag] = FooBar.name
|
|
422
425
|
|
|
423
|
-
expect(is(FooBar, fakeFooBar)).
|
|
424
|
-
expect(iz(FooBar, fakeFooBar)).
|
|
426
|
+
expect(is(FooBar, fakeFooBar)).toBeFalse()
|
|
427
|
+
expect(iz(FooBar, fakeFooBar)).toBeTrue()
|
|
425
428
|
|
|
426
|
-
expect(is(Object, fakeFooBar)).
|
|
427
|
-
expect(iz(Object, fakeFooBar)).
|
|
429
|
+
expect(is(Object, fakeFooBar)).toBeTrue()
|
|
430
|
+
expect(iz(Object, fakeFooBar)).toBeFalse()
|
|
428
431
|
```
|
|
429
432
|
|
|
430
433
|
### jcss
|
|
431
434
|
|
|
432
435
|
```ts
|
|
433
|
-
|
|
436
|
+
const jcss: (root: JcssRoot, splitter?: string) => string;
|
|
434
437
|
```
|
|
435
438
|
|
|
436
439
|
A simple CSS-in-JS helper.
|
|
@@ -468,7 +471,7 @@ a{
|
|
|
468
471
|
padding:1
|
|
469
472
|
}`.replace(/\n\s*/g, '')
|
|
470
473
|
|
|
471
|
-
expect(actual).
|
|
474
|
+
expect(actual).toStrictEqual(expected)
|
|
472
475
|
```
|
|
473
476
|
|
|
474
477
|
```js
|
|
@@ -496,7 +499,7 @@ a.b{
|
|
|
496
499
|
padding:1
|
|
497
500
|
}`.replace(/\n\s*/g, '')
|
|
498
501
|
|
|
499
|
-
expect(actual).
|
|
502
|
+
expect(actual).toStrictEqual(expected)
|
|
500
503
|
```
|
|
501
504
|
|
|
502
505
|
```js
|
|
@@ -571,7 +574,7 @@ div.c2{
|
|
|
571
574
|
}
|
|
572
575
|
}`.replace(/\n\s*/g, '')
|
|
573
576
|
|
|
574
|
-
expect(actual).
|
|
577
|
+
expect(actual).toStrictEqual(expected)
|
|
575
578
|
```
|
|
576
579
|
|
|
577
580
|
```js
|
|
@@ -594,7 +597,7 @@ a.b.d,a.c.d{
|
|
|
594
597
|
margin:2
|
|
595
598
|
}`.replace(/\n\s*/g, '')
|
|
596
599
|
|
|
597
|
-
expect(actual).
|
|
600
|
+
expect(actual).toStrictEqual(expected)
|
|
598
601
|
```
|
|
599
602
|
|
|
600
603
|
```js
|
|
@@ -615,7 +618,7 @@ const expected = `
|
|
|
615
618
|
margin:2
|
|
616
619
|
}`.replace(/\n\s*/g, '')
|
|
617
620
|
|
|
618
|
-
expect(actual).
|
|
621
|
+
expect(actual).toStrictEqual(expected)
|
|
619
622
|
```
|
|
620
623
|
|
|
621
624
|
```js
|
|
@@ -636,13 +639,13 @@ const expected = `
|
|
|
636
639
|
margin:2
|
|
637
640
|
}`.replace(/\n\s*/g, '')
|
|
638
641
|
|
|
639
|
-
expect(actual).
|
|
642
|
+
expect(actual).toStrictEqual(expected)
|
|
640
643
|
```
|
|
641
644
|
|
|
642
645
|
### jsOnParse
|
|
643
646
|
|
|
644
647
|
```ts
|
|
645
|
-
|
|
648
|
+
const jsOnParse: (handlers: Partial<Record<PropertyKey, Function>>, text: string) => any;
|
|
646
649
|
```
|
|
647
650
|
|
|
648
651
|
`JSON.parse` with “JavaScript turned on”.
|
|
@@ -663,9 +666,10 @@ handlers['«handlerName»'](...«params»)
|
|
|
663
666
|
|
|
664
667
|
```js
|
|
665
668
|
const handlers = {
|
|
666
|
-
$hello: (
|
|
669
|
+
$hello: (name: string) => `Hello ${name}!`,
|
|
667
670
|
$foo: () => 'bar'
|
|
668
671
|
}
|
|
672
|
+
|
|
669
673
|
const actual = jsOnParse(handlers, `[
|
|
670
674
|
{
|
|
671
675
|
"$hello": ["World"]
|
|
@@ -686,6 +690,7 @@ const actual = jsOnParse(handlers, `[
|
|
|
686
690
|
"two": 2
|
|
687
691
|
}
|
|
688
692
|
]`)
|
|
693
|
+
|
|
689
694
|
const expected = [
|
|
690
695
|
'Hello World!',
|
|
691
696
|
{
|
|
@@ -701,13 +706,13 @@ const expected = [
|
|
|
701
706
|
}
|
|
702
707
|
]
|
|
703
708
|
|
|
704
|
-
expect(actual).
|
|
709
|
+
expect(actual).toStrictEqual(expected)
|
|
705
710
|
```
|
|
706
711
|
|
|
707
712
|
### locale
|
|
708
713
|
|
|
709
714
|
```ts
|
|
710
|
-
|
|
715
|
+
const locale: (map: Partial<Record<PropertyKey, Partial<Record<PropertyKey, string>>>>, defaultVersion: string) => (text: string, version?: string) => string;
|
|
711
716
|
```
|
|
712
717
|
|
|
713
718
|
Language translations helper.
|
|
@@ -720,24 +725,24 @@ const _ = locale({
|
|
|
720
725
|
button: { Login: 'Zaloguj' }
|
|
721
726
|
}, 'default')
|
|
722
727
|
|
|
723
|
-
expect(_('Login')).
|
|
724
|
-
expect(_('Password')).
|
|
728
|
+
expect(_('Login')).toStrictEqual('Login')
|
|
729
|
+
expect(_('Password')).toStrictEqual('Hasło')
|
|
725
730
|
|
|
726
|
-
expect(_('Undefined text')).
|
|
731
|
+
expect(_('Undefined text')).toStrictEqual('Undefined text')
|
|
727
732
|
|
|
728
|
-
expect(_('Login', 'button')).
|
|
733
|
+
expect(_('Login', 'button')).toStrictEqual('Zaloguj')
|
|
729
734
|
|
|
730
|
-
expect(_('Password', 'undefined_version')).
|
|
731
|
-
expect(_('Undefined text', 'undefined_version')).
|
|
735
|
+
expect(_('Password', 'undefined_version')).toStrictEqual('Hasło')
|
|
736
|
+
expect(_('Undefined text', 'undefined_version')).toStrictEqual('Undefined text')
|
|
732
737
|
|
|
733
|
-
expect(_('toString')).
|
|
734
|
-
expect(_('toString', 'undefined_version')).
|
|
738
|
+
expect(_('toString')).toStrictEqual('toString')
|
|
739
|
+
expect(_('toString', 'undefined_version')).toStrictEqual('toString')
|
|
735
740
|
```
|
|
736
741
|
|
|
737
742
|
### nanolight
|
|
738
743
|
|
|
739
744
|
```ts
|
|
740
|
-
|
|
745
|
+
const nanolight: (pattern: RegExp, highlighters: ((chunk: string, index: number) => HArgs1)[], code: string) => HArgs1[];
|
|
741
746
|
```
|
|
742
747
|
|
|
743
748
|
A generic helper for syntax highlighting (see also `nanolightJs`).
|
|
@@ -745,7 +750,7 @@ A generic helper for syntax highlighting (see also `nanolightJs`).
|
|
|
745
750
|
### nanolightJs
|
|
746
751
|
|
|
747
752
|
```ts
|
|
748
|
-
|
|
753
|
+
const nanolightJs: (code: string) => HArgs1[];
|
|
749
754
|
```
|
|
750
755
|
|
|
751
756
|
A helper for highlighting JavaScript.
|
|
@@ -755,14 +760,14 @@ A helper for highlighting JavaScript.
|
|
|
755
760
|
```js
|
|
756
761
|
const codeJs = 'const answerToLifeTheUniverseAndEverything = 42'
|
|
757
762
|
|
|
758
|
-
expect(h('pre', ['code', ...nanolightJs(codeJs)]).outerHTML).
|
|
763
|
+
expect(h('pre', ['code', ...nanolightJs(codeJs)]).outerHTML).toStrictEqual(
|
|
759
764
|
'<pre><code><span class="keyword">const</span> <span class="literal">answerToLifeTheUniverseAndEverything</span> <span class="operator">=</span> <span class="number">42</span></code></pre>')
|
|
760
765
|
```
|
|
761
766
|
|
|
762
767
|
### omit
|
|
763
768
|
|
|
764
769
|
```ts
|
|
765
|
-
|
|
770
|
+
const omit: <T extends Partial<Record<PropertyKey, any>>, K extends (keyof T)[]>(obj: Partial<Record<PropertyKey, any>>, keys: any[]) => Omit<T, K[number]>;
|
|
766
771
|
```
|
|
767
772
|
|
|
768
773
|
A helper that implements TypeScript’s `Omit` utility type.
|
|
@@ -772,13 +777,13 @@ A helper that implements TypeScript’s `Omit` utility type.
|
|
|
772
777
|
```js
|
|
773
778
|
const obj = { a: 42, b: '42', c: 17 }
|
|
774
779
|
|
|
775
|
-
expect(omit(obj, ['c'])).
|
|
780
|
+
expect(omit(obj, ['c'])).toStrictEqual({ a: 42, b: '42' })
|
|
776
781
|
```
|
|
777
782
|
|
|
778
783
|
### pick
|
|
779
784
|
|
|
780
785
|
```ts
|
|
781
|
-
|
|
786
|
+
const pick: <T extends Partial<Record<PropertyKey, any>>, K extends (keyof T)[]>(obj: Partial<Record<PropertyKey, any>>, keys: any[]) => Pick<T, K[number]>;
|
|
782
787
|
```
|
|
783
788
|
|
|
784
789
|
A helper that implements TypeScript’s `Pick` utility type.
|
|
@@ -788,13 +793,13 @@ A helper that implements TypeScript’s `Pick` utility type.
|
|
|
788
793
|
```js
|
|
789
794
|
const obj = { a: 42, b: '42', c: 17 }
|
|
790
795
|
|
|
791
|
-
expect(pick(obj, ['a', 'b'])).
|
|
796
|
+
expect(pick(obj, ['a', 'b'])).toStrictEqual({ a: 42, b: '42' })
|
|
792
797
|
```
|
|
793
798
|
|
|
794
799
|
### plUral
|
|
795
800
|
|
|
796
801
|
```ts
|
|
797
|
-
|
|
802
|
+
const plUral: (singular: string, plural2: string, plural5: string, value: number) => string;
|
|
798
803
|
```
|
|
799
804
|
|
|
800
805
|
A helper for choosing the correct singular and plural.
|
|
@@ -804,23 +809,23 @@ A helper for choosing the correct singular and plural.
|
|
|
804
809
|
```js
|
|
805
810
|
const auto = plUral.bind(null, 'auto', 'auta', 'aut')
|
|
806
811
|
|
|
807
|
-
expect(auto(0)).
|
|
808
|
-
expect(auto(1)).
|
|
809
|
-
expect(auto(17)).
|
|
810
|
-
expect(auto(42)).
|
|
812
|
+
expect(auto(0)).toStrictEqual('aut')
|
|
813
|
+
expect(auto(1)).toStrictEqual('auto')
|
|
814
|
+
expect(auto(17)).toStrictEqual('aut')
|
|
815
|
+
expect(auto(42)).toStrictEqual('auta')
|
|
811
816
|
|
|
812
817
|
const car = plUral.bind(null, 'car', 'cars', 'cars')
|
|
813
818
|
|
|
814
|
-
expect(car(0)).
|
|
815
|
-
expect(car(1)).
|
|
816
|
-
expect(car(17)).
|
|
817
|
-
expect(car(42)).
|
|
819
|
+
expect(car(0)).toStrictEqual('cars')
|
|
820
|
+
expect(car(1)).toStrictEqual('car')
|
|
821
|
+
expect(car(17)).toStrictEqual('cars')
|
|
822
|
+
expect(car(42)).toStrictEqual('cars')
|
|
818
823
|
```
|
|
819
824
|
|
|
820
825
|
### pro
|
|
821
826
|
|
|
822
827
|
```ts
|
|
823
|
-
|
|
828
|
+
const pro: (ref: any) => any;
|
|
824
829
|
```
|
|
825
830
|
|
|
826
831
|
A helper that protects calls to nested properties by a `Proxy` that initializes non-existent values with an empty object.
|
|
@@ -832,33 +837,33 @@ const ref = {}
|
|
|
832
837
|
|
|
833
838
|
pro(ref).one.two[3][4] = 1234
|
|
834
839
|
|
|
835
|
-
expect(ref).
|
|
840
|
+
expect(ref).toStrictEqual({ one: { two: { 3: { 4: 1234 } } } })
|
|
836
841
|
|
|
837
842
|
pro(ref).one.two.tree = 123
|
|
838
843
|
|
|
839
|
-
expect(ref).
|
|
844
|
+
expect(ref).toStrictEqual({ one: { two: { 3: { 4: 1234 }, tree: 123 } } })
|
|
840
845
|
|
|
841
846
|
pro(ref).one.two = undefined
|
|
842
847
|
|
|
843
|
-
expect(ref).
|
|
848
|
+
expect(ref).toStrictEqual({ one: { two: undefined } })
|
|
844
849
|
|
|
845
850
|
delete pro(ref).one.two
|
|
846
851
|
|
|
847
|
-
expect(ref).
|
|
852
|
+
expect(ref).toStrictEqual({ one: {} })
|
|
848
853
|
|
|
849
854
|
pro(ref).one.two.three.four
|
|
850
855
|
|
|
851
|
-
expect(ref).
|
|
856
|
+
expect(ref).toStrictEqual({ one: { two: { three: { four: {} } } } })
|
|
852
857
|
|
|
853
858
|
pro(ref).one.two.three.four = 1234
|
|
854
859
|
|
|
855
|
-
expect(ref).
|
|
860
|
+
expect(ref).toStrictEqual({ one: { two: { three: { four: 1234 } } } })
|
|
856
861
|
```
|
|
857
862
|
|
|
858
863
|
### refsInfo
|
|
859
864
|
|
|
860
865
|
```ts
|
|
861
|
-
|
|
866
|
+
const refsInfo: (...refs: any[]) => [string, string, string[]][];
|
|
862
867
|
```
|
|
863
868
|
|
|
864
869
|
A helper that provides information about the given `refs`.
|
|
@@ -870,8 +875,8 @@ It returns an array of triples: `[«name», «prototype-name», «array-of-own-p
|
|
|
870
875
|
```js
|
|
871
876
|
const info = refsInfo(Array, Function)
|
|
872
877
|
|
|
873
|
-
expect(info.find(([name]) => name === 'Array')?.[2]?.includes('length')).
|
|
874
|
-
expect(info.find(([name]) => name === 'Function')?.[2]?.includes('length')).
|
|
878
|
+
expect(info.find(([name]) => name === 'Array')?.[2]?.includes('length')).toBeTrue()
|
|
879
|
+
expect(info.find(([name]) => name === 'Function')?.[2]?.includes('length')).toBeTrue()
|
|
875
880
|
```
|
|
876
881
|
|
|
877
882
|
```js
|
|
@@ -901,9 +906,11 @@ console.log(browserFingerprint())
|
|
|
901
906
|
### s
|
|
902
907
|
|
|
903
908
|
```ts
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
909
|
+
const s: {
|
|
910
|
+
<T extends keyof SVGElementTagNameMap>(tag: T, ...args1: HArgs1[]): SVGElementTagNameMap[T];
|
|
911
|
+
<N extends Node>(node: N, ...args1: HArgs1[]): N;
|
|
912
|
+
(tagOrNode: string | Node, ...args1: HArgs1[]): Node;
|
|
913
|
+
};
|
|
907
914
|
```
|
|
908
915
|
|
|
909
916
|
A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style helper for creating and modifying `SVGElement`s (see also `h`).
|
|
@@ -923,10 +930,10 @@ A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style help
|
|
|
923
930
|
### uuid1
|
|
924
931
|
|
|
925
932
|
```ts
|
|
926
|
-
|
|
933
|
+
const uuid1: ({ date, node }?: {
|
|
927
934
|
date?: Date | undefined;
|
|
928
935
|
node?: string | undefined;
|
|
929
|
-
})
|
|
936
|
+
}) => string;
|
|
930
937
|
```
|
|
931
938
|
|
|
932
939
|
A helper that generates a UUID v1 identifier (with a creation timestamp).
|
|
@@ -940,23 +947,23 @@ A helper that generates a UUID v1 identifier (with a creation timestamp).
|
|
|
940
947
|
for (let i = 1; i <= 22136; ++i) {
|
|
941
948
|
const uuid = uuid1()
|
|
942
949
|
|
|
943
|
-
i === 1 && expect(uuid.split('-')[3]).
|
|
944
|
-
i === 4095 && expect(uuid.split('-')[3]).
|
|
945
|
-
i === 4096 && expect(uuid.split('-')[3]).
|
|
946
|
-
i === 9029 && expect(uuid.split('-')[3]).
|
|
947
|
-
i === 13398 && expect(uuid.split('-')[3]).
|
|
948
|
-
i === 16384 && expect(uuid.split('-')[3]).
|
|
949
|
-
i === 17767 && expect(uuid.split('-')[3]).
|
|
950
|
+
i === 1 && expect(uuid.split('-')[3]).toStrictEqual('8001')
|
|
951
|
+
i === 4095 && expect(uuid.split('-')[3]).toStrictEqual('8fff')
|
|
952
|
+
i === 4096 && expect(uuid.split('-')[3]).toStrictEqual('9000')
|
|
953
|
+
i === 9029 && expect(uuid.split('-')[3]).toStrictEqual('a345')
|
|
954
|
+
i === 13398 && expect(uuid.split('-')[3]).toStrictEqual('b456')
|
|
955
|
+
i === 16384 && expect(uuid.split('-')[3]).toStrictEqual('8000')
|
|
956
|
+
i === 17767 && expect(uuid.split('-')[3]).toStrictEqual('8567')
|
|
950
957
|
}
|
|
951
958
|
```
|
|
952
959
|
|
|
953
960
|
```js
|
|
954
|
-
expect(uuid1({ node: '000123456789abc' }).split('-')[4]).
|
|
955
|
-
expect(uuid1({ node: '123456789' }).split('-')[4]).
|
|
961
|
+
expect(uuid1({ node: '000123456789abc' }).split('-')[4]).toStrictEqual('123456789abc')
|
|
962
|
+
expect(uuid1({ node: '123456789' }).split('-')[4]).toStrictEqual('000123456789')
|
|
956
963
|
```
|
|
957
964
|
|
|
958
965
|
```js
|
|
959
|
-
expect(uuid1({ date: new Date(323325000000) }).startsWith('c1399400-9a71-11bd')).
|
|
966
|
+
expect(uuid1({ date: new Date(323325000000) }).startsWith('c1399400-9a71-11bd')).toBeTrue()
|
|
960
967
|
```
|
|
961
968
|
|
|
962
969
|
## License
|