-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💥 Don't let
Object.setPath()
create arrays by default
- Loading branch information
Showing
3 changed files
with
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -697,23 +697,20 @@ defStat(function path(_flag, _obj, _path_string) { | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.1.4 | ||
* @version 0.5.7 | ||
* @version 0.9.2 | ||
*/ | ||
defStat(function setPath(obj, path, value, skipLastEntry, allow_prototype) { | ||
defStat(function setPath(obj, path, value, skip_last_entry, allow_prototype, create_arrays = false) { | ||
|
||
var argLength = arguments.length, | ||
pieces, | ||
here, | ||
key, | ||
let key, | ||
end, | ||
i; | ||
|
||
pieces = Obj.parseDotNotationPath(path); | ||
let pieces = Obj.parseDotNotationPath(path); | ||
|
||
// If no default end value is given, use a new object | ||
// Caution: undefined is also a valid end value, | ||
// so we check the arguments length for that | ||
if (typeof value == 'undefined' && argLength < 3) { | ||
if (typeof value == 'undefined' && arguments.length < 3) { | ||
value = {}; | ||
} | ||
|
||
|
@@ -722,7 +719,7 @@ defStat(function setPath(obj, path, value, skipLastEntry, allow_prototype) { | |
} | ||
|
||
// Set out current position | ||
here = obj; | ||
let here = obj; | ||
|
||
for (i = 0; i < pieces.length; i++) { | ||
key = pieces[i]; | ||
|
@@ -741,14 +738,14 @@ defStat(function setPath(obj, path, value, skipLastEntry, allow_prototype) { | |
if (end) { | ||
|
||
// Only set the last entry if we don't want to skip it | ||
if (!skipLastEntry) { | ||
if (!skip_last_entry) { | ||
here[key] = value; | ||
} | ||
} else { | ||
if (here[key] == null || (typeof here[key] != 'object' && typeof here[key] != 'function')) { | ||
// If the wanted entry doesn't exist | ||
// AND the next key is a number, create an array | ||
if (pieces[i+1] === '' || Number(pieces[i+1]) == pieces[i+1]) { | ||
if (create_arrays && (pieces[i+1] === '' || Number(pieces[i+1]) == pieces[i+1])) { | ||
here[key] = []; | ||
} else { | ||
here[key] = {}; | ||
|
@@ -811,11 +808,11 @@ defStat(function getNextIndex(obj) { | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.1.11 | ||
* @version 0.9.0 | ||
* @version 0.9.2 | ||
*/ | ||
defStat(function setFormPath(obj, form_path, value, skipLastEntry) { | ||
defStat(function setFormPath(obj, form_path, value, skip_last_entry, allow_prototype, create_arrays = true) { | ||
let path = Classes.RURL.parseFormPath(form_path); | ||
return Obj.setPath(obj, path, value, skipLastEntry); | ||
return Obj.setPath(obj, path, value, skip_last_entry, allow_prototype, create_arrays); | ||
}); | ||
|
||
/** | ||
|
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 |
---|---|---|
|
@@ -721,7 +721,7 @@ RURL.setStatic(function parseFormPath(input, limit) { | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.5.7 | ||
* @version 0.5.7 | ||
* @version 0.9.2 | ||
* | ||
* @param {string} input | ||
* @param {Object} options | ||
|
@@ -754,7 +754,7 @@ RURL.setStatic(function parseQuery(input, options) { | |
temp = RURL.parseFormPath(key, options.depth); | ||
|
||
// Set the path (but don't set prototype things) | ||
Obj.setPath(result, temp, val, null, false); | ||
Obj.setPath(result, temp, val, null, false, true); | ||
} | ||
|
||
return result; | ||
|
@@ -1835,7 +1835,7 @@ RURL.setMethod(function addQuery(params, value) { | |
} | ||
|
||
} else { | ||
Obj.setPath(query, pieces, value); | ||
Obj.setPath(query, pieces, value, false, true, true); | ||
} | ||
} | ||
} else if (params && typeof params == 'object') { | ||
|