flavor-js
Version:
FlavorJS the definitive JS natives chainable extensions methods (based on lodash & ES6)
374 lines (334 loc) • 18.5 kB
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Extensions/object.js - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="FlavorJS.html">FlavorJS</a><ul class='methods'><li data-type='method'><a href="FlavorJS.html#delay">delay</a></li><li data-type='method'><a href="FlavorJS.html#extendArray">extendArray</a></li><li data-type='method'><a href="FlavorJS.html#extendBoolean">extendBoolean</a></li><li data-type='method'><a href="FlavorJS.html#extendDate">extendDate</a></li><li data-type='method'><a href="FlavorJS.html#extendFunction">extendFunction</a></li><li data-type='method'><a href="FlavorJS.html#extendLodash">extendLodash</a></li><li data-type='method'><a href="FlavorJS.html#extendNumber">extendNumber</a></li><li data-type='method'><a href="FlavorJS.html#extendObject">extendObject</a></li><li data-type='method'><a href="FlavorJS.html#extendPrototype">extendPrototype</a></li><li data-type='method'><a href="FlavorJS.html#extendPrototypeProperty">extendPrototypeProperty</a></li><li data-type='method'><a href="FlavorJS.html#extendString">extendString</a></li><li data-type='method'><a href="FlavorJS.html#init">init</a></li></ul></li></ul><h3>Namespaces</h3><ul><li><a href="array.html">array</a><ul class='methods'><li data-type='method'><a href="array.html#clone">clone</a></li><li data-type='method'><a href="array.html#concat">concat</a></li><li data-type='method'><a href="array.html#contains">contains</a></li><li data-type='method'><a href="array.html#containsBy">containsBy</a></li><li data-type='method'><a href="array.html#countBy">countBy</a></li><li data-type='method'><a href="array.html#cut">cut</a></li><li data-type='method'><a href="array.html#deepFindBy">deepFindBy</a></li><li data-type='method'><a href="array.html#deepMap">deepMap</a></li><li data-type='method'><a href="array.html#deepSortBy">deepSortBy</a></li><li data-type='method'><a href="array.html#diff">diff</a></li><li data-type='method'><a href="array.html#diffBy">diffBy</a></li><li data-type='method'><a href="array.html#distinct">distinct</a></li><li data-type='method'><a href="array.html#each">each</a></li><li data-type='method'><a href="array.html#filterBy">filterBy</a></li><li data-type='method'><a href="array.html#findBy">findBy</a></li><li data-type='method'><a href="array.html#first">first</a></li><li data-type='method'><a href="array.html#flatten">flatten</a></li><li data-type='method'><a href="array.html#indexBy">indexBy</a></li><li data-type='method'><a href="array.html#intersection">intersection</a></li><li data-type='method'><a href="array.html#isArray">isArray</a></li><li data-type='method'><a href="array.html#last">last</a></li><li data-type='method'><a href="array.html#lorem">lorem</a></li><li data-type='method'><a href="array.html#maxBy">maxBy</a></li><li data-type='method'><a href="array.html#pull">pull</a></li><li data-type='method'><a href="array.html#pullBy">pullBy</a></li><li data-type='method'><a href="array.html#random">random</a></li><li data-type='method'><a href="array.html#removeBy">removeBy</a></li><li data-type='method'><a href="array.html#shuffle">shuffle</a></li><li data-type='method'><a href="array.html#sortBy">sortBy</a></li><li data-type='method'><a href="array.html#split">split</a></li><li data-type='method'><a href="array.html#sum">sum</a></li><li data-type='method'><a href="array.html#tail">tail</a></li><li data-type='method'><a href="array.html#tail">tail</a></li><li data-type='method'><a href="array.html#union">union</a></li></ul></li><li><a href="boolean.html">boolean</a><ul class='methods'><li data-type='method'><a href="boolean.html#isBoolean">isBoolean</a></li><li data-type='method'><a href="boolean.html#random">random</a></li></ul></li><li><a href="date.html">date</a><ul class='methods'><li data-type='method'><a href="date.html#isDate">isDate</a></li><li data-type='method'><a href="date.html#random">random</a></li><li data-type='method'><a href="date.html#toTimestamp">toTimestamp</a></li></ul></li><li><a href="function.html">function</a><ul class='methods'><li data-type='method'><a href="function.html#isFunction">isFunction</a></li><li data-type='method'><a href="function.html#proxy">proxy</a></li></ul></li><li><a href="lodash.html">lodash</a><ul class='methods'><li data-type='method'><a href="lodash.html#deepFindBy">deepFindBy</a></li><li data-type='method'><a href="lodash.html#deepMap">deepMap</a></li><li data-type='method'><a href="lodash.html#deepOrderBy">deepOrderBy</a></li><li data-type='method'><a href="lodash.html#filterByValues">filterByValues</a></li><li data-type='method'><a href="lodash.html#isPercentage">isPercentage</a></li><li data-type='method'><a href="lodash.html#parsePercentage">parsePercentage</a></li><li data-type='method'><a href="lodash.html#pullAllByComparator">pullAllByComparator</a></li><li data-type='method'><a href="lodash.html#timesRange">timesRange</a></li><li data-type='method'><a href="lodash.html#timesReverse">timesReverse</a></li></ul></li><li><a href="number.html">number</a><ul class='methods'><li data-type='method'><a href="number.html#between">between</a></li><li data-type='method'><a href="number.html#degreeDiff">degreeDiff</a></li><li data-type='method'><a href="number.html#degreeDir">degreeDir</a></li><li data-type='method'><a href="number.html#degreeWrap">degreeWrap</a></li><li data-type='method'><a href="number.html#floor">floor</a></li><li data-type='method'><a href="number.html#fromRoman">fromRoman</a></li><li data-type='method'><a href="number.html#isNumber">isNumber</a></li><li data-type='method'><a href="number.html#parse">parse</a></li><li data-type='method'><a href="number.html#random">random</a></li><li data-type='method'><a href="number.html#range">range</a></li><li data-type='method'><a href="number.html#round">round</a></li><li data-type='method'><a href="number.html#round">round</a></li><li data-type='method'><a href="number.html#times">times</a></li><li data-type='method'><a href="number.html#toAbsolute">toAbsolute</a></li><li data-type='method'><a href="number.html#toCurrency">toCurrency</a></li><li data-type='method'><a href="number.html#toFileSize">toFileSize</a></li><li data-type='method'><a href="number.html#toRoman">toRoman</a></li><li data-type='method'><a href="number.html#toSymbolString">toSymbolString</a></li></ul></li><li><a href="object.html">object</a><ul class='methods'><li data-type='method'><a href="object.html#clone">clone</a></li><li data-type='method'><a href="object.html#each">each</a></li><li data-type='method'><a href="object.html#inherit">inherit</a></li><li data-type='method'><a href="object.html#isObject">isObject</a></li><li data-type='method'><a href="object.html#omit">omit</a></li><li data-type='method'><a href="object.html#path">path</a></li><li data-type='method'><a href="object.html#pick">pick</a></li></ul></li><li><a href="string.html">string</a><ul class='methods'><li data-type='method'><a href="string.html#camelCase">camelCase</a></li><li data-type='method'><a href="string.html#capitalize">capitalize</a></li><li data-type='method'><a href="string.html#contains">contains</a></li><li data-type='method'><a href="string.html#decodeURI">decodeURI</a></li><li data-type='method'><a href="string.html#encodeURI">encodeURI</a></li><li data-type='method'><a href="string.html#endsWith">endsWith</a></li><li data-type='method'><a href="string.html#escapeHTML">escapeHTML</a></li><li data-type='method'><a href="string.html#escapeHTML">escapeHTML</a></li><li data-type='method'><a href="string.html#extractDomain">extractDomain</a></li><li data-type='method'><a href="string.html#extractFileExtension">extractFileExtension</a></li><li data-type='method'><a href="string.html#extractQueryString">extractQueryString</a></li><li data-type='method'><a href="string.html#guid">guid</a></li><li data-type='method'><a href="string.html#isRoman">isRoman</a></li><li data-type='method'><a href="string.html#isString">isString</a></li><li data-type='method'><a href="string.html#isUrl">isUrl</a></li><li data-type='method'><a href="string.html#pad">pad</a></li><li data-type='method'><a href="string.html#padLeft">padLeft</a></li><li data-type='method'><a href="string.html#padRight">padRight</a></li><li data-type='method'><a href="string.html#parsePercentage">parsePercentage</a></li><li data-type='method'><a href="string.html#parsePercentage">parsePercentage</a></li><li data-type='method'><a href="string.html#replaceAll">replaceAll</a></li><li data-type='method'><a href="string.html#slugify">slugify</a></li><li data-type='method'><a href="string.html#startsWith">startsWith</a></li><li data-type='method'><a href="string.html#stripTags">stripTags</a></li><li data-type='method'><a href="string.html#toArray">toArray</a></li><li data-type='method'><a href="string.html#toInt">toInt</a></li></ul></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<div id="main">
<h1 class="page-title">Extensions/object.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import _ from 'lodash';
/**
* @namespace object
* @description the JS native Object class
*/
export default {
native: {
/**
* isObject<br><br>
* @example <caption>eg. usage</caption>
* var o = {
* prop1: 1,
* prop2: 'a',
* };
*
* console.log(Object.isObject(o)); // true
*
* console.log(Object.isObject(2)); // true
*
* console.log(Object.isObject('2')); // true
*
* console.log(Object.isObject(null)); // false
* @memberOf object
* @method isObject
* @instance
* @param {object} o - the object
* @return {boolean}
*/
isObject(o) {
return Object.prototype.isObject.call(o);
},
/**
* deep merges a variable list of objects inside this object instance or a new object (useful to implements defaults/options/settings pattern or set multiple properties at the same time or what you want)<br><br>
* @example <caption>eg. usage</caption>
* var o = {
* prop1: 1,
* prop2: 'a',
* };
*
* o.inherit({
* prop1: 2,
* prop3: new Date(),
* }, {
* prop4: 7.52,
* });
*
* console.log(o); // o = {prop1: 2, prop2: 'a', prop3: Date, prop4: 7.52}
* @example <caption>or</caption>
* var o = {
* prop1: 1,
* prop2: 'a',
* };
*
* var p = o.inherit(true, {
* prop1: 2,
* prop3: new Date(),
* }, {
* prop4: 7.52,
* });
*
* console.log(o); // o = {prop1: 1, prop2: 'a'}
*
* console.log(p); // p = {prop1: 2, prop2: 'a', prop3: Date, prop4: 7.52}
* @memberOf object
* @method inherit
* @instance
* @param {object} o - the object to extend
* @param {boolean} [createNew] - specifies to create a new object to merge
* @param {...object} args - the list of objects to merge
* @return {object}
*/
inherit(o, ...args) {
return Object.prototype.inherit.call(o, ...args);
},
/**
* returns a new object that omits the specified properties<br><br>
* @example <caption>eg. usage</caption>
* var o = {
* prop1: 1,
* prop2: 'a',
* };
*
* o.inherit({
* prop1: 2,
* prop3: new Date(),
* }, {
* prop4: 7.52,
* });
*
* console.log(o); // o = {prop1: 2, prop2: 'a', prop3: Date, prop4: 7.52}
*
* console.log(o.omit('prop1')); // {prop2: 'a', prop3: Date, prop4: 7.52}
*
* console.log(o.omit('prop1', 'prop2')); // {prop3: Date, prop4: 7.52}
*
* console.log(o.omit(['prop1', 'prop2'])); // {prop3: Date, prop4: 7.52}
*
* console.log(o.omit(['prop1'], ['prop2'])); // {prop3: Date, prop4: 7.52}
*
* console.log(o); // o = {prop1: 2, prop2: 'a', prop3: Date, prop4: 7.52}
* @memberOf object
* @method omit
* @instance
* @param {object} o - the object
* @param {...object} args - the list of properties to omit
* @return {object}
*/
omit(o, ...args) {
return Object.prototype.omit.call(o, ...args);
},
/**
* returns a new object that picks only the specified properties<br><br>
* @example <caption>eg. usage</caption>
* var o = {
* prop1: 1,
* prop2: 'a',
* };
*
* o.inherit({
* prop1: 2,
* prop3: new Date(),
* }, {
* prop4: 7.52,
* });
*
* console.log(o); // o = {prop1: 2, prop2: 'a', prop3: Date, prop4: 7.52}
*
* console.log(o.pick('prop1')); // {prop1: 2}
*
* console.log(o.pick('prop1', 'prop2')); // {prop1: 2, prop2: 'a'}
*
* console.log(o.pick(['prop1', 'prop2'])); // {prop1: 2, prop2: 'a'}
*
* console.log(o.pick(['prop1'], ['prop2'])); // {prop1: 2, prop2: 'a'}
*
* console.log(o); // o = {prop1: 2, prop2: 'a', prop3: Date, prop4: 7.52}
* @memberOf object
* @method pick
* @instance
* @param {object} o - the object
* @param {...object} args - the list of properties to omit
* @return {*}
*/
pick(o, ...args) {
return Object.prototype.pick.call(o, ...args);
},
/**
* deeply clones an object in a new object<br><br>
* @example <caption>eg. usage</caption>
* var o = {
* prop1: 1,
* prop2: 'a',
* };
*
* var p = o.clone();
*
* console.log(o == p); // true
*
* console.log(o === p); // false
* @memberOf object
* @method clone
* @instance
* @param {object} o - the object
* @return {object}
*/
clone(o) {
return Object.prototype.clone.call(o);
},
/**
* returns the value at the specified path of the object, with a default value<br><br>
* @example <caption>eg. usage</caption>
* var o = {
* prop1: 1,
* prop2: 'a',
* prop3: {
* prop31: 2.52,
* prop32: 'b',
* },
* prop4: new Date(),
* };
*
* console.log(o.path('prop1')); // 1
*
* console.log(o.path('prop3.prop31')); // 2.52
*
* console.log(o.path('prop3.prop34')); // null
*
* console.log(o.path('prop3.prop34', 'c')); // c
* @example <caption>you can also use array paths</caption>
* var o = {
* prop1: 1,
* prop2: 'a',
* prop3: {
* prop31: 2.52,
* prop32: [{
* propO1: 'b',
* }, {
* propO1: 'c',
* }],
* },
* prop4: new Date(),
* };
*
* console.log(o.path('prop3.prop32[0].propO1')); // 'b'
*
* console.log(o.path('prop3.prop32[1]')); // {propO1: 'c'}
*
* console.log(o.path('prop3.prop31[2]')); // null
*
* console.log(o.path('prop3.prop31[2]', {})); // {}
* @memberOf object
* @method path
* @instance
* @param {object} o - the object
* @param {string} path - the path to search inside the object
* @param {object} [def=null] - the default value to return if path is not found
* @return {*}
*/
path(o, path, def = null) {
return Object.prototype.path.call(o, path, def);
},
/**
* executes function for every property in the object<br><br>
* @example <caption>eg. usage</caption>
* var o = {
* prop1: 1,
* prop2: 'a',
* prop3: 'b',
* prop4: new Date(),
* };
*
* o.each(function(value, key) {
* console.log(key, value);
* });
*
* // it logs
* 'prop1', 1
* 'prop2', 'a'
* 'prop3', 'b'
* 'prop4', Date
* @memberOf object
* @method each
* @instance
* @param {object} o - the object
* @param {function} iteratee - the iteratee callback will be invoked with the following parameters<br>
* so your callback has to be something like this<br><br>
* <pre>
* function iteratee(value, key) {}
* </pre>
* {any} iteratee.value - the property value of the object
* {string} iteratee.key - the property key of the object
* {object} to make chainable the method
*/
each(o, iteratee) {
return Object.prototype.each.call(o, iteratee);
},
},
prototype: {
isObject() {
return _.isPlainObject(this);
},
inherit(...args) {
const createNew = _.isBoolean(args[0]) ? args[0] : false;
let newArgs = args;
if (!!createNew) {
newArgs = Array.prototype.slice.call(newArgs, 1);
}
function safeInherit(scope) {
Array.prototype.unshift.call(newArgs, scope);
if (!!createNew) {
Array.prototype.unshift.call(newArgs, {});
}
return _.merge.apply(scope, newArgs);
}
return safeInherit(this);
},
omit(...args) {
return _.omit(this, ...args);
},
pick(...args) {
return _.pick(this, ...args);
},
clone() {
return _.cloneDeep(this);
},
path(path, def = null) {
if (typeof this === 'object' && this instanceof Object) {
return _.get(this, path, def);
}
return def;
},
each(iteratee) {
return _.each(this, iteratee);
},
},
};
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.2</a> on Fri Jun 14 2019 15:20:48 GMT+0200 (CEST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>