-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from blackflux/dev
[Gally]: master <- dev
- Loading branch information
Showing
7 changed files
with
92 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,39 @@ | ||
const align = (target, ref) => { | ||
if ( | ||
!(target instanceof Object) | ||
|| !(ref instanceof Object) | ||
) { | ||
return; | ||
} | ||
if (Array.isArray(target) !== Array.isArray(ref)) { | ||
return; | ||
} | ||
|
||
if (Array.isArray(target)) { | ||
for (let idx = 0, len = Math.min(target.length, ref.length); idx < len; idx += 1) { | ||
align(target[idx], ref[idx]); | ||
} | ||
return; | ||
} | ||
const objectScan = require('object-scan'); | ||
|
||
const align = (target, ref) => { | ||
const keysTarget = Object.keys(target); | ||
const keysRef = Object.keys(ref); | ||
keysTarget | ||
.map((k) => [k, keysRef.indexOf(k)]) | ||
.map(([k, idx]) => (idx === -1 ? [k, Number.MAX_VALUE] : [k, idx])) | ||
.sort(([k1, idx1], [k2, idx2]) => idx1 - idx2) | ||
.forEach(([k, idx]) => { | ||
.sort((e1, e2) => e1[1] - e2[1]) | ||
.forEach(([k]) => { | ||
const value = target[k]; | ||
if (idx !== Number.MAX_VALUE) { | ||
align(value, ref[k]); | ||
} | ||
// eslint-disable-next-line no-param-reassign | ||
delete target[k]; | ||
// eslint-disable-next-line no-param-reassign | ||
target[k] = value; | ||
}); | ||
}; | ||
module.exports = align; | ||
|
||
const scanner = objectScan(['', '**'], { | ||
breakFn: ({ property, context }) => { | ||
const last = context[context.length - 1]; | ||
if (last instanceof Object) { | ||
context.push(property === undefined ? last : last[property]); | ||
return false; | ||
} | ||
context.push(null); | ||
return true; | ||
}, | ||
filterFn: ({ value, context }) => { | ||
const last = context.pop(); | ||
if (last instanceof Object && value instanceof Object) { | ||
align(last, value); | ||
} | ||
} | ||
}); | ||
|
||
module.exports = (tree, ref) => { | ||
scanner(ref, [tree]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const align = (target, ref) => { | ||
if ( | ||
!(target instanceof Object) | ||
|| !(ref instanceof Object) | ||
) { | ||
return; | ||
} | ||
if (Array.isArray(target) !== Array.isArray(ref)) { | ||
return; | ||
} | ||
|
||
if (Array.isArray(target)) { | ||
for (let idx = 0, len = Math.min(target.length, ref.length); idx < len; idx += 1) { | ||
align(target[idx], ref[idx]); | ||
} | ||
return; | ||
} | ||
|
||
const keysTarget = Object.keys(target); | ||
const keysRef = Object.keys(ref); | ||
keysTarget | ||
.map((k) => [k, keysRef.indexOf(k)]) | ||
.map(([k, idx]) => (idx === -1 ? [k, Number.MAX_VALUE] : [k, idx])) | ||
.sort(([k1, idx1], [k2, idx2]) => idx1 - idx2) | ||
.forEach(([k, idx]) => { | ||
const value = target[k]; | ||
if (idx !== Number.MAX_VALUE) { | ||
align(value, ref[k]); | ||
} | ||
// eslint-disable-next-line no-param-reassign | ||
delete target[k]; | ||
// eslint-disable-next-line no-param-reassign | ||
target[k] = value; | ||
}); | ||
}; | ||
module.exports = align; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters