Skip to content

Commit

Permalink
chore: import merge submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Feb 24, 2021
1 parent 36a2529 commit 5aed5d1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ node_modules
*.log

/index.d.ts
/merge
/dist
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./merge": {
"import": "./merge/index.mjs",
"require": "./merge/index.js"
},
"./package.json": "./package.json"
},
"author": {
Expand All @@ -30,9 +34,11 @@
},
"files": [
"*.d.ts",
"merge",
"dist"
],
"modes": {
"merge": "src/merge.js",
"default": "src/index.js"
},
"keywords": [
Expand Down
2 changes: 2 additions & 0 deletions src/merge.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export function merge(foo: any, bar: any): any; // TODO
export function dset<T extends object, V>(obj: T, keys: string | ArrayLike<string | number>, value: V): void;
26 changes: 26 additions & 0 deletions src/merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export function merge(a, b) {
if (typeof a === 'object' && typeof b === 'object')  {
if (Array.isArray(a) && Array.isArray(b)) {
for (let i=0; i < b.length; i++) {
a[i] = merge(a[i], b[i]);
}
} else {
for (let k in b) {
a[k] = merge(a[k], b[k]);
}
}
return a;
}
return b;
}

export function dset(obj, keys, val) {
keys.split && (keys=keys.split('.'));
var i=0, l=keys.length, t=obj, x, k;
for (; i < l;) {
k = keys[i++];
if (k === '__proto__' || k === 'constructor' || k === 'prototype') break;
t = t[k] = (i === l) ? merge(t[k],val) : (typeof(x=t[k])===typeof keys) ? x : (keys[i]*0 !== 0 || !!~(''+keys[i]).indexOf('.')) ? {} : [];
}
}

0 comments on commit 5aed5d1

Please sign in to comment.