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..68fcad5b676d --- /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 access. + +#### 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 access. +- **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..36588be862f3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.assign.js @@ -0,0 +1,65 @@ +/** +* @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 y; + 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 ][ 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( out ) ) { + 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..7bf48f5d6a7a --- /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 access. + + 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 access. + 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..bfb36a53ca05 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/docs/types/index.d.ts @@ -0,0 +1,188 @@ +/* +* @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 `pluck`. +*/ +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 ); + * // returns [ 1, 0.5 ] + * + * var bool = ( out === y ); + * // returns true + */ + 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..01bfd369316f --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/docs/types/test.ts @@ -0,0 +1,115 @@ +/* +* @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 +} + +// 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 +} 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..189728a5074a --- /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 access +* @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 < out.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 access +* @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( toAccessorArray( x ) ), 'a', arraylike2object( toAccessorArray( 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 < odata.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 access +* @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..120cf3dc7bf0 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/lib/index.js @@ -0,0 +1,81 @@ +/** +* @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 out = pluck( x, 'a' ); +* // returns [ 1, 2, 3 ] +* +* @example +* var pluck = require( '@stdlib/array/base/pluck' ); +* +* var x = [ +* { +* 'a': 1 +* }, +* { +* 'a': 2 +* }, +* { +* 'a': 3 +* } +* ]; +* +* 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 // + +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..01db8c9ae071 --- /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 access +* @returns {Array} output array +* +* @example +* var x = [ +* { +* 'a': 1 +* }, +* { +* 'a': 2 +* }, +* { +* 'a': 3 +* } +* ]; +* +* var out = 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..d040c612e3d4 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/pluck/test/test.assign.js @@ -0,0 +1,242 @@ +/** +* @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 zeros = require( '@stdlib/array/base/zeros' ); +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 (generic)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + { + 'a': 1 + }, + { + 'a': 2 + }, + { + 'a': 3 + } + ]; + out = zeros( x.length ); + 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 = zeros( x.length ); + 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 (generic)', 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( 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 `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; + var x; + + x = [ + { + 'a': 1 + }, + { + 'a': 2 + }, + { + 'a': 3 + } + ]; + 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 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; + 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 ); + + t.strictEqual( actual, out, 'returns expected value' ); + 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(); +}); 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(); +});