-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
whenDone
function parameter to the unDry
& unDryFunction
…
…function calls
- Loading branch information
Showing
5 changed files
with
206 additions
and
17 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
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 |
---|---|---|
|
@@ -438,7 +438,7 @@ function generateReviver(reviver, undry_paths) { | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 1.0.0 | ||
* @version 1.0.10 | ||
* @version 1.1.0 | ||
* | ||
* @param {Object} obj | ||
* @param {String} custom_method Custom method to use if available | ||
|
@@ -466,19 +466,52 @@ function clone(obj, custom_method, extra_args, wm) { | |
return wm.get(obj); | ||
} | ||
|
||
if (!wm.when_done_queue) { | ||
wm.when_done_queue = []; | ||
wm.drymap = new Map(); | ||
|
||
wm.whenDone = function whenDone(fnc) { | ||
if (!fnc) return; | ||
wm.when_done_queue.push(fnc); | ||
}; | ||
} | ||
|
||
if (custom_method) { | ||
extra_args = [wm].concat(extra_args); | ||
} | ||
|
||
return real_clone({'_': obj}, custom_method, extra_args, wm)['_']; | ||
let result = real_clone({'_': obj}, custom_method, extra_args, wm)['_'], | ||
i; | ||
|
||
if (wm.when_done_queue.length) { | ||
let temp_val, | ||
key; | ||
|
||
// Iterate over all the objects created with the `toDry` method | ||
wm.drymap.forEach(function each(val, temp) { | ||
for (key in temp) { | ||
temp_val = temp[key]; | ||
|
||
if (wm.drymap.has(temp_val)) { | ||
temp[key] = wm.drymap.get(temp_val); | ||
} | ||
} | ||
}); | ||
|
||
for (i = 0; i < wm.when_done_queue.length; i++) { | ||
wm.when_done_queue[i](); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Deep clone an object | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 1.0.0 | ||
* @version 1.0.10 | ||
* @version 1.1.0 | ||
* | ||
* @param {Object} obj | ||
* @param {String} custom_method Custom method to use if available | ||
|
@@ -548,19 +581,27 @@ function real_clone(obj, custom_method, extra_args, wm) { | |
target[key] = entry.dryClone(wm, custom_method); | ||
} else if (entry.toDry) { | ||
// Perform the toDry function | ||
temp = entry.toDry(); | ||
let uncloned_dried = entry.toDry(); | ||
|
||
// Remember this temporary object to prevent infinite loops | ||
wm.set(entry, uncloned_dried); | ||
|
||
// Clone the value, | ||
// because returned objects aren't necesarilly cloned yet | ||
temp = real_clone(temp, custom_method, extra_args, wm).value; | ||
let cloned_dried = real_clone(uncloned_dried, custom_method, extra_args, wm).value; | ||
|
||
// Perform the undry function | ||
if (entry.constructor.unDry) { | ||
target[key] = entry.constructor.unDry(temp, custom_method || true); | ||
target[key] = entry.constructor.unDry(cloned_dried, custom_method || true, wm.whenDone); | ||
} else { | ||
// If there is no undry function, the clone will be a simple object | ||
target[key] = temp; | ||
target[key] = cloned_dried; | ||
} | ||
|
||
// Remember both entries (for circular undry references) | ||
wm.drymap.set(cloned_dried, target[key]); | ||
wm.drymap.set(uncloned_dried, target[key]); | ||
|
||
} else if (name_type == 'Date') { | ||
target[key] = new Date(entry); | ||
} else if (name_type == 'RegExp') { | ||
|
@@ -1171,7 +1212,7 @@ function walk(obj, fnc, result, seen) { | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 1.0.0 | ||
* @version 1.0.11 | ||
* @version 1.1.0 | ||
* | ||
* @param {Object} value | ||
* | ||
|
@@ -1180,6 +1221,7 @@ function walk(obj, fnc, result, seen) { | |
function parse(object, reviver) { | ||
|
||
var undry_paths = new Map(), | ||
when_done = [], | ||
retrieve = {}, | ||
reviver, | ||
result, | ||
|
@@ -1209,6 +1251,11 @@ function parse(object, reviver) { | |
return result; | ||
} | ||
|
||
function whenDone(fnc) { | ||
if (!fnc) return; | ||
when_done.push(fnc); | ||
} | ||
|
||
// To remember which objects have already been revived | ||
seen = new WeakMap(); | ||
|
||
|
@@ -1225,9 +1272,9 @@ function parse(object, reviver) { | |
regenerate(result, null, entry, seen, retrieve, undry_paths, old, path_array.slice(0)); | ||
|
||
if (entry.unDryConstructor) { | ||
entry.undried = entry.unDryConstructor.unDry(entry.value, false); | ||
entry.undried = entry.unDryConstructor.unDry(entry.value, false, whenDone); | ||
} else if (entry.unDryFunction) { | ||
entry.undried = entry.unDryFunction(entry, null, entry.value); | ||
entry.undried = entry.unDryFunction(entry, null, entry.value, whenDone); | ||
} else { | ||
entry.undried = entry.value; | ||
} | ||
|
@@ -1286,7 +1333,11 @@ function parse(object, reviver) { | |
result = regenerate(result, result, result, seen, retrieve, undry_paths, old, []); | ||
|
||
if (result.undried != null && result.dry) { | ||
return result.undried; | ||
result = result.undried; | ||
} | ||
|
||
for (i = 0; i < when_done.length; i++) { | ||
when_done[i](); | ||
} | ||
|
||
return result; | ||
|
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,7 +1,7 @@ | ||
{ | ||
"name": "json-dry", | ||
"description": "Don't repeat yourself, JSON: Add support for (circular) references, class instances, ...", | ||
"version": "1.0.12", | ||
"version": "1.1.0-alpha", | ||
"author": "Jelle De Loecker <[email protected]>", | ||
"keywords": [ | ||
"json", | ||
|
@@ -12,15 +12,15 @@ | |
"main": "./lib/json-dry.js", | ||
"repository": "[email protected]:skerit/json-dry.git", | ||
"scripts": { | ||
"test" : "node_modules/.bin/mocha --reporter spec", | ||
"test" : "mocha --exit --reporter spec --bail --timeout 5000 --file test/dry.js", | ||
"coverage" : "./node_modules/istanbul/lib/cli.js cover _mocha", | ||
"report-coverage" : "cat ./coverage/lcov.info | coveralls" | ||
"report-coverage" : "codecov" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"coveralls" : "^2.11.6", | ||
"istanbul" : "^0.4.5", | ||
"mocha" : "1.20.x", | ||
"codecov" : "~3.7.0", | ||
"nyc" : "^15.1.0", | ||
"mocha" : "~8.0.1", | ||
"protoblast" : "skerit/protoblast#3218106" | ||
}, | ||
"engines": { | ||
|
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