Skip to content

Commit df65045

Browse files
committed
Add utility to list an object's inherited enumerable properties
1 parent f4f1077 commit df65045

File tree

8 files changed

+1007
-0
lines changed

8 files changed

+1007
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
# inheritedEnumerableProperties
22+
23+
> Return an array of an object's inherited enumerable property names and [symbols][@stdlib/symbol/ctor].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
<!-- eslint-disable id-length -->
30+
31+
```javascript
32+
var inheritedEnumerableProperties = require( '@stdlib/utils/inherited-enumerable-properties' );
33+
```
34+
35+
#### inheritedEnumerableProperties( obj\[, level] )
36+
37+
Returns an `array` of an object's inherited enumerable property names and [symbols][@stdlib/symbol/ctor].
38+
39+
```javascript
40+
var defineProperty = require( '@stdlib/utils/define-property' );
41+
42+
function Foo() {
43+
this.a = 'b';
44+
return this;
45+
}
46+
47+
Foo.prototype.beep = 'boop';
48+
49+
var f = new Foo();
50+
var props = inheritedEnumerableProperties( f );
51+
// returns [ 'beep' ]
52+
```
53+
54+
By default, the function walks an object's entire prototype chain. To limit the inheritance level, provide a `level` argument.
55+
56+
```javascript
57+
var defineProperty = require( '@stdlib/utils/define-property' );
58+
var inherit = require( '@stdlib/utils/inherit' );
59+
60+
function Bar() {
61+
return this;
62+
}
63+
64+
Bar.prototype.boop = 'beep';
65+
66+
function Foo() {
67+
Bar.call( this );
68+
this.a = 'b';
69+
return this;
70+
}
71+
72+
inherit( Foo, Bar );
73+
Foo.prototype.beep = 'boop';
74+
75+
var f = new Foo();
76+
var pros = inheritedEnumerableProperties( f, 1 );
77+
// returns [ 'beep' ]
78+
```
79+
80+
</section>
81+
82+
<!-- /.usage -->
83+
84+
<section class="notes">
85+
86+
## Notes
87+
88+
- Property order is not guaranteed, as `object` property enumeration is not specified according to the [ECMAScript specification][ecma-262-for-in]. In practice, however, most engines use insertion order to sort an `object`'s properties, thus allowing for deterministic extraction.
89+
90+
</section>
91+
92+
<!-- /.notes -->
93+
94+
<section class="examples">
95+
96+
## Examples
97+
98+
<!-- eslint-disable id-length -->
99+
100+
<!-- eslint no-undef: "error" -->
101+
102+
```javascript
103+
var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' );
104+
var Symbol = require( '@stdlib/symbol/ctor' );
105+
var defineProperty = require( '@stdlib/utils/define-property' );
106+
var inheritedEnumerableProperties = require( '@stdlib/utils/inherited-enumerable-properties' );
107+
108+
var hasSymbols = hasSymbolSupport();
109+
var props;
110+
var obj;
111+
112+
function Foo() {
113+
this.beep = 'boop';
114+
this.a = {
115+
'b': 'c'
116+
};
117+
defineProperty( this, 'baz', {
118+
'configurable': false,
119+
'enumerable': false,
120+
'writable': true,
121+
'value': 'qux'
122+
});
123+
if ( hasSymbols ) {
124+
this[ Symbol( 'a' ) ] = 'b';
125+
defineProperty( this, 'beep', {
126+
'configurable': false,
127+
'enumerable': false,
128+
'writable': false,
129+
'value': 'boop'
130+
});
131+
}
132+
return this;
133+
}
134+
135+
Foo.prototype.c = 'd';
136+
defineProperty( Foo.prototype, 'bip', {
137+
'configurable': false,
138+
'enumerable': false,
139+
'writable': false,
140+
'value': 'bop'
141+
});
142+
if ( hasSymbols ) {
143+
Foo.prototype[ Symbol( 'c' ) ] = 'd';
144+
defineProperty( Foo.prototype, Symbol( 'e' ), {
145+
'configurable': false,
146+
'enumerable': false,
147+
'writable': false,
148+
'value': 'f'
149+
});
150+
}
151+
152+
obj = new Foo();
153+
props = inheritedEnumerableProperties( obj );
154+
155+
console.log( props );
156+
// e.g., => [ 'c', ... ]
157+
```
158+
159+
</section>
160+
161+
<!-- /.examples -->
162+
163+
<section class="links">
164+
165+
[ecma-262-for-in]: http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.4
166+
167+
[@stdlib/symbol/ctor]: https://github.com/stdlib-js/stdlib
168+
169+
</section>
170+
171+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 isArray = require( '@stdlib/assert/is-array' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var defineProperty = require( '@stdlib/utils/define-property' );
27+
var pkg = require( './../package.json' ).name;
28+
var inheritedEnumerableProperties = require( './../lib' ); // eslint-disable-line id-length
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var out;
35+
var obj;
36+
var i;
37+
38+
function Foo() {
39+
this.a = 'beep';
40+
this.b = 'boop';
41+
this.c = [ 1, 2, 3 ];
42+
this.d = {};
43+
this.e = null;
44+
this.f = randu();
45+
defineProperty( this, 'g', {
46+
'value': 'bar',
47+
'configurable': true,
48+
'writable': true,
49+
'enumerable': false
50+
});
51+
return this;
52+
}
53+
54+
Foo.prototype.h = [ 'foo' ];
55+
56+
obj = new Foo();
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
obj.f = randu();
61+
out = inheritedEnumerableProperties( obj );
62+
if ( !isArray( out ) ) {
63+
b.fail( 'should return an array' );
64+
}
65+
}
66+
b.toc();
67+
if ( !isArray( out ) ) {
68+
b.fail( 'should return an array' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( value[, level] )
3+
Returns an array of an object's inherited enumerable property names and
4+
symbols.
5+
6+
Property order is not guaranteed, as object property enumeration is not
7+
specified according to the ECMAScript specification. In practice, however,
8+
most engines use insertion order to sort an object's properties, thus
9+
allowing for deterministic extraction.
10+
11+
If provided `null` or `undefined`, the function returns an empty array.
12+
13+
Parameters
14+
----------
15+
value: any
16+
Input value.
17+
18+
level: integer (optional)
19+
Inheritance level.
20+
21+
Returns
22+
-------
23+
props: Array
24+
List of an object's inherited enumerable properties.
25+
26+
Examples
27+
--------
28+
> var props = {{alias}}( {} )
29+
30+
See Also
31+
--------
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' );
22+
var Symbol = require( '@stdlib/symbol/ctor' );
23+
var defineProperty = require( '@stdlib/utils/define-property' );
24+
var inheritedEnumerableProperties = require( './../lib' ); // eslint-disable-line id-length
25+
26+
var hasSymbols = hasSymbolSupport();
27+
var props;
28+
var obj;
29+
30+
function Foo() {
31+
this.beep = 'boop';
32+
this.a = {
33+
'b': 'c'
34+
};
35+
defineProperty( this, 'baz', {
36+
'configurable': false,
37+
'enumerable': false,
38+
'writable': true,
39+
'value': 'qux'
40+
});
41+
if ( hasSymbols ) {
42+
this[ Symbol( 'a' ) ] = 'b';
43+
defineProperty( this, 'beep', {
44+
'configurable': false,
45+
'enumerable': false,
46+
'writable': false,
47+
'value': 'boop'
48+
});
49+
}
50+
return this;
51+
}
52+
53+
Foo.prototype.c = 'd';
54+
defineProperty( Foo.prototype, 'bip', {
55+
'configurable': false,
56+
'enumerable': false,
57+
'writable': false,
58+
'value': 'bop'
59+
});
60+
if ( hasSymbols ) {
61+
Foo.prototype[ Symbol( 'c' ) ] = 'd';
62+
defineProperty( Foo.prototype, Symbol( 'e' ), {
63+
'configurable': false,
64+
'enumerable': false,
65+
'writable': false,
66+
'value': 'f'
67+
});
68+
}
69+
70+
obj = new Foo();
71+
props = inheritedEnumerableProperties( obj );
72+
73+
console.log( props );
74+
// e.g., => [ 'c', ... ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
* Return an array of an object's inherited enumerable property names and symbols.
23+
*
24+
* @module @stdlib/utils/inherited-enumerable-properties
25+
*
26+
* @example
27+
* var inheritedEnumerableProperties = require( '@stdlib/utils/inherited-enumerable-properties' );
28+
*
29+
* var props = inheritedEnumerableProperties( [] );
30+
*/
31+
32+
// MODULES //
33+
34+
var main = require( './main.js' );
35+
36+
37+
// EXPORTS //
38+
39+
module.exports = main;

0 commit comments

Comments
 (0)