Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Shim Object.assign? #54

Closed
kitsonk opened this issue Jul 16, 2015 · 5 comments
Closed

Shim Object.assign? #54

kitsonk opened this issue Jul 16, 2015 · 5 comments
Milestone

Comments

@kitsonk
Copy link
Member

kitsonk commented Jul 16, 2015

Instead of creating the core/lang::assign as it is now, wouldn't it be better to shim so it offloads to native assign (and keeps it functionally aligned?)... something like:

interface ObjectConstructor {
    assign(target: any, ...sources: any[]): any;
}

/* Adapted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill */
let assign = Object.assign ? Object.assign : function(target: any, ...sources: any[]): any {
    'use strict';
    if (typeof target === 'undefined' || target === null) {
        throw new TypeError('Cannot convert first argument to object');
    }

    const to = Object(target);
    for (let i = 0; i < sources.length; i++) {
        let nextSource = sources[i];
        if (typeof nextSource === 'undefined' || nextSource === null) {
            continue;
        }
        nextSource = Object(nextSource);

        const keysArray = Object.keys(Object(nextSource));
        for (let nextIndex = 0; nextIndex < keysArray.length; nextIndex++) {
            const nextKey = keysArray[nextIndex];
            const desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
            if (typeof desc !== 'undefined' && desc.enumerable) {
                to[nextKey] = nextSource[nextKey];
            }
        }
    }
    return to;
};
@kitsonk
Copy link
Member Author

kitsonk commented Jul 16, 2015

I guess it should use some sort of has() flag sort of thing too, so when we settle on how has build optimisation will work...

@bryanforbes
Copy link
Member

We'd need to update the internal _mixin function to behave more like Object.assign, but I don't see a problem doing that. Then lang.assign, lang.deepAssign, lang.mixin, and lang.deepMixin would all be able to accept primitive types without throwing. What do you think, @kfranqueiro?

@bryanforbes
Copy link
Member

I just spoke with @mwistrand and he told me that the internal _mixin function already behaves like Object.assign, so this might not be as hard as we first thought.

@mwistrand
Copy link

Updating lang.assign to delegate to Object.assign when available will be easy. Doing the same for lang.deepAssign might require a bit more effort.

EDIT: On second thought, since lang.deepAssign has to check the types on each source item, delegating to Object.assign doesn't even make sense.

mwistrand pushed a commit to mwistrand/core that referenced this issue Jul 18, 2015
* `lang.assign` delegates to `Object.assign` when avaialble.
* Implement `has('object-assign')` test.
@kitsonk
Copy link
Member Author

kitsonk commented Jul 27, 2015

EDIT: On second thought, since lang.deepAssign has to check the types on each source item, delegating to Object.assign doesn't even make sense.

Agreed, there doesn't seen to be an easy way to offload onto the native implementation.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants