@bemedev/decompose 0.1.3 → 0.1.4

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/package.json CHANGED
@@ -1,20 +1,15 @@
1
1
  {
2
- "version": "0.1.3",
2
+ "version": "0.1.4",
3
3
  "license": "MIT",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
8
- "lib",
9
- "src"
8
+ "lib"
10
9
  ],
11
10
  "engines": {
12
11
  "node": ">=12"
13
12
  },
14
- "exports": {
15
- "require": "./lib/index.cjs",
16
- "default": "./lib/index.modern.js"
17
- },
18
13
  "scripts": {
19
14
  "config": "pnpm install",
20
15
  "start": "pnpm run config && microbundle watch",
@@ -1 +0,0 @@
1
- export const DELIMITER = '-{/./:}-' as const;
@@ -1,72 +0,0 @@
1
- import { expect, it } from 'vitest';
2
- import { decompose } from './decompose';
3
-
4
- it('String', () => {
5
- expect(decompose('Hello World')).toEqual(['Hello World']);
6
- });
7
-
8
- it('Simple object', () => {
9
- const obj = {
10
- a: 'Hello',
11
- b: 'World',
12
- };
13
- expect(decompose(obj)).toEqual(['a', 'b', 'a.Hello', 'b.World']);
14
- });
15
-
16
- it('Complex object', () => {
17
- const obj = {
18
- a: 'Hello',
19
- b: 'World',
20
- c: {
21
- d: 'Hello',
22
- e: {
23
- f: 'World',
24
- g: 'Again',
25
- },
26
- },
27
- };
28
- expect(decompose(obj)).toEqual([
29
- 'a',
30
- 'b',
31
- 'c',
32
- 'a.Hello',
33
- 'b.World',
34
- 'c.d',
35
- 'c.e',
36
- 'c.d.Hello',
37
- 'c.e.f',
38
- 'c.e.g',
39
- 'c.e.f.World',
40
- 'c.e.g.Again',
41
- ]);
42
- });
43
-
44
- it('Complex object with custom order', () => {
45
- const obj = {
46
- a: 'Hello',
47
- b: 'World',
48
- c: {
49
- d: 'Hello',
50
- e: {
51
- f: 'World',
52
- g: 'Again',
53
- },
54
- },
55
- };
56
-
57
- const actual = decompose(obj, (a, b) => a.localeCompare(b));
58
- expect(actual).toEqual([
59
- 'a',
60
- 'a.Hello',
61
- 'b',
62
- 'b.World',
63
- 'c',
64
- 'c.d',
65
- 'c.d.Hello',
66
- 'c.e',
67
- 'c.e.f',
68
- 'c.e.f.World',
69
- 'c.e.g',
70
- 'c.e.g.Again',
71
- ]);
72
- });
package/src/decompose.ts DELETED
@@ -1,31 +0,0 @@
1
- import { DELIMITER } from './constants/strings';
2
- import { sortMap } from './sortMap';
3
- import { StateMatching, StateValue } from './types';
4
-
5
- function ddecompose(val: StateValue, prev = '') {
6
- const _prev = prev ? prev + DELIMITER : '';
7
- const output: string[] = [];
8
- prev !== '' && output.push(prev);
9
- if (typeof val === 'string') {
10
- output.push(`${_prev}${val}`);
11
- } else {
12
- const keys = Object.keys(val);
13
- output.push(
14
- ...keys.map(key => ddecompose(val[key], `${_prev}${key}`)).flat(),
15
- );
16
- }
17
-
18
- return output;
19
- }
20
-
21
- export function decompose<T extends StateValue>(
22
- val: T,
23
- sorter?: (a: string, b: string) => number,
24
- ): readonly StateMatching<T>[] {
25
- const first = ddecompose(val, '');
26
- first.sort(sorter ?? sortMap(DELIMITER));
27
- const regex = new RegExp(DELIMITER, 'g');
28
- const output = first.map(value => value.replace(regex, '.'));
29
-
30
- return Object.freeze(output) as StateMatching<T>[];
31
- }
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './decompose';
2
- export * from './sortMap';
3
- export * from './types';
package/src/sortMap.ts DELETED
@@ -1,12 +0,0 @@
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 DELETED
@@ -1,48 +0,0 @@
1
- export type StateMatching<
2
- T extends Record<string, unknown> | string,
3
- Key = keyof T,
4
- > = T extends Record<string, unknown>
5
- ? Key extends string
6
- ? T[Key] extends Record<string, unknown>
7
- ? `${Key}.${StateMatching<T[Key]>}` | (Key & string)
8
- : `${Key}.${T[Key] & string}` | (Key & string)
9
- : never
10
- : T;
11
-
12
- export type LengthOf<T> = T extends ReadonlyArray<unknown>
13
- ? T['length']
14
- : number;
15
-
16
- export type DecomposeOptions = {
17
- delimiter?: string;
18
- sorter?: (a: string, b: string) => number;
19
- };
20
-
21
- type _UnionToIntersection<U> = (
22
- U extends unknown ? (k: U) => void : never
23
- ) extends (k: infer I) => void
24
- ? I
25
- : never;
26
-
27
- type _LastOf<T> = _UnionToIntersection<
28
- T extends unknown ? () => T : never
29
- > extends () => infer R
30
- ? R
31
- : never;
32
-
33
- type _Push<T extends unknown[], V> = [...T, V];
34
-
35
- type _TuplifyUnionBoolean<T> = [T] extends [never] ? true : false;
36
-
37
- // TS4.1+
38
- export type TuplifyUnion<T> = true extends _TuplifyUnionBoolean<T>
39
- ? []
40
- : _Push<TuplifyUnion<Exclude<T, _LastOf<T>>>, _LastOf<T>>;
41
-
42
- // #endregion
43
-
44
- export type StateValue = string | StateValueMap;
45
-
46
- export interface StateValueMap {
47
- [key: string]: StateValue;
48
- }