From 7ded05830b39f96d8ab0601603e16ebd82d0fe64 Mon Sep 17 00:00:00 2001 From: Jelle De Loecker Date: Sun, 25 Feb 2024 16:36:08 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A5=20Don't=20let=20`Object.setPath()`?= =?UTF-8?q?=20create=20arrays=20by=20default?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + lib/object.js | 25 +++++++++++-------------- lib/rurl.js | 6 +++--- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d47ed4..2fe4bd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/object.js b/lib/object.js index 32c3760..7b8e7fe 100644 --- a/lib/object.js +++ b/lib/object.js @@ -697,23 +697,20 @@ defStat(function path(_flag, _obj, _path_string) { * * @author Jelle De Loecker * @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 * @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); }); /** diff --git a/lib/rurl.js b/lib/rurl.js index a52f020..95acd58 100644 --- a/lib/rurl.js +++ b/lib/rurl.js @@ -721,7 +721,7 @@ RURL.setStatic(function parseFormPath(input, limit) { * * @author Jelle De Loecker * @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') {