@symbo.ls/shorthand 2.34.32 → 2.34.34

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/README.md ADDED
@@ -0,0 +1,181 @@
1
+ # @symbo.ls/shorthand
2
+
3
+ Bidirectional shorthand transpiler for [Symbols](https://github.com/symbo-ls/smbls) component properties. Compresses DOMQL component objects using abbreviated property names and compact string encoding — and losslessly expands them back.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install @symbo.ls/shorthand
9
+ ```
10
+
11
+ ## API
12
+
13
+ Six functions in three complementary pairs:
14
+
15
+ | Pair | Forward | Inverse | Description |
16
+ | ---------- | ----------- | -------- | ------------------------------------------------------------ |
17
+ | **String** | `encode` | `decode` | Flat object ↔ single-line shorthand string |
18
+ | **Object** | `shorten` | `expand` | Recursive key abbreviation preserving structure |
19
+ | **Hybrid** | `stringify` | `parse` | Primitive props → `in` string, structural props stay as keys |
20
+
21
+ ### encode / decode
22
+
23
+ Converts flat primitive props into a compact single-line string.
24
+
25
+ ```js
26
+ import { encode, decode } from '@symbo.ls/shorthand'
27
+
28
+ encode({ padding: 'A B', background: 'red', hidden: true })
29
+ // → 'p:A_B bg:red hid'
30
+
31
+ decode('p:A_B bg:red hid')
32
+ // → { padding: 'A B', background: 'red', hidden: true }
33
+ ```
34
+
35
+ **Syntax rules:**
36
+
37
+ - `abbr:value` — key-value pair
38
+ - `_` — represents spaces inside values
39
+ - `,` — array separator (`ext:Flex,Box` → `extends: ['Flex', 'Box']`)
40
+ - bare `abbr` — boolean `true`
41
+ - `!abbr` — boolean `false`
42
+
43
+ Functions, objects, and other non-serializable values are skipped.
44
+
45
+ ### shorten / expand
46
+
47
+ Recursively abbreviates (or expands) property keys throughout the component tree while preserving the full object structure — child components, selectors, functions, arrays, and everything else stays intact.
48
+
49
+ ```js
50
+ import { shorten, expand } from '@symbo.ls/shorthand'
51
+
52
+ const component = {
53
+ extends: 'Flex',
54
+ padding: 'A B',
55
+ gap: 'C',
56
+ flexDirection: 'column',
57
+ onClick: (e, el) => {},
58
+ Header: { fontSize: 'B' },
59
+ ':hover': { background: 'blue' }
60
+ }
61
+
62
+ shorten(component)
63
+ // {
64
+ // ext: 'Flex',
65
+ // p: 'A B',
66
+ // g: 'C',
67
+ // fxd: 'column',
68
+ // '@ck': (e, el) => {},
69
+ // Header: { fs: 'B' },
70
+ // ':hover': { bg: 'blue' }
71
+ // }
72
+
73
+ expand(shorten(component)) // deeply equals original
74
+ ```
75
+
76
+ **Preservation rules:**
77
+
78
+ - **PascalCase keys** (child components) — key kept as-is, value recursed
79
+ - **Selector keys** (`:hover`, `@dark`, `.isActive`, `> *`) — key kept, value recursed
80
+ - **`state`, `scope`, `attr`, `style`, `data`, `context`, `query`, `class`** — values preserved as-is (no key abbreviation inside)
81
+ - **Functions** — preserved, only the key is shortened
82
+
83
+ ### stringify / parse
84
+
85
+ Hybrid encoding: flat primitive props go into a compact `in` string, while structural props (functions, nested objects, child components, selectors) remain as shortened object keys.
86
+
87
+ ```js
88
+ import { stringify, parse } from '@symbo.ls/shorthand'
89
+
90
+ const component = {
91
+ extends: 'Flex',
92
+ padding: 'A',
93
+ background: 'surface',
94
+ borderRadius: 'B',
95
+ onClick: (e, el) => {},
96
+ Header: { fontSize: 'B', color: 'title' }
97
+ }
98
+
99
+ stringify(component)
100
+ // {
101
+ // in: 'ext:Flex p:A bg:surface bdr:B',
102
+ // '@ck': (e, el) => {},
103
+ // Header: { in: 'fs:B c:title' }
104
+ // }
105
+
106
+ parse(stringify(component)) // deeply equals original
107
+ ```
108
+
109
+ **What goes into `in`:**
110
+
111
+ - String props (except `text`, `html`, `content`, `placeholder`, `src`, `href`)
112
+ - Boolean props
113
+ - Primitive arrays (length > 1)
114
+
115
+ **What stays as object keys:**
116
+
117
+ - Functions, `null`, `undefined`
118
+ - Nested objects, arrays of objects
119
+ - PascalCase children, selector keys
120
+ - Preserved keys (`state`, `scope`, `style`, etc.)
121
+ - Skip-inline keys (`text`, `html`, `content`, `placeholder`, `src`, `href`)
122
+ - Numbers (to preserve type through round-trip)
123
+ - Strings containing `,` or `_` (to avoid encoding ambiguity)
124
+
125
+ ## Registry
126
+
127
+ The package ships with 300+ bidirectional abbreviation mappings covering:
128
+
129
+ - **DOMQL core** — `extends` → `ext`, `childExtends` → `cex`, `state` → `st`, `tag` → `tg`
130
+ - **Symbols shorthand** — `flow` → `fl`, `align` → `aln`, `round` → `rnd`, `boxSize` → `bsz`
131
+ - **CSS properties** — `padding` → `p`, `background` → `bg`, `flexDirection` → `fxd`, `zIndex` → `zi`
132
+ - **HTML attributes** — `placeholder` → `phd`, `disabled` → `dis`, `required` → `req`
133
+ - **ARIA attributes** — `ariaLabel` → `alb`, `ariaHidden` → `ahid`, `role` → `role`
134
+ - **Events** — `onClick` → `@ck`, `onRender` → `@rn`, `onSubmit` → `@sm`, `onKeyDown` → `@kd`
135
+
136
+ Access the maps directly:
137
+
138
+ ```js
139
+ import { propToAbbr, abbrToProp } from '@symbo.ls/shorthand'
140
+
141
+ propToAbbr['padding'] // 'p'
142
+ propToAbbr['onClick'] // '@ck'
143
+ abbrToProp['bg'] // 'background'
144
+ abbrToProp['@rn'] // 'onRender'
145
+ ```
146
+
147
+ ### Helpers
148
+
149
+ ```js
150
+ import {
151
+ isComponentKey,
152
+ isSelectorKey,
153
+ PRESERVE_VALUE_KEYS,
154
+ SKIP_INLINE_KEYS
155
+ } from '@symbo.ls/shorthand'
156
+
157
+ isComponentKey('Header') // true (PascalCase)
158
+ isComponentKey('padding') // false
159
+
160
+ isSelectorKey(':hover') // true
161
+ isSelectorKey('@dark') // true
162
+ isSelectorKey('.isActive') // true
163
+ isSelectorKey('> *') // true
164
+ isSelectorKey('padding') // false
165
+ ```
166
+
167
+ ## Round-trip guarantee
168
+
169
+ All three pairs are lossless — the inverse function always reproduces the original:
170
+
171
+ ```js
172
+ decode(encode(obj)) // ≈ obj (flat primitives only)
173
+ expand(shorten(obj)) // ≡ obj (full structure)
174
+ parse(stringify(obj)) // ≡ obj (full structure)
175
+ ```
176
+
177
+ The test suite verifies round-trip correctness against 200+ real-world Symbols components.
178
+
179
+ ## License
180
+
181
+ ISC
@@ -0,0 +1,182 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var decode_exports = {};
20
+ __export(decode_exports, {
21
+ decode: () => decode,
22
+ expand: () => expand,
23
+ parse: () => parse
24
+ });
25
+ module.exports = __toCommonJS(decode_exports);
26
+ var import_registry = require("./registry.js");
27
+ function decode(str) {
28
+ if (!str || typeof str !== "string") return {};
29
+ const obj = {};
30
+ const tokens = tokenize(str);
31
+ for (const token of tokens) {
32
+ if (token.startsWith("!")) {
33
+ const abbr2 = token.slice(1);
34
+ const prop2 = import_registry.abbrToProp[abbr2] || abbr2;
35
+ obj[prop2] = false;
36
+ continue;
37
+ }
38
+ const colonIdx = token.indexOf(":");
39
+ if (colonIdx === -1) {
40
+ const prop2 = import_registry.abbrToProp[token] || token;
41
+ obj[prop2] = true;
42
+ continue;
43
+ }
44
+ const abbr = token.slice(0, colonIdx);
45
+ const rawVal = token.slice(colonIdx + 1);
46
+ const prop = import_registry.abbrToProp[abbr] || abbr;
47
+ if (rawVal.includes(",")) {
48
+ obj[prop] = rawVal.split(",").map(decodeValue);
49
+ continue;
50
+ }
51
+ obj[prop] = decodeValue(rawVal);
52
+ }
53
+ return obj;
54
+ }
55
+ function tokenize(str) {
56
+ return str.trim().split(/\s+/).filter(Boolean);
57
+ }
58
+ function decodeValue(val) {
59
+ const str = val.replace(/_/g, " ");
60
+ if (/^-?\d+(\.\d+)?$/.test(str)) {
61
+ return Number(str);
62
+ }
63
+ return str;
64
+ }
65
+ function decodeInlineValue(val) {
66
+ return val.replace(/_/g, " ");
67
+ }
68
+ function decodeInline(str) {
69
+ if (!str || typeof str !== "string") return {};
70
+ const obj = {};
71
+ const tokens = tokenize(str);
72
+ for (const token of tokens) {
73
+ if (token.startsWith("!")) {
74
+ const abbr2 = token.slice(1);
75
+ const prop2 = import_registry.abbrToProp[abbr2] || abbr2;
76
+ obj[prop2] = false;
77
+ continue;
78
+ }
79
+ const colonIdx = token.indexOf(":");
80
+ if (colonIdx === -1) {
81
+ const prop2 = import_registry.abbrToProp[token] || token;
82
+ obj[prop2] = true;
83
+ continue;
84
+ }
85
+ const abbr = token.slice(0, colonIdx);
86
+ const rawVal = token.slice(colonIdx + 1);
87
+ const prop = import_registry.abbrToProp[abbr] || abbr;
88
+ if (rawVal.includes(",")) {
89
+ obj[prop] = rawVal.split(",").map(decodeInlineValue);
90
+ continue;
91
+ }
92
+ obj[prop] = decodeInlineValue(rawVal);
93
+ }
94
+ return obj;
95
+ }
96
+ function expand(obj) {
97
+ if (!obj || typeof obj !== "object") return obj;
98
+ if (Array.isArray(obj)) {
99
+ return obj.map(function(item) {
100
+ if (item !== null && typeof item === "object") return expand(item);
101
+ return item;
102
+ });
103
+ }
104
+ const result = {};
105
+ for (const key in obj) {
106
+ const val = obj[key];
107
+ if ((0, import_registry.isComponentKey)(key)) {
108
+ result[key] = expandVal(val);
109
+ continue;
110
+ }
111
+ const fullKey = import_registry.abbrToProp[key] || key;
112
+ if ((0, import_registry.isSelectorKey)(key) && fullKey === key) {
113
+ result[key] = expandVal(val);
114
+ continue;
115
+ }
116
+ if (import_registry.PRESERVE_VALUE_KEYS.has(fullKey) || import_registry.PRESERVE_VALUE_KEYS.has(key)) {
117
+ result[fullKey] = val;
118
+ continue;
119
+ }
120
+ result[fullKey] = expandVal(val);
121
+ }
122
+ return result;
123
+ }
124
+ function expandVal(val) {
125
+ if (val === null || val === void 0) return val;
126
+ if (typeof val === "function") return val;
127
+ if (Array.isArray(val)) {
128
+ return val.map(function(item) {
129
+ if (item !== null && typeof item === "object") return expand(item);
130
+ return item;
131
+ });
132
+ }
133
+ if (typeof val === "object") return expand(val);
134
+ return val;
135
+ }
136
+ function parse(obj) {
137
+ if (!obj || typeof obj !== "object") return obj;
138
+ if (Array.isArray(obj)) {
139
+ return obj.map(function(item) {
140
+ if (item !== null && typeof item === "object") return parse(item);
141
+ return item;
142
+ });
143
+ }
144
+ const result = {};
145
+ if (typeof obj.in === "string") {
146
+ const decoded = decodeInline(obj.in);
147
+ for (const prop in decoded) {
148
+ result[prop] = decoded[prop];
149
+ }
150
+ }
151
+ for (const key in obj) {
152
+ if (key === "in") continue;
153
+ const val = obj[key];
154
+ if ((0, import_registry.isComponentKey)(key)) {
155
+ result[key] = parseVal(val);
156
+ continue;
157
+ }
158
+ const fullKey = import_registry.abbrToProp[key] || key;
159
+ if ((0, import_registry.isSelectorKey)(key) && fullKey === key) {
160
+ result[key] = parseVal(val);
161
+ continue;
162
+ }
163
+ if (import_registry.PRESERVE_VALUE_KEYS.has(fullKey) || import_registry.PRESERVE_VALUE_KEYS.has(key)) {
164
+ result[fullKey] = val;
165
+ continue;
166
+ }
167
+ result[fullKey] = parseVal(val);
168
+ }
169
+ return result;
170
+ }
171
+ function parseVal(val) {
172
+ if (val === null || val === void 0) return val;
173
+ if (typeof val === "function") return val;
174
+ if (Array.isArray(val)) {
175
+ return val.map(function(item) {
176
+ if (item !== null && typeof item === "object") return parse(item);
177
+ return item;
178
+ });
179
+ }
180
+ if (typeof val === "object") return parse(val);
181
+ return val;
182
+ }
@@ -0,0 +1,180 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var encode_exports = {};
20
+ __export(encode_exports, {
21
+ encode: () => encode,
22
+ shorten: () => shorten,
23
+ stringify: () => stringify
24
+ });
25
+ module.exports = __toCommonJS(encode_exports);
26
+ var import_registry = require("./registry.js");
27
+ function encode(obj) {
28
+ if (!obj || typeof obj !== "object") return "";
29
+ const tokens = [];
30
+ for (const prop in obj) {
31
+ const val = obj[prop];
32
+ if (typeof val === "function") continue;
33
+ if (val !== null && typeof val === "object" && !Array.isArray(val)) continue;
34
+ if (val === void 0) continue;
35
+ const abbr = import_registry.propToAbbr[prop] || prop;
36
+ if (val === true) {
37
+ tokens.push(abbr);
38
+ } else if (val === false) {
39
+ tokens.push("!" + abbr);
40
+ } else if (Array.isArray(val)) {
41
+ const items = val.map((item) => encodeValue(item));
42
+ tokens.push(abbr + ":" + items.join(","));
43
+ } else {
44
+ tokens.push(abbr + ":" + encodeValue(val));
45
+ }
46
+ }
47
+ return tokens.join(" ");
48
+ }
49
+ function encodeValue(val) {
50
+ const str = String(val);
51
+ return str.replace(/ /g, "_");
52
+ }
53
+ function shorten(obj) {
54
+ if (!obj || typeof obj !== "object") return obj;
55
+ if (Array.isArray(obj)) {
56
+ return obj.map(function(item) {
57
+ if (item !== null && typeof item === "object") return shorten(item);
58
+ return item;
59
+ });
60
+ }
61
+ const result = {};
62
+ for (const key in obj) {
63
+ const val = obj[key];
64
+ if ((0, import_registry.isComponentKey)(key)) {
65
+ result[key] = shortenVal(val);
66
+ continue;
67
+ }
68
+ if ((0, import_registry.isSelectorKey)(key)) {
69
+ result[key] = shortenVal(val);
70
+ continue;
71
+ }
72
+ const shortKey = import_registry.propToAbbr[key] || key;
73
+ if (import_registry.PRESERVE_VALUE_KEYS.has(key) || import_registry.PRESERVE_VALUE_KEYS.has(shortKey)) {
74
+ result[shortKey] = val;
75
+ continue;
76
+ }
77
+ result[shortKey] = shortenVal(val);
78
+ }
79
+ return result;
80
+ }
81
+ function shortenVal(val) {
82
+ if (val === null || val === void 0) return val;
83
+ if (typeof val === "function") return val;
84
+ if (Array.isArray(val)) {
85
+ return val.map(function(item) {
86
+ if (item !== null && typeof item === "object") return shorten(item);
87
+ return item;
88
+ });
89
+ }
90
+ if (typeof val === "object") return shorten(val);
91
+ return val;
92
+ }
93
+ function stringify(obj) {
94
+ if (!obj || typeof obj !== "object") return obj;
95
+ if (Array.isArray(obj)) {
96
+ return obj.map(function(item) {
97
+ if (item !== null && typeof item === "object") return stringify(item);
98
+ return item;
99
+ });
100
+ }
101
+ const result = {};
102
+ const tokens = [];
103
+ for (const key in obj) {
104
+ const val = obj[key];
105
+ if ((0, import_registry.isComponentKey)(key)) {
106
+ result[key] = stringifyVal(val);
107
+ continue;
108
+ }
109
+ if ((0, import_registry.isSelectorKey)(key)) {
110
+ result[key] = stringifyVal(val);
111
+ continue;
112
+ }
113
+ const shortKey = import_registry.propToAbbr[key] || key;
114
+ if (import_registry.PRESERVE_VALUE_KEYS.has(key) || import_registry.PRESERVE_VALUE_KEYS.has(shortKey)) {
115
+ result[shortKey] = val;
116
+ continue;
117
+ }
118
+ if (typeof val === "function") {
119
+ result[shortKey] = val;
120
+ continue;
121
+ }
122
+ if (val === null || val === void 0) {
123
+ result[shortKey] = val;
124
+ continue;
125
+ }
126
+ if (import_registry.SKIP_INLINE_KEYS.has(key) || import_registry.SKIP_INLINE_KEYS.has(shortKey)) {
127
+ result[shortKey] = val;
128
+ continue;
129
+ }
130
+ if (Array.isArray(val)) {
131
+ const hasObjects = val.some(function(item) {
132
+ return item !== null && typeof item === "object";
133
+ });
134
+ if (hasObjects) {
135
+ result[shortKey] = val.map(function(item) {
136
+ if (item !== null && typeof item === "object") return stringify(item);
137
+ return item;
138
+ });
139
+ continue;
140
+ }
141
+ if (val.length <= 1) {
142
+ result[shortKey] = val;
143
+ continue;
144
+ }
145
+ tokens.push(shortKey + ":" + val.map(encodeValue).join(","));
146
+ continue;
147
+ }
148
+ if (typeof val === "object") {
149
+ result[shortKey] = stringify(val);
150
+ continue;
151
+ }
152
+ if (val === true) {
153
+ tokens.push(shortKey);
154
+ } else if (val === false) {
155
+ tokens.push("!" + shortKey);
156
+ } else if (typeof val === "number") {
157
+ result[shortKey] = val;
158
+ } else if (typeof val === "string" && (val.includes(",") || val.includes("_"))) {
159
+ result[shortKey] = val;
160
+ } else {
161
+ tokens.push(shortKey + ":" + encodeValue(val));
162
+ }
163
+ }
164
+ if (tokens.length) {
165
+ result.in = tokens.join(" ");
166
+ }
167
+ return result;
168
+ }
169
+ function stringifyVal(val) {
170
+ if (val === null || val === void 0) return val;
171
+ if (typeof val === "function") return val;
172
+ if (Array.isArray(val)) {
173
+ return val.map(function(item) {
174
+ if (item !== null && typeof item === "object") return stringify(item);
175
+ return item;
176
+ });
177
+ }
178
+ if (typeof val === "object") return stringify(val);
179
+ return val;
180
+ }