diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/utila/lib/object.js | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/utila/lib/object.js')
| -rw-r--r-- | node_modules/utila/lib/object.js | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/node_modules/utila/lib/object.js b/node_modules/utila/lib/object.js new file mode 100644 index 00000000..47e2c168 --- /dev/null +++ b/node_modules/utila/lib/object.js @@ -0,0 +1,158 @@ +// Generated by CoffeeScript 1.6.3 +var object, _common, + __hasProp = {}.hasOwnProperty; + +_common = require('./_common'); + +module.exports = object = { + isBareObject: _common.isBareObject.bind(_common), + /* + if object is an instance of a class + */ + + isInstance: function(what) { + return !this.isBareObject(what); + }, + /* + Alias to _common.typeOf + */ + + typeOf: _common.typeOf.bind(_common), + /* + Alias to _common.clone + */ + + clone: _common.clone.bind(_common), + /* + Empties an object of its properties. + */ + + empty: function(o) { + var prop; + for (prop in o) { + if (o.hasOwnProperty(prop)) { + delete o[prop]; + } + } + return o; + }, + /* + Empties an object. Doesn't check for hasOwnProperty, so it's a tiny + bit faster. Use it for plain objects. + */ + + fastEmpty: function(o) { + var property; + for (property in o) { + delete o[property]; + } + return o; + }, + /* + Overrides values fomr `newValues` on `base`, as long as they + already exist in base. + */ + + overrideOnto: function(base, newValues) { + var key, newVal, oldVal; + if (!this.isBareObject(newValues) || !this.isBareObject(base)) { + return base; + } + for (key in base) { + oldVal = base[key]; + newVal = newValues[key]; + if (newVal === void 0) { + continue; + } + if (typeof newVal !== 'object' || this.isInstance(newVal)) { + base[key] = this.clone(newVal); + } else { + if (typeof oldVal !== 'object' || this.isInstance(oldVal)) { + base[key] = this.clone(newVal); + } else { + this.overrideOnto(oldVal, newVal); + } + } + } + return base; + }, + /* + Takes a clone of 'base' and runs #overrideOnto on it + */ + + override: function(base, newValues) { + return this.overrideOnto(this.clone(base), newValues); + }, + append: function(base, toAppend) { + return this.appendOnto(this.clone(base), toAppend); + }, + appendOnto: function(base, toAppend) { + var key, newVal, oldVal; + if (!this.isBareObject(toAppend) || !this.isBareObject(base)) { + return base; + } + for (key in toAppend) { + if (!__hasProp.call(toAppend, key)) continue; + newVal = toAppend[key]; + if (newVal === void 0) { + continue; + } + if (typeof newVal !== 'object' || this.isInstance(newVal)) { + base[key] = newVal; + } else { + oldVal = base[key]; + if (typeof oldVal !== 'object' || this.isInstance(oldVal)) { + base[key] = this.clone(newVal); + } else { + this.appendOnto(oldVal, newVal); + } + } + } + return base; + }, + groupProps: function(obj, groups) { + var def, defs, grouped, key, name, shouldAdd, val, _i, _len; + grouped = {}; + for (name in groups) { + defs = groups[name]; + grouped[name] = {}; + } + grouped['rest'] = {}; + top: //; + for (key in obj) { + val = obj[key]; + shouldAdd = false; + for (name in groups) { + defs = groups[name]; + if (!Array.isArray(defs)) { + defs = [defs]; + } + for (_i = 0, _len = defs.length; _i < _len; _i++) { + def = defs[_i]; + if (typeof def === 'string') { + if (key === def) { + shouldAdd = true; + } + } else if (def instanceof RegExp) { + if (def.test(key)) { + shouldAdd = true; + } + } else if (def instanceof Function) { + if (def(key)) { + shouldAdd = true; + } + } else { + throw Error('Group definitions must either\ + be strings, regexes, or functions.'); + } + if (shouldAdd) { + grouped[name][key] = val; + continue top; + } + } + } + grouped['rest'][key] = val; + } + return grouped; + } +}; |
