Skip to content

Commit cfab7bf

Browse files
committed
feat: add array/base/pluck
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 6e79eff commit cfab7bf

File tree

14 files changed

+1364
-0
lines changed

14 files changed

+1364
-0
lines changed
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# pluck
22+
23+
> Extract a property value from elements in an input object array and assign results to elements in a new output array.
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 pluck = require( '@stdlib/array/base/pluck' );
41+
```
42+
43+
#### pluck( x, prop )
44+
45+
Extracts a property value from elements in an input object array and assigns results to elements in a new output array.
46+
47+
```javascript
48+
var x = [
49+
{
50+
'a': 1
51+
},
52+
{
53+
'a': 2
54+
},
55+
{
56+
'a': 3
57+
}
58+
];
59+
60+
var y = pluck( x, 'a' );
61+
// returns [ 1, 2, 3 ]
62+
```
63+
64+
The function accepts the following arguments:
65+
66+
- **x**: input array.
67+
- **prop**: property to extract values from.
68+
69+
#### pluck.assign( x, prop, out, stride, offset )
70+
71+
Extracts a property value from elements in an input object array and assigns results to elements in an output array.
72+
73+
```javascript
74+
var x = [
75+
{
76+
'a': 1
77+
},
78+
{
79+
'a': 2
80+
},
81+
{
82+
'a': 3
83+
}
84+
];
85+
86+
var out = [ 0, 0, 0 ];
87+
var y = pluck.assign( x, 'a', out, 1, 0 );
88+
// returns [ 1, 2, 3 ]
89+
```
90+
91+
The function accepts the following arguments:
92+
93+
- **x**: input array.
94+
- **prop**: property to extract values from.
95+
- **out**: output array.
96+
- **stride**: stride length for output array.
97+
- **offset**: starting index for output array.
98+
99+
</section>
100+
101+
<!-- /.usage -->
102+
103+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
104+
105+
<section class="notes">
106+
107+
## Notes
108+
109+
- The function skips `undefined` and `null` array elements.
110+
111+
<!-- eslint-disable object-property-newline, object-curly-newline, object-curly-spacing -->
112+
113+
```javascript
114+
var arr = [
115+
{ 'a': 1, 'b': 2 },
116+
null,
117+
void 0,
118+
{ 'a': 0.5, 'b': 3 }
119+
];
120+
121+
var out = pluck( arr, 'a' );
122+
// returns [ 1, , , 0.5 ]
123+
```
124+
125+
- Extracted values are **not** cloned.
126+
127+
<!-- eslint-disable object-property-newline, object-curly-newline, object-curly-spacing -->
128+
129+
```javascript
130+
var arr = [
131+
{ 'a': { 'b': 2 } },
132+
{ 'a': { 'b': 3 } }
133+
];
134+
135+
var out = pluck( arr, 'a' );
136+
// returns [ { 'b': 2 }, { 'b': 3 } ]
137+
138+
var bool = ( arr[ 0 ].a === out[ 0 ] );
139+
// returns true
140+
```
141+
142+
- The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
143+
144+
145+
</section>
146+
147+
<!-- /.notes -->
148+
149+
<!-- Package usage examples. -->
150+
151+
<section class="examples">
152+
153+
## Examples
154+
155+
<!-- eslint no-undef: "error" -->
156+
157+
```javascript
158+
var randu = require( '@stdlib/random/base/randu' );
159+
var round = require( '@stdlib/math/base/special/round' );
160+
var pluck = require( '@stdlib/array/base/pluck' );
161+
162+
var arr;
163+
var tmp;
164+
var out;
165+
var i;
166+
var j;
167+
168+
// Generate a 100x5 2d-array...
169+
arr = new Array( 100 );
170+
for ( i = 0; i < arr.length; i++ ) {
171+
tmp = new Array( 5 );
172+
for ( j = 0; j < tmp.length; j++ ) {
173+
tmp[ j ] = round( randu()*100.0*(j+1.0) );
174+
}
175+
arr[ i ] = tmp;
176+
}
177+
178+
// Pluck the 3rd column:
179+
out = pluck( arr, 2 );
180+
console.log( out );
181+
```
182+
183+
</section>
184+
185+
<!-- /.examples -->
186+
187+
<!-- 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. -->
188+
189+
<section class="references">
190+
191+
</section>
192+
193+
<!-- /.references -->
194+
195+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
196+
197+
<section class="related">
198+
199+
</section>
200+
201+
<!-- /.related -->
202+
203+
<!-- Section for all links. Do not manually edit this section, as it is automatically populated. -->
204+
205+
<section class="links">
206+
207+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
208+
209+
</section>
210+
211+
<!-- /.links -->
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 round = require( '@stdlib/math/base/special/round' );
27+
var pkg = require( './../package.json' ).name;
28+
var pluck = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+':assign', function benchmark( b ) {
34+
var out;
35+
var tmp;
36+
var x;
37+
var i;
38+
var j;
39+
40+
x = new Array( 100 );
41+
for ( i = 0; i < x.length; i++ ) {
42+
tmp = new Array( 5 );
43+
for ( j = 0; j < tmp.length; j++ ) {
44+
tmp[ j ] = round( randu()*100.0*(j+1.0) );
45+
}
46+
x[ i ] = tmp;
47+
}
48+
out = new Array( x.length );
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
x[ i % x.length ].y = randu();
53+
j = pluck.assign( x, 'y', out, 1, 0 );
54+
if ( typeof j !== 'object' ) {
55+
b.fail( 'should return an array' );
56+
}
57+
}
58+
b.toc();
59+
if ( !isArray( j ) ) {
60+
b.fail( 'should return an array' );
61+
}
62+
b.pass( 'benchmark finished' );
63+
b.end();
64+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 round = require( '@stdlib/math/base/special/round' );
27+
var pkg = require( './../package.json' ).name;
28+
var pluck = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var out;
35+
var tmp;
36+
var x;
37+
var i;
38+
var j;
39+
40+
x = new Array( 100 );
41+
for ( i = 0; i < x.length; i++ ) {
42+
tmp = new Array( 5 );
43+
for ( j = 0; j < tmp.length; j++ ) {
44+
tmp[ j ] = round( randu()*100.0*(j+1.0) );
45+
}
46+
x[ i ] = tmp;
47+
}
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
x[ i % x.length ][ i % 5 ] = randu();
51+
out = pluck( x, 2 );
52+
if ( typeof out !== 'object' ) {
53+
b.fail( 'should return an array' );
54+
}
55+
}
56+
b.toc();
57+
if ( !isArray( out ) ) {
58+
b.fail( 'should return an array' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});

0 commit comments

Comments
 (0)