| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- /**
- * Convert an Array-like object to a real Array.
- *
- * @param {Array-like} list
- * @param {Number} [i] - start index
- */
- var slice = [].slice
- exports.toArray = function (list, i) {
- return slice.call(list, i || 0)
- }
- /**
- * Mix properties into target object.
- *
- * @param {Object} to
- * @param {Object} from
- */
- exports.mixin = function (to, from) {
- for (var key in from) {
- if (to[key] !== from[key]) {
- to[key] = from[key]
- }
- }
- }
- /**
- * Mixin including non-enumerables, and copy property descriptors.
- *
- * @param {Object} to
- * @param {Object} from
- */
- exports.deepMixin = function (to, from) {
- Object.getOwnPropertyNames(from).forEach(function (key) {
- var descriptor = Object.getOwnPropertyDescriptor(from, key)
- Object.defineProperty(to, key, descriptor)
- })
- }
- /**
- * Proxy a property on one object to another.
- *
- * @param {Object} to
- * @param {Object} from
- * @param {String} key
- */
- exports.proxy = function (to, from, key) {
- if (to.hasOwnProperty(key)) return
- Object.defineProperty(to, key, {
- enumerable: true,
- configurable: true,
- get: function () {
- return from[key]
- },
- set: function (val) {
- from[key] = val
- }
- })
- }
- /**
- * Object type check. Only returns true
- * for plain JavaScript objects.
- *
- * @param {*} obj
- * @return {Boolean}
- */
- exports.isObject = function (obj) {
- return Object.prototype.toString.call(obj) === '[object Object]'
- }
- /**
- * Array type check.
- *
- * @param {*} obj
- * @return {Boolean}
- */
- exports.isArray = function (obj) {
- return Array.isArray(obj)
- }
- /**
- * Define a non-enumerable property
- *
- * @param {Object} obj
- * @param {String} key
- * @param {*} val
- * @param {Boolean} [enumerable]
- */
- exports.define = function (obj, key, val, enumerable) {
- Object.defineProperty(obj, key, {
- value : val,
- enumerable : !!enumerable,
- writable : true,
- configurable : true
- })
- }
- /**
- * Augment an target Object or Array by either
- * intercepting the prototype chain using __proto__,
- * or copy over property descriptors
- *
- * @param {Object|Array} target
- * @param {Object} proto
- */
- if ('__proto__' in {}) {
- exports.augment = function (target, proto) {
- target.__proto__ = proto
- }
- } else {
- exports.augment = exports.deepMixin
- }
|