Created
December 17, 2020 02:25
-
-
Save bdombro/62e228a54e840eb415d6c9e4c7aae089 to your computer and use it in GitHub Desktop.
ObjectN.js - Extended Object for Rhino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var ObjectN = { | |
assign: assign, | |
create: create, | |
entries: entries, | |
fromEntries: fromEntries, | |
pick: pick, | |
pickBy: pickBy, | |
omit: omit, | |
omitBy: omitBy, | |
test: test, | |
}; | |
Object.getOwnPropertyNames(Object).forEach(function(name) { | |
ObjectN[name] = Object[name]; | |
}); | |
module.exports = ObjectN; | |
/** | |
* Like ES6 Object.assign | |
* @param {object} target | |
* @param {object} varArgs | |
*/ | |
function assign(target, varArgs) { // .length of function is 2 | |
if (target === null || target === undefined) { | |
throw new TypeError('Cannot convert undefined or null to object'); | |
} | |
var to = Object(target); | |
for (var index = 1; index < arguments.length; index++) { | |
var nextSource = arguments[index]; | |
if (nextSource !== null && nextSource !== undefined) { | |
for (var nextKey in nextSource) { | |
// Avoid bugs when hasOwnProperty is shadowed | |
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) { | |
to[nextKey] = nextSource[nextKey]; | |
} | |
} | |
} | |
} | |
return to; | |
} | |
/** | |
* Like ES6 Object.create | |
* @param {object} proto | |
* @param {object} propertiesObject | |
*/ | |
function create(proto, propertiesObject) { | |
if (typeof proto !== 'object' && typeof proto !== 'function') { | |
throw new TypeError('Object prototype may only be an Object: ' + proto); | |
} else if (proto === null) { | |
throw new Error("This browser's implementation of Object.create is a shim and doesn't support 'null' as the first argument."); | |
} | |
if (typeof propertiesObject != 'undefined') { | |
throw new Error("This browser's implementation of Object.create is a shim and doesn't support a second argument."); | |
} | |
function F() {} | |
F.prototype = proto; | |
return new F(); | |
} | |
/** | |
* Like ES6 Object.entries | |
* | |
* @returns {[string, any][]} An array of tuples where each tuple is a key/value pair. | |
*/ | |
function entries(obj) { | |
var ownProps = Object.keys( obj ), | |
i = ownProps.length, | |
resArray = new Array(i); // preallocate the Array | |
while (i--) | |
resArray[i] = [ownProps[i], obj[ownProps[i]]]; | |
return resArray; | |
} | |
/** | |
* Like ES7? Object.fromEntries. | |
* | |
* @param {[string, any][]} An array of tuples where each tuple is a key/value pair. | |
* | |
* @returns {object} An object built from entries. | |
*/ | |
function fromEntries(entries) { | |
return entries.reduce(function (prev, [key, value]) { | |
prev[key] = value; | |
return prev; | |
}, {}) | |
} | |
/** | |
* Like Lodash Object.pick | |
* Returns a new object with only the specified properties. | |
* | |
* @param {object} from | |
* @param {string[]} keys | |
* | |
* @returns {object} A new object with only the specified properties. | |
*/ | |
function pick(from, keys) { | |
const filteredEntries = entries(from) | |
.filter(function ([key]) { return keys.indexOf(key) !== -1 }); | |
return fromEntries(filteredEntries); | |
} | |
/** | |
* Like Lodash Object.pickBy | |
* Creates an object composed of the object properties evaluator returns truthy for. The evaluator is invoked with two arguments: (value, key). | |
* | |
* @param {object} from | |
* @param {requestCallback} evaluator function to pick, default to isTruthy | |
* | |
* @returns {object} A new object where properties satisfy eval | |
*/ | |
function pickBy(from, evaluator) { | |
evaluator = evaluator ? evaluator : function(v) { return v } | |
const result = {}; | |
Object.keys(from).forEach(function (key) { | |
if (evaluator(from[key])) { | |
result[key] = from[key]; | |
} | |
}) | |
return result; | |
} | |
/** | |
* Like Lodash Object.omit | |
* Returns a new object with the omitted properties specified. | |
* | |
* @param {object} obj The object to omit properties from | |
* @param {string[]} propertiesToOmit The list of properties to omit. | |
* | |
* @returns {object} A new object with the omitted properties. | |
*/ | |
function omit(from, keys) { | |
const filteredEntries = entries(from) | |
.filter(function ([key]) { return keys.indexOf(key) === -1 }); | |
return fromEntries(filteredEntries); | |
} | |
/** | |
* Like Lodash Object.omitBy | |
* Creates an object composed of the object properties evaluator returns falsy for. The evaluator is invoked with two arguments: (value, key). | |
* | |
* @param {object} from | |
* @param {eval} evaluator function to pick, default to isTruthy | |
* | |
* @returns {object} A new object where properties satisfy eval | |
*/ | |
function omitBy(from, evaluator) { | |
evaluator = evaluator ? evaluator : function(v) { return v } | |
const result = {}; | |
Object.keys(from).forEach(function (key) { | |
if (!evaluator(from[key])) { | |
result[key] = from[key]; | |
} | |
}) | |
return result; | |
} | |
// Add breakpoints to test the result | |
function test() { | |
const o = {k: 'hello', n: 'no'} | |
const arr = entries(o) | |
const o2 = fromEntries(arr) | |
const o3 = pick(o, ['n']); | |
const o4 = pickBy(o, function(v) { return v === 'no' }) | |
const o5 = omit(o, ['n']) | |
const o6 = omitBy(o, function(v) { return v === 'no' }) | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment