@jackens/nnn 2023.8.25 → 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 +39 -22
  2. package/nnn.js +102 -80
  3. package/package.json +8 -1
package/nnn.d.ts CHANGED
@@ -1,15 +1,3 @@
1
- /**
2
- * The type of arguments of the `css` helper.
3
- */
4
- export type CNode = {
5
- [attributeOrSelector: string]: string | number | CNode;
6
- };
7
-
8
- /**
9
- * The type of arguments of the `css` helper.
10
- */
11
- export type CRoot = Record<string, CNode>;
12
-
13
1
  /**
14
2
  * The type of arguments of the `escapeValues` and `escape` helpers.
15
3
  */
@@ -24,17 +12,16 @@ export type HArgs = [
24
12
  ];
25
13
 
26
14
  /**
27
- * A simple CSS-in-JS helper.
28
- *
29
- * The `root` parameter provides a hierarchical description of CSS rules.
30
- *
31
- * - Keys of sub-objects whose values are NOT objects are treated as CSS attribute, and values are treated as values of those CSS attributes; the concatenation of keys of all parent objects is a CSS rule.
32
- * - All keys ignore the part starting with a splitter (default: `$$`) sign until the end of the key (e.g. `src$$1` → `src`, `@font-face$$1` → `@font-face`).
33
- * - In keys specifying CSS attribute, all uppercase letters are replaced by lowercase letters with an additional `-` character preceding them (e.g. `fontFamily` → `font-family`).
34
- * - Commas in keys that makes a CSS rule cause it to “split” and create separate rules for each part (e.g. `{div:{margin:1,'.a,.b,.c':{margin:2}}}` → `div{margin:1}div.a,div.b,div.c{margin:2}`).
35
- * - Top-level keys that begin with `@` are not concatenated with sub-object keys.
15
+ * The type of arguments of the `jcss` helper.
16
+ */
17
+ export type JcssNode = {
18
+ [attributeOrSelector: string]: string | number | JcssNode;
19
+ };
20
+
21
+ /**
22
+ * The type of arguments of the `jcss` helper.
36
23
  */
37
- export function css(root: CRoot, splitter?: string): string;
24
+ export type JcssRoot = Record<string, JcssNode>;
38
25
 
39
26
  /**
40
27
  * A helper for creating a chart based on a table (conf. <https://jackens.github.io/nnn/chartable/>).
@@ -136,6 +123,36 @@ export function is(type: SymbolConstructor, arg: any): arg is symbol;
136
123
  export function is(type: undefined, arg: any): arg is null | undefined;
137
124
  export function is<T extends abstract new (...args: any[]) => any>(type: T, arg: any): arg is InstanceType<T>;
138
125
 
126
+ /**
127
+ * A simple CSS-in-JS helper.
128
+ *
129
+ * The `root` parameter provides a hierarchical description of CSS rules.
130
+ *
131
+ * - Keys of sub-objects whose values are NOT objects are treated as CSS attribute, and values are treated as values of those CSS attributes; the concatenation of keys of all parent objects is a CSS rule.
132
+ * - All keys ignore the part starting with a splitter (default: `$$`) sign until the end of the key (e.g. `src$$1` → `src`, `@font-face$$1` → `@font-face`).
133
+ * - In keys specifying CSS attribute, all uppercase letters are replaced by lowercase letters with an additional `-` character preceding them (e.g. `fontFamily` → `font-family`).
134
+ * - Commas in keys that makes a CSS rule cause it to “split” and create separate rules for each part (e.g. `{div:{margin:1,'.a,.b,.c':{margin:2}}}` → `div{margin:1}div.a,div.b,div.c{margin:2}`).
135
+ * - Top-level keys that begin with `@` are not concatenated with sub-object keys.
136
+ */
137
+ export function jcss(root: JcssRoot, splitter?: string): string;
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
@@ -1,3 +1,7 @@
1
+ /**
2
+ * @typedef {Map<any, (value?: any) => string>} EscapeMap
3
+ */
4
+
1
5
  /**
2
6
  * @typedef {[
3
7
  * string | Node,
@@ -7,91 +11,14 @@
7
11
 
8
12
  /**
9
13
  * @typedef {{
10
- * [attributeOrSelector: string]: string | number | CNode;
11
- * }} CNode
12
- */
13
-
14
- /**
15
- * @typedef {Record<string, CNode>} CRoot
14
+ * [attributeOrSelector: string]: string | number | JcssNode;
15
+ * }} JcssNode
16
16
  */
17
17
 
18
18
  /**
19
- * @typedef {Map<any, (value?: any) => string>} EscapeMap
19
+ * @typedef {Record<string, JcssNode>} JcssRoot
20
20
  */
21
21
 
