-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
improve merging of resolve and parsing options #9126
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Tobias Koppers @sokra | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const mergeCache = new WeakMap(); | ||
|
||
/** | ||
* Merges two given objects and caches the result to avoid computation if same objects passed as arguments again. | ||
* @example | ||
* // performs cleverMerge(first, second), stores the result in WeakMap and returns result | ||
* cachedCleverMerge({a: 1}, {a: 2}) | ||
* {a: 2} | ||
* // when same arguments passed, gets the result from WeakMap and returns it. | ||
* cachedCleverMerge({a: 1}, {a: 2}) | ||
* {a: 2} | ||
* @param {object} first first object | ||
* @param {object} second second object | ||
* @returns {object} merged object of first and second object | ||
*/ | ||
const cachedCleverMerge = (first, second) => { | ||
let innerCache = mergeCache.get(first); | ||
if (innerCache === undefined) { | ||
innerCache = new WeakMap(); | ||
mergeCache.set(first, innerCache); | ||
} | ||
const prevMerge = innerCache.get(second); | ||
if (prevMerge !== undefined) return prevMerge; | ||
const newMerge = cleverMerge(first, second); | ||
innerCache.set(second, newMerge); | ||
return newMerge; | ||
}; | ||
|
||
/** | ||
* Merges two objects. Objects are deeply clever merged. | ||
* Arrays might reference the old value with "..." | ||
* @param {object} first first object | ||
* @param {object} second second object | ||
* @returns {object} merged object of first and second object | ||
*/ | ||
const cleverMerge = (first, second) => { | ||
const newObject = Object.assign({}, first); | ||
for (const key of Object.keys(second)) { | ||
if (!(key in newObject)) { | ||
newObject[key] = second[key]; | ||
continue; | ||
} | ||
const secondValue = second[key]; | ||
if (typeof secondValue !== "object" || secondValue === null) { | ||
newObject[key] = secondValue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should probably be |
||
} | ||
if (Array.isArray(secondValue)) { | ||
const firstValue = newObject[key]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is duplicated in the |
||
if (Array.isArray(firstValue)) { | ||
const newArray = []; | ||
for (const item of secondValue) { | ||
if (item === "...") { | ||
for (const item of firstValue) { | ||
newArray.push(item); | ||
} | ||
} else { | ||
newArray.push(item); | ||
} | ||
} | ||
newObject[key] = newArray; | ||
} else { | ||
newObject[key] = secondValue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above comment above. |
||
} | ||
} else { | ||
const firstValue = newObject[key]; | ||
if ( | ||
typeof firstValue === "object" && | ||
firstValue !== null && | ||
!Array.isArray(firstValue) | ||
) { | ||
newObject[key] = cleverMerge(firstValue, secondValue); | ||
} else { | ||
newObject[key] = secondValue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above. |
||
} | ||
} | ||
} | ||
return newObject; | ||
}; | ||
|
||
exports.cachedCleverMerge = cachedCleverMerge; | ||
exports.cleverMerge = cleverMerge; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
module.exports = require("./wrong"); | ||
module.exports = require("./wrong") + require("./normal") + require("./wrong2"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
module.exports = require("./wrong"); | ||
module.exports = require("./wrong") + require("./normal") + require("./wrong2"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("./wrong") + require("./normal") + require("./wrong2"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
it("should allow to set custom resolving rules", function() { | ||
var a = require("./a"); | ||
expect(a).toBe("ok"); | ||
expect(a).toBe("ok-normal-ok2"); | ||
var b = require("./b"); | ||
expect(b).toBe("wrong"); | ||
expect(b).toBe("ok-normal-ok2-yes"); | ||
var c = require("./c"); | ||
expect(c).toBe("wrong-normal-ok2"); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "-normal-"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "ok-ok"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "ok2"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "ok2-yes"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "wrong2"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assignment should be before the
if
statement above. Then replacesecond[key]
in the body of theif
withsecondValue
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you open issue and describe problem?