Bakhtina Sofya b84cb877be 1st comm | 3 тижнів тому | |
---|---|---|
.. | ||
.github | 3 тижнів тому | |
test | 3 тижнів тому | |
.editorconfig | 3 тижнів тому | |
.eslintrc | 3 тижнів тому | |
.nycrc | 3 тижнів тому | |
CHANGELOG.md | 3 тижнів тому | |
LICENSE | 3 тижнів тому | |
README.md | 3 тижнів тому | |
index.js | 3 тижнів тому | |
package.json | 3 тижнів тому |
Returns true if a value has the characteristics of a valid JavaScript data descriptor.
Install with npm:
$ npm install --save is-data-descriptor
var isDataDesc = require('is-data-descriptor');
var assert = require('assert');
true
when the descriptor has valid properties with valid values.
// `value` can be anything
assert.equal(isDataDesc({ value: 'foo' }), true);
assert.equal(isDataDesc({ value: function () {} }), true);
assert.equal(isDataDesc({ value: true }), true);
false
when not an object
assert.equal(isDataDesc('a'), false);
assert.equal(isDataDesc(null), false);
false
when the object has invalid properties
assert.equal(isDataDesc({ value: 'foo', enumerable: 'baz' }), false);
assert.equal(isDataDesc({ value: 'foo', configurable: 'baz' }), false);
assert.equal(isDataDesc({ value: 'foo', get() {} }), false);
assert.equal(isDataDesc({ get() {}, value: 'foo' }), false);
false
when a value is not the correct type
assert.equal(isDataDesc({ value: 'foo', enumerable: 'foo' }), false);
assert.equal(isDataDesc({ value: 'foo', configurable: 'foo' }), false);
assert.equal(isDataDesc({ value: 'foo', writable: 'foo' }), false);
The only valid data descriptor properties are the following:
configurable
(required)enumerable
(required)value
(optional)writable
(optional)To be a valid data descriptor, either value
or writable
must be defined.
Invalid properties
A descriptor may have additional invalid properties (an error will not be thrown).
var foo = {};
Object.defineProperty(foo, 'bar', {
enumerable: true,
whatever: 'blah', // invalid, but doesn't cause an error
get() {
return 'baz';
}
});
assert.equal(foo.bar, 'baz');
You might also be interested in these projects: