UNPKG

ninjs-lodash

Version:
75 lines (64 loc) 2.16 kB
/** * Object */ 'use strict' const _ = require('lodash') exports = module.exports = { "opt": opt, "avoke": avoke, "deepInvoke": deepInvoke, "acquire": acquire, "acopy": acopy, "mcopy": mcopy, "mprops": mprops, "spect": function(val) { return require('util').inspect(val, false, null) } } _.mixin(exports) function opt(obj, key, def) { return _.isPlainObject(obj) && key && _.isString(key) ? (_.isBoolean(def) ? _.get(obj, key, def) : _.get(obj, key) || def) : null } // attempt to invoke obj[key] -> Error Object returned if exception occurs function avoke(obj, key) { return _.attempt(_.invoke, obj, key, _.drop(_.toArray(arguments), 2)) } function deepInvoke(obj, key) { if(!obj || !_.isPlainObject(obj)) return let args = _.drop(_.toArray(args), 2) || [] _.forIn(obj, function(v, k) { if(k === key && _.isFunction(v)) v.apply(obj, args) if(!_.isPlainObject(v)) return deepInvoke(v, key) }) } // attempt to require a file passing args to require // if 2nd arg(err) is a bool and true -> return the error if an error occurs // otherwise return null if error or return module if success function acquire(src, err) { let mod = _.attempt(require, src) return mod ? _.isError(mod) ? _.isBoolean(err) && err ? mod : null : null : mod } // same as _.assign but does not alter first arg object // assign WILL overwrite undefined values (from right to left) function acopy() { let zargs = _.toArray(arguments) zargs.unshift({}) return _.assign.apply(_, zargs) } // same as _.merge but does not alter first arg object // merge WILL NOT overwrite undefined values (from right to left) function mcopy() { let zargs = _.toArray(arguments) zargs.unshift({}) return _.merge.apply(_, zargs) } function mprops(obj, withInfo) { let ret = {} _.forIn(obj, function (v, k) { ret[k] = mcopy( _.get(v, '__super__.constructor.__super__.constructor.__super__'), _.get(v, '__super__.constructor.__super__'), _.get(v, '__super__'), v) _.unset(ret[k], '__super__') }) return withInfo ? info(ret) : ret }