From cfab7bfa2bebeccf1588e818b7b28cb653e0a8f4 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 22 Feb 2025 22:00:07 +0500 Subject: [PATCH 1/6] 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 --- --- .../@stdlib/array/base/pluck/README.md | 211 ++++++++++++++++++ .../base/pluck/benchmark/benchmark.assign.js | 64 ++++++ .../array/base/pluck/benchmark/benchmark.js | 62 +++++ .../@stdlib/array/base/pluck/docs/repl.txt | 61 +++++ .../array/base/pluck/docs/types/index.d.ts | 185 +++++++++++++++ .../array/base/pluck/docs/types/test.ts | 66 ++++++ .../array/base/pluck/examples/index.js | 43 ++++ .../@stdlib/array/base/pluck/lib/assign.js | 190 ++++++++++++++++ .../@stdlib/array/base/pluck/lib/index.js | 78 +++++++ .../@stdlib/array/base/pluck/lib/main.js | 59 +++++ .../@stdlib/array/base/pluck/package.json | 62 +++++ .../array/base/pluck/test/test.assign.js | 137 ++++++++++++ .../@stdlib/array/base/pluck/test/test.js | 41 ++++ .../array/base/pluck/test/test.main.js | 105 +++++++++ 14 files changed, 1364 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/base/pluck/README.md create mode 100644 lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.assign.js create mode 100644 lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/array/base/pluck/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/array/base/pluck/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/array/base/pluck/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/array/base/pluck/examples/index.js create mode 100644 lib/node_modules/@stdlib/array/base/pluck/lib/assign.js create mode 100644 lib/node_modules/@stdlib/array/base/pluck/lib/index.js create mode 100644 lib/node_modules/@stdlib/array/base/pluck/lib/main.js create mode 100644 lib/node_modules/@stdlib/array/base/pluck/package.json create mode 100644 lib/node_modules/@stdlib/array/base/pluck/test/test.assign.js create mode 100644 lib/node_modules/@stdlib/array/base/pluck/test/test.js create mode 100644 lib/node_modules/@stdlib/array/base/pluck/test/test.main.js diff --git a/lib/node_modules/@stdlib/array/base/pluck/README.md b/lib/node_modules/@stdlib/array/base/pluck/README.md new file mode 100644 index 000000000000..e4a0b25df9a0 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/README.md @@ -0,0 +1,211 @@ + + +# pluck + +> Extract a property value from elements in an input object array and assign results to elements in a new output array. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var pluck = require( '@stdlib/array/base/pluck' ); +``` + +#### pluck( x, prop ) + +Extracts a property value from elements in an input object array and assigns results to elements in a new output array. + +```javascript +var x = [ + { + 'a': 1 + }, + { + 'a': 2 + }, + { + 'a': 3 + } +]; + +var y = pluck( x, 'a' ); +// returns [ 1, 2, 3 ] +``` + +The function accepts the following arguments: + +- **x**: input array. +- **prop**: property to extract values from. + +#### pluck.assign( x, prop, out, stride, offset ) + +Extracts a property value from elements in an input object array and assigns results to elements in an output array. + +```javascript +var x = [ + { + 'a': 1 + }, + { + 'a': 2 + }, + { + 'a': 3 + } +]; + +var out = [ 0, 0, 0 ]; +var y = pluck.assign( x, 'a', out, 1, 0 ); +// returns [ 1, 2, 3 ] +``` + +The function accepts the following arguments: + +- **x**: input array. +- **prop**: property to extract values from. +- **out**: output array. +- **stride**: stride length for output array. +- **offset**: starting index for output array. + +
+ + + + + +
+ +## Notes + +- The function skips `undefined` and `null` array elements. + + + + ```javascript + var arr = [ + { 'a': 1, 'b': 2 }, + null, + void 0, + { 'a': 0.5, 'b': 3 } + ]; + + var out = pluck( arr, 'a' ); + // returns [ 1, , , 0.5 ] + ``` + +- Extracted values are **not** cloned. + + + + ```javascript + var arr = [ + { 'a': { 'b': 2 } }, + { 'a': { 'b': 3 } } + ]; + + var out = pluck( arr, 'a' ); + // returns [ { 'b': 2 }, { 'b': 3 } ] + + var bool = ( arr[ 0 ].a === out[ 0 ] ); + // returns true + ``` + +- 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]). + + +
+ + + + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var pluck = require( '@stdlib/array/base/pluck' ); + +var arr; +var tmp; +var out; +var i; +var j; + +// Generate a 100x5 2d-array... +arr = new Array( 100 ); +for ( i = 0; i < arr.length; i++ ) { + tmp = new Array( 5 ); + for ( j = 0; j < tmp.length; j++ ) { + tmp[ j ] = round( randu()*100.0*(j+1.0) ); + } + arr[ i ] = tmp; +} + +// Pluck the 3rd column: +out = pluck( arr, 2 ); +console.log( out ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..aa5557cf4f96 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.assign.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isArray = require( '@stdlib/assert/is-array' ); +var randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var pkg = require( './../package.json' ).name; +var pluck = require( './../lib' ); + + +// MAIN // + +bench( pkg+':assign', function benchmark( b ) { + var out; + var tmp; + var x; + var i; + var j; + + x = new Array( 100 ); + for ( i = 0; i < x.length; i++ ) { + tmp = new Array( 5 ); + for ( j = 0; j < tmp.length; j++ ) { + tmp[ j ] = round( randu()*100.0*(j+1.0) ); + } + x[ i ] = tmp; + } + out = new Array( x.length ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x[ i % x.length ].y = randu(); + j = pluck.assign( x, 'y', out, 1, 0 ); + if ( typeof j !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( j ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.js b/lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.js new file mode 100644 index 000000000000..70b439da5d4a --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isArray = require( '@stdlib/assert/is-array' ); +var randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var pkg = require( './../package.json' ).name; +var pluck = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var out; + var tmp; + var x; + var i; + var j; + + x = new Array( 100 ); + for ( i = 0; i < x.length; i++ ) { + tmp = new Array( 5 ); + for ( j = 0; j < tmp.length; j++ ) { + tmp[ j ] = round( randu()*100.0*(j+1.0) ); + } + x[ i ] = tmp; + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x[ i % x.length ][ i % 5 ] = randu(); + out = pluck( x, 2 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( out ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/base/pluck/docs/repl.txt b/lib/node_modules/@stdlib/array/base/pluck/docs/repl.txt new file mode 100644 index 000000000000..fa9fc377ffb8 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/docs/repl.txt @@ -0,0 +1,61 @@ + +{{alias}}( x, prop ) + Extracts property values from elements in an input object array and assigns + results to elements in a new output array. + + The function skips `undefined` and `null` array elements. + + Extracted values are not cloned. + + Parameters + ---------- + x: Array + Input array. + prop: string + Property to extract. + + Returns + ------- + out: Array + Output array. + + Examples + -------- + > var x = [ { 'a': 1 }, { 'a': 2 }, { 'a': 3 } ]; + > var y = {{alias}}( x, 'a' ) + [ 1, 2, 3 ] + + +{{alias}}.assign( x, prop, out, stride, offset ) + Extracts property values from elements in an input object array and assigns + results to elements in an output array. + + Parameters + ---------- + x: Array + Input array. + prop: string + Property to extract. + out: Array + Output array. + stride: integer + stride length for output array. + offset: integer + starting index for output array. + + Returns + ------- + out: Array + Output array. + + Examples + -------- + > var x = [ { 'a': 1 }, { 'a': 2 }, { 'a': 3 } ]; + > var y = [ 0, 0, 0 ]; + > var out = {{alias}}.assign( x, 'a', y, 1, 0 ) + [ 1, 2, 3 ] + > var bool = ( y === out ) + true + + See Also + -------- diff --git a/lib/node_modules/@stdlib/array/base/pluck/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/pluck/docs/types/index.d.ts new file mode 100644 index 000000000000..a3fc88dd6912 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/docs/types/index.d.ts @@ -0,0 +1,185 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { PropertyName } from '@stdlib/types/object'; +import { AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Interface describing `cueveryBy`. +*/ +interface Pluck { + /** + * Extracts a property value from elements in an input object array and assigns results to elements in a new output array. + * + * @param x - input array object + * @param prop - property to access + * @returns output array object + * + * @example + * var toAccessorArray = require( '@stdlib/array/to-accessor-array' ); + * + * var x = [ + * { + * 'a': 1, + * 'b': 2 + * }, + * { + * 'a': 0.5, + * 'b': 3 + * } + * ]; + * + * var out = pluck( toAccessorArray( x ), 'a' ); + * // x => [ 1, 0.5 ] + */ + ( x: AccessorArrayLike, prop: PropertyName ): AccessorArrayLike; + + /** + * Extracts a property value from elements in an input object array and assigns results to elements in a new output array. + * + * @param x - input array + * @param prop - property to access + * @returns output array + * + * @example + * var x = [ + * { + * 'a': 1, + * 'b': 2 + * }, + * { + * 'a': 0.5, + * 'b': 3 + * } + * ]; + * + * var out = pluck( x, 'a' ); + * // returns [ 1, 0.5 ] + */ + ( x: Array, prop: PropertyName ): Array; + + /** + * Extracts a property value from elements in an input object array and assigns results to elements in an output array. + * + * @param x - input array object + * @param prop - property to access + * @param out - output array object + * @param stride - stride length for output array + * @param offset - starting index for output array + * @returns output array object + * + * @example + * var toAccessorArray = require( '@stdlib/array/to-accessor-array' ); + * + * var x = [ + * { + * 'a': 1, + * 'b': 2 + * }, + * { + * 'a': 0.5, + * 'b': 3 + * } + * ]; + * var y = [ 0, 0 ] + * + * var out = pluck.assign( toAccessorArray( x ), 'a', toAccessorArray( y ), 1, 0 ); + * // y => [ 1, 0.5 ] + */ + assign( x: AccessorArrayLike, prop: PropertyName, out: AccessorArrayLike, stride: number, offset: number ): AccessorArrayLike; + + /** + * Extracts a property value from elements in an input object array and assigns results to elements in an output array. + * + * @param x - input array + * @param prop - property to access + * @param out - output array + * @param stride - stride length for output array + * @param offset - starting index for output array + * @returns output array + * + * @example + * var x = [ + * { + * 'a': 1, + * 'b': 2 + * }, + * { + * 'a': 0.5, + * 'b': 3 + * } + * ]; + * var y = [ 0, 0 ] + * + * var out = pluck.assign( x, 'a', y, 1, 0 ); + * // y => [ 1, 0.5 ] + */ + assign( x: Array, prop: PropertyName, out: Array, stride: number, offset: number ): Array; +} + +/** +* Extracts a property value from elements in an input object array and assigns results to elements in a new output array. +* +* @param x - input array +* @param prop - property to access +* @returns output array +* +* @example +* var x = [ +* { +* 'a': 1, +* 'b': 2 +* }, +* { +* 'a': 0.5, +* 'b': 3 +* } +* ]; +* +* var result = pluck( x, 'a' ); +* // returns [ 1, 0.5 ] +* +* @example +* var x = [ +* { +* 'a': 1, +* 'b': 2 +* }, +* { +* 'a': 0.5, +* 'b': 3 +* } +* ]; +* var y = [ 0, 0 ] +* +* var out = pluck.assign( x, 'a', y, 1, 0 ); +* // returns [ 1, 0.5 ] +* +* var bool = ( out === y ); +* // returns true +*/ +declare var pluck: Pluck; + + +// EXPORTS // + +export = pluck; diff --git a/lib/node_modules/@stdlib/array/base/pluck/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/pluck/docs/types/test.ts new file mode 100644 index 000000000000..8d4ae62d5b25 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/docs/types/test.ts @@ -0,0 +1,66 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +import pluck = require( './index' ); + + +// TESTS // + +// The function returns an array... +{ + const arr = [ + { 'a': 1, 'b': 2 }, + { 'a': 0.5, 'b': 3 } + ]; + pluck( arr, 'a' ); // $ExpectType any[] + pluck( toAccessorArray( arr ), 'a' ); // $ExpectType AccessorArrayLike +} + +// The compiler throws an error if the function is provided a first argument which is not an array... +{ + pluck( 'abc', 'a' ); // $ExpectError + pluck( 123, 'a' ); // $ExpectError + pluck( null, 'a' ); // $ExpectError + pluck( {}, 'a' ); // $ExpectError + pluck( ( x: number ): number => x, 'a' ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a valid property name... +{ + const arr = [ + { 'a': 1, 'b': 2 }, + { 'a': 0.5, 'b': 3 } + ]; + pluck( arr, true ); // $ExpectError + pluck( arr, false ); // $ExpectError + pluck( arr, [] ); // $ExpectError + pluck( arr, {} ); // $ExpectError + pluck( arr, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + const arr = [ + { 'a': 1, 'b': 2 }, + { 'a': 0.5, 'b': 3 } + ]; + pluck(); // $ExpectError + pluck( arr ); // $ExpectError + pluck( arr, 'a', { 'copy': true }, 2 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/array/base/pluck/examples/index.js b/lib/node_modules/@stdlib/array/base/pluck/examples/index.js new file mode 100644 index 000000000000..b5fae5836b72 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/examples/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var pluck = require( './../lib' ); + +var arr; +var tmp; +var out; +var i; +var j; + +// Generate a 100x5 2d-array... +arr = new Array( 100 ); +for ( i = 0; i < arr.length; i++ ) { + tmp = new Array( 5 ); + for ( j = 0; j < tmp.length; j++ ) { + tmp[ j ] = round( randu()*100.0*(j+1.0) ); + } + arr[ i ] = tmp; +} + +// Pluck the 3rd column: +out = pluck( arr, 2 ); +console.log( out ); diff --git a/lib/node_modules/@stdlib/array/base/pluck/lib/assign.js b/lib/node_modules/@stdlib/array/base/pluck/lib/assign.js new file mode 100644 index 000000000000..9c87b8d50834 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/lib/assign.js @@ -0,0 +1,190 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); + + +// FUNCTIONS // + +/** +* Extracts property values from an input object array and assigns results to elements in an output array. +* +* @private +* @param {Array} x - input array +* @param {string} prop - property to extract +* @param {Array} out - output array +* @param {integer} stride - stride length for output array +* @param {NonNegativeInteger} offset - starting index for output array +* @returns {Array} output array +* +* @example +* var x = [ +* { +* 'a': 1 +* }, +* { +* 'a': 2 +* }, +* { +* 'a': 3 +* } +* ]; +* +* var y = [ 0, 0, 0 ]; +* var out = internal( x, 'a', y, 1, 0 ); +* // returns [ 1, 2, 3 ] +* +* var bool = ( out === y ); +* // returns true +*/ +function internal( x, prop, out, stride, offset ) { + var io; + var i; + var v; + + io = offset; + for ( i = 0; i < x.length; i++ ) { + v = x[ i ]; + if ( + v !== void 0 && + v !== null && + hasOwnProp( v, prop ) + ) { + out[ io ] = v[ prop ]; + } + io += stride; + } + return out; +} + +/** +* Extracts property values from an input object array and assigns results to elements in an output array. +* +* @private +* @param {Object} x - input array object +* @param {string} prop - property to extract +* @param {Object} out - output array object +* @param {integer} stride - stride length for output array +* @param {NonNegativeInteger} offset - starting index for output array +* @returns {Array} output array +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = [ +* { +* 'a': 1 +* }, +* { +* 'a': 2 +* }, +* { +* 'a': 3 +* } +* ]; +* +* var y = [ 0, 0, 0 ]; +* var out = accessors( arraylike2object( x ), 'a', arraylike2object( y ), 1, 0 ); +* // y => [ 1, 2, 3 ] +*/ +function accessors( x, prop, out, stride, offset ) { + var xdata; + var odata; + var xget; + var oset; + var io; + var i; + var v; + + xdata = x.data; + odata = out.data; + + xget = x.accessors[ 0 ]; + oset = out.accessors[ 1 ]; + + io = offset; + for ( i = 0; i < xdata.length; i++ ) { + v = xget( xdata, i ); + if ( + v !== void 0 && + v !== null && + hasOwnProp( v, prop ) + ) { + oset( odata, io, v[ prop ] ); + } + io += stride; + } + return out; +} + + +// MAIN // + +/** +* Extracts property values from an input object array and assigns results to elements in an output array. +* +* @param {Array} x - input array +* @param {string} prop - property to extract +* @param {Array} out - output array +* @param {integer} stride - stride length for output array +* @param {NonNegativeInteger} offset - starting index for output array +* @returns {Array} output array +* +* @example +* var x = [ +* { +* 'a': 1 +* }, +* { +* 'a': 2 +* }, +* { +* 'a': 3 +* } +* ]; +* +* var out = [ 0, 0, 0 ]; +* var y = assign( x, 'a', out, 1, 0 ); +* // returns [ 1, 2, 3 ] +* +* var bool = ( out === y ); +* // returns true +*/ +function assign( x, prop, out, stride, offset ) { + var ox; + var oo; + + ox = arraylike2object( x ); + oo = arraylike2object( out ); + if ( ox.accessorProtocol || oo.accessorProtocol ) { + accessors( ox, prop, oo, stride, offset ); + return out; + } + return internal( x, prop, out, stride, offset ); +} + + +// EXPORTS // + +module.exports = assign; diff --git a/lib/node_modules/@stdlib/array/base/pluck/lib/index.js b/lib/node_modules/@stdlib/array/base/pluck/lib/index.js new file mode 100644 index 000000000000..400025c0c13e --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/lib/index.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Extract property values from an input object array and assign results to elements in a new output array. +* +* @module @stdlib/array/base/pluck +* +* @example +* var pluck = require( '@stdlib/array/base/pluck' ); +* +* var x = [ +* { +* 'a': 1 +* }, +* { +* 'a': 2 +* }, +* { +* 'a': 3 +* } +* ]; +* +* var y = pluck( x, 'a' ); +* // returns [ 1, 2, 3 ] +* +* @example +* var pluck = require( '@stdlib/array/base/pluck' ); +* +* var x = [ +* { +* 'a': 1 +* }, +* { +* 'a': 2 +* }, +* { +* 'a': 3 +* } +* ]; +* +* var out = [ 0, 0, 0 ]; +* var y = pluck.assign( x, 'a', out, 1, 0 ); +* // returns [ 1, 2, 3 ] +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/array/base/pluck/lib/main.js b/lib/node_modules/@stdlib/array/base/pluck/lib/main.js new file mode 100644 index 000000000000..dff8aaf339cb --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/lib/main.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var assign = require( './assign.js' ); + + +// MAIN // + +/** +* Extracts property values from an input object array and assigns results to elements in a new output array. +* +* @param {Array} x - input array +* @param {string} prop - property to extract +* @returns {Array} output array +* +* @example +* var x = [ +* { +* 'a': 1 +* }, +* { +* 'a': 2 +* }, +* { +* 'a': 3 +* } +* ]; +* +* var y = pluck( x, 'a' ); +* // returns [ 1, 2, 3 ] +*/ +function pluck( x, prop ) { + var out = new Array( x.length ); + return assign( x, prop, out, 1, 0 ); +} + + +// EXPORTS // + +module.exports = pluck; diff --git a/lib/node_modules/@stdlib/array/base/pluck/package.json b/lib/node_modules/@stdlib/array/base/pluck/package.json new file mode 100644 index 000000000000..70b585e38838 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/package.json @@ -0,0 +1,62 @@ +{ + "name": "@stdlib/array/base/pluck", + "version": "0.0.0", + "description": "Extract property values from an input object array and assign results to elements in a new output array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdarray", + "array", + "base", + "pluck", + "extract", + "property", + "prop", + "object", + "array-like" + ] +} diff --git a/lib/node_modules/@stdlib/array/base/pluck/test/test.assign.js b/lib/node_modules/@stdlib/array/base/pluck/test/test.assign.js new file mode 100644 index 000000000000..f3baa3085829 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/test/test.assign.js @@ -0,0 +1,137 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var pluck = require( './../lib' ).assign; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof pluck, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function extracts property values from an input object array and assigns results to elements in an output array', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + { + 'a': 1 + }, + { + 'a': 2 + }, + { + 'a': 3 + } + ]; + out = [ 0, 0, 0 ]; + expected = [ 1, 2, 3 ]; + actual = pluck( x, 'a', out, 1, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function extracts property values from an input object array and assigns results to elements in an output array (accessors)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + { + 'a': 1 + }, + { + 'a': 2 + }, + { + 'a': 3 + } + ]; + out = [ 0, 0, 0 ]; + expected = [ 1, 2, 3 ]; + actual = pluck( toAccessorArray( x ), 'a', out, 1, 0 ); + + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing a stride parameter', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + { + 'a': 1 + }, + { + 'a': 2 + }, + { + 'a': 3 + } + ]; + out = [ 0, 0, 0, 0, 0, 0 ]; + expected = [ 1, 0, 2, 0, 3, 0 ]; + actual = pluck( x, 'a', out, 2, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing an offset parameter', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + { + 'a': 1 + }, + { + 'a': 2 + }, + { + 'a': 3 + } + ]; + out = [ 0, 0, 0, 0, 0, 0 ]; + expected = [ 0, 1, 0, 2, 0, 3 ]; + actual = pluck( x, 'a', out, 2, 1 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/base/pluck/test/test.js b/lib/node_modules/@stdlib/array/base/pluck/test/test.js new file mode 100644 index 000000000000..2cdf83a67d5f --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/test/test.js @@ -0,0 +1,41 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var hasMethod = require( '@stdlib/assert/is-method' ); +var pluck = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof pluck, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( hasOwnProp( pluck, 'assign' ), true, 'returns expected value' ); + t.strictEqual( hasMethod( pluck, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/base/pluck/test/test.main.js b/lib/node_modules/@stdlib/array/base/pluck/test/test.main.js new file mode 100644 index 000000000000..65cb5d004fc0 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/test/test.main.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var pluck = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof pluck, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function extracts property values from an input object array and assigns results to elements in a new output array', function test( t ) { + var expected; + var actual; + var x; + + x = [ + { + 'a': 1 + }, + { + 'a': 2 + }, + { + 'a': 3 + } + ]; + expected = [ 1, 2, 3 ]; + actual = pluck( x, 'a' ); + + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function skips undefined and null array elements', function test( t ) { + var expected; + var actual; + var x; + + x = [ + { + 'a': 1 + }, + void 0, + { + 'a': 3 + }, + null, + { + 'a': 5 + } + ]; + expected = [ 1, , 3, , 5 ]; // eslint-disable-line no-sparse-arrays + actual = pluck( x, 'a' ); + + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function extracts property values from an input object array and assigns results to elements in an output array', function test( t ) { + var expected; + var actual; + var x; + + x = [ + { + 'a': 1 + }, + { + 'a': 2 + }, + { + 'a': 3 + } + ]; + expected = [ 1, 2, 3 ]; + actual = pluck( toAccessorArray( x ), 'a' ); + + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +}); From 172380f7b00913656ec194a9dfae346386a075b2 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sat, 22 Feb 2025 23:57:20 +0500 Subject: [PATCH 2/6] bench: apply review suggestion Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- .../array/base/pluck/benchmark/benchmark.assign.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.assign.js index aa5557cf4f96..62bb1bdbe572 100644 --- a/lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.assign.js +++ b/lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.assign.js @@ -34,8 +34,8 @@ bench( pkg+':assign', function benchmark( b ) { var out; var tmp; var x; + var y; var i; - var j; x = new Array( 100 ); for ( i = 0; i < x.length; i++ ) { @@ -49,14 +49,14 @@ bench( pkg+':assign', function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - x[ i % x.length ].y = randu(); - j = pluck.assign( x, 'y', out, 1, 0 ); - if ( typeof j !== 'object' ) { + x[ i % x.length ][ i % 5 ] = randu(); + out = pluck.assign( x, 2, y, 1, 0 ); + if ( typeof out !== 'object' ) { b.fail( 'should return an array' ); } } b.toc(); - if ( !isArray( j ) ) { + if ( !isArray( out ) ) { b.fail( 'should return an array' ); } b.pass( 'benchmark finished' ); From 9b01b13a3a5f4149023606dc7f561047df7a35bf Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 23 Feb 2025 00:13:46 +0500 Subject: [PATCH 3/6] refactor: apply suggestions from code review Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/pluck/README.md | 4 ++-- .../array/base/pluck/benchmark/benchmark.assign.js | 1 + lib/node_modules/@stdlib/array/base/pluck/docs/repl.txt | 4 ++-- .../@stdlib/array/base/pluck/docs/types/index.d.ts | 7 +++++-- lib/node_modules/@stdlib/array/base/pluck/lib/assign.js | 6 +++--- lib/node_modules/@stdlib/array/base/pluck/lib/index.js | 9 ++++++--- lib/node_modules/@stdlib/array/base/pluck/lib/main.js | 4 ++-- 7 files changed, 21 insertions(+), 14 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/pluck/README.md b/lib/node_modules/@stdlib/array/base/pluck/README.md index e4a0b25df9a0..68fcad5b676d 100644 --- a/lib/node_modules/@stdlib/array/base/pluck/README.md +++ b/lib/node_modules/@stdlib/array/base/pluck/README.md @@ -64,7 +64,7 @@ var y = pluck( x, 'a' ); The function accepts the following arguments: - **x**: input array. -- **prop**: property to extract values from. +- **prop**: property to access. #### pluck.assign( x, prop, out, stride, offset ) @@ -91,7 +91,7 @@ var y = pluck.assign( x, 'a', out, 1, 0 ); The function accepts the following arguments: - **x**: input array. -- **prop**: property to extract values from. +- **prop**: property to access. - **out**: output array. - **stride**: stride length for output array. - **offset**: starting index for output array. diff --git a/lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.assign.js index 62bb1bdbe572..36588be862f3 100644 --- a/lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.assign.js +++ b/lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.assign.js @@ -36,6 +36,7 @@ bench( pkg+':assign', function benchmark( b ) { var x; var y; var i; + var j; x = new Array( 100 ); for ( i = 0; i < x.length; i++ ) { diff --git a/lib/node_modules/@stdlib/array/base/pluck/docs/repl.txt b/lib/node_modules/@stdlib/array/base/pluck/docs/repl.txt index fa9fc377ffb8..7bf48f5d6a7a 100644 --- a/lib/node_modules/@stdlib/array/base/pluck/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/base/pluck/docs/repl.txt @@ -12,7 +12,7 @@ x: Array Input array. prop: string - Property to extract. + Property to access. Returns ------- @@ -35,7 +35,7 @@ x: Array Input array. prop: string - Property to extract. + Property to access. out: Array Output array. stride: integer diff --git a/lib/node_modules/@stdlib/array/base/pluck/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/pluck/docs/types/index.d.ts index a3fc88dd6912..bfb36a53ca05 100644 --- a/lib/node_modules/@stdlib/array/base/pluck/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/base/pluck/docs/types/index.d.ts @@ -24,7 +24,7 @@ import { PropertyName } from '@stdlib/types/object'; import { AccessorArrayLike } from '@stdlib/types/array'; /** -* Interface describing `cueveryBy`. +* Interface describing `pluck`. */ interface Pluck { /** @@ -131,7 +131,10 @@ interface Pluck { * var y = [ 0, 0 ] * * var out = pluck.assign( x, 'a', y, 1, 0 ); - * // y => [ 1, 0.5 ] + * // returns [ 1, 0.5 ] + * + * var bool = ( out === y ); + * // returns true */ assign( x: Array, prop: PropertyName, out: Array, stride: number, offset: number ): Array; } diff --git a/lib/node_modules/@stdlib/array/base/pluck/lib/assign.js b/lib/node_modules/@stdlib/array/base/pluck/lib/assign.js index 9c87b8d50834..6dc29de7ca45 100644 --- a/lib/node_modules/@stdlib/array/base/pluck/lib/assign.js +++ b/lib/node_modules/@stdlib/array/base/pluck/lib/assign.js @@ -31,7 +31,7 @@ var hasOwnProp = require( '@stdlib/assert/has-own-property' ); * * @private * @param {Array} x - input array -* @param {string} prop - property to extract +* @param {string} prop - property to access * @param {Array} out - output array * @param {integer} stride - stride length for output array * @param {NonNegativeInteger} offset - starting index for output array @@ -82,7 +82,7 @@ function internal( x, prop, out, stride, offset ) { * * @private * @param {Object} x - input array object -* @param {string} prop - property to extract +* @param {string} prop - property to access * @param {Object} out - output array object * @param {integer} stride - stride length for output array * @param {NonNegativeInteger} offset - starting index for output array @@ -145,7 +145,7 @@ function accessors( x, prop, out, stride, offset ) { * Extracts property values from an input object array and assigns results to elements in an output array. * * @param {Array} x - input array -* @param {string} prop - property to extract +* @param {string} prop - property to access * @param {Array} out - output array * @param {integer} stride - stride length for output array * @param {NonNegativeInteger} offset - starting index for output array diff --git a/lib/node_modules/@stdlib/array/base/pluck/lib/index.js b/lib/node_modules/@stdlib/array/base/pluck/lib/index.js index 400025c0c13e..120cf3dc7bf0 100644 --- a/lib/node_modules/@stdlib/array/base/pluck/lib/index.js +++ b/lib/node_modules/@stdlib/array/base/pluck/lib/index.js @@ -38,7 +38,7 @@ * } * ]; * -* var y = pluck( x, 'a' ); +* var out = pluck( x, 'a' ); * // returns [ 1, 2, 3 ] * * @example @@ -56,9 +56,12 @@ * } * ]; * -* var out = [ 0, 0, 0 ]; -* var y = pluck.assign( x, 'a', out, 1, 0 ); +* var y = [ 0, 0, 0 ]; +* var out = pluck.assign( x, 'a', out, 1, 0 ); * // returns [ 1, 2, 3 ] +* +* var bool = ( out === y ); +* returns true */ // MODULES // diff --git a/lib/node_modules/@stdlib/array/base/pluck/lib/main.js b/lib/node_modules/@stdlib/array/base/pluck/lib/main.js index dff8aaf339cb..01db8c9ae071 100644 --- a/lib/node_modules/@stdlib/array/base/pluck/lib/main.js +++ b/lib/node_modules/@stdlib/array/base/pluck/lib/main.js @@ -29,7 +29,7 @@ var assign = require( './assign.js' ); * Extracts property values from an input object array and assigns results to elements in a new output array. * * @param {Array} x - input array -* @param {string} prop - property to extract +* @param {string} prop - property to access * @returns {Array} output array * * @example @@ -45,7 +45,7 @@ var assign = require( './assign.js' ); * } * ]; * -* var y = pluck( x, 'a' ); +* var out = pluck( x, 'a' ); * // returns [ 1, 2, 3 ] */ function pluck( x, prop ) { From 67b6a3a0bf7be06cfe37a40a9779917464f6bdd2 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sat, 22 Feb 2025 19:26:49 +0000 Subject: [PATCH 4/6] refactor: add missing typescript tests --- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - 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: na - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na --- --- .../array/base/pluck/docs/types/test.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/lib/node_modules/@stdlib/array/base/pluck/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/pluck/docs/types/test.ts index 8d4ae62d5b25..01bfd369316f 100644 --- a/lib/node_modules/@stdlib/array/base/pluck/docs/types/test.ts +++ b/lib/node_modules/@stdlib/array/base/pluck/docs/types/test.ts @@ -64,3 +64,52 @@ import pluck = require( './index' ); pluck( arr ); // $ExpectError pluck( arr, 'a', { 'copy': true }, 2 ); // $ExpectError } + +// Attached to the main export is an `assign` method which returns an array... +{ + const x = [ + { 'a': 1, 'b': 2 }, + { 'a': 0.5, 'b': 3 } + ]; + const y = [ 0, 0 ]; + + pluck.assign( x, 'a', y, 1, 0 ); // $ExpectType any[] + pluck.assign( toAccessorArray( x ), 'a', toAccessorArray( y ), 1, 0 ); // $ExpectType AccessorArrayLike +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an array... +{ + const y = [ 0, 0 ]; + + pluck.assign( 'abc', 'a', y, 1, 0 ); // $ExpectError + pluck.assign( 123, 'a', y, 1, 0 ); // $ExpectError + pluck.assign( null, 'a', y, 1, 0 ); // $ExpectError + pluck.assign( {}, 'a', y, 1, 0 ); // $ExpectError + pluck.assign( ( x: number ): number => x, 'a', y, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not a valid property name... +{ + const arr = [ + { 'a': 1, 'b': 2 }, + { 'a': 0.5, 'b': 3 } + ]; + const y = [ 0, 0 ]; + + pluck.assign( arr, true, y, 1, 0 ); // $ExpectError + pluck.assign( arr, false, y, 1, 0 ); // $ExpectError + pluck.assign( arr, [], y, 1, 0 ); // $ExpectError + pluck.assign( arr, {}, y, 1, 0 ); // $ExpectError + pluck.assign( arr, ( x: number ): number => x, y, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an invalid number of arguments... +{ + const arr = [ + { 'a': 1, 'b': 2 }, + { 'a': 0.5, 'b': 3 } + ]; + pluck(); // $ExpectError + pluck( arr ); // $ExpectError + pluck( arr, 'a', { 'copy': true }, 2 ); // $ExpectError +} From 2acd65bd59926ddbbfeb8b5376484123706d07ed Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sat, 22 Feb 2025 22:40:02 +0000 Subject: [PATCH 5/6] refactor: main loop and tests --- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: passed --- --- .../@stdlib/array/base/pluck/lib/assign.js | 4 +- .../array/base/pluck/test/test.assign.js | 39 ++++++++++++++++--- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/pluck/lib/assign.js b/lib/node_modules/@stdlib/array/base/pluck/lib/assign.js index 6dc29de7ca45..de027b3f86ac 100644 --- a/lib/node_modules/@stdlib/array/base/pluck/lib/assign.js +++ b/lib/node_modules/@stdlib/array/base/pluck/lib/assign.js @@ -63,7 +63,7 @@ function internal( x, prop, out, stride, offset ) { var v; io = offset; - for ( i = 0; i < x.length; i++ ) { + for ( i = 0; i < out.length; i++ ) { v = x[ i ]; if ( v !== void 0 && @@ -124,7 +124,7 @@ function accessors( x, prop, out, stride, offset ) { oset = out.accessors[ 1 ]; io = offset; - for ( i = 0; i < xdata.length; i++ ) { + for ( i = 0; i < odata.length; i++ ) { v = xget( xdata, i ); if ( v !== void 0 && diff --git a/lib/node_modules/@stdlib/array/base/pluck/test/test.assign.js b/lib/node_modules/@stdlib/array/base/pluck/test/test.assign.js index f3baa3085829..28b6750584eb 100644 --- a/lib/node_modules/@stdlib/array/base/pluck/test/test.assign.js +++ b/lib/node_modules/@stdlib/array/base/pluck/test/test.assign.js @@ -22,6 +22,7 @@ var tape = require( 'tape' ); var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var zeros = require( '@stdlib/array/base/zeros' ); var pluck = require( './../lib' ).assign; @@ -50,7 +51,7 @@ tape( 'the function extracts property values from an input object array and assi 'a': 3 } ]; - out = [ 0, 0, 0 ]; + out = zeros( x.length ); expected = [ 1, 2, 3 ]; actual = pluck( x, 'a', out, 1, 0 ); @@ -76,7 +77,7 @@ tape( 'the function extracts property values from an input object array and assi 'a': 3 } ]; - out = [ 0, 0, 0 ]; + out = zeros( x.length ); expected = [ 1, 2, 3 ]; actual = pluck( toAccessorArray( x ), 'a', out, 1, 0 ); @@ -84,7 +85,7 @@ tape( 'the function extracts property values from an input object array and assi t.end(); }); -tape( 'the function supports providing a stride parameter', function test( t ) { +tape( 'the function supports providing a `stride` parameter', function test( t ) { var expected; var actual; var out; @@ -101,7 +102,7 @@ tape( 'the function supports providing a stride parameter', function test( t ) { 'a': 3 } ]; - out = [ 0, 0, 0, 0, 0, 0 ]; + out = zeros( x.length * 2 ); expected = [ 1, 0, 2, 0, 3, 0 ]; actual = pluck( x, 'a', out, 2, 0 ); @@ -110,7 +111,7 @@ tape( 'the function supports providing a stride parameter', function test( t ) { t.end(); }); -tape( 'the function supports providing an offset parameter', function test( t ) { +tape( 'the function supports providing a negative `stride` parameter', function test( t ) { var expected; var actual; var out; @@ -127,7 +128,33 @@ tape( 'the function supports providing an offset parameter', function test( t ) 'a': 3 } ]; - out = [ 0, 0, 0, 0, 0, 0 ]; + out = zeros( x.length ); + expected = [ 3, 2, 1 ]; + actual = pluck( x, 'a', out, -1, 2 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing an `offset` parameter', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + { + 'a': 1 + }, + { + 'a': 2 + }, + { + 'a': 3 + } + ]; + out = zeros( x.length * 2 ); expected = [ 0, 1, 0, 2, 0, 3 ]; actual = pluck( x, 'a', out, 2, 1 ); From 42ad68cd37947af44ba57899071175d80fa19084 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 23 Feb 2025 05:52:49 +0000 Subject: [PATCH 6/6] test: add missing tests --- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: passed --- --- .../@stdlib/array/base/pluck/lib/assign.js | 2 +- .../array/base/pluck/test/test.assign.js | 86 ++++++++++++++++++- 2 files changed, 83 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/pluck/lib/assign.js b/lib/node_modules/@stdlib/array/base/pluck/lib/assign.js index de027b3f86ac..189728a5074a 100644 --- a/lib/node_modules/@stdlib/array/base/pluck/lib/assign.js +++ b/lib/node_modules/@stdlib/array/base/pluck/lib/assign.js @@ -105,7 +105,7 @@ function internal( x, prop, out, stride, offset ) { * ]; * * var y = [ 0, 0, 0 ]; -* var out = accessors( arraylike2object( x ), 'a', arraylike2object( y ), 1, 0 ); +* var out = accessors( arraylike2object( toAccessorArray( x ) ), 'a', arraylike2object( toAccessorArray( y ) ), 1, 0 ); * // y => [ 1, 2, 3 ] */ function accessors( x, prop, out, stride, offset ) { diff --git a/lib/node_modules/@stdlib/array/base/pluck/test/test.assign.js b/lib/node_modules/@stdlib/array/base/pluck/test/test.assign.js index 28b6750584eb..d040c612e3d4 100644 --- a/lib/node_modules/@stdlib/array/base/pluck/test/test.assign.js +++ b/lib/node_modules/@stdlib/array/base/pluck/test/test.assign.js @@ -34,7 +34,7 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'the function extracts property values from an input object array and assigns results to elements in an output array', function test( t ) { +tape( 'the function extracts property values from an input object array and assigns results to elements in an output array (generic)', function test( t ) { var expected; var actual; var out; @@ -85,7 +85,7 @@ tape( 'the function extracts property values from an input object array and assi t.end(); }); -tape( 'the function supports providing a `stride` parameter', function test( t ) { +tape( 'the function supports providing a `stride` parameter (generic)', function test( t ) { var expected; var actual; var out; @@ -111,7 +111,33 @@ tape( 'the function supports providing a `stride` parameter', function test( t ) t.end(); }); -tape( 'the function supports providing a negative `stride` parameter', function test( t ) { +tape( 'the function supports providing a `stride` parameter (accessors)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + { + 'a': 1 + }, + { + 'a': 2 + }, + { + 'a': 3 + } + ]; + out = zeros( x.length * 2 ); + expected = [ 1, 0, 2, 0, 3, 0 ]; + actual = pluck( toAccessorArray( x ), 'a', out, 2, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing a negative `stride` parameter (generic)', function test( t ) { var expected; var actual; var out; @@ -137,7 +163,33 @@ tape( 'the function supports providing a negative `stride` parameter', function t.end(); }); -tape( 'the function supports providing an `offset` parameter', function test( t ) { +tape( 'the function supports providing a negative `stride` parameter (accessors)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + { + 'a': 1 + }, + { + 'a': 2 + }, + { + 'a': 3 + } + ]; + out = zeros( x.length ); + expected = [ 3, 2, 1 ]; + actual = pluck( toAccessorArray( x ), 'a', out, -1, 2 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing an `offset` parameter (generic)', function test( t ) { var expected; var actual; var out; @@ -162,3 +214,29 @@ tape( 'the function supports providing an `offset` parameter', function test( t t.deepEqual( actual, expected, 'returns expected value' ); t.end(); }); + +tape( 'the function supports providing an `offset` parameter (accessors)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + { + 'a': 1 + }, + { + 'a': 2 + }, + { + 'a': 3 + } + ]; + out = zeros( x.length * 2 ); + expected = [ 0, 1, 0, 2, 0, 3 ]; + actual = pluck( toAccessorArray( x ), 'a', out, 2, 1 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +});