Skip to content

Commit

Permalink
💥 Don't let Object.setPath() create arrays by default
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Feb 25, 2024
1 parent 7a0cc81 commit 7ded058
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Clear the entire `Develry.Request.cache` as soon as a non-GET request is made
* Don't let falsy values passed to `Decimal` classes cause an error
* Don't let `Object.setPath()` create arrays by default

## 0.9.1 (2024-02-19)

Expand Down
25 changes: 11 additions & 14 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
}

Expand All @@ -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];
Expand All @@ -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] = {};
Expand Down Expand Up @@ -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);
});

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/rurl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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') {
Expand Down

0 comments on commit 7ded058

Please sign in to comment.