refactor(util): use Object.assign polyfill

This commit is contained in:
Adam Bradley
2016-01-11 11:17:55 -06:00
parent 2f42a55b12
commit b79d6cc8ea
10 changed files with 2520 additions and 2619 deletions

View File

@@ -5,23 +5,10 @@ export function run() {
it('should extend simple', () => {
var obj = { a: '0', c: '0' };
expect( util.extend(obj, { a: '1', b: '2' }) ).toBe(obj);
expect( util.assign(obj, { a: '1', b: '2' }) ).toBe(obj);
expect(obj).toEqual({ a: '1', b: '2', c: '0' });
});
it('should extend complex', () => {
expect(util.extend(
{ a: '0', b: '0' },
{ b: '1', c: '1' },
{ c: '2', d: '2' }
)).toEqual({
a: '0',
b: '1',
c: '2',
d: '2'
});
});
});
describe('defaults', function() {
@@ -29,7 +16,7 @@ export function run() {
it('should simple defaults', () => {
var obj = { a: '1' };
expect(util.defaults(obj, { a: '2', b: '2' })).toBe(obj);
expect(obj).toEqual({
expect(obj).toEqual({
a: '1', b: '2'
});
});

View File

@@ -12,13 +12,26 @@ export function clamp(min, n, max) {
return Math.max(min, Math.min(n, max));
}
// polyfill for Object.assign
var _assign: any;
if (typeof Object.assign !== 'function') {
// use the old-school shallow extend method
_assign = _baseExtend;
} else {
// use the built in ES6 Object.assign method
_assign = Object.assign;
}
/**
* Extend the destination with an arbitrary number of other objects.
* @param dst the destination
* @param ... the param objects
* The assign() method is used to copy the values of all enumerable own
* properties from one or more source objects to a target object. It will
* return the target object. When available, this method will use
* `Object.assign()` under-the-hood.
* @param target The target object
* @param source The source object
*/
export function extend(dst: any, ...args: any[]) {
return _baseExtend(dst, [].slice.call(arguments, 1), false);
export function assign(target: any, source: any): any {
return _assign(target, source);
}
/**
@@ -30,7 +43,7 @@ export function merge(dst: any, ...args: any[]) {
return _baseExtend(dst, [].slice.call(arguments, 1), true);
}
function _baseExtend(dst, objs, deep) {
function _baseExtend(dst, objs, deep = false) {
for (var i = 0, ii = objs.length; i < ii; ++i) {
var obj = objs[i];
if (!obj || !isObject(obj) && !isFunction(obj)) continue;