define-data-property
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -8,2 +8,9 @@ # Changelog | ||
## [v1.1.0](https://github.com/ljharb/define-data-property/compare/v1.0.1...v1.1.0) - 2023-09-13 | ||
### Commits | ||
- [New] add `loose` arg [`155235a`](https://github.com/ljharb/define-data-property/commit/155235a4c4d7741f6de01cd87c99599a56654b72) | ||
- [New] allow `null` to be passed for the non* args [`7d2fa5f`](https://github.com/ljharb/define-data-property/commit/7d2fa5f06be0392736c13b126f7cd38979f34792) | ||
## [v1.0.1](https://github.com/ljharb/define-data-property/compare/v1.0.0...v1.0.1) - 2023-09-12 | ||
@@ -10,0 +17,0 @@ |
@@ -1,3 +0,3 @@ | ||
declare const _exports: (obj: Record<PropertyKey, unknown>, property: PropertyKey, value: unknown, nonEnumerable?: boolean, nonWritable?: boolean, nonConfigurable?: boolean) => void; | ||
declare const _exports: (obj: Record<PropertyKey, unknown>, property: PropertyKey, value: unknown, nonEnumerable?: boolean | null, nonWritable?: boolean | null, nonConfigurable?: boolean | null, loose?: boolean) => void; | ||
export = _exports; | ||
//# sourceMappingURL=index.d.ts.map |
20
index.js
@@ -14,3 +14,3 @@ 'use strict'; | ||
/** @type {(obj: Record<PropertyKey, unknown>, property: PropertyKey, value: unknown, nonEnumerable?: boolean, nonWritable?: boolean, nonConfigurable?: boolean) => void} */ | ||
/** @type {(obj: Record<PropertyKey, unknown>, property: PropertyKey, value: unknown, nonEnumerable?: boolean | null, nonWritable?: boolean | null, nonConfigurable?: boolean | null, loose?: boolean) => void} */ | ||
module.exports = function defineDataProperty( | ||
@@ -27,11 +27,14 @@ obj, | ||
} | ||
if (arguments.length > 3 && typeof arguments[3] !== 'boolean') { | ||
throw new $TypeError('`nonEnumerable`, if provided, must be a boolean`'); | ||
if (arguments.length > 3 && typeof arguments[3] !== 'boolean' && arguments[3] !== null) { | ||
throw new $TypeError('`nonEnumerable`, if provided, must be a boolean or null'); | ||
} | ||
if (arguments.length > 4 && typeof arguments[4] !== 'boolean') { | ||
throw new $TypeError('`nonWritable`, if provided, must be a boolean`'); | ||
if (arguments.length > 4 && typeof arguments[4] !== 'boolean' && arguments[4] !== null) { | ||
throw new $TypeError('`nonWritable`, if provided, must be a boolean or null'); | ||
} | ||
if (arguments.length > 5 && typeof arguments[5] !== 'boolean') { | ||
throw new $TypeError('`nonConfigurable`, if provided, must be a boolean`'); | ||
if (arguments.length > 5 && typeof arguments[5] !== 'boolean' && arguments[5] !== null) { | ||
throw new $TypeError('`nonConfigurable`, if provided, must be a boolean or null'); | ||
} | ||
if (arguments.length > 6 && typeof arguments[6] !== 'boolean') { | ||
throw new $TypeError('`loose`, if provided, must be a boolean'); | ||
} | ||
@@ -41,2 +44,3 @@ var nonEnumerable = arguments.length > 3 ? arguments[3] : null; | ||
var nonConfigurable = arguments.length > 5 ? arguments[5] : null; | ||
var loose = arguments.length > 6 ? arguments[6] : false; | ||
@@ -53,3 +57,3 @@ /* @type {false | TypedPropertyDescriptor<unknown>} */ | ||
}); | ||
} else if (!nonEnumerable && !nonWritable && !nonConfigurable) { | ||
} else if (loose || (!nonEnumerable && !nonWritable && !nonConfigurable)) { | ||
// must fall back to [[Set]], and was not explicitly asked to make non-enumerable, non-writable, or non-configurable | ||
@@ -56,0 +60,0 @@ obj[property] = value; // eslint-disable-line no-param-reassign |
{ | ||
"name": "define-data-property", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Define a data property on an object. Will fall back to assignment in an engine without descriptors.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -12,2 +12,6 @@ # define-data-property <sup>[![Version Badge][npm-version-svg]][package-url]</sup> | ||
The three `non*` argument can also be passed `null`, which will use the existing state if available. | ||
The `loose` argument will mean that if you attempt to set a non-normal data property, in an environment without descriptor support, it will fall back to normal assignment. | ||
## Usage | ||
@@ -25,5 +29,6 @@ | ||
'value', | ||
true, // nonEnumerable | ||
false, // nonWritable | ||
true // nonConfigurable | ||
true, // nonEnumerable, optional | ||
false, // nonWritable, optional | ||
true, // nonConfigurable, optional | ||
false // loose, optional | ||
); | ||
@@ -30,0 +35,0 @@ |
@@ -34,22 +34,24 @@ 'use strict'; | ||
forEach(v.nonBooleans, function (nonBoolean) { | ||
st['throws']( | ||
if (nonBoolean !== null) { | ||
st['throws']( | ||
// @ts-expect-error | ||
function () { defineDataProperty({}, 'key', 'value', nonBoolean); }, | ||
TypeError, | ||
'throws on non-boolean nonEnumerable: ' + inspect(nonBoolean) | ||
); | ||
function () { defineDataProperty({}, 'key', 'value', nonBoolean); }, | ||
TypeError, | ||
'throws on non-boolean nonEnumerable: ' + inspect(nonBoolean) | ||
); | ||
st['throws']( | ||
st['throws']( | ||
// @ts-expect-error | ||
function () { defineDataProperty({}, 'key', 'value', false, nonBoolean); }, | ||
TypeError, | ||
'throws on non-boolean nonWritable: ' + inspect(nonBoolean) | ||
); | ||
function () { defineDataProperty({}, 'key', 'value', false, nonBoolean); }, | ||
TypeError, | ||
'throws on non-boolean nonWritable: ' + inspect(nonBoolean) | ||
); | ||
st['throws']( | ||
st['throws']( | ||
// @ts-expect-error | ||
function () { defineDataProperty({}, 'key', 'value', false, false, nonBoolean); }, | ||
TypeError, | ||
'throws on non-boolean nonConfigurable: ' + inspect(nonBoolean) | ||
); | ||
function () { defineDataProperty({}, 'key', 'value', false, false, nonBoolean); }, | ||
TypeError, | ||
'throws on non-boolean nonConfigurable: ' + inspect(nonBoolean) | ||
); | ||
} | ||
}); | ||
@@ -83,3 +85,3 @@ | ||
defineDataProperty(obj, 'explicit3', 'new value', false, false); | ||
defineDataProperty(obj, 'explicit3', 'new value', false, false, false); | ||
st.ok(has(obj, 'explicit3'), 'has expected own property (explicit configurable)'); | ||
@@ -91,2 +93,68 @@ st.equal(obj.explicit3, 'new value', 'has new expected value (explicit configurable)'); | ||
t.test('loose mode', function (st) { | ||
var obj = { existing: 'existing property' }; | ||
defineDataProperty(obj, 'added', 'added value 1', true, null, null, true); | ||
st.deepEqual( | ||
getOwnPropertyDescriptors(obj), | ||
{ | ||
existing: { | ||
configurable: true, | ||
enumerable: true, | ||
value: 'existing property', | ||
writable: true | ||
}, | ||
added: { | ||
configurable: true, | ||
enumerable: !hasPropertyDescriptors, | ||
value: 'added value 1', | ||
writable: true | ||
} | ||
}, | ||
'in loose mode, obj still adds property 1' | ||
); | ||
defineDataProperty(obj, 'added', 'added value 2', false, true, null, true); | ||
st.deepEqual( | ||
getOwnPropertyDescriptors(obj), | ||
{ | ||
existing: { | ||
configurable: true, | ||
enumerable: true, | ||
value: 'existing property', | ||
writable: true | ||
}, | ||
added: { | ||
configurable: true, | ||
enumerable: true, | ||
value: 'added value 2', | ||
writable: !hasPropertyDescriptors | ||
} | ||
}, | ||
'in loose mode, obj still adds property 2' | ||
); | ||
defineDataProperty(obj, 'added', 'added value 3', false, false, true, true); | ||
st.deepEqual( | ||
getOwnPropertyDescriptors(obj), | ||
{ | ||
existing: { | ||
configurable: true, | ||
enumerable: true, | ||
value: 'existing property', | ||
writable: true | ||
}, | ||
added: { | ||
configurable: !hasPropertyDescriptors, | ||
enumerable: true, | ||
value: 'added value 3', | ||
writable: true | ||
} | ||
}, | ||
'in loose mode, obj still adds property 3' | ||
); | ||
st.end(); | ||
}); | ||
t.test('non-normal data property, ES3', { skip: hasPropertyDescriptors }, function (st) { | ||
@@ -93,0 +161,0 @@ /** @type {Record<PropertyKey, string>} */ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40200
448
68