Skip to content

Commit 0f29e93

Browse files
feat: add object/common-keys-in
Ref: #8755
1 parent 2637835 commit 0f29e93

12 files changed

Lines changed: 1013 additions & 0 deletions

File tree

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2021 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+
# commonKeysIn
22+
23+
> Return the common own and inherited property names of two or more objects.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var commonKeysIn = require( '@stdlib/object/common-keys-in' );
41+
```
42+
43+
#### commonKeysIn( obj1, obj2\[, obj3\[,...,objN]] )
44+
45+
Returns the common own and inherited property names of two or more objects.
46+
47+
```javascript
48+
var obj = {
49+
'a': 1,
50+
'b': 2,
51+
'c': 3
52+
};
53+
54+
var obj2 = {
55+
'a': 1,
56+
'b': 2
57+
};
58+
59+
var keys = commonKeysIn( obj, obj2 );
60+
// returns [ 'a', 'b' ]
61+
```
62+
63+
</section>
64+
65+
<!-- /.usage -->
66+
67+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
68+
69+
<section class="notes">
70+
71+
</section>
72+
73+
<!-- /.notes -->
74+
75+
<!-- Package usage examples. -->
76+
77+
<section class="examples">
78+
79+
## Examples
80+
81+
<!-- eslint no-undef: "error" -->
82+
83+
```javascript
84+
var commonKeysIn = require( '@stdlib/object/common-keys-in' );
85+
86+
function Foo() {
87+
this.beep = 'boop';
88+
this.a = {
89+
'b': 'c'
90+
};
91+
return this;
92+
}
93+
94+
Foo.prototype.foo = [ 'bar' ];
95+
96+
var obj1 = new Foo();
97+
98+
var obj2 = {
99+
'beep': 'boop',
100+
'foo': 'bar'
101+
};
102+
103+
var keys = commonKeysIn( obj1, obj2 );
104+
// returns [ 'beep', 'foo' ]
105+
```
106+
107+
</section>
108+
109+
<!-- /.examples -->
110+
111+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
112+
113+
<section class="references">
114+
115+
</section>
116+
117+
<!-- /.references -->
118+
119+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="links">
122+
123+
</section>
124+
125+
<!-- /.links -->
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
25+
var isEmptyArray = require( '@stdlib/assert/is-empty-array' );
26+
var fromCodePoint = require( '@stdlib/string/from-code-point' );
27+
var pkg = require( './../package.json' ).name;
28+
var commonKeysIn = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var obj1;
35+
var obj2;
36+
var out;
37+
var i;
38+
39+
obj1 = {
40+
'b': 3.141592653589793,
41+
'c': {
42+
'c1': 'bap',
43+
'c3': {
44+
'c3b': 5,
45+
'c3c': 'bop'
46+
},
47+
'c4': 1337
48+
},
49+
'd': [ 4, 5, 6 ],
50+
'e': true
51+
};
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
obj2 = {
55+
'a': 'beep',
56+
'b': 'baz',
57+
'c': 'boop'
58+
};
59+
obj2[ fromCodePoint( i%126 ) ] = 'bar';
60+
out = commonKeysIn( obj1, obj2 );
61+
if ( typeof out !== 'object' ) {
62+
b.fail( 'should return an object' );
63+
}
64+
}
65+
b.toc();
66+
if ( !isStringArray( out ) && !isEmptyArray( out ) ) {
67+
b.fail( 'should return an array of strings' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
});
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 pow = require( '@stdlib/math/base/special/pow' );
25+
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
26+
var isEmptyArray = require( '@stdlib/assert/is-empty-array' );
27+
var pkg = require( './../package.json' ).name;
28+
var commonKeysIn = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} N - number of arguments
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( N ) {
41+
var args;
42+
var o;
43+
var i;
44+
var j;
45+
46+
args = [];
47+
for ( i = 0; i < N; i++ ) {
48+
o = {};
49+
for ( j = 0; j < 10; j++ ) {
50+
o[ j ] = i * j;
51+
}
52+
args.push( o );
53+
}
54+
return benchmark;
55+
56+
/**
57+
* Benchmark function.
58+
*
59+
* @private
60+
* @param {Benchmark} b - benchmark instance
61+
*/
62+
function benchmark( b ) {
63+
var keys;
64+
var i;
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
keys = commonKeysIn.apply( null, args );
69+
if ( typeof keys !== 'object' ) {
70+
b.fail( 'should return an object' );
71+
}
72+
}
73+
b.toc();
74+
if ( !isStringArray( keys ) && !isEmptyArray( keys ) ) {
75+
b.fail( 'should return an array of strings' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var len;
92+
var min;
93+
var max;
94+
var f;
95+
var i;
96+
97+
min = 1; // 2^min
98+
max = 6; // 2^max
99+
100+
for ( i = min; i <= max; i++ ) {
101+
len = pow( 2, i );
102+
f = createBenchmark( len );
103+
bench( pkg+':num_args='+len+',num_properties=10', f );
104+
}
105+
}
106+
107+
main();

0 commit comments

Comments
 (0)