Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ That is, where `obj` is equivalent to:

flat = DataObjectParser.untranspose(structured)

structured = DataObjectParser.transpose(flat)
structured = DataObjectParser.transpose(flat, [options] /*default: {transposeNumAsArray : false}*/)

Where the equivalent results would be:

Expand Down
16 changes: 13 additions & 3 deletions lib/utils/dataobject-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ function _isDate(value) {
return value instanceof Date;
}

function DataObjectParser($data){
function DataObjectParser($data, options){
this._data = $data || {};
options = options || {};
this._numbersAreArrays = !!options.transposeNumAsArray;
}

/**
Expand Down Expand Up @@ -69,6 +71,14 @@ DataObjectParser.prototype.set = function($path, $value) {
$obj[$key] = $data;
}
};

var isTreatLikeArray = function(key) {
if(_isNumber(key)) {
return this._numbersAreArrays;
}

return key === "[]";
}

while(pathList.length > 0) {
parentKey = pathList.shift().replace(/["']/g, '');
Expand All @@ -78,8 +88,8 @@ DataObjectParser.prototype.set = function($path, $value) {
throw new Error('Prototype pollution attempt detected');
}

// Number, treat it as an array
if (!isNaN(+parentKey) || parentKey === "[]") {
// Number, check whether to treat as Array
if (isTreatLikeArray(parentKey)) {
if(!_isArray(parent) /* prevent overwritting */ ) {
parent = [];
addObj(grandParent, grandParentKey, parent);
Expand Down