@bemedev/decompose 0.1.0
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/LICENSE +21 -0
- package/README.md +1 -0
- package/lib/constants/strings.d.ts +1 -0
- package/lib/decompose.d.ts +3 -0
- package/lib/index.cjs +2 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.esm.js +2 -0
- package/lib/index.esm.js.map +1 -0
- package/lib/index.modern.js +2 -0
- package/lib/index.modern.js.map +1 -0
- package/lib/index.umd.js +2 -0
- package/lib/index.umd.js.map +1 -0
- package/lib/sortMap.d.ts +1 -0
- package/lib/types.d.ts +14 -0
- package/package.json +70 -0
- package/src/constants/strings.ts +1 -0
- package/src/decompose.test.ts +71 -0
- package/src/decompose.ts +32 -0
- package/src/index.ts +3 -0
- package/src/sortMap.ts +12 -0
- package/src/types.ts +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 chlbri
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Package NPM
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DELIMITER: "-{/./:}-";
|
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
function e(e){return void 0===e&&(e="-{/./:}-"),function(t,r){return t.split(e).length-r.split(e).length||t.localeCompare(r)}}function t(e,r){void 0===r&&(r="");var n=r?r+"-{/./:}-":"",o=[];if(""!==r&&o.push(r),"string"==typeof e)o.push(""+n+e);else{var p=Object.keys(e);o.push.apply(o,p.map(function(r){return t(e[r],""+n+r)}).flat())}return o}exports.decompose=function(r,n){var o=t(r,"");o.sort(null!=n?n:e("-{/./:}-"));var p=new RegExp("-{/./:}-","g"),u=o.map(function(e){return e.replace(p,".")});return Object.freeze(u)},exports.sortMap=e;
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/sortMap.ts","../src/constants/strings.ts","../src/decompose.ts"],"sourcesContent":["import { DELIMITER } from './constants/strings';\n\nexport function sortMap(\n delimiter: string = DELIMITER,\n): ((a: string, b: string) => number) | undefined {\n return (a, b) => {\n return (\n a.split(delimiter).length - b.split(delimiter).length ||\n a.localeCompare(b)\n );\n };\n}\n","export const DELIMITER = '-{/./:}-' as const;\n","import { StateValue } from 'xstate';\nimport { DELIMITER } from './constants/strings';\nimport { sortMap } from './sortMap';\nimport { StateMatching } from './types';\n\nfunction ddecompose(val: StateValue, prev = '') {\n const _prev = prev ? prev + DELIMITER : '';\n const output: string[] = [];\n prev !== '' && output.push(prev);\n if (typeof val === 'string') {\n output.push(`${_prev}${val}`);\n } else {\n const keys = Object.keys(val);\n output.push(\n ...keys.map(key => ddecompose(val[key], `${_prev}${key}`)).flat(),\n );\n }\n\n return output;\n}\n\nexport function decompose<T extends StateValue>(\n val: T,\n sorter?: (a: string, b: string) => number,\n): readonly StateMatching<T>[] {\n const first = ddecompose(val, '');\n first.sort(sorter ?? sortMap(DELIMITER));\n const regex = new RegExp(DELIMITER, 'g');\n const output = first.map(value => value.replace(regex, '.'));\n\n return Object.freeze(output) as StateMatching<T>[];\n}\n"],"names":["sortMap","delimiter","a","b","split","length","localeCompare","ddecompose","val","prev","_prev","output","push","Object","keys","map","key","flat","sorter","first","sort","value","replace","regex","freeze"],"mappings":"AAEgBA,SAAAA,EACdC,GAEA,YAF6B,IAA7BA,IAAAA,ECHuB,YDKhB,SAACC,EAAGC,GACT,OACED,EAAEE,MAAMH,GAAWI,OAASF,EAAEC,MAAMH,GAAWI,QAC/CH,EAAEI,cAAcH,EAEnB,CACF,CEND,SAAAI,EAAoBC,EAAiBC,YAAAA,IAAAA,EAAO,IAC1C,IAAWC,EAAGD,EAAOA,EDNE,WCMiB,KACf,GAEzB,GADS,KAATA,GAAeE,EAAOC,KAAKH,GACR,mBACjBE,EAAOC,KAAP,GAAeF,EAAQF,OAClB,CACL,MAAaK,OAAOC,KAAKN,GACzBG,EAAOC,WAAPD,EACKG,EAAKC,IAAI,SAAAC,GAAOT,OAAAA,EAAWC,EAAIQ,GAAL,GAAcN,EAAQM,EAApC,GAA4CC,OAE9D,CAED,OAAON,CACR,mBAEe,SACdH,EACAU,GAEA,MAAcX,EAAWC,EAAK,IAC9BW,EAAMC,KAAN,MAAWF,EAAAA,EAAUlB,ED1BE,aC2BvB,MAAc,WD3BS,WC2Ba,KAC9BW,EAASQ,EAAMJ,IAAI,SAAAM,GAASA,OAAAA,EAAMC,QAAQC,EAAO,IAAzB,GAE9B,OAAOV,OAAOW,OAAOb,EACtB"}
|
package/lib/index.d.ts
ADDED
package/lib/index.esm.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
function e(e){return void 0===e&&(e="-{/./:}-"),function(n,t){return n.split(e).length-t.split(e).length||n.localeCompare(t)}}function n(e,t){void 0===t&&(t="");var r=t?t+"-{/./:}-":"",u=[];if(""!==t&&u.push(t),"string"==typeof e)u.push(""+r+e);else{var p=Object.keys(e);u.push.apply(u,p.map(function(t){return n(e[t],""+r+t)}).flat())}return u}function t(t,r){var u=n(t,"");u.sort(null!=r?r:e("-{/./:}-"));var p=new RegExp("-{/./:}-","g"),o=u.map(function(e){return e.replace(p,".")});return Object.freeze(o)}export{t as decompose,e as sortMap};
|
|
2
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/sortMap.ts","../src/constants/strings.ts","../src/decompose.ts"],"sourcesContent":["import { DELIMITER } from './constants/strings';\n\nexport function sortMap(\n delimiter: string = DELIMITER,\n): ((a: string, b: string) => number) | undefined {\n return (a, b) => {\n return (\n a.split(delimiter).length - b.split(delimiter).length ||\n a.localeCompare(b)\n );\n };\n}\n","export const DELIMITER = '-{/./:}-' as const;\n","import { StateValue } from 'xstate';\nimport { DELIMITER } from './constants/strings';\nimport { sortMap } from './sortMap';\nimport { StateMatching } from './types';\n\nfunction ddecompose(val: StateValue, prev = '') {\n const _prev = prev ? prev + DELIMITER : '';\n const output: string[] = [];\n prev !== '' && output.push(prev);\n if (typeof val === 'string') {\n output.push(`${_prev}${val}`);\n } else {\n const keys = Object.keys(val);\n output.push(\n ...keys.map(key => ddecompose(val[key], `${_prev}${key}`)).flat(),\n );\n }\n\n return output;\n}\n\nexport function decompose<T extends StateValue>(\n val: T,\n sorter?: (a: string, b: string) => number,\n): readonly StateMatching<T>[] {\n const first = ddecompose(val, '');\n first.sort(sorter ?? sortMap(DELIMITER));\n const regex = new RegExp(DELIMITER, 'g');\n const output = first.map(value => value.replace(regex, '.'));\n\n return Object.freeze(output) as StateMatching<T>[];\n}\n"],"names":["sortMap","delimiter","a","b","split","length","localeCompare","ddecompose","val","prev","_prev","output","push","Object","keys","map","key","flat","decompose","sorter","first","sort","value","replace","regex","freeze"],"mappings":"AAEgBA,SAAAA,EACdC,GAEA,YAF6B,IAA7BA,IAAAA,ECHuB,YDKhB,SAACC,EAAGC,GACT,OACED,EAAEE,MAAMH,GAAWI,OAASF,EAAEC,MAAMH,GAAWI,QAC/CH,EAAEI,cAAcH,EAEnB,CACF,CEND,SAAAI,EAAoBC,EAAiBC,YAAAA,IAAAA,EAAO,IAC1C,IAAWC,EAAGD,EAAOA,EDNE,WCMiB,KACf,GAEzB,GADS,KAATA,GAAeE,EAAOC,KAAKH,GACR,mBACjBE,EAAOC,KAAP,GAAeF,EAAQF,OAClB,CACL,MAAaK,OAAOC,KAAKN,GACzBG,EAAOC,WAAPD,EACKG,EAAKC,IAAI,SAAAC,GAAOT,OAAAA,EAAWC,EAAIQ,GAAL,GAAcN,EAAQM,EAApC,GAA4CC,OAE9D,CAED,OAAON,CACR,CAEe,SAAAO,EACdV,EACAW,GAEA,MAAcZ,EAAWC,EAAK,IAC9BY,EAAMC,KAAN,MAAWF,EAAAA,EAAUnB,ED1BE,aC2BvB,MAAc,WD3BS,WC2Ba,KAC9BW,EAASS,EAAML,IAAI,SAAAO,GAASA,OAAAA,EAAMC,QAAQC,EAAO,IAAzB,GAE9B,OAAOX,OAAOY,OAAOd,EACtB"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
function e(e="-{/./:}-"){return(t,n)=>t.split(e).length-n.split(e).length||t.localeCompare(n)}function t(e,n=""){const s=n?n+"-{/./:}-":"",o=[];if(""!==n&&o.push(n),"string"==typeof e)o.push(`${s}${e}`);else{const n=Object.keys(e);o.push(...n.map(n=>t(e[n],`${s}${n}`)).flat())}return o}function n(n,s){const o=t(n,"");o.sort(null!=s?s:e("-{/./:}-"));const p=new RegExp("-{/./:}-","g"),r=o.map(e=>e.replace(p,"."));return Object.freeze(r)}export{n as decompose,e as sortMap};
|
|
2
|
+
//# sourceMappingURL=index.modern.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.modern.js","sources":["../src/sortMap.ts","../src/constants/strings.ts","../src/decompose.ts"],"sourcesContent":["import { DELIMITER } from './constants/strings';\n\nexport function sortMap(\n delimiter: string = DELIMITER,\n): ((a: string, b: string) => number) | undefined {\n return (a, b) => {\n return (\n a.split(delimiter).length - b.split(delimiter).length ||\n a.localeCompare(b)\n );\n };\n}\n","export const DELIMITER = '-{/./:}-' as const;\n","import { StateValue } from 'xstate';\nimport { DELIMITER } from './constants/strings';\nimport { sortMap } from './sortMap';\nimport { StateMatching } from './types';\n\nfunction ddecompose(val: StateValue, prev = '') {\n const _prev = prev ? prev + DELIMITER : '';\n const output: string[] = [];\n prev !== '' && output.push(prev);\n if (typeof val === 'string') {\n output.push(`${_prev}${val}`);\n } else {\n const keys = Object.keys(val);\n output.push(\n ...keys.map(key => ddecompose(val[key], `${_prev}${key}`)).flat(),\n );\n }\n\n return output;\n}\n\nexport function decompose<T extends StateValue>(\n val: T,\n sorter?: (a: string, b: string) => number,\n): readonly StateMatching<T>[] {\n const first = ddecompose(val, '');\n first.sort(sorter ?? sortMap(DELIMITER));\n const regex = new RegExp(DELIMITER, 'g');\n const output = first.map(value => value.replace(regex, '.'));\n\n return Object.freeze(output) as StateMatching<T>[];\n}\n"],"names":["delimiter","a","b","split","length","localeCompare","ddecompose","val","prev","_prev","output","push","Object","keys","map","key","flat","decompose","sorter","first","sort","sortMap","RegExp","value","replace","regex","freeze"],"mappings":"AAEgB,WACdA,ECHuB,YDKvB,MAAO,CAACC,EAAGC,MAELC,MAAMH,GAAWI,OAASF,EAAEC,MAAMH,GAAWI,QAC/CH,EAAEI,cAAcH,EAGrB,CEND,SAASI,EAAWC,EAAiBC,EAAO,IAC1C,MAAWC,EAAGD,EAAOA,EDNE,WCMiB,KACf,GAEzB,GADS,KAATA,GAAeE,EAAOC,KAAKH,GACR,mBACjBE,EAAOC,KAAK,GAAGF,IAAQF,SAClB,CACL,QAAaK,OAAOC,KAAKN,GACzBG,EAAOC,QACFE,EAAKC,IAAIC,GAAOT,EAAWC,EAAIQ,GAAS,GAAAN,IAAQM,MAAQC,OAE9D,CAED,OAAON,CACR,CAEe,SAAAO,EACdV,EACAW,GAEA,QAAcZ,EAAWC,EAAK,IAC9BY,EAAMC,KAAN,MAAWF,EAAAA,EAAUG,ED1BE,aC2BvB,QAAc,IAAAC,OD3BS,WC2Ba,OACrBH,EAAML,IAAIS,GAASA,EAAMC,QAAQC,EAAO,MAEvD,OAAOb,OAAOc,OAAOhB,EACtB"}
|
package/lib/index.umd.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e||self).decompose={})}(this,function(e){var n="-{/./:}-";function t(e){return void 0===e&&(e=n),function(n,t){return n.split(e).length-t.split(e).length||n.localeCompare(t)}}function o(e,t){void 0===t&&(t="");var r=t?t+n:"",f=[];if(""!==t&&f.push(t),"string"==typeof e)f.push(""+r+e);else{var i=Object.keys(e);f.push.apply(f,i.map(function(n){return o(e[n],""+r+n)}).flat())}return f}e.decompose=function(e,r){var f=o(e,"");f.sort(null!=r?r:t(n));var i=new RegExp(n,"g"),p=f.map(function(e){return e.replace(i,".")});return Object.freeze(p)},e.sortMap=t});
|
|
2
|
+
//# sourceMappingURL=index.umd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/constants/strings.ts","../src/sortMap.ts","../src/decompose.ts"],"sourcesContent":["export const DELIMITER = '-{/./:}-' as const;\n","import { DELIMITER } from './constants/strings';\n\nexport function sortMap(\n delimiter: string = DELIMITER,\n): ((a: string, b: string) => number) | undefined {\n return (a, b) => {\n return (\n a.split(delimiter).length - b.split(delimiter).length ||\n a.localeCompare(b)\n );\n };\n}\n","import { StateValue } from 'xstate';\nimport { DELIMITER } from './constants/strings';\nimport { sortMap } from './sortMap';\nimport { StateMatching } from './types';\n\nfunction ddecompose(val: StateValue, prev = '') {\n const _prev = prev ? prev + DELIMITER : '';\n const output: string[] = [];\n prev !== '' && output.push(prev);\n if (typeof val === 'string') {\n output.push(`${_prev}${val}`);\n } else {\n const keys = Object.keys(val);\n output.push(\n ...keys.map(key => ddecompose(val[key], `${_prev}${key}`)).flat(),\n );\n }\n\n return output;\n}\n\nexport function decompose<T extends StateValue>(\n val: T,\n sorter?: (a: string, b: string) => number,\n): readonly StateMatching<T>[] {\n const first = ddecompose(val, '');\n first.sort(sorter ?? sortMap(DELIMITER));\n const regex = new RegExp(DELIMITER, 'g');\n const output = first.map(value => value.replace(regex, '.'));\n\n return Object.freeze(output) as StateMatching<T>[];\n}\n"],"names":["DELIMITER","sortMap","delimiter","a","b","split","length","localeCompare","ddecompose","val","prev","_prev","output","push","Object","keys","map","key","flat","sorter","first","sort","value","replace","regex","freeze"],"mappings":"mOAAO,IAAeA,EAAG,WCETC,SAAAA,EACdC,GAEA,YAF6B,IAA7BA,IAAAA,EAAoBF,GAEb,SAACG,EAAGC,GACT,OACED,EAAEE,MAAMH,GAAWI,OAASF,EAAEC,MAAMH,GAAWI,QAC/CH,EAAEI,cAAcH,EAEnB,CACF,CCND,SAAAI,EAAoBC,EAAiBC,YAAAA,IAAAA,EAAO,IAC1C,IAAWC,EAAGD,EAAOA,EAAOV,EAAY,KACf,GAEzB,GADS,KAATU,GAAeE,EAAOC,KAAKH,GACR,mBACjBE,EAAOC,KAAP,GAAeF,EAAQF,OAClB,CACL,MAAaK,OAAOC,KAAKN,GACzBG,EAAOC,WAAPD,EACKG,EAAKC,IAAI,SAAAC,GAAOT,OAAAA,EAAWC,EAAIQ,GAAL,GAAcN,EAAQM,EAApC,GAA4CC,OAE9D,CAED,OAAON,CACR,aAEe,SACdH,EACAU,GAEA,MAAcX,EAAWC,EAAK,IAC9BW,EAAMC,KAAN,MAAWF,EAAAA,EAAUlB,EAAQD,IAC7B,MAAc,WAAWA,EAAW,KAC9BY,EAASQ,EAAMJ,IAAI,SAAAM,GAASA,OAAAA,EAAMC,QAAQC,EAAO,IAAzB,GAE9B,OAAOV,OAAOW,OAAOb,EACtB"}
|
package/lib/sortMap.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function sortMap(delimiter?: string): ((a: string, b: string) => number) | undefined;
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { EventFrom, Interpreter } from 'xstate';
|
|
2
|
+
export declare type StateMatching<T extends Record<string, unknown> | string, Key = keyof T> = T extends Record<string, unknown> ? Key extends string ? T[Key] extends Record<string, unknown> ? `${Key}.${StateMatching<T[Key]>}` | (Key & string) : `${Key}.${T[Key] & string}` | (Key & string) : never : T;
|
|
3
|
+
export declare type LengthOf<T> = T extends ReadonlyArray<unknown> ? T['length'] : number;
|
|
4
|
+
export declare type DecomposeOptions = {
|
|
5
|
+
delimiter?: string;
|
|
6
|
+
sorter?: (a: string, b: string) => number;
|
|
7
|
+
};
|
|
8
|
+
declare type _UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
9
|
+
declare type _LastOf<T> = _UnionToIntersection<T extends unknown ? () => T : never> extends () => infer R ? R : never;
|
|
10
|
+
declare type _Push<T extends unknown[], V> = [...T, V];
|
|
11
|
+
declare type _TuplifyUnionBoolean<T> = [T] extends [never] ? true : false;
|
|
12
|
+
export declare type TuplifyUnion<T> = true extends _TuplifyUnionBoolean<T> ? [] : _Push<TuplifyUnion<Exclude<T, _LastOf<T>>>, _LastOf<T>>;
|
|
13
|
+
export declare type OmitEvent<T extends Interpreter<never, never, never, never, never>, K extends EventFrom<T>['type']> = LengthOf<TuplifyUnion<keyof EventFrom<T, K>>> extends 1 ? () => unknown : (data: Omit<EventFrom<T>, 'type'>) => unknown;
|
|
14
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "0.1.0",
|
|
3
|
+
"license": "MIT",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"umd:main": "lib/index.umd.js",
|
|
7
|
+
"unpkg": "lib/index.umd.js",
|
|
8
|
+
"typings": "lib/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"lib",
|
|
11
|
+
"src"
|
|
12
|
+
],
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=12"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
"require": "./lib/index.cjs",
|
|
18
|
+
"default": "./lib/index.modern.js"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"config": "pnpm install",
|
|
22
|
+
"start": "pnpm run config && microbundle watch",
|
|
23
|
+
"build": "rm -rf lib && microbundle",
|
|
24
|
+
"test": "jest",
|
|
25
|
+
"remove": "rm -rf lib node_modules pnpm-lock.yaml",
|
|
26
|
+
"clean": "pnpm run remove && pnpm run config",
|
|
27
|
+
"test-watch": "pnpm test -- --watch",
|
|
28
|
+
"prettier": "prettier --write \"src/**/*.{ts,tsx}\"",
|
|
29
|
+
"eslint": "eslint \"{src/**/*,App}.{ts,tsx}\"",
|
|
30
|
+
"lint": "pnpm run prettier && pnpm run eslint --fix",
|
|
31
|
+
"lint-watch": "onchange --await-write-finish 5500 \"**/*\" -- pnpm run lint",
|
|
32
|
+
"postpublish": "git fetch & git pull",
|
|
33
|
+
"prepublishOnly": "pnpm run build && pnpm run lint"
|
|
34
|
+
},
|
|
35
|
+
"husky": {
|
|
36
|
+
"hooks": {
|
|
37
|
+
"pre-commit": "prettier --write \"**/**.{ts,tsx,json,js,jsx,yml}\"",
|
|
38
|
+
"post-commit": "a"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"name": "@bemedev/decompose",
|
|
42
|
+
"author": "chlbri",
|
|
43
|
+
"module": "lib/decompose.esm.js",
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"tslib": "^2.4.0",
|
|
46
|
+
"typescript": "^4.7.4",
|
|
47
|
+
"xstate": "^4.33.2"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@swc/core": "^1.2.239",
|
|
51
|
+
"@swc/jest": "^0.2.22",
|
|
52
|
+
"@types/jest": "^28.1.7",
|
|
53
|
+
"@types/node": "^18.7.6",
|
|
54
|
+
"@typescript-eslint/eslint-plugin": "^5.33.1",
|
|
55
|
+
"@typescript-eslint/parser": "^5.33.1",
|
|
56
|
+
"eslint": "^8.22.0",
|
|
57
|
+
"husky": "^8.0.1",
|
|
58
|
+
"jest": "27.5.0",
|
|
59
|
+
"jest-extended": "^3.0.2",
|
|
60
|
+
"microbundle": "^0.15.1",
|
|
61
|
+
"onchange": "^7.1.0",
|
|
62
|
+
"prettier": "^2.7.1",
|
|
63
|
+
"size-limit": "^8.0.1",
|
|
64
|
+
"typescript": "^4.7.4",
|
|
65
|
+
"xstate": "^4.33.2"
|
|
66
|
+
},
|
|
67
|
+
"wallaby": {
|
|
68
|
+
"slowTestThreshold": 200
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const DELIMITER = '-{/./:}-' as const;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { decompose } from './decompose';
|
|
2
|
+
|
|
3
|
+
it('String', () => {
|
|
4
|
+
expect(decompose('Hello World')).toEqual(['Hello World']);
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
it('Simple object', () => {
|
|
8
|
+
const obj = {
|
|
9
|
+
a: 'Hello',
|
|
10
|
+
b: 'World',
|
|
11
|
+
};
|
|
12
|
+
expect(decompose(obj)).toEqual(['a', 'b', 'a.Hello', 'b.World']);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('Complex object', () => {
|
|
16
|
+
const obj = {
|
|
17
|
+
a: 'Hello',
|
|
18
|
+
b: 'World',
|
|
19
|
+
c: {
|
|
20
|
+
d: 'Hello',
|
|
21
|
+
e: {
|
|
22
|
+
f: 'World',
|
|
23
|
+
g: 'Again',
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
expect(decompose(obj)).toEqual([
|
|
28
|
+
'a',
|
|
29
|
+
'b',
|
|
30
|
+
'c',
|
|
31
|
+
'a.Hello',
|
|
32
|
+
'b.World',
|
|
33
|
+
'c.d',
|
|
34
|
+
'c.e',
|
|
35
|
+
'c.d.Hello',
|
|
36
|
+
'c.e.f',
|
|
37
|
+
'c.e.g',
|
|
38
|
+
'c.e.f.World',
|
|
39
|
+
'c.e.g.Again',
|
|
40
|
+
]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('Complex object with custom order', () => {
|
|
44
|
+
const obj = {
|
|
45
|
+
a: 'Hello',
|
|
46
|
+
b: 'World',
|
|
47
|
+
c: {
|
|
48
|
+
d: 'Hello',
|
|
49
|
+
e: {
|
|
50
|
+
f: 'World',
|
|
51
|
+
g: 'Again',
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const actual = decompose(obj, (a, b) => a.localeCompare(b));
|
|
57
|
+
expect(actual).toEqual([
|
|
58
|
+
'a',
|
|
59
|
+
'a.Hello',
|
|
60
|
+
'b',
|
|
61
|
+
'b.World',
|
|
62
|
+
'c',
|
|
63
|
+
'c.d',
|
|
64
|
+
'c.d.Hello',
|
|
65
|
+
'c.e',
|
|
66
|
+
'c.e.f',
|
|
67
|
+
'c.e.f.World',
|
|
68
|
+
'c.e.g',
|
|
69
|
+
'c.e.g.Again',
|
|
70
|
+
]);
|
|
71
|
+
});
|
package/src/decompose.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { StateValue } from 'xstate';
|
|
2
|
+
import { DELIMITER } from './constants/strings';
|
|
3
|
+
import { sortMap } from './sortMap';
|
|
4
|
+
import { StateMatching } from './types';
|
|
5
|
+
|
|
6
|
+
function ddecompose(val: StateValue, prev = '') {
|
|
7
|
+
const _prev = prev ? prev + DELIMITER : '';
|
|
8
|
+
const output: string[] = [];
|
|
9
|
+
prev !== '' && output.push(prev);
|
|
10
|
+
if (typeof val === 'string') {
|
|
11
|
+
output.push(`${_prev}${val}`);
|
|
12
|
+
} else {
|
|
13
|
+
const keys = Object.keys(val);
|
|
14
|
+
output.push(
|
|
15
|
+
...keys.map(key => ddecompose(val[key], `${_prev}${key}`)).flat(),
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return output;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function decompose<T extends StateValue>(
|
|
23
|
+
val: T,
|
|
24
|
+
sorter?: (a: string, b: string) => number,
|
|
25
|
+
): readonly StateMatching<T>[] {
|
|
26
|
+
const first = ddecompose(val, '');
|
|
27
|
+
first.sort(sorter ?? sortMap(DELIMITER));
|
|
28
|
+
const regex = new RegExp(DELIMITER, 'g');
|
|
29
|
+
const output = first.map(value => value.replace(regex, '.'));
|
|
30
|
+
|
|
31
|
+
return Object.freeze(output) as StateMatching<T>[];
|
|
32
|
+
}
|
package/src/index.ts
ADDED
package/src/sortMap.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DELIMITER } from './constants/strings';
|
|
2
|
+
|
|
3
|
+
export function sortMap(
|
|
4
|
+
delimiter: string = DELIMITER,
|
|
5
|
+
): ((a: string, b: string) => number) | undefined {
|
|
6
|
+
return (a, b) => {
|
|
7
|
+
return (
|
|
8
|
+
a.split(delimiter).length - b.split(delimiter).length ||
|
|
9
|
+
a.localeCompare(b)
|
|
10
|
+
);
|
|
11
|
+
};
|
|
12
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { EventFrom, Interpreter } from 'xstate';
|
|
2
|
+
|
|
3
|
+
export type StateMatching<
|
|
4
|
+
T extends Record<string, unknown> | string,
|
|
5
|
+
Key = keyof T,
|
|
6
|
+
> = T extends Record<string, unknown>
|
|
7
|
+
? Key extends string
|
|
8
|
+
? T[Key] extends Record<string, unknown>
|
|
9
|
+
? `${Key}.${StateMatching<T[Key]>}` | (Key & string)
|
|
10
|
+
: `${Key}.${T[Key] & string}` | (Key & string)
|
|
11
|
+
: never
|
|
12
|
+
: T;
|
|
13
|
+
|
|
14
|
+
export type LengthOf<T> = T extends ReadonlyArray<unknown>
|
|
15
|
+
? T['length']
|
|
16
|
+
: number;
|
|
17
|
+
|
|
18
|
+
export type DecomposeOptions = {
|
|
19
|
+
delimiter?: string;
|
|
20
|
+
sorter?: (a: string, b: string) => number;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type _UnionToIntersection<U> = (
|
|
24
|
+
U extends unknown ? (k: U) => void : never
|
|
25
|
+
) extends (k: infer I) => void
|
|
26
|
+
? I
|
|
27
|
+
: never;
|
|
28
|
+
|
|
29
|
+
type _LastOf<T> = _UnionToIntersection<
|
|
30
|
+
T extends unknown ? () => T : never
|
|
31
|
+
> extends () => infer R
|
|
32
|
+
? R
|
|
33
|
+
: never;
|
|
34
|
+
|
|
35
|
+
type _Push<T extends unknown[], V> = [...T, V];
|
|
36
|
+
|
|
37
|
+
type _TuplifyUnionBoolean<T> = [T] extends [never] ? true : false;
|
|
38
|
+
|
|
39
|
+
// TS4.1+
|
|
40
|
+
export type TuplifyUnion<T> = true extends _TuplifyUnionBoolean<T>
|
|
41
|
+
? []
|
|
42
|
+
: _Push<TuplifyUnion<Exclude<T, _LastOf<T>>>, _LastOf<T>>;
|
|
43
|
+
|
|
44
|
+
// #endregion
|
|
45
|
+
|
|
46
|
+
export type OmitEvent<
|
|
47
|
+
T extends Interpreter<never, never, never, never, never>,
|
|
48
|
+
K extends EventFrom<T>['type'],
|
|
49
|
+
> = LengthOf<TuplifyUnion<keyof EventFrom<T, K>>> extends 1
|
|
50
|
+
? () => unknown
|
|
51
|
+
: (data: Omit<EventFrom<T>, 'type'>) => unknown;
|