Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

objob

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

objob - npm Package Compare versions

Comparing version 1.4.1 to 1.5.0

lib/functions.js

311

lib/objob.js
'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');

@@ -9,16 +7,18 @@

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _typeOf = require('type-of');
function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }
var _typeOf2 = _interopRequireDefault(_typeOf);
var type = function type(x) {
if (Object.prototype.toString.call(x) === '[object Array]') {
return 'array';
} else if ((typeof x === 'undefined' ? 'undefined' : _typeof(x)) === 'object') {
return 'object';
} else {
return typeof x === 'undefined' ? 'undefined' : _typeof(x);
}
};
var _stringContains = require('string-contains');
var _stringContains2 = _interopRequireDefault(_stringContains);
var _deepmerge = require('deepmerge');
var _deepmerge2 = _interopRequireDefault(_deepmerge);
var _functions = require('./functions');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**

@@ -36,3 +36,3 @@ * Returns an objob object

var allKeys = ob(subject).keys();
var allKeys = ob(ob(subject).flatten()).keys();
var keysToKeep = [];

@@ -96,125 +96,120 @@

console.log(keysToKeep);
return this.select(keysToKeep);
return ob(subject).select(keysToKeep);
},
expand: function expand() {
var depth = arguments.length <= 0 || arguments[0] === undefined ? 1 : arguments[0];
var res = undefined;
subject = (0, _functions.makeFlattenedShallow)(subject);
if (type(subject) === 'array') {
res = [];
// Determine if an array is represented by the flattened object
var rootObjectPresent = true;
if (depth === 1) {
rootObjectPresent = false;
for (var key in subject) {
var rootArrayPresent = key.match(/^\d/ig);
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
rootObjectPresent = rootObjectPresent || !rootArrayPresent;
}
}
try {
for (var _iterator3 = subject[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var i = _step3.value;
res = res.concat(ob(i).expand());
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
if (rootObjectPresent === false && depth === 1) {
res = [];
for (var key in subject) {
res.push(subject[key]);
}
return res;
} else {
var keyChains = ob(subject).keys();
// Get the empty object ready for the data
for (var keyChain in subject) {
var subkeys = keyChain.split('.');
// When the object is just {'example.example': y}
// One key and one value
if (keyChains.length === 1) {
var tmp = {};
var obj = tmp;
var keyChain = keyChains[0]; // something like 'first.another.another'
var value = subject[keyChain];
var count = undefined;
var count = 1;
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
res = tmp; // Poining to tmp so that we have a place holder before nesting
count = 1;
var keys = keyChain.split('.');
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator4 = subkeys[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var subkey = _step4.value;
for (var _iterator3 = keys[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var key = _step3.value;
// Set the value if the end of the keys
if (count === subkeys.length) {
tmp[subkey.replace(/\[\]/g, '')] = subject[keyChain];
if (count === keys.length) {
tmp[key] = value;
} else {
// If array create the array, else create the object
if (subkey.indexOf('[]') !== -1) {
subkey = subkey.replace(/\[\]/g, '');
tmp[subkey] = [];
var isArray = (0, _stringContains2.default)(key, '[]');
if (isArray) {
key = key.replace('[]', '');
tmp[key] = [];
} else {
tmp[subkey] = {};
tmp[key] = {};
}
tmp = tmp[subkey];
count++;
tmp = tmp[key];
}
count++;
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
for (var i in obj) {
if (obj[i] === null || obj[i] === undefined) {
delete obj[i];
}
} else {
// If multiple keychains in the object, simplify our logic a bit
res = {};
for (var i in subject) {
var tmp = {};
tmp[i] = subject[i];
res = (0, _deepmerge2.default)(res, ob(tmp).expand(++depth));
}
res = _extends({}, obj, res);
}
}
return res;
//return ob(res).removeUndefs();
},
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 counter = arguments.length <= 2 || arguments[2] === undefined ? 0 : arguments[2];
var depth = arguments.length <= 1 || arguments[1] === undefined ? 1 : arguments[1];
var res = undefined;
if (type(subject) === 'array' && counter === 0) {
if ((0, _typeOf2.default)(subject) === 'array' && depth === 1) {
res = [];
var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator5 = subject[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
var i = _step5.value;
for (var _iterator4 = subject[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var i = _step4.value;
res = res.concat(ob(i).flatten(prefix, shallow, counter++));
res = res.concat(ob(i).flatten(prefix, ++depth));
}
} catch (err) {
_didIteratorError5 = true;
_iteratorError5 = err;
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion5 && _iterator5.return) {
_iterator5.return();
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError5) {
throw _iteratorError5;
if (_didIteratorError4) {
throw _iteratorError4;
}

@@ -228,3 +223,3 @@ }

if (type(subject) === 'object' || type(subject) === 'array') {
if ((0, _typeOf2.default)(subject) === 'object' || (0, _typeOf2.default)(subject) === 'array') {

@@ -239,3 +234,3 @@ for (var i in subject) {

if (type(subject[i]) === 'array') {
if ((0, _typeOf2.default)(subject[i]) === 'array') {
tmpPrefix = tmpPrefix + '[]';

@@ -246,7 +241,3 @@ }

if (type(subject[i]) === 'array' && shallow) {
res = _extends({}, res, ob(subject[i][0]).flatten(tmpPrefix, shallow, counter++));
} else {
res = _extends({}, res, ob(subject[i]).flatten(tmpPrefix, shallow, counter++));
}
res = (0, _deepmerge2.default)(res, ob(subject[i]).flatten(tmpPrefix, ++depth));
}

@@ -260,10 +251,10 @@ }

if (type(subject) === 'array') {
var _iteratorNormalCompletion6 = true;
var _didIteratorError6 = false;
var _iteratorError6 = undefined;
if ((0, _typeOf2.default)(subject) === 'array') {
var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
try {
for (var _iterator6 = subject[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
var i = _step6.value;
for (var _iterator5 = subject[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
var i = _step5.value;

@@ -273,12 +264,12 @@ keys = keys.concat(ob(i).keys());

} catch (err) {
_didIteratorError6 = true;
_iteratorError6 = err;
_didIteratorError5 = true;
_iteratorError5 = err;
} finally {
try {
if (!_iteratorNormalCompletion6 && _iterator6.return) {
_iterator6.return();
if (!_iteratorNormalCompletion5 && _iterator5.return) {
_iterator5.return();
}
} finally {
if (_didIteratorError6) {
throw _iteratorError6;
if (_didIteratorError5) {
throw _iteratorError5;
}

@@ -300,3 +291,3 @@ }

if (type(subject) === 'array') {
if ((0, _typeOf2.default)(subject) === 'array') {
return subject;

@@ -311,2 +302,28 @@ } else {

},
removeUndefs: function removeUndefs() {
var res = undefined;
if ((0, _typeOf2.default)(subject) === 'array') {
res = [];
for (var key in subject) {
if (subject[key] === undefined) {} else {
res.push(ob(subject[key]).removeUndefs());
}
}
} else if ((0, _typeOf2.default)(subject) === 'object') {
for (var key in subject) {
if (subject[key] === undefined) {
delete subject[key];
} else {
subject[key] = ob(subject[key]).removeUndefs();
}
}
return subject;
} else {
return subject;
}
return res;
},
select: function select() {

@@ -317,12 +334,12 @@ var keys = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];

if (type(subject) === 'array') {
if ((0, _typeOf2.default)(subject) === 'array') {
resp = [];
var _iteratorNormalCompletion7 = true;
var _didIteratorError7 = false;
var _iteratorError7 = undefined;
var _iteratorNormalCompletion6 = true;
var _didIteratorError6 = false;
var _iteratorError6 = undefined;
try {
for (var _iterator7 = subject[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
var i = _step7.value;
for (var _iterator6 = subject[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
var i = _step6.value;

@@ -332,12 +349,12 @@ resp = resp.concat(ob(i).select(keys));

} catch (err) {
_didIteratorError7 = true;
_iteratorError7 = err;
_didIteratorError6 = true;
_iteratorError6 = err;
} finally {
try {
if (!_iteratorNormalCompletion7 && _iterator7.return) {
_iterator7.return();
if (!_iteratorNormalCompletion6 && _iterator6.return) {
_iterator6.return();
}
} finally {
if (_didIteratorError7) {
throw _iteratorError7;
if (_didIteratorError6) {
throw _iteratorError6;
}

@@ -352,9 +369,9 @@ }

for (var actualKey in flat) {
var _iteratorNormalCompletion8 = true;
var _didIteratorError8 = false;
var _iteratorError8 = undefined;
var _iteratorNormalCompletion7 = true;
var _didIteratorError7 = false;
var _iteratorError7 = undefined;
try {
for (var _iterator8 = keys[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
var desiredKey = _step8.value;
for (var _iterator7 = keys[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
var desiredKey = _step7.value;

@@ -366,12 +383,12 @@ if (actualKey === desiredKey) {

} catch (err) {
_didIteratorError8 = true;
_iteratorError8 = err;
_didIteratorError7 = true;
_iteratorError7 = err;
} finally {
try {
if (!_iteratorNormalCompletion8 && _iterator8.return) {
_iterator8.return();
if (!_iteratorNormalCompletion7 && _iterator7.return) {
_iterator7.return();
}
} finally {
if (_didIteratorError8) {
throw _iteratorError8;
if (_didIteratorError7) {
throw _iteratorError7;
}

@@ -386,13 +403,19 @@ }

},
shallow: function shallow() {
var x = ob(subject).flatten();
x = (0, _functions.makeFlattenedShallow)(x);
return ob(x).expand();
},
values: function values() {
var values = [];
if (type(subject) === 'array') {
var _iteratorNormalCompletion9 = true;
var _didIteratorError9 = false;
var _iteratorError9 = undefined;
if ((0, _typeOf2.default)(subject) === 'array') {
var _iteratorNormalCompletion8 = true;
var _didIteratorError8 = false;
var _iteratorError8 = undefined;
try {
for (var _iterator9 = subject[Symbol.iterator](), _step9; !(_iteratorNormalCompletion9 = (_step9 = _iterator9.next()).done); _iteratorNormalCompletion9 = true) {
var i = _step9.value;
for (var _iterator8 = subject[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
var i = _step8.value;

@@ -402,12 +425,12 @@ values = values.concat(ob(i).values());

} catch (err) {
_didIteratorError9 = true;
_iteratorError9 = err;
_didIteratorError8 = true;
_iteratorError8 = err;
} finally {
try {
if (!_iteratorNormalCompletion9 && _iterator9.return) {
_iterator9.return();
if (!_iteratorNormalCompletion8 && _iterator8.return) {
_iterator8.return();
}
} finally {
if (_didIteratorError9) {
throw _iteratorError9;
if (_didIteratorError8) {
throw _iteratorError8;
}

@@ -414,0 +437,0 @@ }

{
"name": "objob",
"version": "1.4.1",
"version": "1.5.0",
"description": "A tool for controlling and manipulating javascript object fields and output.",

@@ -22,2 +22,5 @@ "main": "lib/objob.js",

"dependencies": {
"deepmerge": "^0.2.10",
"string-contains": "^0.1.0",
"type-of": "^2.0.1",
"uniques": "0.0.1"

@@ -24,0 +27,0 @@ },

'use strict';
import uniques from 'uniques';
import type from 'type-of';
import contains from 'string-contains';
import merge from 'deepmerge';
import { makeFlattenedShallow } from './functions';
let type = function(x) {
if( Object.prototype.toString.call(x) === '[object Array]' ) {
return 'array';
} else if(typeof x === 'object') {
return 'object';
} else {
return typeof x;
}
};
/**

@@ -25,3 +19,3 @@ * Returns an objob object

deselect: function(keys = []){
let allKeys = ob(subject).keys();
let allKeys = ob(ob(subject).flatten()).keys();
let keysToKeep = [];

@@ -42,60 +36,77 @@

}
console.log(keysToKeep);
return this.select(keysToKeep);
return ob(subject).select(keysToKeep);
},
expand: function(){
expand: function(depth = 1){
let res;
subject = makeFlattenedShallow(subject);
if(type(subject) === 'array') {
res = [];
// Determine if an array is represented by the flattened object
let rootObjectPresent = true;
if(depth === 1) {
rootObjectPresent = false;
for(let key in subject) {
let rootArrayPresent = key.match(/^\d/ig);
for(let i of subject){
res = res.concat(ob(i).expand());
rootObjectPresent = (rootObjectPresent || !rootArrayPresent);
}
}
return res;
if(rootObjectPresent === false && depth === 1) {
res = [];
for(let key in subject) {
res.push(subject[key]);
}
} else {
let keyChains = ob(subject).keys();
// Get the empty object ready for the data
for(let keyChain in subject){
let subkeys = keyChain.split('.');
// When the object is just {'example.example': y}
// One key and one value
if(keyChains.length === 1) {
let tmp = {};
let obj = tmp;
let keyChain = keyChains[0]; // something like 'first.another.another'
let value = subject[keyChain];
let count;
let count = 1;
for(let subkey of subkeys) {
// Set the value if the end of the keys
if(count === subkeys.length) {
tmp[subkey.replace(/\[\]/g, '')] = subject[keyChain];
res = tmp; // Poining to tmp so that we have a place holder before nesting
count = 1;
let keys = keyChain.split('.');
for(let key of keys) {
if(count === keys.length) {
tmp[key] = value;
} else {
// If array create the array, else create the object
if(subkey.indexOf('[]') !== -1){
subkey = subkey.replace(/\[\]/g, '');
tmp[subkey] = [];
let isArray = contains(key, '[]');
if(isArray) {
key = key.replace('[]','');
tmp[key] = [];
} else {
tmp[subkey] = {};
tmp[key] = {};
}
tmp = tmp[subkey];
count++;
tmp = tmp[key];
}
count++;
}
for (let i in obj) {
if (obj[i] === null || obj[i] === undefined) {
delete obj[i];
}
} else {
// If multiple keychains in the object, simplify our logic a bit
res = {};
for(let i in subject) {
let tmp = {};
tmp[i] = subject[i];
res = merge(res, ob(tmp).expand(++depth));
}
res = {...obj, ...res};
}
}
return res;
//return ob(res).removeUndefs();
},
flatten: function(prefix='', shallow=false, counter = 0){
flatten: function(prefix='', depth = 1){
let res;
if(type(subject) === 'array' && counter === 0) {
if(type(subject) === 'array' && depth === 1) {
res = [];
for(let i of subject){
res = res.concat(ob(i).flatten(prefix, shallow, counter++));
res = res.concat(ob(i).flatten(prefix, ++depth));
}

@@ -123,7 +134,3 @@

if(type(subject[i]) === 'array' && shallow) {
res = {...res, ...ob(subject[i][0]).flatten(tmpPrefix, shallow, counter++)};
} else {
res = {...res, ...ob(subject[i]).flatten(tmpPrefix, shallow, counter++)};
}
res = merge(res, ob(subject[i]).flatten(tmpPrefix, ++depth));
}

@@ -162,2 +169,29 @@ }

},
removeUndefs: () => {
let res;
if(type(subject) === 'array') {
res = [];
for(let key in subject) {
if(subject[key] === undefined) {
} else {
res.push(ob(subject[key]).removeUndefs());
}
}
} else if(type(subject) === 'object') {
for(let key in subject) {
if(subject[key] === undefined) {
delete subject[key];
} else {
subject[key] = ob(subject[key]).removeUndefs();
}
}
return subject;
} else {
return subject;
}
return res;
},
select: (keys = []) => {

@@ -189,2 +223,8 @@ let resp;

},
shallow: () => {
let x = ob(subject).flatten();
x = makeFlattenedShallow(x);
return ob(x).expand();
},
values:() => {

@@ -191,0 +231,0 @@ let values = [];

@@ -91,2 +91,5 @@ /* eslint max-nested-callbacks: 0*/

expect(ob(ob2).select(['name', 'age'])).to.deep.equal({name: ob2.name, age: ob2.age});
expect(ob(ob3).select(['eyes[].0.location'])).to.deep.equal({
eyes: [{location: 'left'}],
});
done();

@@ -117,2 +120,13 @@ });

done();
expect(ob(ob3).deselect(['eyes[].0.location'])).to.deep.equal({
name: ob3.name,
feet: ob3.feet,
body: {
feet: {
toes: ob3.body.feet.toes,
},
},
eyes: [{color: 'blue'}, ob3.eyes[1]],
});
});

@@ -151,20 +165,2 @@

});
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();
});
});

@@ -171,0 +167,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc