@jackens/nnn 2023.9.23 → 2023.10.13

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.
Files changed (3) hide show
  1. package/nnn.d.ts +5 -10
  2. package/nnn.js +47 -117
  3. package/package.json +1 -1
package/nnn.d.ts CHANGED
@@ -83,11 +83,6 @@ export function escapeValues(escapeMap: EscapeMap, values: any[]): string[];
83
83
  */
84
84
  export function fixTypography(node: Node): void;
85
85
 
86
- /**
87
- * A convenient helper for getting values of nested objects.
88
- */
89
- export function get(defaultValue: any, ref: any, ...keys: (string | number | symbol)[]): any;
90
-
91
86
  /**
92
87
  * A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style helper for creating and modifying `HTMLElement`s (see also `s`).
93
88
  *
@@ -181,6 +176,11 @@ export function nanolightJs(codeJs: string): HArgs[1][];
181
176
  */
182
177
  export function plUral(singular: string, plural2: string, plural5: string, value: number): string;
183
178
 
179
+ /**
180
+ * A helper that protect calls to nested properties by `Proxy` which initializes non-existent values with an empty object.
181
+ */
182
+ export function pro(ref: any): any;
183
+
184
184
  /**
185
185
  * A helper that provides information about the given `refs`.
186
186
  *
@@ -207,11 +207,6 @@ export function s<T extends keyof SVGElementTagNameMap>(tag: T, ...args: HArgs[1
207
207
  export function s<N extends Node>(node: N, ...args: HArgs[1][]): N;
208
208
  export function s(...args: HArgs): Node;
209
209
 
210
- /**
211
- * A convenient helper for setting values of nested objects.
212
- */
213
- export function set(value: any, ref: any, ...keys: (string | number | symbol)[]): void;
214
-
215
210
  /**
216
211
  * A helper that generates a UUID v1 identifier (with a creation timestamp).
217
212
  *
package/nnn.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable no-console */
2
2
 
3
3
  export const _test = {}
4
- export const _version = '2023.9.23'
4
+ export const _version = '2023.10.13'
5
5
 
6
6
  /**
7
7
  * ```ts
@@ -156,7 +156,8 @@ export const chartable = ({
156
156
  const value = col.innerText
157
157
 
158
158
  if (x >= 0 && z >= 0 && zxYNotPassed) {
159
- set(parseFloat(value), zxY, z, x)
159
+ zxY[z] = zxY[z] ?? []
160
+ zxY[z][x] = parseFloat(value)
160
161
  } else if (x >= 0 && z < 0 && xLabelsNotPassed) {
161
162
  xLabels[x] = value
162
163
  } else if (x < 0 && z >= 0 && zLabelsNotPassed) {
@@ -500,48 +501,6 @@ _test.fixTypography = () => {
500
501
  '(zob. https://\u200Bpl.\u200Bwikipedia.\u200Borg/\u200Bwiki/\u200BPangram).')
501
502
  }
502
503
 
503
- /**
504
- * ```ts
505
- * export function get(defaultValue: any, ref: any, ...keys: (string | number | symbol)[]): any;
506
- * ```
507
- */
508
-
509
- /**
510
- * A convenient helper for getting values of nested objects.
511
- */
512
- export const get = (
513
- /** @type {any} */ defaultValue,
514
- /** @type {any} */ ref,
515
- /** @type {(string | number | symbol)[]} */ ...keys
516
- ) => {
517
- for (const key of keys) {
518
- if (!has(key, ref)) {
519
- return defaultValue
520
- }
521
-
522
- ref = ref[key]
523
- }
524
-
525
- return ref
526
- }
527
-
528
- _test.get = () => {
529
- const ref = { one: { two: { 3: 'value' } } }
530
-
531
- console.assert(get('default', ref, 'one', 'two', 3) === 'value')
532
-
533
- // @ts-expect-error
534
- ref.one.two[3] = undefined
535
-
536
- console.assert(get('default', ref, 'one', 'two', 3) === undefined)
537
-
538
- // @ts-expect-error
539
- ref.one.two = {}
540
-
541
- console.assert(get('default', ref, 'one', 'two', 3) === 'default')
542
- console.assert(get('default', ref) === ref)
543
- }
544
-
545
504
  const /** @type {Record<string, string>} */ _NS = {
546
505
  xlink: 'http://www.w3.org/1999/xlink'
547
506
  }
@@ -1383,6 +1342,50 @@ _test.plUral = () => {
1383
1342
  console.assert(car(42) === 'cars')
1384
1343
  }
1385
1344
 
