@bemedev/decompose 2.0.2 → 2.0.3
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/lib/contexts/assign.cjs
CHANGED
|
@@ -8,7 +8,10 @@ const _assignByKey = (obj, key, value) => {
|
|
|
8
8
|
const out = obj ?? helpers.nextDefault(first);
|
|
9
9
|
if (rest.length === 0) {
|
|
10
10
|
if (helpers.isArrayIndex(first)) {
|
|
11
|
-
|
|
11
|
+
let idx = helpers.parseIndex(first);
|
|
12
|
+
if (idx > out.length)
|
|
13
|
+
idx = out.length;
|
|
14
|
+
out[idx] = value;
|
|
12
15
|
}
|
|
13
16
|
else {
|
|
14
17
|
out[first] = value;
|
|
@@ -19,7 +22,9 @@ const _assignByKey = (obj, key, value) => {
|
|
|
19
22
|
const next = rest[0];
|
|
20
23
|
const _nextDefault = helpers.nextDefault(next);
|
|
21
24
|
if (helpers.isArrayIndex(first)) {
|
|
22
|
-
|
|
25
|
+
let idx = helpers.parseIndex(first);
|
|
26
|
+
if (idx > out.length)
|
|
27
|
+
idx = out.length;
|
|
23
28
|
out[idx] = _assignByKey(out[idx] ?? _nextDefault, nextKey, value);
|
|
24
29
|
}
|
|
25
30
|
else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assign.cjs","sources":["../../src/contexts/assign.ts"],"sourcesContent":["import {\n isArrayIndex,\n nextDefault,\n parseIndex,\n splitKey,\n} from '../helpers';\nimport type { Decompose, Ru } from '../types.types';\nimport type { DEFAULT_OPTIONS } from './constants';\n\n// #region type AssignByBey_F\nexport type AssignByBey_F = <\n T extends Ru,\n D extends Decompose<T, typeof DEFAULT_OPTIONS>,\n K extends Extract<keyof D, string>,\n R extends D[K],\n>(\n obj: T,\n key: K,\n value: R,\n) => T;\n// #endregion\n\nexport interface AssignByKey {\n (obj: any, key: string, value: any): any;\n low: (obj: any, key: string, value: any) => any;\n typed: AssignByBey_F;\n}\n\nconst _assignByKey: AssignByKey['low'] = (obj, key, value) => {\n const segments = splitKey(key);\n\n const [first, ...rest] = segments;\n const out: any = obj ?? nextDefault(first);\n\n if (rest.length === 0) {\n if (isArrayIndex(first)) {\n
|
|
1
|
+
{"version":3,"file":"assign.cjs","sources":["../../src/contexts/assign.ts"],"sourcesContent":["import {\n isArrayIndex,\n nextDefault,\n parseIndex,\n splitKey,\n} from '../helpers';\nimport type { Decompose, Ru } from '../types.types';\nimport type { DEFAULT_OPTIONS } from './constants';\n\n// #region type AssignByBey_F\nexport type AssignByBey_F = <\n T extends Ru,\n D extends Decompose<T, typeof DEFAULT_OPTIONS>,\n K extends Extract<keyof D, string>,\n R extends D[K],\n>(\n obj: T,\n key: K,\n value: R,\n) => T;\n// #endregion\n\nexport interface AssignByKey {\n (obj: any, key: string, value: any): any;\n low: (obj: any, key: string, value: any) => any;\n typed: AssignByBey_F;\n}\n\nconst _assignByKey: AssignByKey['low'] = (obj, key, value) => {\n const segments = splitKey(key);\n\n const [first, ...rest] = segments;\n const out: any = obj ?? nextDefault(first);\n\n if (rest.length === 0) {\n if (isArrayIndex(first)) {\n let idx = parseIndex(first);\n if (idx > out.length) idx = out.length;\n out[idx] = value;\n } else {\n out[first] = value;\n }\n return out;\n }\n\n const nextKey = rest.join('.');\n const next = rest[0];\n const _nextDefault = nextDefault(next);\n\n if (isArrayIndex(first)) {\n let idx = parseIndex(first);\n if (idx > out.length) idx = out.length;\n out[idx] = _assignByKey(out[idx] ?? _nextDefault, nextKey, value);\n } else {\n out[first] = _assignByKey(out[first] ?? _nextDefault, nextKey, value);\n }\n\n return out;\n};\n\n/**\n * Assigns a value to a path in an object.\n * @param obj The object to assign the value to\n * @param path The key to assign the value to, can be a nested key (e.g. 'a.b.c')\n * @param value The value to assign to the key\n * @returns The modified object with the value assigned to the specified key\n *\n * @see {@linkcode Decompose} for more details on object decomposition.\n */\nexport const assignByKey: AssignByKey = (obj, path, value) => {\n return _assignByKey(obj, path, value);\n};\n\nassignByKey.low = assignByKey;\n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sources":["../../src/contexts/assign.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAGnD,MAAM,MAAM,aAAa,GAAG,CAC1B,CAAC,SAAS,EAAE,EACZ,CAAC,SAAS,SAAS,CAAC,CAAC,EAAE,OAAO,eAAe,CAAC,EAC9C,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,EAClC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,EAEd,GAAG,EAAE,CAAC,EACN,GAAG,EAAE,CAAC,EACN,KAAK,EAAE,CAAC,KACL,CAAC,CAAC;AAGP,MAAM,WAAW,WAAW;IAC1B,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,GAAG,CAAC;IACzC,GAAG,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,GAAG,CAAC;IAChD,KAAK,EAAE,aAAa,CAAC;CACtB;AAkCD;;;;;;;;GAQG;AACH,eAAO,MAAM,WAAW,EAAE,WAEzB,CAAC"}
|
package/lib/contexts/assign.js
CHANGED
|
@@ -6,7 +6,10 @@ const _assignByKey = (obj, key, value) => {
|
|
|
6
6
|
const out = obj ?? nextDefault(first);
|
|
7
7
|
if (rest.length === 0) {
|
|
8
8
|
if (isArrayIndex(first)) {
|
|
9
|
-
|
|
9
|
+
let idx = parseIndex(first);
|
|
10
|
+
if (idx > out.length)
|
|
11
|
+
idx = out.length;
|
|
12
|
+
out[idx] = value;
|
|
10
13
|
}
|
|
11
14
|
else {
|
|
12
15
|
out[first] = value;
|
|
@@ -17,7 +20,9 @@ const _assignByKey = (obj, key, value) => {
|
|
|
17
20
|
const next = rest[0];
|
|
18
21
|
const _nextDefault = nextDefault(next);
|
|
19
22
|
if (isArrayIndex(first)) {
|
|
20
|
-
|
|
23
|
+
let idx = parseIndex(first);
|
|
24
|
+
if (idx > out.length)
|
|
25
|
+
idx = out.length;
|
|
21
26
|
out[idx] = _assignByKey(out[idx] ?? _nextDefault, nextKey, value);
|
|
22
27
|
}
|
|
23
28
|
else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assign.js","sources":["../../src/contexts/assign.ts"],"sourcesContent":["import {\n isArrayIndex,\n nextDefault,\n parseIndex,\n splitKey,\n} from '../helpers';\nimport type { Decompose, Ru } from '../types.types';\nimport type { DEFAULT_OPTIONS } from './constants';\n\n// #region type AssignByBey_F\nexport type AssignByBey_F = <\n T extends Ru,\n D extends Decompose<T, typeof DEFAULT_OPTIONS>,\n K extends Extract<keyof D, string>,\n R extends D[K],\n>(\n obj: T,\n key: K,\n value: R,\n) => T;\n// #endregion\n\nexport interface AssignByKey {\n (obj: any, key: string, value: any): any;\n low: (obj: any, key: string, value: any) => any;\n typed: AssignByBey_F;\n}\n\nconst _assignByKey: AssignByKey['low'] = (obj, key, value) => {\n const segments = splitKey(key);\n\n const [first, ...rest] = segments;\n const out: any = obj ?? nextDefault(first);\n\n if (rest.length === 0) {\n if (isArrayIndex(first)) {\n
|
|
1
|
+
{"version":3,"file":"assign.js","sources":["../../src/contexts/assign.ts"],"sourcesContent":["import {\n isArrayIndex,\n nextDefault,\n parseIndex,\n splitKey,\n} from '../helpers';\nimport type { Decompose, Ru } from '../types.types';\nimport type { DEFAULT_OPTIONS } from './constants';\n\n// #region type AssignByBey_F\nexport type AssignByBey_F = <\n T extends Ru,\n D extends Decompose<T, typeof DEFAULT_OPTIONS>,\n K extends Extract<keyof D, string>,\n R extends D[K],\n>(\n obj: T,\n key: K,\n value: R,\n) => T;\n// #endregion\n\nexport interface AssignByKey {\n (obj: any, key: string, value: any): any;\n low: (obj: any, key: string, value: any) => any;\n typed: AssignByBey_F;\n}\n\nconst _assignByKey: AssignByKey['low'] = (obj, key, value) => {\n const segments = splitKey(key);\n\n const [first, ...rest] = segments;\n const out: any = obj ?? nextDefault(first);\n\n if (rest.length === 0) {\n if (isArrayIndex(first)) {\n let idx = parseIndex(first);\n if (idx > out.length) idx = out.length;\n out[idx] = value;\n } else {\n out[first] = value;\n }\n return out;\n }\n\n const nextKey = rest.join('.');\n const next = rest[0];\n const _nextDefault = nextDefault(next);\n\n if (isArrayIndex(first)) {\n let idx = parseIndex(first);\n if (idx > out.length) idx = out.length;\n out[idx] = _assignByKey(out[idx] ?? _nextDefault, nextKey, value);\n } else {\n out[first] = _assignByKey(out[first] ?? _nextDefault, nextKey, value);\n }\n\n return out;\n};\n\n/**\n * Assigns a value to a path in an object.\n * @param obj The object to assign the value to\n * @param path The key to assign the value to, can be a nested key (e.g. 'a.b.c')\n * @param value The value to assign to the key\n * @returns The modified object with the value assigned to the specified key\n *\n * @see {@linkcode Decompose} for more details on object decomposition.\n */\nexport const assignByKey: AssignByKey = (obj, path, value) => {\n return _assignByKey(obj, path, value);\n};\n\nassignByKey.low = assignByKey;\nassignByKey.typed = assignByKey;\n"],"names":[],"mappings":";;AA4BA,MAAM,YAAY,GAAuB,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,KAAI;AAC3D,IAAA,MAAM,QAAQ,GAAG,QAAQ,CAAC,GAAG,CAAC;IAE9B,MAAM,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,GAAG,QAAQ;IACjC,MAAM,GAAG,GAAQ,GAAG,IAAI,WAAW,CAAC,KAAK,CAAC;AAE1C,IAAA,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;AACrB,QAAA,IAAI,YAAY,CAAC,KAAK,CAAC,EAAE;AACvB,YAAA,IAAI,GAAG,GAAG,UAAU,CAAC,KAAK,CAAC;AAC3B,YAAA,IAAI,GAAG,GAAG,GAAG,CAAC,MAAM;AAAE,gBAAA,GAAG,GAAG,GAAG,CAAC,MAAM;AACtC,YAAA,GAAG,CAAC,GAAG,CAAC,GAAG,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