all files / lib/src/utils/ generic.js

100% Statements 29/29
100% Branches 16/16
100% Functions 7/7
100% Lines 27/27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 26× 34×   26× 25× 33× 192× 15× 14×   15×   177×       23×           288×         64× 34× 34× 198×   30× 27× 39×                  
export function deepAssign (target) {
	var sources = [], len = arguments.length - 1;
	while ( len-- > 0 ) sources[ len ] = arguments[ len + 1 ];
 
	if (isObject(target)) {
		each(sources, function (source) {
			each(source, function (data, key) {
				if (isObject(data)) {
					if (!target[key] || !isObject(target[key])) {
						target[key] = {}
					}
					deepAssign(target[key], data)
				} else {
					target[key] = data
				}
			})
		})
		return target
	} else {
		throw new TypeError('Expected an object literal.')
	}
}
 
 
export function isObject (object) {
	return object !== null && typeof object === 'object'
		&& (object.constructor === Object || Object.prototype.toString.call(object) === '[object Object]')
}
 
 
export function each (collection, callback) {
	if (isObject(collection)) {
		var keys = Object.keys(collection)
		for (var i = 0; i < keys.length; i++) {
			callback(collection[ keys[i] ], keys[i], collection)
		}
	} else if (Array.isArray(collection)) {
		for (var i$1 = 0; i$1 < collection.length; i$1++) {
			callback(collection[i$1], i$1, collection)
		}
	} else {
		throw new TypeError('Expected either an array or object literal.')
	}
}
 
 
export var nextUniqueId = (function () {
	var uid = 0
	return function () { return uid++; }
})()