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.0 to 1.4.1

435

lib/objob.js

@@ -23,17 +23,2 @@ 'use strict';

var getNestedObject = function getNestedObject(ob, key) {
var keys = key.split('.');
var newOb = {};
if (keys.length > 1) {
// Get the key deselect the fist word separated by a period
var newKey = key.replace(/^(\w|\di|_|$)*./g, '');
newOb[keys[0]] = getNestedObject(ob[keys[0]], newKey);
} else {
newOb[key] = ob[key];
}
return newOb;
};
/**

@@ -48,63 +33,246 @@ * Returns an objob object

return {
keys: function keys() {
var keys = [];
deselect: function deselect() {
var keys = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
var allKeys = ob(subject).keys();
var keysToKeep = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = allKeys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var subjectKey = _step.value;
var keepKey = true;
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = keys[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var keyToRemove = _step2.value;
if (subjectKey === keyToRemove) {
keepKey = false;
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
if (keepKey) {
keysToKeep.push(subjectKey);
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
console.log(keysToKeep);
return this.select(keysToKeep);
},
expand: function expand() {
var res = undefined;
if (type(subject) === 'array') {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
res = [];
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator = subject[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var i = _step.value;
for (var _iterator3 = subject[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var i = _step3.value;
keys = keys.concat(ob(i).keys());
res = res.concat(ob(i).expand());
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
return res;
} else {
for (var k in subject) {
keys.push(k);
};
// Get the empty object ready for the data
for (var keyChain in subject) {
var subkeys = keyChain.split('.');
var tmp = {};
var obj = tmp;
var count = 1;
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = subkeys[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var subkey = _step4.value;
// Set the value if the end of the keys
if (count === subkeys.length) {
tmp[subkey.replace(/\[\]/g, '')] = subject[keyChain];
} else {
// If array create the array, else create the object
if (subkey.indexOf('[]') !== -1) {
subkey = subkey.replace(/\[\]/g, '');
tmp[subkey] = [];
} else {
tmp[subkey] = {};
}
tmp = tmp[subkey];
count++;
}
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
for (var i in obj) {
if (obj[i] === null || obj[i] === undefined) {
delete obj[i];
}
}
res = _extends({}, obj, res);
}
}
return res;
},
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];
return (0, _uniques2.default)(keys);
var res = undefined;
if (type(subject) === 'array' && counter === 0) {
res = [];
var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
try {
for (var _iterator5 = subject[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
var i = _step5.value;
res = res.concat(ob(i).flatten(prefix, shallow, counter++));
}
} catch (err) {
_didIteratorError5 = true;
_iteratorError5 = err;
} finally {
try {
if (!_iteratorNormalCompletion5 && _iterator5.return) {
_iterator5.return();
}
} finally {
if (_didIteratorError5) {
throw _iteratorError5;
}
}
}
return res;
} else {
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, counter++));
} else {
res = _extends({}, res, ob(subject[i]).flatten(tmpPrefix, shallow, counter++));
}
}
}
}
return res;
},
values: function values() {
var values = [];
keys: function keys() {
var keys = [];
if (type(subject) === 'array') {
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
var _iteratorNormalCompletion6 = true;
var _didIteratorError6 = false;
var _iteratorError6 = undefined;
try {
for (var _iterator2 = subject[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var i = _step2.value;
for (var _iterator6 = subject[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
var i = _step6.value;
values = values.concat(ob(i).values());
keys = keys.concat(ob(i).keys());
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
_didIteratorError6 = true;
_iteratorError6 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
if (!_iteratorNormalCompletion6 && _iterator6.return) {
_iterator6.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
if (_didIteratorError6) {
throw _iteratorError6;
}

@@ -115,7 +283,7 @@ }

for (var k in subject) {
values.push(subject[k]);
keys.push(k);
};
}
return (0, _uniques2.default)(values);
return (0, _uniques2.default)(keys);
},

@@ -145,9 +313,9 @@ many: function many() {

var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
var _iteratorNormalCompletion7 = true;
var _didIteratorError7 = false;
var _iteratorError7 = undefined;
try {
for (var _iterator3 = subject[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var i = _step3.value;
for (var _iterator7 = subject[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
var i = _step7.value;

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

} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
_didIteratorError7 = true;
_iteratorError7 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
if (!_iteratorNormalCompletion7 && _iterator7.return) {
_iterator7.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
if (_didIteratorError7) {
throw _iteratorError7;
}

@@ -174,131 +342,72 @@ }

var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
var flat = ob(subject).flatten();
try {
for (var _iterator4 = keys[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var key = _step4.value;
for (var actualKey in flat) {
var _iteratorNormalCompletion8 = true;
var _didIteratorError8 = false;
var _iteratorError8 = undefined;
if (key.split('.').length > 1) {
var searchKey = key.replace(/^(\w|\di|_|$)*./g, '');
var currentKey = key.replace('.' + searchKey, '');
resp[currentKey] = getNestedObject(subject[currentKey], searchKey);
} else {
resp[key] = subject[key];
}
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
}
for (var _iterator8 = keys[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
var desiredKey = _step8.value;
return resp;
},
deselect: function deselect() {
var keys = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
var allKeys = this.keys(subject);
var keysToKeep = [];
var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
try {
for (var _iterator5 = allKeys[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
var subjectKey = _step5.value;
var keepKey = true;
var _iteratorNormalCompletion6 = true;
var _didIteratorError6 = false;
var _iteratorError6 = undefined;
try {
for (var _iterator6 = keys[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
var keyToRemove = _step6.value;
if (subjectKey === keyToRemove) {
keepKey = false;
if (actualKey === desiredKey) {
resp[actualKey] = flat[actualKey];
}
}
} catch (err) {
_didIteratorError6 = true;
_iteratorError6 = err;
_didIteratorError8 = true;
_iteratorError8 = err;
} finally {
try {
if (!_iteratorNormalCompletion6 && _iterator6.return) {
_iterator6.return();
if (!_iteratorNormalCompletion8 && _iterator8.return) {
_iterator8.return();
}
} finally {
if (_didIteratorError6) {
throw _iteratorError6;
if (_didIteratorError8) {
throw _iteratorError8;
}
}
}
if (keepKey) {
keysToKeep.push(subjectKey);
}
}
} catch (err) {
_didIteratorError5 = true;
_iteratorError5 = err;
} finally {
try {
if (!_iteratorNormalCompletion5 && _iterator5.return) {
_iterator5.return();
}
} finally {
if (_didIteratorError5) {
throw _iteratorError5;
}
}
resp = ob(resp).expand();
}
return this.select(keysToKeep);
return resp;
},
flatten: function flatten() {
var prefix = arguments.length <= 0 || arguments[0] === undefined ? '' : arguments[0];
var shallow = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
values: function values() {
var values = [];
var res = {};
if (type(subject) === 'array') {
var _iteratorNormalCompletion9 = true;
var _didIteratorError9 = false;
var _iteratorError9 = undefined;
if (type(subject) === 'object' || type(subject) === 'array') {
try {
for (var _iterator9 = subject[Symbol.iterator](), _step9; !(_iteratorNormalCompletion9 = (_step9 = _iterator9.next()).done); _iteratorNormalCompletion9 = true) {
var i = _step9.value;
for (var i in subject) {
var tmpPrefix = undefined;
if (prefix === '') {
tmpPrefix = '' + i;
} else {
tmpPrefix = prefix + '.' + i;
values = values.concat(ob(i).values());
}
if (type(subject[i]) === 'array') {
tmpPrefix = tmpPrefix + '[]';
} catch (err) {
_didIteratorError9 = true;
_iteratorError9 = err;
} finally {
try {
if (!_iteratorNormalCompletion9 && _iterator9.return) {
_iterator9.return();
}
} finally {
if (_didIteratorError9) {
throw _iteratorError9;
}
}
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));
}
}
} else {
for (var k in subject) {
values.push(subject[k]);
};
}
return res;
return (0, _uniques2.default)(values);
}

@@ -305,0 +414,0 @@ };

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

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

@@ -15,18 +15,2 @@ 'use strict';

let getNestedObject = function(ob, key) {
let keys = key.split('.');
let newOb = {};
if(keys.length > 1) {
// Get the key deselect the fist word separated by a period
let newKey = key.replace(/^(\w|\di|_|$)*./g, '');
newOb[keys[0]] = getNestedObject(ob[keys[0]], newKey);
} else {
newOb[key] = ob[key];
}
return newOb;
};
/**

@@ -41,31 +25,123 @@ * Returns an objob object

return {
keys:function() {
let keys = [];
deselect: function(keys = []){
let allKeys = ob(subject).keys();
let keysToKeep = [];
for( let subjectKey of allKeys ) {
let keepKey = true;
for( let keyToRemove of keys ){
if(subjectKey === keyToRemove){
keepKey = false;
}
}
if(keepKey){
keysToKeep.push(subjectKey);
}
}
console.log(keysToKeep);
return this.select(keysToKeep);
},
expand: function(){
let res;
if(type(subject) === 'array') {
res = [];
for(let i of subject){
keys = keys.concat(ob(i).keys());
res = res.concat(ob(i).expand());
}
return res;
} else {
for(let k in subject) {
keys.push(k);
};
// Get the empty object ready for the data
for(let keyChain in subject){
let subkeys = keyChain.split('.');
let tmp = {};
let obj = tmp;
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];
} else {
// If array create the array, else create the object
if(subkey.indexOf('[]') !== -1){
subkey = subkey.replace(/\[\]/g, '');
tmp[subkey] = [];
} else {
tmp[subkey] = {};
}
tmp = tmp[subkey];
count++;
}
}
for (let i in obj) {
if (obj[i] === null || obj[i] === undefined) {
delete obj[i];
}
}
res = {...obj, ...res};
}
}
return res;
},
flatten: function(prefix='', shallow=false, counter = 0){
let res;
return uniques(keys);
if(type(subject) === 'array' && counter === 0) {
res = [];
for(let i of subject){
res = res.concat(ob(i).flatten(prefix, shallow, counter++));
}
return res;
} else {
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, counter++)};
} else {
res = {...res, ...ob(subject[i]).flatten(tmpPrefix, shallow, counter++)};
}
}
}
}
return res;
},
values:() => {
let values = [];
keys:function() {
let keys = [];
if(type(subject) === 'array') {
for(let i of subject){
values = values.concat(ob(i).values());
keys = keys.concat(ob(i).keys());
}
} else {
for(let k in subject) {
values.push(subject[k]);
keys.push(k);
};
}
return uniques(values);
return uniques(keys);
},

@@ -97,11 +173,12 @@ many: (num = 2) => {

for (let key of keys) {
if(key.split('.').length > 1) {
let searchKey = key.replace(/^(\w|\di|_|$)*./g, '');
let currentKey = key.replace('.'+searchKey, '');
resp[currentKey] = getNestedObject(subject[currentKey], searchKey);
} else {
resp[key] = subject[key];
let flat = ob(subject).flatten();
for (let actualKey in flat){
for (let desiredKey of keys){
if(actualKey === desiredKey) {
resp[actualKey] = flat[actualKey];
}
}
}
resp = ob(resp).expand();
}

@@ -111,51 +188,17 @@

},
deselect: function(keys = []){
let allKeys = this.keys(subject);
let keysToKeep = [];
values:() => {
let values = [];
for( let subjectKey of allKeys ) {
let keepKey = true;
for( let keyToRemove of keys ){
if(subjectKey === keyToRemove){
keepKey = false;
}
if(type(subject) === 'array') {
for(let i of subject){
values = values.concat(ob(i).values());
}
if(keepKey){
keysToKeep.push(subjectKey);
}
} else {
for(let k in subject) {
values.push(subject[k]);
};
}
return this.select(keysToKeep);
return uniques(values);
},
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;
},
};

@@ -162,0 +205,0 @@ };

@@ -7,5 +7,3 @@ /* eslint max-nested-callbacks: 0*/

