Skip to content

Commit 566a909

Browse files
committed
Add assertion utility to test if an object property has a data descriptor
1 parent 84145fd commit 566a909

File tree

8 files changed

+676
-0
lines changed

8 files changed

+676
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# isDataPropertyIn
22+
23+
> Test if an object's own or inherited property has a data descriptor.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isDataPropertyIn = require( '@stdlib/assert/is-data-property-in' );
31+
```
32+
33+
#### isDataPropertyIn( value, property )
34+
35+
Returns a `boolean` indicating if a `value` has a data `property` (either own or inherited).
36+
37+
<!-- eslint-disable no-restricted-syntax -->
38+
39+
```javascript
40+
var defineProperty = require( '@stdlib/utils/define-property' );
41+
42+
var bool;
43+
var obj;
44+
45+
function Foo() {
46+
this.foo = 'bar';
47+
return this;
48+
}
49+
50+
defineProperty( Foo.prototype, 'beep', {
51+
'configurable': false,
52+
'enumerable': false,
53+
'writable': false,
54+
'value': 'boop'
55+
});
56+
57+
defineProperty( Foo.prototype, 'accessor', {
58+
'configurable': false,
59+
'enumerable': false,
60+
'get': function getter() {
61+
return obj.foo;
62+
},
63+
'set': function setter( v ) {
64+
obj.foo = v;
65+
}
66+
});
67+
68+
obj = new Foo();
69+
70+
bool = isDataPropertyIn( obj, 'foo' );
71+
// returns true
72+
73+
bool = isDataPropertyIn( obj, 'beep' );
74+
// returns true
75+
76+
bool = isDataPropertyIn( obj, 'accessor' );
77+
// returns false
78+
```
79+
80+
</section>
81+
82+
<!-- /.usage -->
83+
84+
<section class="notes">
85+
86+
## Notes
87+
88+
- Value arguments other than `null` or `undefined` are coerced to `objects`.
89+
90+
```javascript
91+
var bool = isDataPropertyIn( 'beep', 'length' );
92+
// returns true
93+
```
94+
95+
- Non-symbol property arguments are coerced to `strings`.
96+
97+
```javascript
98+
var obj = {
99+
'null': 'foo'
100+
};
101+
102+
var bool = isDataPropertyIn( obj, null );
103+
// returns true
104+
```
105+
106+
</section>
107+
108+
<!-- /.notes -->
109+
110+
<section class="examples">
111+
112+
## Examples
113+
114+
<!-- eslint-disable object-curly-newline -->
115+
116+
<!-- eslint no-undef: "error" -->
117+
118+
```javascript
119+
var isDataPropertyIn = require( '@stdlib/assert/is-data-property-in' );
120+
121+
var bool = isDataPropertyIn( [ 'a' ], 'length' );
122+
// returns true
123+
124+
bool = isDataPropertyIn( { 'a': 'b' }, 'a' );
125+
// returns true
126+
127+
bool = isDataPropertyIn( [ 'a' ], 0 );
128+
// returns true
129+
130+
bool = isDataPropertyIn( { 'null': false }, null );
131+
// returns true
132+
133+
bool = isDataPropertyIn( { '[object Object]': false }, {} );
134+
// returns true
135+
136+
bool = isDataPropertyIn( {}, 'toString' );
137+
// returns true
138+
139+
bool = isDataPropertyIn( {}, 'hasOwnProperty' );
140+
// returns true
141+
142+
bool = isDataPropertyIn( null, 'a' );
143+
// returns false
144+
145+
bool = isDataPropertyIn( void 0, 'a' );
146+
// returns false
147+
```
148+
149+
</section>
150+
151+
<!-- /.examples -->
152+
153+
<section class="links">
154+
155+
</section>
156+
157+
<!-- /.links -->
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var isDataPropertyIn = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var bool;
33+
var arr;
34+
var i;
35+
36+
arr = new Array( 100 );
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
bool = isDataPropertyIn( arr, i );
41+
if ( !isBoolean( bool ) ) {
42+
b.fail( 'should return a boolean' );
43+
}
44+
}
45+
b.toc();
46+
if ( !isBoolean( bool ) ) {
47+
b.fail( 'should return a boolean' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( value, property )
3+
Tests if an object's own or inherited property has a data descriptor.
4+
5+
Parameters
6+
----------
7+
value: any
8+
Value to test.
9+
10+
property: any
11+
Property to test.
12+
13+
Returns
14+
-------
15+
bool: boolean
16+
Boolean indicating if an object's own or inherited property has a data
17+
descriptor.
18+
19+
Examples
20+
--------
21+
> var obj = { 'boop': true };
22+
> var desc = {};
23+
> desc.configurable = false;
24+
> desc.enumerable = false;
25+
> desc.get = function getter() { return 'beep'; };
26+
> {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc );
27+
> var bool = {{alias}}( obj, 'boop' )
28+
true
29+
> bool = {{alias}}( obj, 'beep' )
30+
false
31+
32+
See Also
33+
--------
34+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/* eslint-disable object-curly-newline */
20+
21+
'use strict';
22+
23+
var isDataPropertyIn = require( './../lib' );
24+
25+
var bool = isDataPropertyIn( [ 'a' ], 'length' );
26+
console.log( bool );
27+
// => true
28+
29+
bool = isDataPropertyIn( { 'a': 'b' }, 'a' );
30+
console.log( bool );
31+
// => true
32+
33+
bool = isDataPropertyIn( [ 'a' ], 0 );
34+
console.log( bool );
35+
// => true
36+
37+
bool = isDataPropertyIn( { 'null': false }, null );
38+
console.log( bool );
39+
// => true
40+
41+
bool = isDataPropertyIn( { '[object Object]': false }, {} );
42+
console.log( bool );
43+
// => true
44+
45+
bool = isDataPropertyIn( {}, 'toString' );
46+
console.log( bool );
47+
// => true
48+
49+
bool = isDataPropertyIn( {}, 'hasOwnProperty' );
50+
console.log( bool );
51+
// => true
52+
53+
bool = isDataPropertyIn( null, 'a' );
54+
console.log( bool );
55+
// => false
56+
57+
bool = isDataPropertyIn( void 0, 'a' );
58+
console.log( bool );
59+
// => false
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Test whether an object's own or inherited property has a data descriptor.
23+
*
24+
* @module @stdlib/assert/is-data-property-in
25+
*
26+
* @example
27+
* var defineProperty = require( '@stdlib/utils/define-property' );
28+
* var isDataPropertyIn = require( '@stdlib/assert/is-data-property-in' );
29+
*
30+
* var obj = {
31+
* 'boop': true
32+
* };
33+
*
34+
* function getter() {
35+
* return 'beep';
36+
* }
37+
*
38+
* defineProperty( obj, 'beep', {
39+
* 'configurable': false,
40+
* 'enumerable': false,
41+
* 'get': getter
42+
* });
43+
*
44+
* var bool = isDataPropertyIn( obj, 'boop' );
45+
* // returns true
46+
*
47+
* bool = isDataPropertyIn( obj, 'beep' );
48+
* // returns false
49+
*/
50+
51+
// MODULES //
52+
53+
var isDataPropertyIn = require( './main.js' );
54+
55+
56+
// EXPORTS //
57+
58+
module.exports = isDataPropertyIn;

0 commit comments

Comments
 (0)