@symbo.ls/shorthand 2.34.34 → 3.2.7

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.
@@ -0,0 +1,167 @@
1
+ import {
2
+ abbrToProp,
3
+ PRESERVE_VALUE_KEYS,
4
+ isComponentKey,
5
+ isSelectorKey
6
+ } from "./registry.js";
7
+ function decode(str) {
8
+ if (!str || typeof str !== "string") return {};
9
+ const obj = {};
10
+ const tokens = tokenize(str);
11
+ for (const token of tokens) {
12
+ if (token.startsWith("!")) {
13
+ const abbr2 = token.slice(1);
14
+ const prop2 = abbrToProp[abbr2] || abbr2;
15
+ obj[prop2] = false;
16
+ continue;
17
+ }
18
+ const colonIdx = token.indexOf(":");
19
+ if (colonIdx === -1) {
20
+ const prop2 = abbrToProp[token] || token;
21
+ obj[prop2] = true;
22
+ continue;
23
+ }
24
+ const abbr = token.slice(0, colonIdx);
25
+ const rawVal = token.slice(colonIdx + 1);
26
+ const prop = abbrToProp[abbr] || abbr;
27
+ if (rawVal.includes(",")) {
28
+ obj[prop] = rawVal.split(",").map(decodeValue);
29
+ continue;
30
+ }
31
+ obj[prop] = decodeValue(rawVal);
32
+ }
33
+ return obj;
34
+ }
35
+ function tokenize(str) {
36
+ return str.trim().split(/\s+/).filter(Boolean);
37
+ }
38
+ function decodeValue(val) {
39
+ const str = val.replace(/_/g, " ");
40
+ if (/^-?\d+(\.\d+)?$/.test(str)) {
41
+ return Number(str);
42
+ }
43
+ return str;
44
+ }
45
+ function decodeInlineValue(val) {
46
+ return val.replace(/_/g, " ");
47
+ }
48
+ function decodeInline(str) {
49
+ if (!str || typeof str !== "string") return {};
50
+ const obj = {};
51
+ const tokens = tokenize(str);
52
+ for (const token of tokens) {
53
+ if (token.startsWith("!")) {
54
+ const abbr2 = token.slice(1);
55
+ const prop2 = abbrToProp[abbr2] || abbr2;
56
+ obj[prop2] = false;
57
+ continue;
58
+ }
59
+ const colonIdx = token.indexOf(":");
60
+ if (colonIdx === -1) {
61
+ const prop2 = abbrToProp[token] || token;
62
+ obj[prop2] = true;
63
+ continue;
64
+ }
65
+ const abbr = token.slice(0, colonIdx);
66
+ const rawVal = token.slice(colonIdx + 1);
67
+ const prop = abbrToProp[abbr] || abbr;
68
+ if (rawVal.includes(",")) {
69
+ obj[prop] = rawVal.split(",").map(decodeInlineValue);
70
+ continue;
71
+ }
72
+ obj[prop] = decodeInlineValue(rawVal);
73
+ }
74
+ return obj;
75
+ }
76
+ function expand(obj) {
77
+ if (!obj || typeof obj !== "object") return obj;
78
+ if (Array.isArray(obj)) {
79
+ return obj.map(function(item) {
80
+ if (item !== null && typeof item === "object") return expand(item);
81
+ return item;
82
+ });
83
+ }
84
+ const result = {};
85
+ for (const key in obj) {
86
+ const val = obj[key];
87
+ if (isComponentKey(key)) {
88
+ result[key] = expandVal(val);
89
+ continue;
90
+ }
91
+ const fullKey = abbrToProp[key] || key;
92
+ if (isSelectorKey(key) && fullKey === key) {
93
+ result[key] = expandVal(val);
94
+ continue;
95
+ }
96
+ if (PRESERVE_VALUE_KEYS.has(fullKey) || PRESERVE_VALUE_KEYS.has(key)) {
97
+ result[fullKey] = val;
98
+ continue;
99
+ }
100
+ result[fullKey] = expandVal(val);
101
+ }
102
+ return result;
103
+ }
104
+ function expandVal(val) {
105
+ if (val === null || val === void 0) return val;
106
+ if (typeof val === "function") return val;
107
+ if (Array.isArray(val)) {
108
+ return val.map(function(item) {
109
+ if (item !== null && typeof item === "object") return expand(item);
110
+ return item;
111
+ });
112
+ }
113
+ if (typeof val === "object") return expand(val);
114
+ return val;
115
+ }
116
+ function parse(obj) {
117
+ if (!obj || typeof obj !== "object") return obj;
118
+ if (Array.isArray(obj)) {
119
+ return obj.map(function(item) {
120
+ if (item !== null && typeof item === "object") return parse(item);
121
+ return item;
122
+ });
123
+ }
124
+ const result = {};
125
+ if (typeof obj.in === "string") {
126
+ const decoded = decodeInline(obj.in);
127
+ for (const prop in decoded) {
128
+ result[prop] = decoded[prop];
129
+ }
130
+ }
131
+ for (const key in obj) {
132
+ if (key === "in") continue;
133
+ const val = obj[key];
134
+ if (isComponentKey(key)) {
135
+ result[key] = parseVal(val);
136
+ continue;
137
+ }
138
+ const fullKey = abbrToProp[key] || key;
139
+ if (isSelectorKey(key) && fullKey === key) {
140
+ result[key] = parseVal(val);
141
+ continue;
142
+ }
143
+ if (PRESERVE_VALUE_KEYS.has(fullKey) || PRESERVE_VALUE_KEYS.has(key)) {
144
+ result[fullKey] = val;
145
+ continue;
146
+ }
147
+ result[fullKey] = parseVal(val);
148
+ }
149
+ return result;
150
+ }
151
+ function parseVal(val) {
152
+ if (val === null || val === void 0) return val;
153
+ if (typeof val === "function") return val;
154
+ if (Array.isArray(val)) {
155
+ return val.map(function(item) {
156
+ if (item !== null && typeof item === "object") return parse(item);
157
+ return item;
158
+ });
159
+ }
160
+ if (typeof val === "object") return parse(val);
161
+ return val;
162
+ }
163
+ export {
164
+ decode,
165
+ expand,
166
+ parse
167
+ };
@@ -0,0 +1,166 @@
1
+ import {
2
+ propToAbbr,
3
+ PRESERVE_VALUE_KEYS,
4
+ SKIP_INLINE_KEYS,
5
+ isComponentKey,
6
+ isSelectorKey
7
+ } from "./registry.js";
8
+ function encode(obj) {
9
+ if (!obj || typeof obj !== "object") return "";
10
+ const tokens = [];
11
+ for (const prop in obj) {
12
+ const val = obj[prop];
13
+ if (typeof val === "function") continue;
14
+ if (val !== null && typeof val === "object" && !Array.isArray(val)) continue;
15
+ if (val === void 0) continue;
16
+ const abbr = propToAbbr[prop] || prop;
17
+ if (val === true) {
18
+ tokens.push(abbr);
19
+ } else if (val === false) {
20
+ tokens.push("!" + abbr);
21
+ } else if (Array.isArray(val)) {
22
+ const items = val.map((item) => encodeValue(item));
23
+ tokens.push(abbr + ":" + items.join(","));
24
+ } else {
25
+ tokens.push(abbr + ":" + encodeValue(val));
26
+ }
27
+ }
28
+ return tokens.join(" ");
29
+ }
30
+ function encodeValue(val) {
31
+ const str = String(val);
32
+ return str.replace(/ /g, "_");
33
+ }
34
+ function shorten(obj) {
35
+ if (!obj || typeof obj !== "object") return obj;
36
+ if (Array.isArray(obj)) {
37
+ return obj.map(function(item) {
38
+ if (item !== null && typeof item === "object") return shorten(item);
39
+ return item;
40
+ });
41
+ }
42
+ const result = {};
43
+ for (const key in obj) {
44
+ const val = obj[key];
45
+ if (isComponentKey(key)) {
46
+ result[key] = shortenVal(val);
47
+ continue;
48
+ }
49
+ if (isSelectorKey(key)) {
50
+ result[key] = shortenVal(val);
51
+ continue;
52
+ }
53
+ const shortKey = propToAbbr[key] || key;
54
+ if (PRESERVE_VALUE_KEYS.has(key) || PRESERVE_VALUE_KEYS.has(shortKey)) {
55
+ result[shortKey] = val;
56
+ continue;
57
+ }
58
+ result[shortKey] = shortenVal(val);
59
+ }
60
+ return result;
61
+ }
62
+ function shortenVal(val) {
63
+ if (val === null || val === void 0) return val;
64
+ if (typeof val === "function") return val;
65
+ if (Array.isArray(val)) {
66
+ return val.map(function(item) {
67
+ if (item !== null && typeof item === "object") return shorten(item);
68
+ return item;
69
+ });
70
+ }
71
+ if (typeof val === "object") return shorten(val);
72
+ return val;
73
+ }
74
+ function stringify(obj) {
75
+ if (!obj || typeof obj !== "object") return obj;
76
+ if (Array.isArray(obj)) {
77
+ return obj.map(function(item) {
78
+ if (item !== null && typeof item === "object") return stringify(item);
79
+ return item;
80
+ });
81
+ }
82
+ const result = {};
83
+ const tokens = [];
84
+ for (const key in obj) {
85
+ const val = obj[key];
86
+ if (isComponentKey(key)) {
87
+ result[key] = stringifyVal(val);
88
+ continue;
89
+ }
90
+ if (isSelectorKey(key)) {
91
+ result[key] = stringifyVal(val);
92
+ continue;
93
+ }
94
+ const shortKey = propToAbbr[key] || key;
95
+ if (PRESERVE_VALUE_KEYS.has(key) || PRESERVE_VALUE_KEYS.has(shortKey)) {
96
+ result[shortKey] = val;
97
+ continue;
98
+ }
99
+ if (typeof val === "function") {
100
+ result[shortKey] = val;
101
+ continue;
102
+ }
103
+ if (val === null || val === void 0) {
104
+ result[shortKey] = val;
105
+ continue;
106
+ }
107
+ if (SKIP_INLINE_KEYS.has(key) || SKIP_INLINE_KEYS.has(shortKey)) {
108
+ result[shortKey] = val;
109
+ continue;
110
+ }
111
+ if (Array.isArray(val)) {
112
+ const hasObjects = val.some(function(item) {
113
+ return item !== null && typeof item === "object";
114
+ });
115
+ if (hasObjects) {
116
+ result[shortKey] = val.map(function(item) {
117
+ if (item !== null && typeof item === "object") return stringify(item);
118
+ return item;
119
+ });
120
+ continue;
121
+ }
122
+ if (val.length <= 1) {
123
+ result[shortKey] = val;
124
+ continue;
125
+ }
126
+ tokens.push(shortKey + ":" + val.map(encodeValue).join(","));
127
+ continue;
128
+ }
129
+ if (typeof val === "object") {
130
+ result[shortKey] = stringify(val);
131
+ continue;
132
+ }
133
+ if (val === true) {
134
+ tokens.push(shortKey);
135
+ } else if (val === false) {
136
+ tokens.push("!" + shortKey);
137
+ } else if (typeof val === "number") {
138
+ result[shortKey] = val;
139
+ } else if (typeof val === "string" && (val.includes(",") || val.includes("_"))) {
140
+ result[shortKey] = val;
141
+ } else {
142
+ tokens.push(shortKey + ":" + encodeValue(val));
143
+ }
144
+ }
145
+ if (tokens.length) {
146
+ result.in = tokens.join(" ");
147
+ }
148
+ return result;
149
+ }
150
+ function stringifyVal(val) {
151
+ if (val === null || val === void 0) return val;
152
+ if (typeof val === "function") return val;
153
+ if (Array.isArray(val)) {
154
+ return val.map(function(item) {
155
+ if (item !== null && typeof item === "object") return stringify(item);
156
+ return item;
157
+ });
158
+ }
159
+ if (typeof val === "object") return stringify(val);
160
+ return val;
161
+ }
162
+ export {
163
+ encode,
164
+ shorten,
165
+ stringify
166
+ };
@@ -0,0 +1,250 @@
1
+ import {
2
+ propToAbbr,
3
+ abbrToProp,
4
+ PRESERVE_VALUE_KEYS,
5
+ SKIP_INLINE_KEYS,
6
+ isComponentKey,
7
+ isSelectorKey
8
+ } from "./registry.js";
9
+ function escapeValue(val) {
10
+ const str = String(val);
11
+ return str.replace(/\\/g, "\\\\").replace(/_/g, "\\_").replace(/,/g, "\\,").replace(/ /g, "_");
12
+ }
13
+ function unescapeValue(val) {
14
+ let result = "";
15
+ let i = 0;
16
+ while (i < val.length) {
17
+ if (val[i] === "\\" && i + 1 < val.length) {
18
+ const next = val[i + 1];
19
+ if (next === "," || next === "_" || next === "\\") {
20
+ result += next;
21
+ i += 2;
22
+ continue;
23
+ }
24
+ }
25
+ if (val[i] === "_") {
26
+ result += " ";
27
+ } else {
28
+ result += val[i];
29
+ }
30
+ i++;
31
+ }
32
+ return result;
33
+ }
34
+ function splitOnUnescapedCommas(str) {
35
+ const parts = [];
36
+ let current = "";
37
+ let i = 0;
38
+ while (i < str.length) {
39
+ if (str[i] === "\\" && i + 1 < str.length) {
40
+ current += str[i] + str[i + 1];
41
+ i += 2;
42
+ continue;
43
+ }
44
+ if (str[i] === ",") {
45
+ parts.push(current);
46
+ current = "";
47
+ i++;
48
+ continue;
49
+ }
50
+ current += str[i];
51
+ i++;
52
+ }
53
+ parts.push(current);
54
+ return parts;
55
+ }
56
+ function canCollapse(val) {
57
+ if (val === null || val === void 0) return false;
58
+ if (typeof val !== "object" || Array.isArray(val)) return false;
59
+ const keys = Object.keys(val);
60
+ return keys.length === 1 && keys[0] === "in" && typeof val.in === "string";
61
+ }
62
+ function stringifyFurther(obj) {
63
+ if (!obj || typeof obj !== "object") return obj;
64
+ if (Array.isArray(obj)) {
65
+ return obj.map(function(item) {
66
+ if (item !== null && typeof item === "object")
67
+ return stringifyFurther(item);
68
+ return item;
69
+ });
70
+ }
71
+ const result = {};
72
+ const tokens = [];
73
+ for (const key in obj) {
74
+ const val = obj[key];
75
+ if (isComponentKey(key)) {
76
+ const child = stringifyFurtherVal(val);
77
+ result[key] = canCollapse(child) ? child.in : child;
78
+ continue;
79
+ }
80
+ if (isSelectorKey(key)) {
81
+ const child = stringifyFurtherVal(val);
82
+ result[key] = canCollapse(child) ? child.in : child;
83
+ continue;
84
+ }
85
+ const shortKey = propToAbbr[key] || key;
86
+ if (PRESERVE_VALUE_KEYS.has(key) || PRESERVE_VALUE_KEYS.has(shortKey)) {
87
+ result[shortKey] = val;
88
+ continue;
89
+ }
90
+ if (typeof val === "function") {
91
+ result[shortKey] = val;
92
+ continue;
93
+ }
94
+ if (val === null || val === void 0) {
95
+ result[shortKey] = val;
96
+ continue;
97
+ }
98
+ if (SKIP_INLINE_KEYS.has(key) || SKIP_INLINE_KEYS.has(shortKey)) {
99
+ result[shortKey] = val;
100
+ continue;
101
+ }
102
+ if (Array.isArray(val)) {
103
+ const hasObjects = val.some(function(item) {
104
+ return item !== null && typeof item === "object";
105
+ });
106
+ if (hasObjects) {
107
+ result[shortKey] = val.map(function(item) {
108
+ if (item !== null && typeof item === "object")
109
+ return stringifyFurther(item);
110
+ return item;
111
+ });
112
+ continue;
113
+ }
114
+ if (val.length <= 1) {
115
+ result[shortKey] = val;
116
+ continue;
117
+ }
118
+ tokens.push(shortKey + ":" + val.map(escapeValue).join(","));
119
+ continue;
120
+ }
121
+ if (typeof val === "object") {
122
+ result[shortKey] = stringifyFurther(val);
123
+ continue;
124
+ }
125
+ if (val === true) {
126
+ tokens.push(shortKey);
127
+ continue;
128
+ }
129
+ if (val === false) {
130
+ tokens.push("!" + shortKey);
131
+ continue;
132
+ }
133
+ if (typeof val === "number") {
134
+ tokens.push(shortKey + ":#" + val);
135
+ continue;
136
+ }
137
+ tokens.push(shortKey + ":" + escapeValue(val));
138
+ }
139
+ if (tokens.length) {
140
+ result.in = tokens.join(" ");
141
+ }
142
+ return result;
143
+ }
144
+ function stringifyFurtherVal(val) {
145
+ if (val === null || val === void 0) return val;
146
+ if (typeof val === "function") return val;
147
+ if (Array.isArray(val)) {
148
+ return val.map(function(item) {
149
+ if (item !== null && typeof item === "object")
150
+ return stringifyFurther(item);
151
+ return item;
152
+ });
153
+ }
154
+ if (typeof val === "object") return stringifyFurther(val);
155
+ return val;
156
+ }
157
+ function decodeFurtherValue(val) {
158
+ if (val.startsWith("#") && /^#-?\d+(\.\d+)?$/.test(val)) {
159
+ return Number(val.slice(1));
160
+ }
161
+ return unescapeValue(val);
162
+ }
163
+ function decodeFurtherInline(str) {
164
+ if (!str || typeof str !== "string") return {};
165
+ const obj = {};
166
+ const tokens = str.trim().split(/\s+/).filter(Boolean);
167
+ for (const token of tokens) {
168
+ if (token.startsWith("!")) {
169
+ const abbr2 = token.slice(1);
170
+ const prop2 = abbrToProp[abbr2] || abbr2;
171
+ obj[prop2] = false;
172
+ continue;
173
+ }
174
+ const colonIdx = token.indexOf(":");
175
+ if (colonIdx === -1) {
176
+ const prop2 = abbrToProp[token] || token;
177
+ obj[prop2] = true;
178
+ continue;
179
+ }
180
+ const abbr = token.slice(0, colonIdx);
181
+ const rawVal = token.slice(colonIdx + 1);
182
+ const prop = abbrToProp[abbr] || abbr;
183
+ const parts = splitOnUnescapedCommas(rawVal);
184
+ if (parts.length > 1) {
185
+ obj[prop] = parts.map(decodeFurtherValue);
186
+ continue;
187
+ }
188
+ obj[prop] = decodeFurtherValue(rawVal);
189
+ }
190
+ return obj;
191
+ }
192
+ function parseFurther(obj) {
193
+ if (!obj || typeof obj !== "object") return obj;
194
+ if (Array.isArray(obj)) {
195
+ return obj.map(function(item) {
196
+ if (item !== null && typeof item === "object") return parseFurther(item);
197
+ return item;
198
+ });
199
+ }
200
+ const result = {};
201
+ if (typeof obj.in === "string") {
202
+ const decoded = decodeFurtherInline(obj.in);
203
+ for (const prop in decoded) {
204
+ result[prop] = decoded[prop];
205
+ }
206
+ }
207
+ for (const key in obj) {
208
+ if (key === "in") continue;
209
+ const val = obj[key];
210
+ if (isComponentKey(key)) {
211
+ if (typeof val === "string") {
212
+ result[key] = decodeFurtherInline(val);
213
+ } else {
214
+ result[key] = parseFurtherVal(val);
215
+ }
216
+ continue;
217
+ }
218
+ const fullKey = abbrToProp[key] || key;
219
+ if (isSelectorKey(key) && fullKey === key) {
220
+ if (typeof val === "string") {
221
+ result[key] = decodeFurtherInline(val);
222
+ } else {
223
+ result[key] = parseFurtherVal(val);
224
+ }
225
+ continue;
226
+ }
227
+ if (PRESERVE_VALUE_KEYS.has(fullKey) || PRESERVE_VALUE_KEYS.has(key)) {
228
+ result[fullKey] = val;
229
+ continue;
230
+ }
231
+ result[fullKey] = parseFurtherVal(val);
232
+ }
233
+ return result;
234
+ }
235
+ function parseFurtherVal(val) {
236
+ if (val === null || val === void 0) return val;
237
+ if (typeof val === "function") return val;
238
+ if (Array.isArray(val)) {
239
+ return val.map(function(item) {
240
+ if (item !== null && typeof item === "object") return parseFurther(item);
241
+ return item;
242
+ });
243
+ }
244
+ if (typeof val === "object") return parseFurther(val);
245
+ return val;
246
+ }
247
+ export {
248
+ parseFurther,
249
+ stringifyFurther
250
+ };
@@ -0,0 +1,27 @@
1
+ import { encode, shorten, stringify } from "./encode.js";
2
+ import { decode, expand, parse } from "./decode.js";
3
+ import { stringifyFurther, parseFurther } from "./further.js";
4
+ import {
5
+ propToAbbr,
6
+ abbrToProp,
7
+ PRESERVE_VALUE_KEYS,
8
+ SKIP_INLINE_KEYS,
9
+ isComponentKey,
10
+ isSelectorKey
11
+ } from "./registry.js";
12
+ export {
13
+ PRESERVE_VALUE_KEYS,
14
+ SKIP_INLINE_KEYS,
15
+ abbrToProp,
16
+ decode,
17
+ encode,
18
+ expand,
19
+ isComponentKey,
20
+ isSelectorKey,
21
+ parse,
22
+ parseFurther,
23
+ propToAbbr,
24
+ shorten,
25
+ stringify,
26
+ stringifyFurther
27
+ };