1345
+ /**
1346
+ * ```ts
1347
+ * export function pro(ref: any): any;
1348
+ * ```
1349
+ */
1350
+
1351
+ /**
1352
+ * A helper that protect calls to nested properties by `Proxy` which initializes non-existent values with an empty object.
1353
+ */
1354
+ // @ts-expect-error
1355
+ export const pro = (/** @type {any} */ ref) => new Proxy(ref, {
1356
+ get (target, key) {
1357
+ return pro(target[key] = target[key] ?? {})
1358
+ }
1359
+ })
1360
+
1361
+ _test.pro = () => {
1362
+ const ref = {}
1363
+
1364
+ pro(ref).one.two[3][4] = 1234
1365
+
1366
+ console.assert(eq(ref, { one: { two: { 3: { 4: 1234 } } } }))
1367
+
1368
+ pro(ref).one.two.tree = 123
1369
+
1370
+ console.assert(eq(ref, { one: { two: { 3: { 4: 1234 }, tree: 123 } } }))
1371
+
1372
+ pro(ref).one.two = undefined
1373
+
1374
+ console.assert(eq(ref, { one: { two: undefined } }))
1375
+
1376
+ delete pro(ref).one.two
1377
+
1378
+ console.assert(eq(ref, { one: {} }))
1379
+
1380
+ pro(ref).one.two.three.four
1381
+
1382
+ console.assert(eq(ref, { one: { two: { three: { four: {} } } } }))
1383
+
1384
+ pro(ref).one.two.three.four = 1234
1385
+
1386
+ console.assert(eq(ref, { one: { two: { three: { four: 1234 } } } }))
1387
+ }
1388
+
1386
1389
  /**
1387
1390
  * ```ts
1388
1391
  * export function refsInfo(...refs: any[]): [string, string, string[]][];
@@ -1468,79 +1471,6 @@ _test['refsInfo: browserFingerprint'] = () => {
1468
1471
  */
1469
1472
  export const s = _h('http://www.w3.org/2000/svg')
1470
1473
 
1471
- /**
1472
- * ```ts
1473
- * export function set(value: any, ref: any, ...keys: (string | number | symbol)[]): void;
1474
- * ```
1475
- */
1476
-
1477
- /**
1478
- * A convenient helper for setting values of nested objects.
1479
- */
1480
- export const set = (
1481
- /** @type {any} */ value,
1482
- /** @type {any} */ ref,
1483
- /** @type {(string | number | symbol)[]} */ ...keys
1484
- ) => {
1485
- const last = keys.length - 1
1486
-
1487
- if (last < 0) {
1488
- return
1489
- }
1490
-
1491
- for (let k = 0; k < last; ++k) {
1492
- const key = keys[k]
1493
-
1494
- if (!has(key, ref)) {
1495
- ref[key] = is(Number, keys[k + 1]) ? [] : {}
1496
- }
1497
-
1498
- ref = ref[key]
1499
- }
1500
-
1501
- ref[keys[last]] = value
1502
- }
1503
-
1504
- _test.set = () => {
1505
- const ref = {}
1506
-
1507
- set('value', ref, 'one', 'two', 3, 4)
1508
-
1509
- console.assert(eq(ref, {
1510
- one: {
1511
- two: [undefined, undefined, undefined, [
1512
- undefined, undefined, undefined, undefined, 'value']
1513
- ]
1514
- }
1515
- }))
1516
-
1517
- set(undefined, ref, 'one', 'two', 3, 4)
1518
-
1519
- console.assert(eq(ref, {
1520
- one: {
1521
- two: [undefined, undefined, undefined, [
1522
- undefined, undefined, undefined, undefined, undefined]
1523
- ]
1524
- }
1525
- }))
1526
-
1527
- set({}, ref, 'one', 'two', 3)
1528
-
1529
- console.assert(eq(ref, {
1530
- one: {
1531
- two: [undefined, undefined, undefined, {}]
1532
- }
1533
- }))
1534
-
1535
- set('value', ref)
1536
-
1537
- console.assert(eq(ref, {
1538
- one: {
1539
- two: [undefined, undefined, undefined, {}]
1540
- }
1541
- }))
1542
- }
1543
-
1544
1474
  const _ZEROS = '0'.repeat(16)
1545
1475
  let _counter = 0
1546
1476
 
package/package.json CHANGED
@@ -40,5 +40,5 @@
40
40
  "types": "nnn.d.ts",
41
41
  "name": "@jackens/nnn",
42
42
  "type": "module",
43
- "version": "2023.9.23"
43
+ "version": "2023.10.13"
44
44
  }