@jackens/nnn 2023.8.25 → 2023.8.26

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 +22 -22
  2. package/nnn.js +80 -80
  3. package/package.json +4 -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.
36
16
  */
37
- export function css(root: CRoot, splitter?: string): string;
17
+ export type JcssNode = {
18
+ [attributeOrSelector: string]: string | number | JcssNode;
19
+ };
20
+
21
+ /**
22
+ * The type of arguments of the `jcss` helper.
23
+ */
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,19 @@ 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
139
  /**
140
140
  * Language translations helper.
141
141
  */
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
14
+ * [attributeOrSelector: string]: string | number | JcssNode;
15
+ * }} JcssNode
12
16
  */
13
17
 
14
18
  /**
15
- * @typedef {Record<string, CNode>} CRoot
19
+ * @typedef {Record<string, JcssNode>} JcssRoot
16
20
  */
17
21
 
18
- /**
19
- * @typedef {Map<any, (value?: any) => string>} EscapeMap
20
- */
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,79 @@ 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
514
  const _locale = (
515
515
  /** @type {Record<string, Record<string, string | Record<string, string>>>} */ locales,
516
516
  /** @type {string} */ language,
package/package.json CHANGED
@@ -9,14 +9,17 @@
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",
16
18
  "highlight",
17
19
  "HyperScript",
18
20
  "in",
19
21
  "is",
22
+ "jcss",
20
23
  "locale",
21
24
  "localization",
22
25
  "nanolight",
@@ -33,5 +36,5 @@
33
36
  "types": "nnn.d.ts",
34
37
  "name": "@jackens/nnn",
35
38
  "type": "module",
36
- "version": "2023.8.25"
39
+ "version": "2023.8.26"
37
40
  }