Skip to content

Commit

Permalink
Use ignoreNonConfigurable for properties deletion (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored and sindresorhus committed Jun 13, 2019
1 parent ea9e7d3 commit 64ebaf2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const {hasOwnProperty} = Object.prototype;

const copyProperty = (to, from, property, {ignoreNonConfigurable = false}) => {
const copyProperty = (to, from, property, ignoreNonConfigurable) => {
// `Function#length` should reflect the parameters of `to` not `from` since we keep its body.
// `Function#prototype` is non-writable and non-configurable so can never be modified.
if (property === 'length' || property === 'prototype') {
Expand Down Expand Up @@ -41,29 +41,29 @@ const changePrototype = (to, from) => {
};

// If `to` has properties that `from` does not have, remove them
const removeProperty = (to, from, property) => {
const removeProperty = (to, from, property, ignoreNonConfigurable) => {
if (hasOwnProperty.call(from, property)) {
return;
}

const {configurable, writable} = Object.getOwnPropertyDescriptor(to, property);
const {configurable} = Object.getOwnPropertyDescriptor(to, property);

if (configurable) {
delete to[property];
} else if (writable) {
to[property] = undefined;
if (!configurable && ignoreNonConfigurable) {
return;
}

delete to[property];
};

const mimicFn = (to, from, options = {}) => {
const mimicFn = (to, from, {ignoreNonConfigurable = false} = {}) => {
for (const property of Reflect.ownKeys(from)) {
copyProperty(to, from, property, options);
copyProperty(to, from, property, ignoreNonConfigurable);
}

changePrototype(to, from);

for (const property of Reflect.ownKeys(to)) {
removeProperty(to, from, property);
removeProperty(to, from, property, ignoreNonConfigurable);
}

return to;
Expand Down
19 changes: 10 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,30 @@ test('should copy inherited properties', t => {
t.is(wrapper.inheritedProp, foo.inheritedProp);
});

test('should delete extra configurable writable properties', t => {
test('should delete extra configurable properties', t => {
const wrapper = function () {};
wrapper.extra = true;
mimicFn(wrapper, foo);

t.false(hasOwnProperty.call(wrapper, 'extra'));
});

test('should set to undefined extra non-configurable writable properties', t => {
test('should throw on extra non-configurable properties', t => {
const wrapper = function () {};
Object.defineProperty(wrapper, 'extra', {value: true, configurable: false, writable: true});
mimicFn(wrapper, foo);

t.true(hasOwnProperty.call(wrapper, 'extra'));
t.is(wrapper.extra, undefined);
t.throws(() => {
mimicFn(wrapper, foo);
});
});

test('should skip extra non-configurable non-writable properties', t => {
test('should not throw on extra non-configurable properties with ignoreNonConfigurable', t => {
const wrapper = function () {};
Object.defineProperty(wrapper, 'extra', {value: true, configurable: false, writable: false});
mimicFn(wrapper, foo);
Object.defineProperty(wrapper, 'extra', {value: true, configurable: false, writable: true});

t.is(wrapper.extra, true);
t.notThrows(() => {
mimicFn(wrapper, foo, {ignoreNonConfigurable: true});
});
});

test('should not copy prototypes', t => {
Expand Down

0 comments on commit 64ebaf2

Please sign in to comment.