New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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.5.0 to 2.0.1

.jsdoc.json

7

lib/functions.js

@@ -6,2 +6,9 @@ "use strict";

});
/*
* Returns a shallow version of the shallow object to remove redundancy
* and simplify complex operations.
*
* @param {object} subject the flattened object to perform the operation on.
* @returns {object}
*/
var makeFlattenedShallow = exports.makeFlattenedShallow = function makeFlattenedShallow(subject) {

@@ -8,0 +15,0 @@ var resp = {};

773

lib/objob.js

@@ -24,410 +24,567 @@ 'use strict';

/**
* Returns an objob object
*
* @param {(object|object[])} subject
* @returns {(object|object[])}
*/
var ob = function ob(subject) {
* @namespace
* @version 2.0.0
* */
var ob = {
/**
* Returns an object without the given keys.
* @example <caption>Basic usage.</caption>
* let x = {
* a: 1,
* b: 2,
* c: 3,
* }
*
* ob.deselect(x, ['a','b']);
* // → {c: 3}
* @example <caption>Advanced usage.</caption>
* let x = {
* c: 3,
* d: {e: 4, f: [5,6]},
* g: [7, 8]
* }
*
* ob.deselect(x, ['d.e','d.f[].0','g[].1']);
* // → {c: 3, d: {f:[6]}, g:[7]}
*
* @param {object} subject The object to perform the deselect operation on.
* @param {string[]} keys The keys of the object or nested object that you would like to deselect.
* @returns {object} The object without the deselected keys
*/
deselect: function deselect(subject) {
var keys = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];
return {
deselect: function deselect() {
var keys = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
var allKeys = ob.keys(ob.flatten(subject));
var keysToKeep = [];
var allKeys = ob(ob(subject).flatten()).keys();
var keysToKeep = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
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;
try {
for (var _iterator = allKeys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var subjectKey = _step.value;
var keepKey = true;
var keepKey = true;
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
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 {
for (var _iterator2 = keys[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var keyToRemove = _step2.value;
if (subjectKey === keyToRemove) {
keepKey = false;
}
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
if (keepKey) {
keysToKeep.push(subjectKey);
}
if (keepKey) {
keysToKeep.push(subjectKey);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return ob(subject).select(keysToKeep);
},
expand: function expand() {
var depth = arguments.length <= 0 || arguments[0] === undefined ? 1 : arguments[0];
return ob.select(subject, keysToKeep);
},
/**
* Takes a flattened object and expands it back to a full object.
*
* @example
* let x = {
* 'a.b.c': 1,
* 'a.b.d': [2,3]
* 'a.b.d[].0': 2,
* 'a.b.d[].1': 3',
* }
*
* ob.expand(x)
* // → {a: {b: {c: 1, d: [2,3]}}}
*
* @param {object} subject The object to expand
* @returns {object} The expanded object.
*/
expand: function expand(subject) {
var depth = arguments.length <= 1 || arguments[1] === undefined ? 1 : arguments[1];
var res = undefined;
subject = (0, _functions.makeFlattenedShallow)(subject);
var res = undefined;
subject = (0, _functions.makeFlattenedShallow)(subject);
// 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);
// 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);
rootObjectPresent = rootObjectPresent || !rootArrayPresent;
}
rootObjectPresent = rootObjectPresent || !rootArrayPresent;
}
}
if (rootObjectPresent === false && depth === 1) {
res = [];
for (var key in subject) {
res.push(subject[key]);
}
} else {
var keyChains = ob(subject).keys();
if (rootObjectPresent === false && depth === 1) {
res = [];
for (var key in subject) {
res.push(subject[key]);
}
} else {
var keyChains = ob.keys(subject);
// When the object is just {'example.example': y}
// One key and one value
if (keyChains.length === 1) {
var tmp = {};
var keyChain = keyChains[0]; // something like 'first.another.another'
var value = subject[keyChain];
var count = undefined;
// When the object is just {'example.example': y}
// One key and one value
if (keyChains.length === 1) {
var tmp = {};
var keyChain = keyChains[0]; // something like 'first.another.another'
var value = subject[keyChain];
var count = 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;
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 _iterator3 = keys[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var key = _step3.value;
try {
for (var _iterator3 = keys[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var key = _step3.value;
if (count === keys.length) {
tmp[key] = value;
if (count === keys.length) {
tmp[key] = value;
} else {
var isArray = (0, _stringContains2.default)(key, '[]');
if (isArray) {
key = key.replace('[]', '');
tmp[key] = [];
} else {
var isArray = (0, _stringContains2.default)(key, '[]');
if (isArray) {
key = key.replace('[]', '');
tmp[key] = [];
} else {
tmp[key] = {};
}
tmp[key] = {};
}
tmp = tmp[key];
}
count++;
tmp = tmp[key];
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
count++;
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
if (_didIteratorError3) {
throw _iteratorError3;
}
}
} 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));
}
}
} 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.expand(tmp, ++depth));
}
}
return res;
//return ob(res).removeUndefs();
},
flatten: function flatten() {
var prefix = arguments.length <= 0 || arguments[0] === undefined ? '' : arguments[0];
var depth = arguments.length <= 1 || arguments[1] === undefined ? 1 : arguments[1];
}
return res;
},
/**
* Takes an object and return a flattened representation of that object that has one level of depth. This allows you to do complex operations on your object while it's in a format that's easier to work with.
*
* @example
* let x = {
* a:{
* b:{
* c: 1,
* d: [2,3]
* }
* }
* }
*
* ob.flatten(x)
* // → {
* 'a.b.c': 1,
* 'a.b.d': [2,3]
* 'a.b.d[].0': 2,
* 'a.b.d[].1': 3',
* }
*
* @param {object} subject The object to perform the flattening on
* @returns {object} The flat representation of the object
*/
flatten: function flatten(subject) {
var prefix = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1];
var depth = arguments.length <= 2 || arguments[2] === undefined ? 1 : arguments[2];
var res = undefined;
var res = undefined;
if ((0, _typeOf2.default)(subject) === 'array' && depth === 1) {
res = [];
if ((0, _typeOf2.default)(subject) === 'array' && depth === 1) {
res = [];
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = subject[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var i = _step4.value;
res = res.concat(ob.flatten(i, prefix, ++depth));
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
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, ++depth));
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
return res;
} else {
res = {};
return res;
} else {
res = {};
if ((0, _typeOf2.default)(subject) === 'object' || (0, _typeOf2.default)(subject) === 'array') {
if ((0, _typeOf2.default)(subject) === 'object' || (0, _typeOf2.default)(subject) === 'array') {
for (var i in subject) {
var tmpPrefix = undefined;
if (prefix === '') {
tmpPrefix = '' + i;
} else {
tmpPrefix = prefix + '.' + i;
}
for (var i in subject) {
var tmpPrefix = undefined;
if (prefix === '') {
tmpPrefix = '' + i;
} else {
tmpPrefix = prefix + '.' + i;
}
if ((0, _typeOf2.default)(subject[i]) === 'array') {
tmpPrefix = tmpPrefix + '[]';
}
if ((0, _typeOf2.default)(subject[i]) === 'array') {
tmpPrefix = tmpPrefix + '[]';
}
res[tmpPrefix] = subject[i];
res[tmpPrefix] = subject[i];
res = (0, _deepmerge2.default)(res, ob(subject[i]).flatten(tmpPrefix, ++depth));
}
res = (0, _deepmerge2.default)(res, ob.flatten(subject[i], tmpPrefix, ++depth));
}
}
return res;
},
keys: function keys() {
var keys = [];
}
return res;
},
/**
* Return all keys for an object.
*
* @example
* let x = {
* a: 1,
* b: 2,
* c: 3
* }
*
* ob.keys(x)
* // → ['a','b','c']
*
* @param {object} subject The object whose keys you wish to retrieve.
* @returns {string[]} The keys
*/
keys: function keys(subject) {
var keys = [];
if ((0, _typeOf2.default)(subject) === 'array') {
var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
if ((0, _typeOf2.default)(subject) === 'array') {
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;
keys = keys.concat(ob.keys(i));
}
} catch (err) {
_didIteratorError5 = true;
_iteratorError5 = err;
} finally {
try {
for (var _iterator5 = subject[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
var i = _step5.value;
keys = keys.concat(ob(i).keys());
if (!_iteratorNormalCompletion5 && _iterator5.return) {
_iterator5.return();
}
} catch (err) {
_didIteratorError5 = true;
_iteratorError5 = err;
} finally {
try {
if (!_iteratorNormalCompletion5 && _iterator5.return) {
_iterator5.return();
}
} finally {
if (_didIteratorError5) {
throw _iteratorError5;
}
if (_didIteratorError5) {
throw _iteratorError5;
}
}
} else {
for (var k in subject) {
keys.push(k);
};
}
} else {
for (var k in subject) {
keys.push(k);
};
}
return (0, _uniques2.default)(keys);
},
many: function many() {
var num = arguments.length <= 0 || arguments[0] === undefined ? 2 : arguments[0];
return (0, _uniques2.default)(keys);
},
/**
* Returns many of the object. If an array is passed, it will just return
* the given array.
*
* @example
* let x = {
* a: 1,
* b: 2,
* c: 3
* }
*
* ob.many(x)
* // → [{a:1,b:2,c:3},{a:1,b:2,c:3}]
*
*
* @param {object} subject The object you would like many of
* @param {integer} num the number of desired objects
* @returns {object[]} An array with n copies of your object.
*/
many: function many(subject) {
var num = arguments.length <= 1 || arguments[1] === undefined ? 2 : arguments[1];
var arr = [];
var arr = [];
if ((0, _typeOf2.default)(subject) === 'array') {
return subject;
} else {
for (var i = 0; i < num; i++) {
arr.push(subject);
}
if ((0, _typeOf2.default)(subject) === 'array') {
return subject;
} else {
for (var i = 0; i < num; i++) {
arr.push(subject);
}
}
return arr;
},
removeUndefs: function removeUndefs() {
var res = undefined;
return arr;
},
/**
* Removes all keys with undefined values from an object.
*
* @example
* let x = {
* a: undefined,
* b: {
* c: undefined,
* d: 2,
* },
* e: [undefined, 1, 2]
* }
*
* ob.removeUndefs(x)
* // → {
* b: {
* d: 2,
* },
* e: [1, 2]
* }
*
*
* @param {object} subject The object you would like to remove undefined values from.
* @returns {object} The object without any undefined values
*/
removeUndefs: function removeUndefs(subject) {
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());
}
if ((0, _typeOf2.default)(subject) === 'array') {
res = [];
for (var key in subject) {
if (subject[key] === undefined) {} else {
res.push(ob.removeUndefs(subject[key]));
}
} 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();
}
}
} else if ((0, _typeOf2.default)(subject) === 'object') {
for (var key in subject) {
if (subject[key] === undefined) {
delete subject[key];
} else {
subject[key] = ob.removeUndefs(subject[key]);
}
return subject;
} else {
return subject;
}
return res;
},
select: function select() {
var keys = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
return subject;
} else {
return subject;
}
var resp = undefined;
return res;
},
/**
* Returns an object only with the given keys.
*
* @example <caption>Basic usage.</caption>
* let x = {
* a: 1,
* b: 2,
* c: 3,
* }
*
* ob.select(x, ['a','b']);
* // → {a: 1, b: 2}
*
* @example <caption>Advanced usage.</caption>
* let x = {
* c: 3,
* d: {e: 4, f: [5,6]},
* g: [7, 8]
* }
*
* ob.select(x, ['d.e','d.f[].0','g[].1']);
* // → {d: {e: 4, f: [5]}, g: [8]}
*
* @param {object} subject The object to perform the select operation on
* @param {string[]} keys The keys you would like to select
* @returns {object} The object only with the selected keys.
*/
select: function select(subject) {
var keys = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];
if ((0, _typeOf2.default)(subject) === 'array') {
resp = [];
var resp = undefined;
var _iteratorNormalCompletion6 = true;
var _didIteratorError6 = false;
var _iteratorError6 = undefined;
if ((0, _typeOf2.default)(subject) === 'array') {
resp = [];
var _iteratorNormalCompletion6 = true;
var _didIteratorError6 = false;
var _iteratorError6 = undefined;
try {
for (var _iterator6 = subject[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
var i = _step6.value;
resp = resp.concat(ob.select(i, keys));
}
} catch (err) {
_didIteratorError6 = true;
_iteratorError6 = err;
} finally {
try {
for (var _iterator6 = subject[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
var i = _step6.value;
resp = resp.concat(ob(i).select(keys));
if (!_iteratorNormalCompletion6 && _iterator6.return) {
_iterator6.return();
}
} catch (err) {
_didIteratorError6 = true;
_iteratorError6 = err;
} finally {
try {
if (!_iteratorNormalCompletion6 && _iterator6.return) {
_iterator6.return();
}
} finally {
if (_didIteratorError6) {
throw _iteratorError6;
}
if (_didIteratorError6) {
throw _iteratorError6;
}
}
} else {
resp = {};
}
} else {
resp = {};
var flat = ob(subject).flatten();
var flat = ob.flatten(subject);
for (var actualKey in flat) {
var _iteratorNormalCompletion7 = true;
var _didIteratorError7 = false;
var _iteratorError7 = undefined;
for (var actualKey in flat) {
var _iteratorNormalCompletion7 = true;
var _didIteratorError7 = false;
var _iteratorError7 = undefined;
try {
for (var _iterator7 = keys[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
var desiredKey = _step7.value;
try {
for (var _iterator7 = keys[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
var desiredKey = _step7.value;
if (actualKey === desiredKey) {
resp[actualKey] = flat[actualKey];
}
if (actualKey === desiredKey) {
resp[actualKey] = flat[actualKey];
}
} catch (err) {
_didIteratorError7 = true;
_iteratorError7 = err;
}
} catch (err) {
_didIteratorError7 = true;
_iteratorError7 = err;
} finally {
try {
if (!_iteratorNormalCompletion7 && _iterator7.return) {
_iterator7.return();
}
} finally {
try {
if (!_iteratorNormalCompletion7 && _iterator7.return) {
_iterator7.return();
}
} finally {
if (_didIteratorError7) {
throw _iteratorError7;
}
if (_didIteratorError7) {
throw _iteratorError7;
}
}
}
resp = ob(resp).expand();
}
resp = ob.expand(resp);
}
return resp;
},
shallow: function shallow() {
var x = ob(subject).flatten();
x = (0, _functions.makeFlattenedShallow)(x);
return resp;
},
/**
* Returns all values for a given object.
*
* @example
* let x = {
* a: 1,
* b: 2,
* c: 3
* }
*
* ob.values(x)
* // → [1, 2, 3]
*
* @param {object} subject The object to get the values of
* @returns {any[]}
*/
values: function values(subject) {
var values = [];
return ob(x).expand();
},
values: function values() {
var values = [];
if ((0, _typeOf2.default)(subject) === 'array') {
var _iteratorNormalCompletion8 = true;
var _didIteratorError8 = false;
var _iteratorError8 = undefined;
if ((0, _typeOf2.default)(subject) === 'array') {
var _iteratorNormalCompletion8 = true;
var _didIteratorError8 = false;
var _iteratorError8 = undefined;
try {
for (var _iterator8 = subject[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
var i = _step8.value;
values = values.concat(ob.values(i));
}
} catch (err) {
_didIteratorError8 = true;
_iteratorError8 = err;
} finally {
try {
for (var _iterator8 = subject[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
var i = _step8.value;
values = values.concat(ob(i).values());
if (!_iteratorNormalCompletion8 && _iterator8.return) {
_iterator8.return();
}
} catch (err) {
_didIteratorError8 = true;
_iteratorError8 = err;
} finally {
try {
if (!_iteratorNormalCompletion8 && _iterator8.return) {
_iterator8.return();
}
} finally {
if (_didIteratorError8) {
throw _iteratorError8;
}
if (_didIteratorError8) {
throw _iteratorError8;
}
}
} else {
for (var k in subject) {
values.push(subject[k]);
};
}
} else {
for (var k in subject) {
values.push(subject[k]);
};
}
return (0, _uniques2.default)(values);
}
};
return (0, _uniques2.default)(values);
}
};

@@ -434,0 +591,0 @@

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

@@ -9,3 +9,4 @@ "main": "lib/objob.js",

"build": "./node_modules/babel-cli/bin/babel.js -d lib src/",
"prepublish": "npm run build"
"prepublish": "npm run build",
"generate-docs": "jsdoc --configure .jsdoc.json --verbose"
},

@@ -34,4 +35,5 @@ "keywords": [

"chai": "^3.4.1",
"jsdoc": "^3.4.0",
"mocha": "^2.3.4"
}
}

@@ -1,5 +0,16 @@

## Objob.js
# Ob Job
A tool for controlling and manipulating javascript object fields and output.
A library of functions for advanced object manipulation.
Docs coming soon.
```bash
import ob from 'objob';
```
## Further Reading
* [API Documentation](https://rawgit.com/chiedolabs/objob/master/docs/ob.html)
* [Changelog](./CHANGELOG.md)
* [Contributing](./CONTRIBUTING.md)
## License
MIT

@@ -0,1 +1,8 @@

/*
* Returns a shallow version of the shallow object to remove redundancy
* and simplify complex operations.
*
* @param {object} subject the flattened object to perform the operation on.
* @returns {object}
*/
export let makeFlattenedShallow = (subject) => {

@@ -2,0 +9,0 @@ let resp = {};

@@ -10,233 +10,390 @@ 'use strict';

/**
* Returns an objob object
*
* @param {(object|object[])} subject
* @returns {(object|object[])}
*/
let ob = function (subject) {
* @namespace
* @version 2.0.0
* */
let ob = {
/**
* Returns an object without the given keys.
* @example <caption>Basic usage.</caption>
* let x = {
* a: 1,
* b: 2,
* c: 3,
* }
*
* ob.deselect(x, ['a','b']);
* // → {c: 3}
* @example <caption>Advanced usage.</caption>
* let x = {
* c: 3,
* d: {e: 4, f: [5,6]},
* g: [7, 8]
* }
*
* ob.deselect(x, ['d.e','d.f[].0','g[].1']);
* // → {c: 3, d: {f:[6]}, g:[7]}
*
* @param {object} subject The object to perform the deselect operation on.
* @param {string[]} keys The keys of the object or nested object that you would like to deselect.
* @returns {object} The object without the deselected keys
*/
deselect: function(subject, keys = []){
let allKeys = ob.keys(ob.flatten(subject));
let keysToKeep = [];
return {
deselect: function(keys = []){
let allKeys = ob(ob(subject).flatten()).keys();
let keysToKeep = [];
for( let subjectKey of allKeys ) {
let keepKey = true;
for( let subjectKey of allKeys ) {
let keepKey = true;
for( let keyToRemove of keys ){
if(subjectKey === keyToRemove){
keepKey = false;
}
for( let keyToRemove of keys ){
if(subjectKey === keyToRemove){
keepKey = false;
}
}
if(keepKey){
keysToKeep.push(subjectKey);
}
if(keepKey){
keysToKeep.push(subjectKey);
}
}
return ob(subject).select(keysToKeep);
},
expand: function(depth = 1){
let res;
subject = makeFlattenedShallow(subject);
return ob.select(subject, keysToKeep);
},
/**
* Takes a flattened object and expands it back to a full object.
*
* @example
* let x = {
* 'a.b.c': 1,
* 'a.b.d': [2,3]
* 'a.b.d[].0': 2,
* 'a.b.d[].1': 3',
* }
*
* ob.expand(x)
* // → {a: {b: {c: 1, d: [2,3]}}}
*
* @param {object} subject The object to expand
* @returns {object} The expanded object.
*/
expand: function(subject, depth = 1){
let res;
subject = makeFlattenedShallow(subject);
// 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);
// 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);
rootObjectPresent = (rootObjectPresent || !rootArrayPresent);
}
rootObjectPresent = (rootObjectPresent || !rootArrayPresent);
}
}
if(rootObjectPresent === false && depth === 1) {
res = [];
for(let key in subject) {
res.push(subject[key]);
}
} else {
let keyChains = ob(subject).keys();
if(rootObjectPresent === false && depth === 1) {
res = [];
for(let key in subject) {
res.push(subject[key]);
}
} else {
let keyChains = ob.keys(subject);
// When the object is just {'example.example': y}
// One key and one value
if(keyChains.length === 1) {
let tmp = {};
let keyChain = keyChains[0]; // something like 'first.another.another'
let value = subject[keyChain];
let count;
// When the object is just {'example.example': y}
// One key and one value
if(keyChains.length === 1) {
let tmp = {};
let keyChain = keyChains[0]; // something like 'first.another.another'
let value = subject[keyChain];
let count;
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;
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 {
let isArray = contains(key, '[]');
if(isArray) {
key = key.replace('[]','');
tmp[key] = [];
} else {
let isArray = contains(key, '[]');
if(isArray) {
key = key.replace('[]','');
tmp[key] = [];
} else {
tmp[key] = {};
}
tmp = tmp[key];
tmp[key] = {};
}
count++;
}
} 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));
tmp = tmp[key];
}
count++;
}
} 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.expand(tmp, ++depth));
}
}
return res;
//return ob(res).removeUndefs();
},
flatten: function(prefix='', depth = 1){
let res;
}
return res;
},
/**
* Takes an object and return a flattened representation of that object that has one level of depth. This allows you to do complex operations on your object while it's in a format that's easier to work with.
*
* @example
* let x = {
* a:{
* b:{
* c: 1,
* d: [2,3]
* }
* }
* }
*
* ob.flatten(x)
* // → {
* 'a.b.c': 1,
* 'a.b.d': [2,3]
* 'a.b.d[].0': 2,
* 'a.b.d[].1': 3',
* }
*
* @param {object} subject The object to perform the flattening on
* @returns {object} The flat representation of the object
*/
flatten: function(subject, prefix='', depth = 1){
let res;
if(type(subject) === 'array' && depth === 1) {
res = [];
if(type(subject) === 'array' && depth === 1) {
res = [];
for(let i of subject){
res = res.concat(ob(i).flatten(prefix, ++depth));
}
for(let i of subject){
res = res.concat(ob.flatten(i, prefix, ++depth));
}
return res;
} else {
res = {};
return res;
} else {
res = {};
if(type(subject) === 'object' || type(subject) === 'array'){
if(type(subject) === 'object' || type(subject) === 'array'){
for(let i in subject) {
let tmpPrefix;
if(prefix === '') {
tmpPrefix = `${i}`;
} else {
tmpPrefix = `${prefix}.${i}`;
}
for(let i in subject) {
let tmpPrefix;
if(prefix === '') {
tmpPrefix = `${i}`;
} else {
tmpPrefix = `${prefix}.${i}`;
}
if(type(subject[i]) === 'array') {
tmpPrefix = tmpPrefix + '[]';
}
if(type(subject[i]) === 'array') {
tmpPrefix = tmpPrefix + '[]';
}
res[tmpPrefix] = subject[i];
res[tmpPrefix] = subject[i];
res = merge(res, ob(subject[i]).flatten(tmpPrefix, ++depth));
}
res = merge(res, ob.flatten(subject[i],tmpPrefix, ++depth));
}
}
return res;
},
keys:function() {
let keys = [];
}
return res;
},
/**
* Return all keys for an object.
*
* @example
* let x = {
* a: 1,
* b: 2,
* c: 3
* }
*
* ob.keys(x)
* // → ['a','b','c']
*
* @param {object} subject The object whose keys you wish to retrieve.
* @returns {string[]} The keys
*/
keys:function(subject) {
let keys = [];
if(type(subject) === 'array') {
for(let i of subject){
keys = keys.concat(ob(i).keys());
}
} else {
for(let k in subject) {
keys.push(k);
};
if(type(subject) === 'array') {
for(let i of subject){
keys = keys.concat(ob.keys(i));
}
} else {
for(let k in subject) {
keys.push(k);
};
}
return uniques(keys);
},
many: (num = 2) => {
let arr = [];
return uniques(keys);
},
/**
* Returns many of the object. If an array is passed, it will just return
* the given array.
*
* @example
* let x = {
* a: 1,
* b: 2,
* c: 3
* }
*
* ob.many(x)
* // → [{a:1,b:2,c:3},{a:1,b:2,c:3}]
*
*
* @param {object} subject The object you would like many of
* @param {integer} num the number of desired objects
* @returns {object[]} An array with n copies of your object.
*/
many: (subject, num = 2) => {
let arr = [];
if(type(subject) === 'array') {
return subject;
} else {
for(let i = 0; i < num; i++){
arr.push(subject);
}
if(type(subject) === 'array') {
return subject;
} else {
for(let i = 0; i < num; i++){
arr.push(subject);
}
}
return arr;
},
removeUndefs: () => {
let res;
return arr;
},
/**
* Removes all keys with undefined values from an object.
*
* @example
* let x = {
* a: undefined,
* b: {
* c: undefined,
* d: 2,
* },
* e: [undefined, 1, 2]
* }
*
* ob.removeUndefs(x)
* // → {
* b: {
* d: 2,
* },
* e: [1, 2]
* }
*
*
* @param {object} subject The object you would like to remove undefined values from.
* @returns {object} The object without any undefined values
*/
removeUndefs: (subject) => {
let res;
if(type(subject) === 'array') {
res = [];
for(let key in subject) {
if(subject[key] === undefined) {
} else {
res.push(ob(subject[key]).removeUndefs());
}
if(type(subject) === 'array') {
res = [];
for(let key in subject) {
if(subject[key] === undefined) {
} else {
res.push(ob.removeUndefs(subject[key]));
}
} else if(type(subject) === 'object') {
for(let key in subject) {
if(subject[key] === undefined) {
delete subject[key];
} else {
subject[key] = 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.removeUndefs(subject[key]);
}
return subject;
} else {
return subject;
}
return res;
},
select: (keys = []) => {
let resp;
return subject;
} else {
return subject;
}
if(type(subject) === 'array') {
resp = [];
return res;
},
/**
* Returns an object only with the given keys.
*
* @example <caption>Basic usage.</caption>
* let x = {
* a: 1,
* b: 2,
* c: 3,
* }
*
* ob.select(x, ['a','b']);
* // → {a: 1, b: 2}
*
* @example <caption>Advanced usage.</caption>
* let x = {
* c: 3,
* d: {e: 4, f: [5,6]},
* g: [7, 8]
* }
*
* ob.select(x, ['d.e','d.f[].0','g[].1']);
* // → {d: {e: 4, f: [5]}, g: [8]}
*
* @param {object} subject The object to perform the select operation on
* @param {string[]} keys The keys you would like to select
* @returns {object} The object only with the selected keys.
*/
select: (subject, keys = []) => {
let resp;
for(let i of subject){
resp = resp.concat(ob(i).select(keys));
}
} else {
resp = {};
if(type(subject) === 'array') {
resp = [];
let flat = ob(subject).flatten();
for(let i of subject){
resp = resp.concat(ob.select(i, keys));
}
} else {
resp = {};
for (let actualKey in flat){
for (let desiredKey of keys){
if(actualKey === desiredKey) {
resp[actualKey] = flat[actualKey];
}
let flat = ob.flatten(subject);
for (let actualKey in flat){
for (let desiredKey of keys){
if(actualKey === desiredKey) {
resp[actualKey] = flat[actualKey];
}
}
resp = ob(resp).expand();
}
resp = ob.expand(resp);
}
return resp;
},
shallow: () => {
let x = ob(subject).flatten();
x = makeFlattenedShallow(x);
return resp;
},
/**
* Returns all values for a given object.
*
* @example
* let x = {
* a: 1,
* b: 2,
* c: 3
* }
*
* ob.values(x)
* // → [1, 2, 3]
*
* @param {object} subject The object to get the values of
* @returns {any[]}
*/
values:(subject) => {
let values = [];
return ob(x).expand();
},
values:() => {
let values = [];
if(type(subject) === 'array') {
for(let i of subject){
values = values.concat(ob(i).values());
}
} else {
for(let k in subject) {
values.push(subject[k]);
};
if(type(subject) === 'array') {
for(let i of subject){
values = values.concat(ob.values(i));
}
} else {
for(let k in subject) {
values.push(subject[k]);
};
}
return uniques(values);
},
};
return uniques(values);
},
};

@@ -243,0 +400,0 @@

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

it('should return all keys for an object', (done) => {
expect(ob(ob1).keys()).to.include.members(['name','age', 'weight']);
expect(ob.keys(ob1)).to.include.members(['name','age', 'weight']);
done();
});
it('should return all keys for an array', (done) => {
expect(ob(obArr1).keys()).to.include.members(['name','age', 'weight','feet']);
expect(ob.keys(obArr1)).to.include.members(['name','age', 'weight','feet']);
done();

@@ -66,7 +66,7 @@ });

it('should return all values for an object', (done) => {
expect(ob(ob1).values()).to.include.members([ob1.name, ob1.age, ob1.weight]);
expect(ob.values(ob1)).to.include.members([ob1.name, ob1.age, ob1.weight]);
done();
});
it('should return all values for an array', (done) => {
expect(ob(obArr1).values()).to.include.members([ob1.name, ob1.age, ob1.weight, ob2.feet]);
expect(ob.values(obArr1)).to.include.members([ob1.name, ob1.age, ob1.weight, ob2.feet]);
done();

@@ -78,4 +78,4 @@ });

it('should return the correct number of objects', (done) => {
expect(ob(ob1).many()).to.deep.equal([ob1,ob1]);
expect(ob(ob1).many(5)).to.deep.equal([ob1,ob1,ob1,ob1,ob1]);
expect(ob.many(ob1)).to.deep.equal([ob1,ob1]);
expect(ob.many(ob1, 5)).to.deep.equal([ob1,ob1,ob1,ob1,ob1]);
done();

@@ -85,3 +85,3 @@ });

it('should return the array if an array', (done) => {
expect(ob(obArr1).many()).to.equal(obArr1);
expect(ob.many(obArr1)).to.equal(obArr1);
done();

@@ -93,5 +93,5 @@ });

it('should return the object only select the given keys', (done) => {
expect(ob(ob1).select(['name'])).to.deep.equal({name: ob1.name});
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({
expect(ob.select(ob1, ['name'])).to.deep.equal({name: ob1.name});
expect(ob.select(ob2,['name', 'age'])).to.deep.equal({name: ob2.name, age: ob2.age});
expect(ob.select(ob3, ['eyes[].0.location'])).to.deep.equal({
eyes: [{location: 'left'}],

@@ -103,3 +103,3 @@ });

it('should return the object only select the given keys using nested object', (done) => {
expect(ob(ob3).select(['body.feet'])).to.deep.equal({body: {feet: ob3.body.feet}});
expect(ob.select(ob3, ['body.feet'])).to.deep.equal({body: {feet: ob3.body.feet}});
done();

@@ -109,3 +109,3 @@ });

it('should return the array only select the given keys using nested object', (done) => {
expect(ob([ob3, ob3]).select(['body.feet']))
expect(ob.select([ob3, ob3], ['body.feet']))
.to.deep.equal([{body: {feet: ob3.body.feet}}, {body: {feet: ob3.body.feet}}]);

@@ -116,3 +116,3 @@ done();

it('should return an array of objects only select the given keys', (done) => {
expect(ob(obArr1).select(['name'])).to.deep.equal([{name: ob1.name},{name: ob2.name}]);
expect(ob.select(obArr1, ['name'])).to.deep.equal([{name: ob1.name},{name: ob2.name}]);
done();

@@ -124,7 +124,7 @@ });

it('should return the object only deselect the given keys', (done) => {
expect(ob(ob1).deselect(['name'])).to.deep.equal({age: ob1.age, weight: ob1.weight});
expect(ob(ob2).deselect(['name', 'age'])).to.deep.equal({weight: ob2.weight, feet: ob2.feet});
expect(ob.deselect(ob1, ['name'])).to.deep.equal({age: ob1.age, weight: ob1.weight});
expect(ob.deselect(ob2, ['name', 'age'])).to.deep.equal({weight: ob2.weight, feet: ob2.feet});
done();
expect(ob(ob3).deselect(['eyes[].0.location'])).to.deep.equal({
expect(ob.deselect(ob3, ['eyes[].0.location'])).to.deep.equal({
name: ob3.name,

@@ -142,3 +142,3 @@ feet: ob3.feet,

it('should return an array of objects only deselect the given keys', (done) => {
expect(ob(obArr1).deselect(['name', 'weight', 'feet']))
expect(ob.deselect(obArr1, ['name', 'weight', 'feet']))
.to.deep.equal([{age: ob1.age}, {age: ob2.age}]);

@@ -152,3 +152,3 @@

it('should return the flattened object', (done) => {
expect(ob(ob3).flatten()).to.deep.equal({
expect(ob.flatten(ob3)).to.deep.equal({
name: 'Bob',

@@ -171,3 +171,3 @@ feet: 5,

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

@@ -179,6 +179,6 @@ });

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);
expect(ob.expand(ob.flatten(ob3))).to.deep.equal(ob3);
expect(ob.expand(ob.flatten(ob2))).to.deep.equal(ob2);
expect(ob.expand(ob.flatten(ob4))).to.deep.equal(ob4);
expect(ob.expand(ob.flatten(obArr1))).to.deep.equal(obArr1);
done();

@@ -190,3 +190,3 @@ });

it('should return many of an object after filtering select select', (done) => {
expect(ob(ob(ob1).select(['name', 'age'])).many(3))
expect(ob.many(ob.select(ob1, ['name', 'age']),3))
.to.deep.equal([

@@ -193,0 +193,0 @@ {age: ob1.age, name: ob1.name},

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