@arbor-css/util 0.0.1
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/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/package.json +29 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a nested object to a flat mapping of
|
|
3
|
+
* concatenated keys to values. Pass a check to decide
|
|
4
|
+
* when to stop recursing.
|
|
5
|
+
*/
|
|
6
|
+
export declare function toFlatKeys<V = any>(obj: Record<string, any>, stop?: (value: any) => boolean, options?: {
|
|
7
|
+
separator?: string;
|
|
8
|
+
}, prefix?: string): Record<string, V>;
|
|
9
|
+
/**
|
|
10
|
+
* Takes a key like "action-primary-fg" and looks it up from a nested object.
|
|
11
|
+
* We don't necessarily know if a - is part of the next key or a separator,
|
|
12
|
+
* so we first try to find the longest matching key and then work our way down until we find a match or run out of keys.
|
|
13
|
+
*/
|
|
14
|
+
export declare function getByConcatKey(obj: Record<string, any>, concatKey: string, separator?: string): any;
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,CAAC,GAAG,GAAG,EACjC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACxB,IAAI,GAAE,CAAC,KAAK,EAAE,GAAG,KAAK,OACsB,EAC5C,OAAO,GAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAA;CAAO,EACpC,MAAM,SAAK,GACT,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAanB;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAC7B,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACxB,SAAS,EAAE,MAAM,EACjB,SAAS,SAAM,GACb,GAAG,CAeL"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a nested object to a flat mapping of
|
|
3
|
+
* concatenated keys to values. Pass a check to decide
|
|
4
|
+
* when to stop recursing.
|
|
5
|
+
*/
|
|
6
|
+
export function toFlatKeys(obj, stop = (value) => typeof value !== 'object' || value === null, options = {}, prefix = '') {
|
|
7
|
+
const flatObj = {};
|
|
8
|
+
const separator = options.separator ?? '-';
|
|
9
|
+
for (const key in obj) {
|
|
10
|
+
const value = obj[key];
|
|
11
|
+
const flatKey = prefix ? `${prefix}${separator}${key}` : key;
|
|
12
|
+
if (!stop(value)) {
|
|
13
|
+
Object.assign(flatObj, toFlatKeys(value, stop, options, flatKey));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
flatObj[flatKey] = value;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return flatObj;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Takes a key like "action-primary-fg" and looks it up from a nested object.
|
|
23
|
+
* We don't necessarily know if a - is part of the next key or a separator,
|
|
24
|
+
* so we first try to find the longest matching key and then work our way down until we find a match or run out of keys.
|
|
25
|
+
*/
|
|
26
|
+
export function getByConcatKey(obj, concatKey, separator = '-') {
|
|
27
|
+
const parts = concatKey.split(separator);
|
|
28
|
+
for (let i = parts.length; i > 0; i--) {
|
|
29
|
+
const key = parts.slice(0, i).join(separator);
|
|
30
|
+
if (key in obj) {
|
|
31
|
+
const value = obj[key];
|
|
32
|
+
if (i === parts.length) {
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
else if (typeof value === 'object' && value !== null) {
|
|
36
|
+
const restKey = parts.slice(i).join(separator);
|
|
37
|
+
return getByConcatKey(value, restKey, separator);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42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AAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAC9C,IAAI,GAAG,IAAI,GAAG,EAAE,CAAC;YAChB,MAAM,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;YACvB,IAAI,CAAC,KAAK,KAAK,CAAC,MAAM,EAAE,CAAC;gBACxB,OAAO,KAAK,CAAC;YACd,CAAC;iBAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBACxD,MAAM,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;gBAC/C,OAAO,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;YAClD,CAAC;QACF,CAAC;IACF,CAAC;IACD,OAAO,SAAS,CAAC;AAClB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@arbor-css/util",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"development": "./src/index.ts",
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"node": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"typescript": "^6.0.3"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc",
|
|
27
|
+
"dev": "tsc -w --preserveWatchOutput"
|
|
28
|
+
}
|
|
29
|
+
}
|