22
- const _css = (
23
- /** @type {CNode} */ node,
24
- /** @type {string} */ prefix,
25
- /** @type {string[]} */ result,
26
- /** @type {(text: string) => string} */ split
27
- ) => {
28
- const /** @type {[CNode | string[], string][]} */ queue = [[node, prefix]]
29
-
30
- while (queue.length) {
31
- const [style2, prefix2] = queue.shift() ?? []
32
-
33
- if (style2 == null || prefix2 == null) {
34
- continue
35
- }
36
-
37
- if (is(Array, style2)) {
38
- result.push(prefix2, prefix2 !== '' ? '{' : '', style2.join(';'), prefix2 !== '' ? '}' : '')
39
- } else {
40
- const /** @type {[CNode | string[], string][]} */ todo = []
41
- let /** @type {string[]} */ attributes = []
42
- let attributesPushed = false
43
-
44
- for (const key in style2) {
45
- const value = style2[key]
46
-
47
- if (is(String, value) || is(Number, value)) {
48
- if (!attributesPushed) {
49
- attributesPushed = true
50
- attributes = []
51
- todo.push([attributes, prefix2])
52
- }
53
-
54
- attributes.push(`${split(key).replace(/([A-Z])/g, (_, letter) => '-' + letter.toLowerCase())}:${value}`)
55
- } else {
56
- attributesPushed = false
57
-
58
- const /** @type {string[]} */ newPrefix = []
59
- const keySplitted = key.split(',')
60
-
61
- for (const prefixItem of prefix2.split(',')) {
62
- for (const keyItem of keySplitted) {
63
- newPrefix.push(prefixItem + keyItem)
64
- }
65
- }
66
-
67
- todo.push([value, newPrefix.join(',')])
68
- }
69
- }
70
-
71
- queue.unshift(...todo)
72
- }
73
- }
74
- }
75
-
76
- export const css = (/** @type {CRoot} */ root, splitter = '$$') => {
77
- const split = (/** @type {string} */ text) => text.split(splitter)[0]
78
- const /** @type {string[]} */ result = []
79
-
80
- for (const key in root) {
81
- const value = root[key]
82
-
83
- if (key[0] === '@') {
84
- result.push(split(key) + '{')
85
- _css(value, '', result, split)
86
- result.push('}')
87
- } else {
88
- _css(value, split(key), result, split)
89
- }
90
- }
91
-
92
- return result.join('')
93
- }
94
-
95
22
  const _COLORS = ['#e22', '#e73', '#fc3', '#ad4', '#4d9', '#3be', '#45d', '#c3e']
96
23
 
97
24
  /**
@@ -511,6 +438,101 @@ export const has = (/** @type {any} */ key, /** @type {any} */ ref) =>
511
438
  */
512
439
  export const is = (/** @type {T} */ type, /** @type {any} */ arg) => arg?.constructor === type
513
440
 
441
+ const _jcss = (
442
+ /** @type {JcssNode} */ node,
443
+ /** @type {string} */ prefix,
444
+ /** @type {string[]} */ result,
445
+ /** @type {(text: string) => string} */ split
446
+ ) => {
447
+ const /** @type {[JcssNode | string[], string][]} */ queue = [[node, prefix]]
448
+
449
+ while (queue.length) {
450
+ const [style2, prefix2] = queue.shift() ?? []
451
+
452
+ if (style2 == null || prefix2 == null) {
453
+ continue
454
+ }
455
+
456
+ if (is(Array, style2)) {
457
+ result.push(prefix2, prefix2 !== '' ? '{' : '', style2.join(';'), prefix2 !== '' ? '}' : '')
458
+ } else {
459
+ const /** @type {[JcssNode | string[], string][]} */ todo = []
460
+ let /** @type {string[]} */ attributes = []
461
+ let attributesPushed = false
462
+
463
+ for (const key in style2) {
464
+ const value = style2[key]
465
+
466
+ if (is(String, value) || is(Number, value)) {
467
+ if (!attributesPushed) {
468
+ attributesPushed = true
469
+ attributes = []
470
+ todo.push([attributes, prefix2])
471
+ }
472
+
473
+ attributes.push(`${split(key).replace(/([A-Z])/g, (_, letter) => '-' + letter.toLowerCase())}:${value}`)
474
+ } else {
475
+ attributesPushed = false
476
+
477
+ const /** @type {string[]} */ newPrefix = []
478
+ const keySplitted = key.split(',')
479
+
480
+ for (const prefixItem of prefix2.split(',')) {
481
+ for (const keyItem of keySplitted) {
482
+ newPrefix.push(prefixItem + keyItem)
483
+ }
484
+ }
485
+
486
+ todo.push([value, newPrefix.join(',')])
487
+ }
488
+ }
489
+
490
+ queue.unshift(...todo)
491
+ }
492
+ }
493
+ }
494
+
495
+ export const jcss = (/** @type {JcssRoot} */ root, splitter = '$$') => {
496
+ const split = (/** @type {string} */ text) => text.split(splitter)[0]
497
+ const /** @type {string[]} */ result = []
498
+
499
+ for (const key in root) {
500
+ const value = root[key]
501
+
502
+ if (key[0] === '@') {
503
+ result.push(split(key) + '{')
504
+ _jcss(value, '', result, split)
505
+ result.push('}')
506
+ } else {
507
+ _jcss(value, split(key), result, split)
508
+ }
509
+ }
510
+
511
+ return result.join('')
512
+ }
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
@@ -9,19 +9,26 @@
9
9
  "CSS-in-JS",
10
10
  "deepEqual",
11
11
  "DOM",
12
+ "eq",
12
13
  "equal",
13
14
  "escape",
15
+ "Gantt",
14
16
  "graph",
15
17
  "h",
18
+ "has",
16
19
  "highlight",
17
20
  "HyperScript",
18
21
  "in",
19
22
  "is",
23
+ "jcss",
24
+ "JSON",
25
+ "jsOnParse",
20
26
  "locale",
21
27
  "localization",
22
28
  "nanolight",
23
29
  "nnn",
24
30
  "RWD",
31
+ "SVG",
25
32
  "translation",
26
33
  "typography",
27
34
  "uuid",
@@ -33,5 +40,5 @@
33
40
  "types": "nnn.d.ts",
34
41
  "name": "@jackens/nnn",
35
42
  "type": "module",
36
- "version": "2023.8.25"
43
+ "version": "2023.8.27"
37
44
  }