Skip to content

Commit

Permalink
Merge pull request #9 from tmoiron/master
Browse files Browse the repository at this point in the history
Adds a parameter (useArray) to allow converting object without using arrays.
  • Loading branch information
rhalff committed Sep 30, 2015
2 parents 83e176f + f620bec commit baa4202
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 12 deletions.
24 changes: 18 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ function parseKey(key, val) {
return key;
}

function isIndex(k) {
return /^\d+/.test(k);
function isIndex(k, useIndex) {
return useIndex && /^\d+/.test(k);
}

function parsePath(path, sep) {
Expand All @@ -42,22 +42,24 @@ function parsePath(path, sep) {
return path.split(sep);
}

function DotObject(seperator, override) {
function DotObject(seperator, override, useArray) {

if (!(this instanceof DotObject)) {
return new DotObject(seperator, override);
return new DotObject(seperator, override, useArray);
}

if (typeof seperator === 'undefined') { seperator = '.'; }
if (typeof override === 'undefined') { override = false; }
if (typeof useArray === 'undefined') { useArray = true; }
this.seperator = seperator;
this.override = override;
this.useArray = useArray;

// contains touched arrays
this.cleanup = [];
}

var dotDefault = new DotObject('.', false);
var dotDefault = new DotObject('.', false, true);
function wrap(method) {
return function() {
return dotDefault[method].apply(dotDefault, arguments);
Expand All @@ -68,7 +70,8 @@ DotObject.prototype._fill = function(a, obj, v, mod) {
var k = a.shift();

if (a.length > 0) {
obj[k] = obj[k] || (a.length === 1 && isIndex(a[0]) ? [] : {});
obj[k] = obj[k] ||
(a.length === 1 && isIndex(a[0], this.useArray) ? [] : {});

if (obj[k] !== Object(obj[k])) {
if (this.override) {
Expand Down Expand Up @@ -442,6 +445,15 @@ DotObject.dot = wrap('dot');
});
});

Object.defineProperty(DotObject, 'useArray', {
get: function() {
return dotDefault.useArray;
},
set: function(val) {
dotDefault.useArray = val;
}
});

DotObject._process = _process;

module.exports = DotObject;
24 changes: 18 additions & 6 deletions src/dot-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ function parseKey(key, val) {
return key;
}

function isIndex(k) {
return /^\d+/.test(k);
function isIndex(k, useIndex) {
return useIndex && /^\d+/.test(k);
}

function parsePath(path, sep) {
Expand All @@ -42,22 +42,24 @@ function parsePath(path, sep) {
return path.split(sep);
}

function DotObject(seperator, override) {
function DotObject(seperator, override, useArray) {

if (!(this instanceof DotObject)) {
return new DotObject(seperator, override);
return new DotObject(seperator, override, useArray);
}

if (typeof seperator === 'undefined') { seperator = '.'; }
if (typeof override === 'undefined') { override = false; }
if (typeof useArray === 'undefined') { useArray = true; }
this.seperator = seperator;
this.override = override;
this.useArray = useArray;

// contains touched arrays
this.cleanup = [];
}

var dotDefault = new DotObject('.', false);
var dotDefault = new DotObject('.', false, true);
function wrap(method) {
return function() {
return dotDefault[method].apply(dotDefault, arguments);
Expand All @@ -68,7 +70,8 @@ DotObject.prototype._fill = function(a, obj, v, mod) {
var k = a.shift();

if (a.length > 0) {
obj[k] = obj[k] || (a.length === 1 && isIndex(a[0]) ? [] : {});
obj[k] = obj[k] ||
(a.length === 1 && isIndex(a[0], this.useArray) ? [] : {});

if (obj[k] !== Object(obj[k])) {
if (this.override) {
Expand Down Expand Up @@ -442,4 +445,13 @@ DotObject.dot = wrap('dot');
});
});

Object.defineProperty(DotObject, 'useArray', {
get: function() {
return dotDefault.useArray;
},
set: function(val) {
dotDefault.useArray = val;
}
});

DotObject._process = _process;
72 changes: 72 additions & 0 deletions test/useArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

require('should');
var Dot = require('../index');

describe('useArray:', function () {

var dotObject, arrayObject, object;
var arrayObjectExpected, objectExpected, dotObjectExpected;

beforeEach(function () {

dotObject = {
'a.0': 'value'
};
dotObjectExpected = {
'a.0': 'value'
};
arrayObject = {
a: ['value']
};
arrayObjectExpected = {
a: ['value']
};
object = {
a: {
'0': 'value'
}
};
objectExpected = {
a: {
'0': 'value'
}
};

});


it('default is using array ', function () {
var dotLocal = require('../index');
arrayObjectExpected.should.eql(dotLocal.object(dotObject));
});
it('Should convert dot using arrays ', function () {
Dot.useArray = true;
arrayObjectExpected.should.eql(Dot.object(dotObject));
});
it('Should convert dot not using arrays ', function () {
Dot.useArray = false;
objectExpected.should.eql(Dot.object(dotObject));
});
it('Should convert object using arrays ', function () {
Dot.useArray = true;
arrayObjectExpected.should.eql(Dot.object(dotObject));
});
it('Should convert dot using arrays and convert back equals source', function () {
Dot.useArray = true;
dotObjectExpected.should.eql(Dot.dot(Dot.object(dotObject)));
});
it('Should convert object using arrays and convert back equals source', function () {
Dot.useArray = true;
arrayObjectExpected.should.eql(Dot.object(Dot.dot(object)));
});
it('Should convert dot not using arrays and convert back equals source', function () {
Dot.useArray = false;
dotObjectExpected.should.eql(Dot.dot(Dot.object(dotObject)));
});
it('Should convert object not using arrays and convert back equals source', function () {
Dot.useArray = false;
objectExpected.should.eql(Dot.object(Dot.dot(arrayObject)));
});

});

0 comments on commit baa4202

Please sign in to comment.