describe('Objob', () => {
let ob1;
let ob2;
let ob3;
let ob1, ob2, ob3, ob4;
let obArr1;

@@ -23,2 +21,3 @@

feet: 5,
age: 100,
weight: 170,

@@ -38,2 +37,14 @@ };

ob4 = {
name: 'Bob',
feet: 5,
body: {
feet: {
toes: [1,2,10],
heels: [{number: 1},{number: 7},{number: 8}],
},
},
eyes: [{location: 'left', color: 'blue'}, {location: 'right', color: 'red'}],
};
obArr1 = [ob1, ob2];

@@ -112,3 +123,3 @@

expect(ob(obArr1).deselect(['name', 'weight', 'feet']))
.to.deep.equal([{age: ob1.age},{age: ob2.age}]);
.to.deep.equal([{age: ob1.age}, {age: ob2.age}]);

@@ -139,2 +150,3 @@ done();

});
expect(ob(ob1).flatten()).to.deep.equal(ob1);
done();

@@ -162,2 +174,12 @@ });

describe('expand', () => {
it('should return the expanded object', (done) => {
expect(ob(ob(ob3).flatten()).expand()).to.deep.equal(ob3);
expect(ob(ob(ob2).flatten()).expand()).to.deep.equal(ob2);
expect(ob(ob(ob4).flatten()).expand()).to.deep.equal(ob4);
expect(ob(ob(obArr1).flatten()).expand()).to.deep.equal(obArr1);
done();
});
});
describe('Chaining', () => {

@@ -164,0 +186,0 @@ it('should return many of an object after filtering select select', (done) => {

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