Comparing version 1.3.0 to 1.4.0
'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _uniques = require('uniques'); | ||
@@ -16,2 +18,4 @@ | ||
return 'object'; | ||
} else { | ||
return typeof x === 'undefined' ? 'undefined' : _typeof(x); | ||
} | ||
@@ -42,5 +46,2 @@ }; | ||
var ob = function ob(subject) { | ||
if (type(subject) !== 'array' && type(subject) !== 'object') { | ||
return undefined; | ||
} | ||
@@ -267,2 +268,34 @@ return { | ||
return this.select(keysToKeep); | ||
}, | ||
flatten: function flatten() { | ||
var prefix = arguments.length <= 0 || arguments[0] === undefined ? '' : arguments[0]; | ||
var shallow = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1]; | ||
var res = {}; | ||
if (type(subject) === 'object' || type(subject) === 'array') { | ||
for (var i in subject) { | ||
var tmpPrefix = undefined; | ||
if (prefix === '') { | ||
tmpPrefix = '' + i; | ||
} else { | ||
tmpPrefix = prefix + '.' + i; | ||
} | ||
if (type(subject[i]) === 'array') { | ||
tmpPrefix = tmpPrefix + '[]'; | ||
} | ||
res[tmpPrefix] = subject[i]; | ||
if (type(subject[i]) === 'array' && shallow) { | ||
res = _extends({}, res, ob(subject[i][0]).flatten(tmpPrefix, shallow)); | ||
} else { | ||
res = _extends({}, res, ob(subject[i]).flatten(tmpPrefix, shallow)); | ||
} | ||
} | ||
} | ||
return res; | ||
} | ||
@@ -269,0 +302,0 @@ }; |
{ | ||
"name": "objob", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "A tool for controlling and manipulating javascript object fields and output.", | ||
@@ -28,2 +28,3 @@ "main": "lib/objob.js", | ||
"babel-preset-es2015": "^6.3.13", | ||
"babel-preset-stage-0": "^6.3.13", | ||
"chai": "^3.4.1", | ||
@@ -30,0 +31,0 @@ "mocha": "^2.3.4" |
@@ -10,2 +10,4 @@ 'use strict'; | ||
return 'object'; | ||
} else { | ||
return typeof x; | ||
} | ||
@@ -37,5 +39,2 @@ }; | ||
let ob = function (subject) { | ||
if(type(subject) !== 'array' && type(subject) !== 'object') { | ||
return undefined; | ||
} | ||
@@ -131,2 +130,31 @@ return { | ||
}, | ||
flatten: function(prefix='', shallow=false){ | ||
let res = {}; | ||
if(type(subject) === 'object' || type(subject) === 'array'){ | ||
for(let i in subject) { | ||
let tmpPrefix; | ||
if(prefix === '') { | ||
tmpPrefix = `${i}`; | ||
} else { | ||
tmpPrefix = `${prefix}.${i}`; | ||
} | ||
if(type(subject[i]) === 'array') { | ||
tmpPrefix = tmpPrefix + '[]'; | ||
} | ||
res[tmpPrefix] = subject[i]; | ||
if(type(subject[i]) === 'array' && shallow) { | ||
res = {...res, ...ob(subject[i][0]).flatten(tmpPrefix, shallow)}; | ||
} else { | ||
res = {...res, ...ob(subject[i]).flatten(tmpPrefix, shallow)}; | ||
} | ||
} | ||
} | ||
return res; | ||
}, | ||
}; | ||
@@ -133,0 +161,0 @@ }; |
42
tests.js
@@ -115,2 +115,44 @@ /* eslint max-nested-callbacks: 0*/ | ||
describe('flatten', () => { | ||
it('should return the flattened object', (done) => { | ||
expect(ob(ob3).flatten()).to.deep.equal({ | ||
name: 'Bob', | ||
feet: 5, | ||
body: { | ||
feet: { | ||
toes: 2, | ||
}, | ||
}, | ||
'body.feet': {toes: 2}, | ||
'body.feet.toes': 2, | ||
'eyes[]': [{location: 'left', color: 'blue'}, {location: 'right', color: 'red'}], | ||
'eyes[].0': {location: 'left', color: 'blue'}, | ||
'eyes[].1': {location: 'right', color: 'red'}, | ||
'eyes[].0.location': 'left', | ||
'eyes[].1.location': 'right', | ||
'eyes[].0.color': 'blue', | ||
'eyes[].1.color': 'red', | ||
}); | ||
done(); | ||
}); | ||
it('should return the flattened shallow object', (done) => { | ||
expect(ob(ob3).flatten('', true)).to.deep.equal({ | ||
name: 'Bob', | ||
feet: 5, | ||
body: { | ||
feet: { | ||
toes: 2, | ||
}, | ||
}, | ||
'body.feet': {toes: 2}, | ||
'body.feet.toes': 2, | ||
'eyes[]': [{location: 'left', color: 'blue'}, {location: 'right', color: 'red'}], | ||
'eyes[].location': 'left', | ||
'eyes[].color': 'blue', | ||
}); | ||
done(); | ||
}); | ||
}); | ||
describe('Chaining', () => { | ||
@@ -117,0 +159,0 @@ it('should return many of an object after filtering select select', (done) => { |
Sorry, the diff of this file is not supported yet
18125
536
6