diff --git a/index.js b/index.js index 453afb9..a787496 100644 --- a/index.js +++ b/index.js @@ -16,8 +16,14 @@ var hasPropertyDescriptors = require('has-property-descriptors')(); var supportsDescriptors = origDefineProperty && hasPropertyDescriptors; var defineProperty = function (object, name, value, predicate) { - if (name in object && (!isFunction(predicate) || !predicate())) { - return; + if (name in object) { + if (predicate === true) { + if (object[name] === value) { + return; + } + } else if (!isFunction(predicate) || !predicate()) { + return; + } } if (supportsDescriptors) { origDefineProperty(object, name, { diff --git a/test/index.js b/test/index.js index 2fc975d..9e27802 100644 --- a/test/index.js +++ b/test/index.js @@ -75,6 +75,29 @@ test('defineProperties', function (dt) { t.deepEqual(Object.getOwnPropertyDescriptor(obj, 'a'), getDescriptor(2), 'existing property "a" was overridden and is not enumerable'); t.deepEqual(['b', 'c'], keys(obj), 'overridden keys are not enumerable'); + define( + obj, + { + a: 4, + b: 3, + c: 3 + }, + { + a: true, + b: true, + c: true + } + ); + t.deepEqual( + obj, + { c: 3 }, + 'properties overriden when predicate is `true` and ===' + ); + t.deepEqual(Object.getOwnPropertyDescriptor(obj, 'a'), getDescriptor(4), 'existing property "a" was overridden and is not enumerable'); + t.deepEqual(Object.getOwnPropertyDescriptor(obj, 'b'), getDescriptor(3), 'existing property "b" was overridden and is not enumerable'); + t.deepEqual(Object.getOwnPropertyDescriptor(obj, 'd'), getDescriptor(5), 'existing property "d" remained and is not enumerable'); + t.deepEqual(['c'], keys(obj), 'overridden keys are not enumerable'); + t.end(); });