@jackens/nnn 2023.8.26 → 2023.8.27

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 +17 -0
  2. package/nnn.js +22 -0
  3. package/package.json +6 -2
package/nnn.d.ts CHANGED
@@ -136,6 +136,23 @@ export function is<T extends abstract new (...args: any[]) => any>(type: T, arg:
136
136
  */
137
137
  export function jcss(root: JcssRoot, splitter?: string): string;
138
138
 
139
+ /**
140
+ * `JSON.parse` with “JavaScript turned on”.
141
+ *
142
+ * Objects having *exactly* one property which is present in the `handlers` map, i.e. objects of the form:
143
+ *
144
+ * ```js
145
+ * { "«handlerName»": [«params»] }
146
+ * ```
147
+ *
148
+ * are replaced by the result of call
149
+ *
150
+ * ```js
151
+ * handlers['«handlerName»'](...«params»)
152
+ * ```
153
+ */
154
+ export function jsOnParse(handlers: Record<string, Function>, text: string): any;
155
+
139
156
  /**
140
157
  * Language translations helper.
141
158
  */
package/nnn.js CHANGED
@@ -511,6 +511,28 @@ export const jcss = (/** @type {JcssRoot} */ root, splitter = '$$') => {
511
511
  return result.join('')
512
512
  }
513
513
 
514
+ export const jsOnParse = (
515
+ /** @type {Record<string, Function>} */ handlers,
516
+ /** @type {string} */ text
517
+ ) => JSON.parse(text, (key, value) => {
518
+ if (is(Object, value)) {
519
+ let isSecondKey = false
520
+
521
+ for (key in value) {
522
+ if (isSecondKey) {
523
+ return value
524
+ }
525
+ isSecondKey = true
526
+ }
527
+
528
+ if (has(key, handlers) && is(Array, value[key])) {
529
+ return handlers[key](...value[key])
530
+ }
531
+ }
532
+
533
+ return value
534
+ })
535
+
514
536
  const _locale = (
515
537
  /** @type {Record<string, Record<string, string | Record<string, string>>>} */ locales,
516
538
  /** @type {string} */ language,
package/package.json CHANGED
@@ -12,19 +12,23 @@
12
12
  "eq",
13
13
  "equal",
14
14
  "escape",
15
- "gantt",
15
+ "Gantt",
16
16
  "graph",
17
17
  "h",
18
+ "has",
18
19
  "highlight",
19
20
  "HyperScript",
20
21
  "in",
21
22
  "is",
22
23
  "jcss",
24
+ "JSON",
25
+ "jsOnParse",
23
26
  "locale",
24
27
  "localization",
25
28
  "nanolight",
26
29
  "nnn",
27
30
  "RWD",
31
+ "SVG",
28
32
  "translation",
29
33
  "typography",
30
34
  "uuid",
@@ -36,5 +40,5 @@
36
40
  "types": "nnn.d.ts",
37
41
  "name": "@jackens/nnn",
38
42
  "type": "module",
39
- "version": "2023.8.26"
43
+ "version": "2023.8.27"
40
44
  }