From 40421e23587619a5f51af6687f94e01efb97e04b Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 14 Sep 2025 17:55:39 +0000 Subject: [PATCH 01/22] feat: add ndarray/flatten-by --- .../@stdlib/ndarray/flatten-by/README.md | 229 ++++ .../ndarray/flatten-by/benchmark/benchmark.js | 323 +++++ .../@stdlib/ndarray/flatten-by/docs/repl.txt | 50 + .../ndarray/flatten-by/docs/types/index.d.ts | 1043 +++++++++++++++++ .../ndarray/flatten-by/docs/types/test.ts | 150 +++ .../ndarray/flatten-by/examples/index.js | 41 + .../@stdlib/ndarray/flatten-by/lib/index.js | 54 + .../@stdlib/ndarray/flatten-by/lib/main.js | 118 ++ .../@stdlib/ndarray/flatten-by/package.json | 65 + .../@stdlib/ndarray/flatten-by/test/test.js | 636 ++++++++++ 10 files changed, 2709 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-by/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-by/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-by/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-by/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-by/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md new file mode 100644 index 000000000000..c8ebe3392442 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md @@ -0,0 +1,229 @@ + + +# flattenBy + +> Apply a callback to elements in an input [ndarray][@stdlib/ndarray/ctor] and return a flattened output [ndarray][@stdlib/ndarray/ctor]. + +
+ +
+ + + +
+ +## Usage + +```javascript +var flattenBy = require( '@stdlib/ndarray/flatten-by' ); +``` + +#### flattenBy( x\[, options], fcn[, thisArg] ) + +Applies a callback to elements in an input [ndarray][@stdlib/ndarray/ctor] and returns a flattened output [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function scale( value ) { + return value * 2.0; +} + +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +// returns + +var y = flatten( x, scale ); +// returns + +var arr = ndarray2array( y ); +// returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). +- **fcn**: callback function. +- **thisArg**: callback execution context. + +The function accepts the following options: + +- **order**: order in which input [ndarray][@stdlib/ndarray/ctor] elements should be flattened. Must be one of the following: + + - `'row-major'`: flatten elements in lexicographic order. For example, given a two-dimensional input [ndarray][@stdlib/ndarray/ctor] (i.e., a matrix), flattening in lexicographic order means flattening the input [ndarray][@stdlib/ndarray/ctor] row-by-row. + - `'column-major'`: flatten elements in colexicographic order. For example, given a two-dimensional input [ndarray][@stdlib/ndarray/ctor] (i.e., a matrix), flattening in colexicographic order means flattening the input [ndarray][@stdlib/ndarray/ctor] column-by-column. + - `'any'`: flatten according to the physical layout of the input [ndarray][@stdlib/ndarray/ctor] data in memory, regardless of the stated [order][@stdlib/ndarray/orders] of the input [ndarray][@stdlib/ndarray/ctor]. + - `'same'`: flatten according to the stated [order][@stdlib/ndarray/orders] of the input [ndarray][@stdlib/ndarray/ctor]. + + Default: `'row-major'`. + +- **depth**: maximum number of input [ndarray][@stdlib/ndarray/ctor] dimensions to flatten. + +By default, the function flattens all dimensions of the input [ndarray][@stdlib/ndarray/ctor]. To flatten to a desired depth, specify the `depth` option. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function scale( value ) { + return value * 2.0; +} + +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +// returns + +var y = flatten( x, { + 'depth': 1 +}); +// returns + +var arr = ndarray2array( y ); +// returns [ [ 2.0, 4.0 ], [ 6.0, 8.0 ], [ 10.0, 12.0 ] ] +``` + +By default, the input [ndarray][@stdlib/ndarray/ctor] is flattened in lexicographic order. To flatten elements in a different order, specify the `order` option. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function scale( value ) { + return value * 2.0; +} + +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +// returns + +var y = flatten( x, { + 'order': 'column-major' +}); +// returns + +var arr = ndarray2array( y ); +// returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] +``` + +To set the callback function execution context, provide a `thisArg`. + + + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function scale( value ) { + this.count += 1; + return value * 2.0; +} + +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +// returns + +var ctx = { + 'count': 0 +}; + +var y = flatten( x, scale, ctx ); +// returns + +var arr = ndarray2array( y ); +// returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] + +var count = ctx.count; +// returns 6 +``` + +
+ + + +
+ +## Notes + +- The function **always** returns a copy of input [ndarray][@stdlib/ndarray/ctor] data, even when an input [ndarray][@stdlib/ndarray/ctor] already has the desired number of dimensions. + +- The callback function is provided the following arguments: + + - **value**: current array element. + - **indices**: current array element indices. + - **arr**: the input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var flattenBy = require( '@stdlib/ndarray/flatten-by' ); + +function scale( value ) { + return value * 2.0; +} + +var xbuf = discreteUniform( 12, -100, 100, { + 'dtype': 'generic' +}); + +var x = array( xbuf, { + 'shape': [ 2, 2, 3 ], + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +var y = flatten( x, scale ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/flatten-by/benchmark/benchmark.js new file mode 100644 index 000000000000..3a944024eb6a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/benchmark/benchmark.js @@ -0,0 +1,323 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var zeros = require( '@stdlib/ndarray/base/zeros' ); +var pkg = require( './../package.json' ).name; +var flattenBy = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @param {number} value - current value +* @returns {number} output value +*/ +function clbk( value ) { + return value + 1; +} + + +// MAIN // + +bench( pkg+'::2d:row-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 10, 10 ], 'row-major' ), + zeros( 'float32', [ 10, 10 ], 'row-major' ), + zeros( 'int32', [ 10, 10 ], 'row-major' ), + zeros( 'complex128', [ 10, 10 ], 'row-major' ), + zeros( 'generic', [ 10, 10 ], 'row-major' ) + ]; + opts = { + 'depth': 1, + 'order': 'row-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenBy( values[ j ], opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d:column-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 10, 10 ], 'row-major' ), + zeros( 'float32', [ 10, 10 ], 'row-major' ), + zeros( 'int32', [ 10, 10 ], 'row-major' ), + zeros( 'complex128', [ 10, 10 ], 'row-major' ), + zeros( 'generic', [ 10, 10 ], 'row-major' ) + ]; + opts = { + 'depth': 1, + 'order': 'column-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenBy( values[ j ], opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:row-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 2, 5, 10 ], 'row-major' ), + zeros( 'float32', [ 2, 5, 10 ], 'row-major' ), + zeros( 'int32', [ 2, 5, 10 ], 'row-major' ), + zeros( 'complex128', [ 2, 5, 10 ], 'row-major' ), + zeros( 'generic', [ 2, 5, 10 ], 'row-major' ) + ]; + opts = { + 'depth': 2, + 'order': 'row-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenBy( values[ j ], opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:column-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 2, 5, 10 ], 'row-major' ), + zeros( 'float32', [ 2, 5, 10 ], 'row-major' ), + zeros( 'int32', [ 2, 5, 10 ], 'row-major' ), + zeros( 'complex128', [ 2, 5, 10 ], 'row-major' ), + zeros( 'generic', [ 2, 5, 10 ], 'row-major' ) + ]; + opts = { + 'depth': 2, + 'order': 'column-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenBy( values[ j ], opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:row-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 2, 5, 2, 5 ], 'row-major' ), + zeros( 'float32', [ 2, 5, 2, 5 ], 'row-major' ), + zeros( 'int32', [ 2, 5, 2, 5 ], 'row-major' ), + zeros( 'complex128', [ 2, 5, 2, 5 ], 'row-major' ), + zeros( 'generic', [ 2, 5, 2, 5 ], 'row-major' ) + ]; + opts = { + 'depth': 3, + 'order': 'row-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenBy( values[ j ], opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:column-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 2, 5, 2, 5 ], 'row-major' ), + zeros( 'float32', [ 2, 5, 2, 5 ], 'row-major' ), + zeros( 'int32', [ 2, 5, 2, 5 ], 'row-major' ), + zeros( 'complex128', [ 2, 5, 2, 5 ], 'row-major' ), + zeros( 'generic', [ 2, 5, 2, 5 ], 'row-major' ) + ]; + opts = { + 'depth': 3, + 'order': 'column-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenBy( values[ j ], opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:row-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 2, 5, 2, 5, 1 ], 'row-major' ), + zeros( 'float32', [ 2, 5, 2, 5, 1 ], 'row-major' ), + zeros( 'int32', [ 2, 5, 2, 5, 1 ], 'row-major' ), + zeros( 'complex128', [ 2, 5, 2, 5, 1 ], 'row-major' ), + zeros( 'generic', [ 2, 5, 2, 5, 1 ], 'row-major' ) + ]; + opts = { + 'depth': 4, + 'order': 'row-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenBy( values[ j ], opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:column-major', function benchmark( b ) { + var values; + var opts; + var y; + var i; + var j; + + values = [ + zeros( 'float64', [ 2, 5, 2, 5, 1 ], 'row-major' ), + zeros( 'float32', [ 2, 5, 2, 5, 1 ], 'row-major' ), + zeros( 'int32', [ 2, 5, 2, 5, 1 ], 'row-major' ), + zeros( 'complex128', [ 2, 5, 2, 5, 1 ], 'row-major' ), + zeros( 'generic', [ 2, 5, 2, 5, 1 ], 'row-major' ) + ]; + opts = { + 'depth': 4, + 'order': 'column-major' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % values.length; + y = flattenBy( values[ j ], opts, clbk ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/repl.txt new file mode 100644 index 000000000000..b760777e183a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/repl.txt @@ -0,0 +1,50 @@ + +{{alias}}( x[, options], fcn[, thisArg] ) + Applies a callback to elements in an input ndarray and returns a flattened + output ndarray. + + Parameters + ---------- + x: ndarray + Input ndarray. + + options: Object (optional) + Function options. + + options.depth: integer (optional) + Maximum number of dimensions to flatten. By default, the function + flattens all input ndarray dimensions. + + options.order: string (optional) + Order in which input ndarray elements should be flattened. The following + orders are supported: + + - row-major: flatten in lexicographic order. + - column-major: flatten in colexicographic order. + - same: flatten according to the stated order of the input ndarray. + - any: flatten according to physical layout of the input ndarray data in + memory, regardless of the stated order of the input ndarray. + + Default: 'row-major'. + + fcn: Function + Callback function. + + thisArg: any (optional) + Callback execution context. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); + > function f ( v ) { return v * 2.0 }; + > var y = {{alias}}( x, f ); + > var arr = {{alias:@stdlib/ndarray/to-array}}( y ) + [ 2.0, 4.0, 6.0, 8.0 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts new file mode 100644 index 000000000000..dd01fd443d0e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts @@ -0,0 +1,1043 @@ +/* +* @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 { float64ndarray, float32ndarray, complex128ndarray, complex64ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, boolndarray, genericndarray, Order } from '@stdlib/types/ndarray'; +import { Complex64, Complex128, ComplexLike } from '@stdlib/types/complex'; + +/** +* Callback invoked for each ndarray element. +* +* @returns output value +*/ +type Nullary = ( this: W ) => V; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @returns output value +*/ +type Unary = ( this: W, value: T ) => V; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +* @returns output value +*/ +type Binary = ( this: W, value: T, indices: Array ) => V; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +* @returns output value +*/ +type Ternary = ( this: W, value: T, indices: Array, arr: U ) => V; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +* @returns output value +*/ +type Callback = Nullary | Unary | Binary | Ternary; + +/** +* Interface defining function options. +*/ +interface Options { + /** + * Maximum number of dimensions to flatten. + * + * ## Notes + * + * - By default, the function flattens all input ndarray dimensions. + */ + depth?: number; + + /** + * Order in which input ndarray elements should be flattened. + * + * ## Notes + * + * - The following orders are supported: + * + * - **row-major**: flatten in lexicographic order. + * - **column-major**: flatten in colexicographic order. + * - **same**: flatten according to the stated order of the input ndarray. + * - **any**: flatten according to the physical layout of the input ndarray data in memory, regardless of the stated order of the input ndarray. + * + * - Default: 'row-major'. + */ + order?: Order | 'same' | 'any'; +} + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var y = flattenBy( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] +*/ +declare function flattenBy( x: float64ndarray, fcn: Callback, thisArg?: ThisParameterType> ): float64ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'float32', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var y = flattenBy( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] +*/ +declare function flattenBy( x: float32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): float32ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Complex64Array = require( '@stdlib/array/complex64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function identity( value ) { +* return value; +* } +* +* var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 1 , 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex64', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var y = flattenBy( x, identity ); +* // returns +*/ +declare function flattenBy( x: complex64ndarray, fcn: Callback, thisArg?: ThisParameterType> ): complex64ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function identity( value ) { +* return value; +* } +* +* var buffer = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 1 , 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex128', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var y = flattenBy( x, identity ); +* // returns +*/ +declare function flattenBy( x: complex128ndarray, fcn: Callback, thisArg?: ThisParameterType> ): complex128ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Int32Array = require( '@stdlib/array/int32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Int32Array( [ 1, 2, 3, 4, 5, 6 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'int32', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var y = flattenBy( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2, 4, 6, 8, 10, 12 ] +*/ +declare function flattenBy( x: int32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): int32ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Int16Array = require( '@stdlib/array/int32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Int16Array( [ 1, 2, 3, 4, 5, 6 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'int16', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var y = flattenBy( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2, 4, 6, 8, 10, 12 ] +*/ +declare function flattenBy( x: int16ndarray, fcn: Callback, thisArg?: ThisParameterType> ): int16ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Int8Array = require( '@stdlib/array/int8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Int8Array( [ 1, 2, 3, 4, 5, 6 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'int8', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var y = flattenBy( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2, 4, 6, 8, 10, 12 ] +*/ +declare function flattenBy( x: int8ndarray, fcn: Callback, thisArg?: ThisParameterType> ): int8ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Uint32Array = require( '@stdlib/array/uint32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Uint32Array( [ 1, 2, 3, 4, 5, 6 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint32', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var y = flattenBy( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2, 4, 6, 8, 10, 12 ] +*/ +declare function flattenBy( x: uint32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint32ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Uint16Array = require( '@stdlib/array/uint16' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Uint16Array( [ 1, 2, 3, 4, 5, 6 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint16', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var y = flattenBy( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2, 4, 6, 8, 10, 12 ] +*/ +declare function flattenBy( x: uint16ndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint16ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Uint8Array = require( '@stdlib/array/uint8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Uint8Array( [ 1, 2, 3, 4, 5, 6 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint8', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var y = flattenBy( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2, 4, 6, 8, 10, 12 ] +*/ +declare function flattenBy( x: uint8ndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint8ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Uint8ClampedArray( [ 1, 2, 3, 4, 5, 6 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var y = flattenBy( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2, 4, 6, 8, 10, 12 ] +*/ +declare function flattenBy( x: uint8cndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint8cndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var BooleanArray = require( '@stdlib/array/bool' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function invert( value ) { +* return !value; +* } +* +* var buffer = new BooleanArray( [ true, false, true, false, true, false ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var y = flattenBy( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ false, true, false, true, false, true ] +*/ +declare function flattenBy( x: boolndarray, fcn: Callback, thisArg?: ThisParameterType> ): boolndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var y = flattenBy( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] +*/ +declare function flattenBy( x: genericndarray, fcn: Callback, V, W>, thisArg?: ThisParameterType, V, W>> ): genericndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var opts = { +* 'depth': 1 +* }; +* +* var y = flattenBy( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2.0, 4.0, 6.0 ], [ 8.0, 10.0, 12.0 ] ] +*/ +declare function flattenBy( x: float64ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): float64ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'float32', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var opts = { +* 'depth': 1 +* }; +* +* var y = flattenBy( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2.0, 4.0, 6.0 ], [ 8.0, 10.0, 12.0 ] ] +*/ +declare function flattenBy( x: float32ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): float32ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Complex64Array = require( '@stdlib/array/complex64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function identity( value ) { +* return value; +* } +* +* var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 1 , 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex64', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var opts = { +* 'depth': 1 +* }; +* +* var y = flattenBy( x, opts, identity ); +* // returns +*/ +declare function flattenBy( x: complex64ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): complex64ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function identity( value ) { +* return value; +* } +* +* var buffer = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 1 , 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex128', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var opts = { +* 'depth': 1 +* }; +* +* var y = flattenBy( x, opts, identity ); +* // returns +*/ +declare function flattenBy( x: complex128ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): complex128ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Int32Array = require( '@stdlib/array/int32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Int32Array( [ 1, 2, 3, 4, 5, 6 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'int32', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var opts = { +* 'depth': 1 +* }; +* +* var y = flattenBy( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2, 4, 6 ], [ 8, 10, 12 ] ] +*/ +declare function flattenBy( x: int32ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): int32ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Int16Array = require( '@stdlib/array/int32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Int16Array( [ 1, 2, 3, 4, 5, 6 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'int16', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var opts = { +* 'depth': 1 +* }; +* +* var y = flattenBy( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2, 4, 6 ], [ 8, 10, 12 ] ] +*/ +declare function flattenBy( x: int16ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): int16ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Int8Array = require( '@stdlib/array/int8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Int8Array( [ 1, 2, 3, 4, 5, 6 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'int8', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var opts = { +* 'depth': 1 +* }; +* +* var y = flattenBy( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2, 4, 6 ], [ 8, 10, 12 ] ] +*/ +declare function flattenBy( x: int8ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): int8ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Uint32Array = require( '@stdlib/array/uint32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Uint32Array( [ 1, 2, 3, 4, 5, 6 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint32', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var opts = { +* 'depth': 1 +* }; +* +* var y = flattenBy( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2, 4, 6 ], [ 8, 10, 12 ] ] +*/ +declare function flattenBy( x: uint32ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): uint32ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Uint16Array = require( '@stdlib/array/uint16' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Uint16Array( [ 1, 2, 3, 4, 5, 6 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint16', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var opts = { +* 'depth': 1 +* }; +* +* var y = flattenBy( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2, 4, 6 ], [ 8, 10, 12 ] ] +*/ +declare function flattenBy( x: uint16ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): uint16ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Uint8Array = require( '@stdlib/array/uint8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Uint8Array( [ 1, 2, 3, 4, 5, 6 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint8', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var opts = { +* 'depth': 1 +* }; +* +* var y = flattenBy( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2, 4, 6 ], [ 8, 10, 12 ] ] +*/ +declare function flattenBy( x: uint8ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): uint8ndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = new Uint8ClampedArray( [ 1, 2, 3, 4, 5, 6 ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var opts = { +* 'depth': 1 +* }; +* +* var y = flattenBy( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2, 4, 6 ], [ 8, 10, 12 ] ] +*/ +declare function flattenBy( x: uint8cndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): uint8cndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var BooleanArray = require( '@stdlib/array/bool' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function invert( value ) { +* return !value; +* } +* +* var buffer = new BooleanArray( [ true, false, true, false, true, false ] ); +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var opts = { +* 'depth': 1 +* }; +* +* var y = flattenBy( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ false, true, false, true, false, true ] +*/ +declare function flattenBy( x: boolndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): boolndarray; + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output ndarray +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 1, 2 ]; +* var strides = [ 2, 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // return +* +* var opts = { +* 'depth': 1 +* }; +* +* var y = flattenBy( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 2.0, 4.0, 6.0 ], [ 8.0, 10.0, 12.0 ] ] +*/ +declare function flattenBy( x: genericndarray, options: Options, fcn: Callback, V, W>, thisArg?: ThisParameterType, V, W>> ): genericndarray; + +// EXPORTS // + +export = flattenBy; diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts new file mode 100644 index 000000000000..a9ed59eef273 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts @@ -0,0 +1,150 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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 empty = require( '@stdlib/ndarray/base/empty' ); +import zeros = require( '@stdlib/ndarray/base/zeros' ); +import flattenBy = require( './index' ); + +/** +* Evaluates the identity function. +* +* @param x - input value +* @returns input value +*/ +function identity( x: any ): any { + return x; +} + +// The function returns an ndarray... +{ + const sh = [ 2, 2 ]; + const ord = 'row-major'; + + flattenBy( zeros( 'float64', sh, ord ), identity ); // $ExpectType float64ndarray + flattenBy( zeros( 'float64', sh, ord ), {}, identity ); // $ExpectType float64ndarray + flattenBy( zeros( 'float64', sh, ord ), identity, {} ); // $ExpectType float64ndarray + flattenBy( zeros( 'float64', sh, ord ), {}, identity, {} ); // $ExpectType float64ndarray + flattenBy( zeros( 'float32', sh, ord ), identity ); // $ExpectType float32ndarray + flattenBy( zeros( 'float32', sh, ord ), {}, identity ); // $ExpectType float32ndarray + flattenBy( zeros( 'float32', sh, ord ), identity, {} ); // $ExpectType float32ndarray + flattenBy( zeros( 'float32', sh, ord ), {}, identity, {} ); // $ExpectType float32ndarray + flattenBy( zeros( 'complex64', sh, ord ), identity ); // $ExpectType complex64ndarray + flattenBy( zeros( 'complex64', sh, ord ), {}, identity ); // $ExpectType complex64ndarray + flattenBy( zeros( 'complex64', sh, ord ), identity, {} ); // $ExpectType complex64ndarray + flattenBy( zeros( 'complex64', sh, ord ), {}, identity, {} ); // $ExpectType complex64ndarray + flattenBy( zeros( 'complex128', sh, ord ), identity ); // $ExpectType complex128ndarray + flattenBy( zeros( 'complex128', sh, ord ), {}, identity ); // $ExpectType complex128ndarray + flattenBy( zeros( 'complex128', sh, ord ), identity, {} ); // $ExpectType complex128ndarray + flattenBy( zeros( 'complex128', sh, ord ), {}, identity, {} ); // $ExpectType complex128ndarray + flattenBy( zeros( 'int32', sh, ord ), identity ); // $ExpectType int32ndarray + flattenBy( zeros( 'int32', sh, ord ), {}, identity ); // $ExpectType int32ndarray + flattenBy( zeros( 'int32', sh, ord ), identity, {} ); // $ExpectType int32ndarray + flattenBy( zeros( 'int32', sh, ord ), {}, identity, {} ); // $ExpectType int32ndarray + flattenBy( zeros( 'int16', sh, ord ), identity ); // $ExpectType int16ndarray + flattenBy( zeros( 'int16', sh, ord ), {}, identity ); // $ExpectType int16ndarray + flattenBy( zeros( 'int16', sh, ord ), identity, {} ); // $ExpectType int16ndarray + flattenBy( zeros( 'int16', sh, ord ), {}, identity, {} ); // $ExpectType int16ndarray + flattenBy( zeros( 'int8', sh, ord ), identity ); // $ExpectType int8ndarray + flattenBy( zeros( 'int8', sh, ord ), {}, identity ); // $ExpectType int8ndarray + flattenBy( zeros( 'int8', sh, ord ), identity, {} ); // $ExpectType int8ndarray + flattenBy( zeros( 'int8', sh, ord ), {}, identity, {} ); // $ExpectType int8ndarray + flattenBy( zeros( 'uint32', sh, ord ), identity ); // $ExpectType uint32ndarray + flattenBy( zeros( 'uint32', sh, ord ), {}, identity ); // $ExpectType uint32ndarray + flattenBy( zeros( 'uint32', sh, ord ), identity, {} ); // $ExpectType uint32ndarray + flattenBy( zeros( 'uint16', sh, ord ), identity ); // $ExpectType uint16ndarray + flattenBy( zeros( 'uint16', sh, ord ), {}, identity ); // $ExpectType uint16ndarray + flattenBy( zeros( 'uint16', sh, ord ), identity, {} ); // $ExpectType uint16ndarray + flattenBy( zeros( 'uint16', sh, ord ), {}, identity, {} ); // $ExpectType uint16ndarray + flattenBy( zeros( 'uint8', sh, ord ), identity ); // $ExpectType uint8ndarray + flattenBy( zeros( 'uint8', sh, ord ), {}, identity ); // $ExpectType uint8ndarray + flattenBy( zeros( 'uint8', sh, ord ), identity, {} ); // $ExpectType uint8ndarray + flattenBy( zeros( 'uint8', sh, ord ), {}, identity, {} ); // $ExpectType uint8ndarray + flattenBy( zeros( 'uint8c', sh, ord ), identity ); // $ExpectType uint8cndarray + flattenBy( zeros( 'uint8c', sh, ord ), {}, identity ); // $ExpectType uint8cndarray + flattenBy( zeros( 'uint8c', sh, ord ), identity, {} ); // $ExpectType uint8cndarray + flattenBy( zeros( 'uint8c', sh, ord ), {}, identity, {} ); // $ExpectType uint8cndarray + flattenBy( empty( 'bool', sh, ord ), identity ); // $ExpectType boolndarray + flattenBy( empty( 'bool', sh, ord ), {}, identity ); // $ExpectType boolndarray + flattenBy( empty( 'bool', sh, ord ), identity, {} ); // $ExpectType boolndarray + flattenBy( empty( 'bool', sh, ord ), {}, identity, {} ); // $ExpectType boolndarray + flattenBy( zeros( 'generic', sh, ord ), identity ); // $ExpectType genericndarray + flattenBy( zeros( 'generic', sh, ord ), {}, identity ); // $ExpectType genericndarray + flattenBy( zeros( 'generic', sh, ord ), identity, {} ); // $ExpectType genericndarray + flattenBy( zeros( 'generic', sh, ord ), {}, identity, {} ); // $ExpectType genericndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + flattenBy( 5, identity ); // $ExpectError + flattenBy( true, identity ); // $ExpectError + flattenBy( false, identity ); // $ExpectError + flattenBy( null, identity ); // $ExpectError + flattenBy( undefined, identity ); // $ExpectError + flattenBy( {}, identity ); // $ExpectError + flattenBy( [ 1 ], identity ); // $ExpectError + flattenBy( ( x: number ): number => x, identity ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), '5', identity ); // $ExpectError + flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), true, identity ); // $ExpectError + flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), false, identity ); // $ExpectError + flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), null, identity ); // $ExpectError + flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), [ 1 ], identity ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument with invalid `depth` option... +{ + flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': '5' }, identity ); // $ExpectError + flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': true }, identity ); // $ExpectError + flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': false }, identity ); // $ExpectError + flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': null }, identity ); // $ExpectError + flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': [ 1 ] }, identity ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument with invalid `order` option... +{ + flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': '5' }, identity ); // $ExpectError + flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': true }, identity ); // $ExpectError + flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': false }, identity ); // $ExpectError + flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': null }, identity ); // $ExpectError + flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': [ 1 ] }, identity ); // $ExpectError +} + +// The compiler throws an error if the function is provided a callback which is not a function... +{ + const x = zeros( 'generic', [ 2, 2 ], 'row-major' ); + + flattenBy( x, {}, '5' ); // $ExpectError + flattenBy( x, {}, true ); // $ExpectError + flattenBy( x, {}, false ); // $ExpectError + flattenBy( x, {}, null ); // $ExpectError + flattenBy( x, {}, undefined ); // $ExpectError + flattenBy( x, {}, {} ); // $ExpectError + flattenBy( x, {}, [ 1 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + flattenBy(); // $ExpectError + flattenBy( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError + flattenBy( zeros( 'float64', [ 2, 2 ], 'row-major' ), {}, ( x: number ): number => x, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/examples/index.js b/lib/node_modules/@stdlib/ndarray/flatten-by/examples/index.js new file mode 100644 index 000000000000..e55d817ec277 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/examples/index.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'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var flattenBy = require( './../lib' ); + +function scale( value ) { + return value * 2.0; +} + +var xbuf = discreteUniform( 12, -100, 100, { + 'dtype': 'generic' +}); + +var x = array( xbuf, { + 'shape': [ 2, 2, 3 ], + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +var y = flattenBy( x, scale ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js new file mode 100644 index 000000000000..3707dae03068 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js @@ -0,0 +1,54 @@ +/** +* @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'; + +/** +* Apply a callback to elements in an input ndarray and return a flattened output ndarray. +* +* @module @stdlib/ndarray/flatten-by +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var flattenBy = require( '@stdlib/ndarray/flatten-by' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* // Create an input ndarray: +* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +* // return +* +* // Flatten the input ndarray: +* var y = flatten( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js new file mode 100644 index 000000000000..f5ca1ecd0b6d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js @@ -0,0 +1,118 @@ +/** +* @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 isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ); +var isOrder = require( '@stdlib/ndarray/base/assert/is-order' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getStrides = require( '@stdlib/ndarray/strides' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var getDType = require( '@stdlib/ndarray/base/dtype' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var strides2order = require( '@stdlib/ndarray/base/strides2order' ); +var flattenShape = require( '@stdlib/ndarray/base/flatten-shape' ); +var assign = require( '@stdlib/ndarray/base/assign' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var map = require( '@stdlib/ndarray/map' ); +var flatten = require( '@stdlib/ndarray/flatten' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* +* @param {ndarray} x - input ndarray +* @param {Options} [options] - function options +* @param {NonNegativeInteger} [options.depth] - maximum number of dimensions to flatten +* @param {string} [options.order='row-major'] - order in which input ndarray elements should be flattened +* @param {Function} fcn - callback function +* @param {*} thisArg - callback execution context +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {TypeError} callback argument must be a function +* @throws {TypeError} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( value ) { +* return value * 2.0; +* } +* +* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); +* // return +* +* var y = flattenBy( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] +*/ +function flattenBy( x, options, fcn, thisArg ) { + var opts; + var ctx; + var cb; + var y; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + opts = {}; + if ( arguments.length < 3 ) { // Case: flattenBy( x, fcn ) + cb = options; + } else if ( arguments.length === 3 ) { + if ( isFunction( options ) ) { // Case: flattenBy( x, fcn, thisArg ) + cb = options; + ctx = fcn; + } else { // Case: flattenBy( x, options, fcn ) + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + opts = options; + cb = fcn; + } + } else { + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + opts = options; + cb = fcn; + ctx = thisArg; + } + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) ); + } + y = map( x, cb, ctx ); + return flatten( y, opts ); +} + + +// EXPORTS // + +module.exports = flattenBy; diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/package.json b/lib/node_modules/@stdlib/ndarray/flatten-by/package.json new file mode 100644 index 000000000000..6710105cdaca --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/ndarray/flatten-by", + "version": "0.0.0", + "description": "Apply a callback to elements in an input ndarray and return a flattened output ndarray.", + "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" + }, + "types": "./docs/types", + "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", + "multidimensional", + "array", + "ndarray", + "tensor", + "matrix", + "flat", + "flatten", + "map", + "copy", + "transform" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js new file mode 100644 index 000000000000..9b18074ece84 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js @@ -0,0 +1,636 @@ +/** +* @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. +*/ + +/* eslint-disable max-len */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var flattenBy = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @param value - current value +* @returns output value +*/ +function clbk( value ) { + return value + 1; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof flattenBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenBy( value, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenBy( value, {}, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenBy( zeros( [ 2, 2, 2 ] ), value, clbk ); + }; + } +}); + +tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, contiguous)', function test( t ) { + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flattenBy( x, clbk ); + expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, non-contiguous)', function test( t ) { + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = [ 8, 4, 2 ]; + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, NaN, 2.0, NaN, 3.0, NaN, 4.0, NaN, 5.0, NaN, 6.0, NaN, 7.0, NaN, 8.0, NaN ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flattenBy( x, clbk ); + expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (column-major, contiguous)', function test( t ) { + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flattenBy( x, clbk ); + expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (column-major, non-contiguous)', function test( t ) { + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = [ 2, 4, 8 ]; + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, NaN, 5.0, NaN, 3.0, NaN, 7.0, NaN, 2.0, NaN, 6.0, NaN, 4.0, NaN, 8.0, NaN ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flattenBy( x, clbk ); + expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the maximum number of dimensions to flatten (row-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'depth': 0 + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ + [ 2.0, 3.0 ], + [ 4.0, 5.0 ] + ], + [ + [ 6.0, 7.0 ], + [ 8.0, 9.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + opts = { + 'depth': 1 + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ 2.0, 3.0 ], + [ 4.0, 5.0 ], + [ 6.0, 7.0 ], + [ 8.0, 9.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + opts = { + 'depth': 2 + }; + y = flattenBy( x, opts, clbk ); + expected = [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the maximum number of dimensions to flatten (column-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'depth': 0 + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ + [ 2.0, 3.0 ], + [ 4.0, 5.0 ] + ], + [ + [ 6.0, 7.0 ], + [ 8.0, 9.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + opts = { + 'depth': 1 + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ 2.0, 3.0 ], + [ 4.0, 5.0 ], + [ 6.0, 7.0 ], + [ 8.0, 9.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + opts = { + 'depth': 2 + }; + y = flattenBy( x, opts, clbk ); + expected = [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a provided input ndarray in lexicographic order (row-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'row-major' + }; + y = flattenBy( x, opts, clbk ); + expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + opts = { + 'depth': 1, + 'order': 'row-major' + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ 2.0, 3.0 ], + [ 4.0, 5.0 ], + [ 6.0, 7.0 ], + [ 8.0, 9.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a provided input ndarray in lexicographic order (column-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'row-major' + }; + y = flattenBy( x, opts, clbk ); + expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + opts = { + 'depth': 1, + 'order': 'row-major' + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ 2.0, 3.0 ], + [ 4.0, 5.0 ], + [ 6.0, 7.0 ], + [ 8.0, 9.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); From 5a11ffb9884e31eb3e4ba61586bfbafd8f39f733 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 14 Sep 2025 23:42:11 +0500 Subject: [PATCH 02/22] refactor: apply suggestions from code review --- 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: passed - 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 --- --- .../ndarray/flatten-by/examples/index.js | 2 +- .../@stdlib/ndarray/flatten-by/lib/index.js | 3 +- .../@stdlib/ndarray/flatten-by/lib/main.js | 32 +- .../@stdlib/ndarray/flatten-by/test/test.js | 1370 ++++++++++------- 4 files changed, 832 insertions(+), 575 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/examples/index.js b/lib/node_modules/@stdlib/ndarray/flatten-by/examples/index.js index e55d817ec277..d99b9ccc65e3 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/examples/index.js @@ -24,7 +24,7 @@ var ndarray2array = require( '@stdlib/ndarray/to-array' ); var flattenBy = require( './../lib' ); function scale( value ) { - return value * 2.0; + return value * 2.0; } var xbuf = discreteUniform( 12, -100, 100, { diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js index 3707dae03068..e5d9dea50188 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js @@ -36,8 +36,7 @@ * var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); * // return * -* // Flatten the input ndarray: -* var y = flatten( x, scale ); +* var y = flattenBy( x, scale ); * // returns * * var arr = ndarray2array( y ); diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js index f5ca1ecd0b6d..f101d4925ca7 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js @@ -21,20 +21,8 @@ // MODULES // var isPlainObject = require( '@stdlib/assert/is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); -var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ); -var isOrder = require( '@stdlib/ndarray/base/assert/is-order' ); -var getShape = require( '@stdlib/ndarray/shape' ); -var getOrder = require( '@stdlib/ndarray/order' ); -var getStrides = require( '@stdlib/ndarray/strides' ); -var getData = require( '@stdlib/ndarray/base/data-buffer' ); -var getDType = require( '@stdlib/ndarray/base/dtype' ); -var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); -var strides2order = require( '@stdlib/ndarray/base/strides2order' ); -var flattenShape = require( '@stdlib/ndarray/base/flatten-shape' ); -var assign = require( '@stdlib/ndarray/base/assign' ); -var emptyLike = require( '@stdlib/ndarray/empty-like' ); var map = require( '@stdlib/ndarray/map' ); var flatten = require( '@stdlib/ndarray/flatten' ); var format = require( '@stdlib/string/format' ); @@ -80,10 +68,10 @@ function flattenBy( x, options, fcn, thisArg ) { var cb; var y; - if ( !isndarrayLike( x ) ) { + if ( !isndarrayLike( x ) ) { throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); } - opts = {}; + opts = {}; if ( arguments.length < 3 ) { // Case: flattenBy( x, fcn ) cb = options; } else if ( arguments.length === 3 ) { @@ -91,16 +79,16 @@ function flattenBy( x, options, fcn, thisArg ) { cb = options; ctx = fcn; } else { // Case: flattenBy( x, options, fcn ) - if ( !isPlainObject( options ) ) { - throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); - } + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } opts = options; cb = fcn; } } else { - if ( !isPlainObject( options ) ) { - throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); - } + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } opts = options; cb = fcn; ctx = thisArg; @@ -108,7 +96,7 @@ function flattenBy( x, options, fcn, thisArg ) { if ( !isFunction( cb ) ) { throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) ); } - y = map( x, cb, ctx ); + y = map( x, cb, ctx ); return flatten( y, opts ); } diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js index 9b18074ece84..376bb2af2b73 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js @@ -33,7 +33,6 @@ var getOrder = require( '@stdlib/ndarray/order' ); var getData = require( '@stdlib/ndarray/data-buffer' ); var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); -var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var flattenBy = require( './../lib' ); @@ -43,594 +42,865 @@ var flattenBy = require( './../lib' ); /** * Callback function. * -* @param value - current value -* @returns output value +* @param {number} value - current value +* @returns {number} output value */ function clbk( value ) { - return value + 1; + return value + 1; } // TESTS // tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof flattenBy, 'function', 'main export is a function' ); - t.end(); + t.ok( true, __filename ); + t.strictEqual( typeof flattenBy, 'function', 'main export is a function' ); + t.end(); }); tape( 'the function throws an error if provided a first argument which is not an ndarray', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - flattenBy( value, clbk ); - }; - } + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenBy( value, clbk ); + }; + } }); tape( 'the function throws an error if provided a first argument which is not an ndarray (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - flattenBy( value, {}, clbk ); - }; - } + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenBy( value, {}, clbk ); + }; + } }); tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - function noop() {} - ]; - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - flattenBy( zeros( [ 2, 2, 2 ] ), value, clbk ); - }; - } + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenBy( zeros( [ 2, 2, 2 ] ), value, clbk ); + }; + } }); tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, contiguous)', function test( t ) { - var expected; - var xbuf; - var ord; - var sh; - var st; - var dt; - var o; - var x; - var y; - - dt = 'float64'; - ord = 'row-major'; - sh = [ 2, 2, 2 ]; - st = shape2strides( sh, ord ); - o = strides2offset( sh, st ); - - /* - * [ - * [ - * [ 1.0, 2.0 ], - * [ 3.0, 4.0 ] - * ], - * [ - * [ 5.0, 6.0 ], - * [ 7.0, 8.0 ] - * ] - * ] - */ - xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); - x = new ndarray( dt, xbuf, sh, st, o, ord ); - - y = flattenBy( x, clbk ); - expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - t.end(); + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flattenBy( x, clbk ); + expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); }); tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, non-contiguous)', function test( t ) { - var expected; - var xbuf; - var ord; - var sh; - var st; - var dt; - var o; - var x; - var y; - - dt = 'float64'; - ord = 'row-major'; - sh = [ 2, 2, 2 ]; - st = [ 8, 4, 2 ]; - o = strides2offset( sh, st ); - - /* - * [ - * [ - * [ 1.0, 2.0 ], - * [ 3.0, 4.0 ] - * ], - * [ - * [ 5.0, 6.0 ], - * [ 7.0, 8.0 ] - * ] - * ] - */ - xbuf = new Float64Array( [ 1.0, NaN, 2.0, NaN, 3.0, NaN, 4.0, NaN, 5.0, NaN, 6.0, NaN, 7.0, NaN, 8.0, NaN ] ); - x = new ndarray( dt, xbuf, sh, st, o, ord ); - - y = flattenBy( x, clbk ); - expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - t.end(); + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = [ 8, 4, 2 ]; + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, NaN, 2.0, NaN, 3.0, NaN, 4.0, NaN, 5.0, NaN, 6.0, NaN, 7.0, NaN, 8.0, NaN ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flattenBy( x, clbk ); + expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); }); tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (column-major, contiguous)', function test( t ) { - var expected; - var xbuf; - var ord; - var sh; - var st; - var dt; - var o; - var x; - var y; - - dt = 'float64'; - ord = 'column-major'; - sh = [ 2, 2, 2 ]; - st = shape2strides( sh, ord ); - o = strides2offset( sh, st ); - - /* - * [ - * [ - * [ 1.0, 2.0 ], - * [ 3.0, 4.0 ] - * ], - * [ - * [ 5.0, 6.0 ], - * [ 7.0, 8.0 ] - * ] - * ] - */ - xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); - x = new ndarray( dt, xbuf, sh, st, o, ord ); - - y = flattenBy( x, clbk ); - expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - t.end(); + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flattenBy( x, clbk ); + expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); }); tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (column-major, non-contiguous)', function test( t ) { - var expected; - var xbuf; - var ord; - var sh; - var st; - var dt; - var o; - var x; - var y; - - dt = 'float64'; - ord = 'column-major'; - sh = [ 2, 2, 2 ]; - st = [ 2, 4, 8 ]; - o = strides2offset( sh, st ); - - /* - * [ - * [ - * [ 1.0, 2.0 ], - * [ 3.0, 4.0 ] - * ], - * [ - * [ 5.0, 6.0 ], - * [ 7.0, 8.0 ] - * ] - * ] - */ - xbuf = new Float64Array( [ 1.0, NaN, 5.0, NaN, 3.0, NaN, 7.0, NaN, 2.0, NaN, 6.0, NaN, 4.0, NaN, 8.0, NaN ] ); - x = new ndarray( dt, xbuf, sh, st, o, ord ); - - y = flattenBy( x, clbk ); - expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - t.end(); + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = [ 2, 4, 8 ]; + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, NaN, 5.0, NaN, 3.0, NaN, 7.0, NaN, 2.0, NaN, 6.0, NaN, 4.0, NaN, 8.0, NaN ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flattenBy( x, clbk ); + expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); }); tape( 'the function supports specifying the maximum number of dimensions to flatten (row-major)', function test( t ) { - var expected; - var xbuf; - var opts; - var ord; - var sh; - var st; - var dt; - var o; - var x; - var y; - - dt = 'float64'; - ord = 'row-major'; - sh = [ 2, 2, 2 ]; - st = shape2strides( sh, ord ); - o = strides2offset( sh, st ); - - /* - * [ - * [ - * [ 1.0, 2.0 ], - * [ 3.0, 4.0 ] - * ], - * [ - * [ 5.0, 6.0 ], - * [ 7.0, 8.0 ] - * ] - * ] - */ - xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); - x = new ndarray( dt, xbuf, sh, st, o, ord ); - - opts = { - 'depth': 0 - }; - y = flattenBy( x, opts, clbk ); - expected = [ - [ - [ 2.0, 3.0 ], - [ 4.0, 5.0 ] - ], - [ - [ 6.0, 7.0 ], - [ 8.0, 9.0 ] - ] - ]; - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - opts = { - 'depth': 1 - }; - y = flattenBy( x, opts, clbk ); - expected = [ - [ 2.0, 3.0 ], - [ 4.0, 5.0 ], - [ 6.0, 7.0 ], - [ 8.0, 9.0 ] - ]; - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - opts = { - 'depth': 2 - }; - y = flattenBy( x, opts, clbk ); - expected = [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]; - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - t.end(); + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'depth': 0 + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ + [ 2.0, 3.0 ], + [ 4.0, 5.0 ] + ], + [ + [ 6.0, 7.0 ], + [ 8.0, 9.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + opts = { + 'depth': 1 + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ 2.0, 3.0 ], + [ 4.0, 5.0 ], + [ 6.0, 7.0 ], + [ 8.0, 9.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + opts = { + 'depth': 2 + }; + y = flattenBy( x, opts, clbk ); + expected = [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); }); tape( 'the function supports specifying the maximum number of dimensions to flatten (column-major)', function test( t ) { - var expected; - var xbuf; - var opts; - var ord; - var sh; - var st; - var dt; - var o; - var x; - var y; - - dt = 'float64'; - ord = 'column-major'; - sh = [ 2, 2, 2 ]; - st = shape2strides( sh, ord ); - o = strides2offset( sh, st ); - - /* - * [ - * [ - * [ 1.0, 2.0 ], - * [ 3.0, 4.0 ] - * ], - * [ - * [ 5.0, 6.0 ], - * [ 7.0, 8.0 ] - * ] - * ] - */ - xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); - x = new ndarray( dt, xbuf, sh, st, o, ord ); - - opts = { - 'depth': 0 - }; - y = flattenBy( x, opts, clbk ); - expected = [ - [ - [ 2.0, 3.0 ], - [ 4.0, 5.0 ] - ], - [ - [ 6.0, 7.0 ], - [ 8.0, 9.0 ] - ] - ]; - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - opts = { - 'depth': 1 - }; - y = flattenBy( x, opts, clbk ); - expected = [ - [ 2.0, 3.0 ], - [ 4.0, 5.0 ], - [ 6.0, 7.0 ], - [ 8.0, 9.0 ] - ]; - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - opts = { - 'depth': 2 - }; - y = flattenBy( x, opts, clbk ); - expected = [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]; - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - t.end(); + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'depth': 0 + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ + [ 2.0, 3.0 ], + [ 4.0, 5.0 ] + ], + [ + [ 6.0, 7.0 ], + [ 8.0, 9.0 ] + ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + opts = { + 'depth': 1 + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ 2.0, 3.0 ], + [ 4.0, 5.0 ], + [ 6.0, 7.0 ], + [ 8.0, 9.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + opts = { + 'depth': 2 + }; + y = flattenBy( x, opts, clbk ); + expected = [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); }); tape( 'the function supports flattening a provided input ndarray in lexicographic order (row-major)', function test( t ) { - var expected; - var xbuf; - var opts; - var ord; - var sh; - var st; - var dt; - var o; - var x; - var y; - - dt = 'float64'; - ord = 'row-major'; - sh = [ 2, 2, 2 ]; - st = shape2strides( sh, ord ); - o = strides2offset( sh, st ); - - /* - * [ - * [ - * [ 1.0, 2.0 ], - * [ 3.0, 4.0 ] - * ], - * [ - * [ 5.0, 6.0 ], - * [ 7.0, 8.0 ] - * ] - * ] - */ - xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); - x = new ndarray( dt, xbuf, sh, st, o, ord ); - - opts = { - 'order': 'row-major' - }; - y = flattenBy( x, opts, clbk ); - expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - opts = { - 'depth': 1, - 'order': 'row-major' - }; - y = flattenBy( x, opts, clbk ); - expected = [ - [ 2.0, 3.0 ], - [ 4.0, 5.0 ], - [ 6.0, 7.0 ], - [ 8.0, 9.0 ] - ]; - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - t.end(); + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'row-major' + }; + y = flattenBy( x, opts, clbk ); + expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + opts = { + 'depth': 1, + 'order': 'row-major' + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ 2.0, 3.0 ], + [ 4.0, 5.0 ], + [ 6.0, 7.0 ], + [ 8.0, 9.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); }); tape( 'the function supports flattening a provided input ndarray in lexicographic order (column-major)', function test( t ) { - var expected; - var xbuf; - var opts; - var ord; - var sh; - var st; - var dt; - var o; - var x; - var y; - - dt = 'float64'; - ord = 'column-major'; - sh = [ 2, 2, 2 ]; - st = shape2strides( sh, ord ); - o = strides2offset( sh, st ); - - /* - * [ - * [ - * [ 1.0, 2.0 ], - * [ 3.0, 4.0 ] - * ], - * [ - * [ 5.0, 6.0 ], - * [ 7.0, 8.0 ] - * ] - * ] - */ - xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); - x = new ndarray( dt, xbuf, sh, st, o, ord ); - - opts = { - 'order': 'row-major' - }; - y = flattenBy( x, opts, clbk ); - expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - opts = { - 'depth': 1, - 'order': 'row-major' - }; - y = flattenBy( x, opts, clbk ); - expected = [ - [ 2.0, 3.0 ], - [ 4.0, 5.0 ], - [ 6.0, 7.0 ], - [ 8.0, 9.0 ] - ]; - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - t.end(); + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'row-major' + }; + y = flattenBy( x, opts, clbk ); + expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + opts = { + 'depth': 1, + 'order': 'row-major' + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ 2.0, 3.0 ], + [ 4.0, 5.0 ], + [ 6.0, 7.0 ], + [ 8.0, 9.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a provided input ndarray in colexicographic order (row-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'column-major' + }; + y = flattenBy( x, opts, clbk ); + expected = new Float64Array( [ 2.0, 6.0, 4.0, 8.0, 3.0, 7.0, 5.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + opts = { + 'depth': 1, + 'order': 'column-major' + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ 2.0, 3.0 ], + [ 6.0, 7.0 ], + [ 4.0, 5.0 ], + [ 8.0, 9.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a provided input ndarray in colexicographic order (column-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'column-major' + }; + y = flattenBy( x, opts, clbk ); + expected = new Float64Array( [ 2.0, 6.0, 4.0, 8.0, 3.0, 7.0, 5.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + opts = { + 'depth': 1, + 'order': 'column-major' + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ 2.0, 3.0 ], + [ 6.0, 7.0 ], + [ 4.0, 5.0 ], + [ 8.0, 9.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a provided input ndarray in same order as the input ndarray (row-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'same' + }; + y = flattenBy( x, opts, clbk ); + expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + opts = { + 'depth': 1, + 'order': 'same' + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ 2.0, 3.0 ], + [ 4.0, 5.0 ], + [ 6.0, 7.0 ], + [ 8.0, 9.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a provided input ndarray in same order as the input ndarray (column-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'same' + }; + y = flattenBy( x, opts, clbk ); + expected = new Float64Array( [ 2.0, 6.0, 4.0, 8.0, 3.0, 7.0, 5.0, 9.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + opts = { + 'depth': 1, + 'order': 'same' + }; + y = flattenBy( x, opts, clbk ); + expected = [ + [ 2.0, 3.0 ], + [ 6.0, 7.0 ], + [ 4.0, 5.0 ], + [ 8.0, 9.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); }); From d8b8cdbab63b71bb13b6498ac68df390f8c2aeb1 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 14 Sep 2025 23:49:35 +0500 Subject: [PATCH 03/22] docs: apply review suggestion Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts index a9ed59eef273..0c2812f0f983 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2024 The Stdlib Authors. +* 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. From 5465edd0a8fba991f1f32e7486d40ba5b7dc1c76 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 14 Sep 2025 23:56:40 +0500 Subject: [PATCH 04/22] docs: apply suggestions from code review Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- .../@stdlib/ndarray/flatten-by/README.md | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md index c8ebe3392442..8212b67a1f45 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md @@ -51,7 +51,7 @@ function scale( value ) { var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); // returns -var y = flatten( x, scale ); +var y = flattenBy( x, scale ); // returns var arr = ndarray2array( y ); @@ -91,9 +91,10 @@ function scale( value ) { var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); // returns -var y = flatten( x, { - 'depth': 1 -}); +var opts = { + 'depth: 1 +}; +var y = flatten( x, opts, scale ); // returns var arr = ndarray2array( y ); @@ -113,9 +114,11 @@ function scale( value ) { var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); // returns -var y = flatten( x, { - 'order': 'column-major' -}); +var opts = { + 'order': 'column-major' +}; + +var y = flatten( x, opts, scale ); // returns var arr = ndarray2array( y ); @@ -142,7 +145,7 @@ var ctx = { 'count': 0 }; -var y = flatten( x, scale, ctx ); +var y = flattenBy( x, scale, ctx ); // returns var arr = ndarray2array( y ); @@ -198,7 +201,7 @@ var x = array( xbuf, { }); console.log( ndarray2array( x ) ); -var y = flatten( x, scale ); +var y = flattenBy( x, scale ); console.log( ndarray2array( y ) ); ``` From aea5ad2a27d6ebb0ba807ef325fa72851e12227c Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 14 Sep 2025 23:59:54 +0500 Subject: [PATCH 05/22] refactor: apply suggestions from code review Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- .../@stdlib/ndarray/flatten-by/benchmark/benchmark.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/flatten-by/benchmark/benchmark.js index 3a944024eb6a..857667a08525 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/benchmark/benchmark.js @@ -53,7 +53,6 @@ bench( pkg+'::2d:row-major', function benchmark( b ) { zeros( 'float64', [ 10, 10 ], 'row-major' ), zeros( 'float32', [ 10, 10 ], 'row-major' ), zeros( 'int32', [ 10, 10 ], 'row-major' ), - zeros( 'complex128', [ 10, 10 ], 'row-major' ), zeros( 'generic', [ 10, 10 ], 'row-major' ) ]; opts = { @@ -88,7 +87,6 @@ bench( pkg+'::2d:column-major', function benchmark( b ) { zeros( 'float64', [ 10, 10 ], 'row-major' ), zeros( 'float32', [ 10, 10 ], 'row-major' ), zeros( 'int32', [ 10, 10 ], 'row-major' ), - zeros( 'complex128', [ 10, 10 ], 'row-major' ), zeros( 'generic', [ 10, 10 ], 'row-major' ) ]; opts = { @@ -123,7 +121,6 @@ bench( pkg+'::3d:row-major', function benchmark( b ) { zeros( 'float64', [ 2, 5, 10 ], 'row-major' ), zeros( 'float32', [ 2, 5, 10 ], 'row-major' ), zeros( 'int32', [ 2, 5, 10 ], 'row-major' ), - zeros( 'complex128', [ 2, 5, 10 ], 'row-major' ), zeros( 'generic', [ 2, 5, 10 ], 'row-major' ) ]; opts = { @@ -158,7 +155,6 @@ bench( pkg+'::3d:column-major', function benchmark( b ) { zeros( 'float64', [ 2, 5, 10 ], 'row-major' ), zeros( 'float32', [ 2, 5, 10 ], 'row-major' ), zeros( 'int32', [ 2, 5, 10 ], 'row-major' ), - zeros( 'complex128', [ 2, 5, 10 ], 'row-major' ), zeros( 'generic', [ 2, 5, 10 ], 'row-major' ) ]; opts = { @@ -193,7 +189,6 @@ bench( pkg+'::4d:row-major', function benchmark( b ) { zeros( 'float64', [ 2, 5, 2, 5 ], 'row-major' ), zeros( 'float32', [ 2, 5, 2, 5 ], 'row-major' ), zeros( 'int32', [ 2, 5, 2, 5 ], 'row-major' ), - zeros( 'complex128', [ 2, 5, 2, 5 ], 'row-major' ), zeros( 'generic', [ 2, 5, 2, 5 ], 'row-major' ) ]; opts = { @@ -228,7 +223,6 @@ bench( pkg+'::4d:column-major', function benchmark( b ) { zeros( 'float64', [ 2, 5, 2, 5 ], 'row-major' ), zeros( 'float32', [ 2, 5, 2, 5 ], 'row-major' ), zeros( 'int32', [ 2, 5, 2, 5 ], 'row-major' ), - zeros( 'complex128', [ 2, 5, 2, 5 ], 'row-major' ), zeros( 'generic', [ 2, 5, 2, 5 ], 'row-major' ) ]; opts = { @@ -263,7 +257,6 @@ bench( pkg+'::5d:row-major', function benchmark( b ) { zeros( 'float64', [ 2, 5, 2, 5, 1 ], 'row-major' ), zeros( 'float32', [ 2, 5, 2, 5, 1 ], 'row-major' ), zeros( 'int32', [ 2, 5, 2, 5, 1 ], 'row-major' ), - zeros( 'complex128', [ 2, 5, 2, 5, 1 ], 'row-major' ), zeros( 'generic', [ 2, 5, 2, 5, 1 ], 'row-major' ) ]; opts = { @@ -298,7 +291,6 @@ bench( pkg+'::5d:column-major', function benchmark( b ) { zeros( 'float64', [ 2, 5, 2, 5, 1 ], 'row-major' ), zeros( 'float32', [ 2, 5, 2, 5, 1 ], 'row-major' ), zeros( 'int32', [ 2, 5, 2, 5, 1 ], 'row-major' ), - zeros( 'complex128', [ 2, 5, 2, 5, 1 ], 'row-major' ), zeros( 'generic', [ 2, 5, 2, 5, 1 ], 'row-major' ) ]; opts = { From 2ea0120414f72fc16c6ec187999927f8e0feb6ed Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 14 Sep 2025 19:08:10 +0000 Subject: [PATCH 06/22] docs: apply suggestions from code review --- .../@stdlib/ndarray/flatten-by/README.md | 9 +++++---- .../ndarray/flatten-by/docs/types/index.d.ts | 14 ++++---------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md index 8212b67a1f45..23b98cf10e2d 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md @@ -92,9 +92,10 @@ var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); // returns var opts = { - 'depth: 1 + 'depth': 1 }; -var y = flatten( x, opts, scale ); + +var y = flattenBy( x, opts, scale ); // returns var arr = ndarray2array( y ); @@ -115,10 +116,10 @@ var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); // returns var opts = { - 'order': 'column-major' + 'order': 'column-major' }; -var y = flatten( x, opts, scale ); +var y = flattenBy( x, opts, scale ); // returns var arr = ndarray2array( y ); diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts index dd01fd443d0e..6cfd06a3a91c 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts @@ -113,7 +113,7 @@ interface Options { * function scale( value ) { * return value * 2.0; * } -* +* * var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); * var shape = [ 3, 1, 2 ]; * var strides = [ 2, 2, 1 ]; @@ -146,7 +146,7 @@ declare function flattenBy( x: float64ndarray, fcn: Callback( x: float32ndarray, fcn: Callback( x: complex64ndarray, fcn: Callback( x: genericnda * function scale( value ) { * return value * 2.0; * } -* +* * var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); * var shape = [ 3, 1, 2 ]; * var strides = [ 2, 2, 1 ]; @@ -584,7 +578,7 @@ declare function flattenBy( x: float64ndarray, options: Options, fc * function scale( value ) { * return value * 2.0; * } -* +* * var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); * var shape = [ 3, 1, 2 ]; * var strides = [ 2, 2, 1 ]; From 08134c46a7b418442713d08b1e902e47cad8e95e Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 14 Sep 2025 19:09:35 +0000 Subject: [PATCH 07/22] docs: apply suggestions from code review --- .../@stdlib/ndarray/flatten-by/docs/types/index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts index 6cfd06a3a91c..0b5ded998201 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts @@ -20,6 +20,8 @@ /// +/* eslint-disable max-lines */ + import { float64ndarray, float32ndarray, complex128ndarray, complex64ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, boolndarray, genericndarray, Order } from '@stdlib/types/ndarray'; import { Complex64, Complex128, ComplexLike } from '@stdlib/types/complex'; From 3af01ed7243ee389adbbe31df5d69a6f8bb6f4a5 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 14 Sep 2025 19:15:58 +0000 Subject: [PATCH 08/22] docs: apply suggestions from code review --- lib/node_modules/@stdlib/ndarray/flatten-by/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md index 23b98cf10e2d..cf4ef718840c 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md @@ -123,7 +123,7 @@ var y = flattenBy( x, opts, scale ); // returns var arr = ndarray2array( y ); -// returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] +// returns [ 2.0, 6.0, 4.0, 10.0, 8.0, 12.0 ] ``` To set the callback function execution context, provide a `thisArg`. From 49533b74c97da89a2290e2063b54e497cddaba52 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Mon, 15 Sep 2025 00:19:54 +0500 Subject: [PATCH 09/22] docs: apply suggestions from code review Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- lib/node_modules/@stdlib/ndarray/flatten-by/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md index cf4ef718840c..3b4a2a087e96 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md @@ -123,7 +123,7 @@ var y = flattenBy( x, opts, scale ); // returns var arr = ndarray2array( y ); -// returns [ 2.0, 6.0, 4.0, 10.0, 8.0, 12.0 ] +// returns [ 2.0, 6.0, 10.0, 4.0, 8.0, 12.0 ] ``` To set the callback function execution context, provide a `thisArg`. From 959a92d40011b3c8ef11da47dc80cf0c30d451db Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Tue, 16 Sep 2025 08:20:26 +0000 Subject: [PATCH 10/22] refactor: apply suggestions from code review --- .../@stdlib/ndarray/flatten-by/README.md | 4 +- .../@stdlib/ndarray/flatten-by/docs/repl.txt | 3 +- .../ndarray/flatten-by/docs/types/index.d.ts | 52 ++++++------ .../@stdlib/ndarray/flatten-by/lib/index.js | 4 +- .../@stdlib/ndarray/flatten-by/lib/main.js | 82 ++++++++++++++++--- .../@stdlib/ndarray/flatten-by/package.json | 2 +- 6 files changed, 103 insertions(+), 44 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md index 3b4a2a087e96..4c60f9dda8f9 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md @@ -20,7 +20,7 @@ limitations under the License. # flattenBy -> Apply a callback to elements in an input [ndarray][@stdlib/ndarray/ctor] and return a flattened output [ndarray][@stdlib/ndarray/ctor]. +> Flatten an [ndarray][@stdlib/ndarray/ctor] according to a callback function.
@@ -38,7 +38,7 @@ var flattenBy = require( '@stdlib/ndarray/flatten-by' ); #### flattenBy( x\[, options], fcn[, thisArg] ) -Applies a callback to elements in an input [ndarray][@stdlib/ndarray/ctor] and returns a flattened output [ndarray][@stdlib/ndarray/ctor]. +Flattens an [ndarray][@stdlib/ndarray/ctor] according to a callback function. ```javascript var array = require( '@stdlib/ndarray/array' ); diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/repl.txt index b760777e183a..9db607b8d160 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/repl.txt @@ -1,7 +1,6 @@ {{alias}}( x[, options], fcn[, thisArg] ) - Applies a callback to elements in an input ndarray and returns a flattened - output ndarray. + Flattens an ndarray according to a callback function. Parameters ---------- diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts index 0b5ded998201..0d3f2d937ae6 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts @@ -100,7 +100,7 @@ interface Options { } /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param fcn - callback function @@ -133,7 +133,7 @@ interface Options { declare function flattenBy( x: float64ndarray, fcn: Callback, thisArg?: ThisParameterType> ): float64ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param fcn - callback function @@ -166,7 +166,7 @@ declare function flattenBy( x: float64ndarray, fcn: Callback( x: float32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): float32ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param fcn - callback function @@ -196,7 +196,7 @@ declare function flattenBy( x: float32ndarray, fcn: Callback( x: complex64ndarray, fcn: Callback, thisArg?: ThisParameterType> ): complex64ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param fcn - callback function @@ -226,7 +226,7 @@ declare function flattenBy( x: complex64ndarray, fcn: Callback( x: complex128ndarray, fcn: Callback, thisArg?: ThisParameterType> ): complex128ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param fcn - callback function @@ -259,7 +259,7 @@ declare function flattenBy( x: complex128ndarray, fcn: Callback( x: int32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): int32ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param fcn - callback function @@ -292,7 +292,7 @@ declare function flattenBy( x: int32ndarray, fcn: Callback( x: int16ndarray, fcn: Callback, thisArg?: ThisParameterType> ): int16ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param fcn - callback function @@ -325,7 +325,7 @@ declare function flattenBy( x: int16ndarray, fcn: Callback( x: int8ndarray, fcn: Callback, thisArg?: ThisParameterType> ): int8ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param fcn - callback function @@ -358,7 +358,7 @@ declare function flattenBy( x: int8ndarray, fcn: Callback( x: uint32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint32ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param fcn - callback function @@ -391,7 +391,7 @@ declare function flattenBy( x: uint32ndarray, fcn: Callback( x: uint16ndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint16ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param fcn - callback function @@ -424,7 +424,7 @@ declare function flattenBy( x: uint16ndarray, fcn: Callback( x: uint8ndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint8ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param fcn - callback function @@ -457,7 +457,7 @@ declare function flattenBy( x: uint8ndarray, fcn: Callback( x: uint8cndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint8cndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param fcn - callback function @@ -490,7 +490,7 @@ declare function flattenBy( x: uint8cndarray, fcn: Callback( x: boolndarray, fcn: Callback, thisArg?: ThisParameterType> ): boolndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param fcn - callback function @@ -522,7 +522,7 @@ declare function flattenBy( x: boolndarray, fcn: Callback( x: genericndarray, fcn: Callback, V, W>, thisArg?: ThisParameterType, V, W>> ): genericndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param options - function options @@ -562,7 +562,7 @@ declare function flattenBy( x: genericnda declare function flattenBy( x: float64ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): float64ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param options - function options @@ -602,7 +602,7 @@ declare function flattenBy( x: float64ndarray, options: Options, fc declare function flattenBy( x: float32ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): float32ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param options - function options @@ -639,7 +639,7 @@ declare function flattenBy( x: float32ndarray, options: Options, fc declare function flattenBy( x: complex64ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): complex64ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param options - function options @@ -676,7 +676,7 @@ declare function flattenBy( x: complex64ndarray, options: Options, declare function flattenBy( x: complex128ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): complex128ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param options - function options @@ -716,7 +716,7 @@ declare function flattenBy( x: complex128ndarray, options: Options, declare function flattenBy( x: int32ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): int32ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param options - function options @@ -756,7 +756,7 @@ declare function flattenBy( x: int32ndarray, options: Options, fcn: declare function flattenBy( x: int16ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): int16ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param options - function options @@ -796,7 +796,7 @@ declare function flattenBy( x: int16ndarray, options: Options, fcn: declare function flattenBy( x: int8ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): int8ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param options - function options @@ -836,7 +836,7 @@ declare function flattenBy( x: int8ndarray, options: Options, fcn: declare function flattenBy( x: uint32ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): uint32ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param options - function options @@ -876,7 +876,7 @@ declare function flattenBy( x: uint32ndarray, options: Options, fcn declare function flattenBy( x: uint16ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): uint16ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param options - function options @@ -916,7 +916,7 @@ declare function flattenBy( x: uint16ndarray, options: Options, fcn declare function flattenBy( x: uint8ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): uint8ndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param options - function options @@ -956,7 +956,7 @@ declare function flattenBy( x: uint8ndarray, options: Options, fcn: declare function flattenBy( x: uint8cndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): uint8cndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param options - function options @@ -996,7 +996,7 @@ declare function flattenBy( x: uint8cndarray, options: Options, fcn declare function flattenBy( x: boolndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): boolndarray; /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param x - input ndarray * @param options - function options diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js index e5d9dea50188..79d65e0ca80a 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js @@ -19,10 +19,10 @@ 'use strict'; /** -* Apply a callback to elements in an input ndarray and return a flattened output ndarray. +* Flatten an ndarray according to a callback function. * * @module @stdlib/ndarray/flatten-by -* +*s * @example * var array = require( '@stdlib/ndarray/array' ); * var ndarray2array = require( '@stdlib/ndarray/to-array' ); diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js index f101d4925ca7..401484a8ad18 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js @@ -23,15 +23,28 @@ var isPlainObject = require( '@stdlib/assert/is-plain-object' ); var isFunction = require( '@stdlib/assert/is-function' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); -var map = require( '@stdlib/ndarray/map' ); -var flatten = require( '@stdlib/ndarray/flatten' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ); +var isOrder = require( '@stdlib/ndarray/base/assert/is-order' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getStrides = require( '@stdlib/ndarray/strides' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var getDType = require( '@stdlib/ndarray/base/dtype' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var strides2order = require( '@stdlib/ndarray/base/strides2order' ); +var flattenShape = require( '@stdlib/ndarray/base/flatten-shape' ); +var map = require( '@stdlib/ndarray/base/map' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); var format = require( '@stdlib/string/format' ); // MAIN // /** -* Applies a callback to elements in an input ndarray and returns a flattened output ndarray. +* Flattens an ndarray according to a callback function. * * @param {ndarray} x - input ndarray * @param {Options} [options] - function options @@ -71,10 +84,18 @@ function flattenBy( x, options, fcn, thisArg ) { if ( !isndarrayLike( x ) ) { throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); } - opts = {}; - if ( arguments.length < 3 ) { // Case: flattenBy( x, fcn ) + nargs = arguments.length; + xsh = getShape( x ); + + // Define default options: + opts = { + 'depth': xsh.length, // by default, flatten to a one-dimensional ndarray + 'order': ROW_MAJOR // by default, flatten in lexicographic order (i.e., trailing dimensions first; e.g., if `x` is a matrix, flatten row-by-row) + }; + + if ( nargs <= 2 ) { // Case: flattenBy( x, fcn ) cb = options; - } else if ( arguments.length === 3 ) { + } else if ( nargs <= 3 ) { if ( isFunction( options ) ) { // Case: flattenBy( x, fcn, thisArg ) cb = options; ctx = fcn; @@ -82,22 +103,61 @@ function flattenBy( x, options, fcn, thisArg ) { if ( !isPlainObject( options ) ) { throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); } - opts = options; cb = fcn; } - } else { + } else { // Case: flattenBy( x, options, fcn, thisArg ) if ( !isPlainObject( options ) ) { throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); } - opts = options; cb = fcn; ctx = thisArg; } if ( !isFunction( cb ) ) { throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) ); } - y = map( x, cb, ctx ); - return flatten( y, opts ); + if ( hasOwnProp( options, 'depth' ) ) { + if ( !isNonNegativeInteger( options.depth ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', options.depth ) ); + } + opts.depth = options.depth; + } + if ( hasOwnProp( options, 'order' ) ) { + if ( options.order === 'any' ) { + // When 'any', we want to flatten according to the physical layout of the data in memory... + o = strides2order( getStrides( x ) ); + if ( o === 1 ) { + // Data is currently arranged in row-major order: + opts.order = ROW_MAJOR; + } else if ( o === 2 ) { + // Data is currently arranged in column-major order: + opts.order = COL_MAJOR; + } else { // o === 0 || o === 3 (i.e., neither row- nor column-major || both row- and column-major + // When the data is either both row- and column-major (e.g., a one-dimensional ndarray) or neither row- nor column-major (e.g., unordered strides), fallback to flattening according to the stated order of the input ndarray: + opts.order = getOrder( x ); + } + } else if ( options.order === 'same' ) { + // When 'same', we want to flatten according to the stated order of the input ndarray: + opts.order = getOrder( x ); + } else if ( isOrder( options.order ) ) { + // When provided a specific order, flatten according to that order regardless of the order of the input ndarray: + opts.order = options.order; + } else { + throw new TypeError( format( 'invalid option. `%s` option must be a recognized order. Option: `%s`.', 'order', options.order ) ); + } + } + // Create an output ndarray having contiguous memory: + y = emptyLike( x, { + 'shape': flattenShape( xsh, opts.depth ), + 'order': opts.order + }); + + // Create a view on top of output ndarray having the same shape as the input ndarray: + st = ( xsh.length > 0 ) ? shape2strides( xsh, opts.order ) : [ 0 ]; + view = ndarray( getDType( y ), getData( y ), xsh, st, 0, opts.order ); + + // Transform and assign elements to the output ndarray: + map( [ x, view ], cb, ctx ); + return y; } diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/package.json b/lib/node_modules/@stdlib/ndarray/flatten-by/package.json index 6710105cdaca..1317829b37c8 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/package.json +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/ndarray/flatten-by", "version": "0.0.0", - "description": "Apply a callback to elements in an input ndarray and return a flattened output ndarray.", + "description": "Flatten an ndarray according to a callback function.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", From cc413a819a7a5fc8c84f9933b55e144c0882dc7d Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Tue, 16 Sep 2025 08:27:27 +0000 Subject: [PATCH 11/22] docs: addd note --- lib/node_modules/@stdlib/ndarray/flatten-by/README.md | 2 ++ lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js | 1 + 2 files changed, 3 insertions(+) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md index 4c60f9dda8f9..cad827797a26 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md @@ -172,6 +172,8 @@ var count = ctx.count; - **indices**: current array element indices. - **arr**: the input [ndarray][@stdlib/ndarray/ctor]. +- The traversal [order][@stdlib/ndarray/orders] of array elements depends on the memory layout of both the input and output ndarrays. As a result, the callback is not guaranteed to receive elements in the view [order][@stdlib/ndarray/orders]. +
diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js index 401484a8ad18..9c2e33311a7e 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js @@ -76,6 +76,7 @@ var format = require( '@stdlib/string/format' ); * // returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] */ function flattenBy( x, options, fcn, thisArg ) { + var nargs; var opts; var ctx; var cb; From 6c55278d95de2dd0b4c26234b022098a2b7dd983 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Tue, 16 Sep 2025 08:41:11 +0000 Subject: [PATCH 12/22] refactor: apply suggestions from code review --- lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js index 9c2e33311a7e..56e4bf43ae79 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js @@ -40,6 +40,11 @@ var emptyLike = require( '@stdlib/ndarray/empty-like' ); var ndarray = require( '@stdlib/ndarray/ctor' ); var format = require( '@stdlib/string/format' ); +// VARIABLES // + +var ROW_MAJOR = 'row-major'; +var COL_MAJOR = 'column-major'; + // MAIN // @@ -77,10 +82,14 @@ var format = require( '@stdlib/string/format' ); */ function flattenBy( x, options, fcn, thisArg ) { var nargs; + var view; var opts; var ctx; + var xsh; var cb; + var st; var y; + var o; if ( !isndarrayLike( x ) ) { throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); From b62117ff19cc7b387f3b84895e95918b0cae6132 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Tue, 16 Sep 2025 08:46:15 +0000 Subject: [PATCH 13/22] test: add missing test cases --- .../@stdlib/ndarray/flatten-by/lib/index.js | 2 +- .../@stdlib/ndarray/flatten-by/test/test.js | 63 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js index 79d65e0ca80a..0ed4e631e02b 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js @@ -22,7 +22,7 @@ * Flatten an ndarray according to a callback function. * * @module @stdlib/ndarray/flatten-by -*s +* * @example * var array = require( '@stdlib/ndarray/array' ); * var ndarray2array = require( '@stdlib/ndarray/to-array' ); diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js index 376bb2af2b73..f21f1bb730cd 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js @@ -139,6 +139,69 @@ tape( 'the function throws an error if provided an options argument which is not } }); +tape( 'the function throws an error if provided an invalid `depth` option', function test( t ) { + var values; + var opts; + var i; + + values = [ + '5', + -5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + opts = { + 'depth': value + }; + flattenBy( zeros( [ 2 ] ), opts, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid `order` option', function test( t ) { + var values; + var opts; + var i; + + values = [ + '5', + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + opts = { + 'order': value + }; + flattenBy( zeros( [ 2 ] ), opts, clbk ); + }; + } +}); + tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, contiguous)', function test( t ) { var expected; var xbuf; From a530c29eb545facc042060f1ce0ee371d7202bda Mon Sep 17 00:00:00 2001 From: headlessNode Date: Tue, 16 Sep 2025 14:42:15 +0500 Subject: [PATCH 14/22] docs: remove spaces --- 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: 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: na - task: lint_license_headers status: passed --- --- .../ndarray/flatten-by/benchmark/benchmark.js | 2 +- .../ndarray/flatten-by/docs/types/index.d.ts | 70 +++++++++---------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/flatten-by/benchmark/benchmark.js index 857667a08525..85beef563891 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/benchmark/benchmark.js @@ -36,7 +36,7 @@ var flattenBy = require( './../lib' ); * @returns {number} output value */ function clbk( value ) { - return value + 1; + return value + 1; } diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts index 0d3f2d937ae6..b83f67605b81 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts @@ -186,7 +186,7 @@ declare function flattenBy( x: float32ndarray, fcn: Callback * @@ -216,7 +216,7 @@ declare function flattenBy( x: complex64ndarray, fcn: Callback * @@ -246,7 +246,7 @@ declare function flattenBy( x: complex128ndarray, fcn: Callback * @@ -279,7 +279,7 @@ declare function flattenBy( x: int32ndarray, fcn: Callback * @@ -312,7 +312,7 @@ declare function flattenBy( x: int16ndarray, fcn: Callback * @@ -345,7 +345,7 @@ declare function flattenBy( x: int8ndarray, fcn: Callback * @@ -378,7 +378,7 @@ declare function flattenBy( x: uint32ndarray, fcn: Callback * @@ -411,7 +411,7 @@ declare function flattenBy( x: uint16ndarray, fcn: Callback * @@ -444,7 +444,7 @@ declare function flattenBy( x: uint8ndarray, fcn: Callback * @@ -477,7 +477,7 @@ declare function flattenBy( x: uint8cndarray, fcn: Callback * @@ -509,7 +509,7 @@ declare function flattenBy( x: boolndarray, fcn: Callback * @@ -552,7 +552,7 @@ declare function flattenBy( x: genericnda * var opts = { * 'depth': 1 * }; -* +* * var y = flattenBy( x, opts, scale ); * // returns * @@ -592,7 +592,7 @@ declare function flattenBy( x: float64ndarray, options: Options, fc * var opts = { * 'depth': 1 * }; -* +* * var y = flattenBy( x, opts, scale ); * // returns * @@ -625,14 +625,14 @@ declare function flattenBy( x: float32ndarray, options: Options, fc * var shape = [ 1 , 3 ]; * var strides = [ 3, 1 ]; * var offset = 0; -* +* * var x = ndarray( 'complex64', buffer, shape, strides, offset, 'row-major' ); * // return * * var opts = { * 'depth': 1 * }; -* +* * var y = flattenBy( x, opts, identity ); * // returns */ @@ -662,14 +662,14 @@ declare function flattenBy( x: complex64ndarray, options: Options, * var shape = [ 1 , 3 ]; * var strides = [ 3, 1 ]; * var offset = 0; -* +* * var x = ndarray( 'complex128', buffer, shape, strides, offset, 'row-major' ); * // return * * var opts = { * 'depth': 1 * }; -* +* * var y = flattenBy( x, opts, identity ); * // returns */ @@ -699,14 +699,14 @@ declare function flattenBy( x: complex128ndarray, options: Options, * var shape = [ 3, 1, 2 ]; * var strides = [ 2, 2, 1 ]; * var offset = 0; -* +* * var x = ndarray( 'int32', buffer, shape, strides, offset, 'row-major' ); * // return * * var opts = { * 'depth': 1 * }; -* +* * var y = flattenBy( x, opts, scale ); * // returns * @@ -739,14 +739,14 @@ declare function flattenBy( x: int32ndarray, options: Options, fcn: * var shape = [ 3, 1, 2 ]; * var strides = [ 2, 2, 1 ]; * var offset = 0; -* +* * var x = ndarray( 'int16', buffer, shape, strides, offset, 'row-major' ); * // return * * var opts = { * 'depth': 1 * }; -* +* * var y = flattenBy( x, opts, scale ); * // returns * @@ -779,14 +779,14 @@ declare function flattenBy( x: int16ndarray, options: Options, fcn: * var shape = [ 3, 1, 2 ]; * var strides = [ 2, 2, 1 ]; * var offset = 0; -* +* * var x = ndarray( 'int8', buffer, shape, strides, offset, 'row-major' ); * // return * * var opts = { * 'depth': 1 * }; -* +* * var y = flattenBy( x, opts, scale ); * // returns * @@ -819,14 +819,14 @@ declare function flattenBy( x: int8ndarray, options: Options, fcn: * var shape = [ 3, 1, 2 ]; * var strides = [ 2, 2, 1 ]; * var offset = 0; -* +* * var x = ndarray( 'uint32', buffer, shape, strides, offset, 'row-major' ); * // return * * var opts = { * 'depth': 1 * }; -* +* * var y = flattenBy( x, opts, scale ); * // returns * @@ -859,14 +859,14 @@ declare function flattenBy( x: uint32ndarray, options: Options, fcn * var shape = [ 3, 1, 2 ]; * var strides = [ 2, 2, 1 ]; * var offset = 0; -* +* * var x = ndarray( 'uint16', buffer, shape, strides, offset, 'row-major' ); * // return * * var opts = { * 'depth': 1 * }; -* +* * var y = flattenBy( x, opts, scale ); * // returns * @@ -899,14 +899,14 @@ declare function flattenBy( x: uint16ndarray, options: Options, fcn * var shape = [ 3, 1, 2 ]; * var strides = [ 2, 2, 1 ]; * var offset = 0; -* +* * var x = ndarray( 'uint8', buffer, shape, strides, offset, 'row-major' ); * // return * * var opts = { * 'depth': 1 * }; -* +* * var y = flattenBy( x, opts, scale ); * // returns * @@ -939,14 +939,14 @@ declare function flattenBy( x: uint8ndarray, options: Options, fcn: * var shape = [ 3, 1, 2 ]; * var strides = [ 2, 2, 1 ]; * var offset = 0; -* +* * var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); * // return * * var opts = { * 'depth': 1 * }; -* +* * var y = flattenBy( x, opts, scale ); * // returns * @@ -979,14 +979,14 @@ declare function flattenBy( x: uint8cndarray, options: Options, fcn * var shape = [ 3, 1, 2 ]; * var strides = [ 2, 2, 1 ]; * var offset = 0; -* +* * var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); * // return * * var opts = { * 'depth': 1 * }; -* +* * var y = flattenBy( x, opts, scale ); * // returns * @@ -1018,14 +1018,14 @@ declare function flattenBy( x: boolndarray, options: Options, fcn: * var shape = [ 3, 1, 2 ]; * var strides = [ 2, 2, 1 ]; * var offset = 0; -* +* * var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); * // return * * var opts = { * 'depth': 1 * }; -* +* * var y = flattenBy( x, opts, scale ); * // returns * From 0e6522c880c7346e625cc0587f9f78fc9a5d58dc Mon Sep 17 00:00:00 2001 From: headlessNode Date: Tue, 16 Sep 2025 14:47:22 +0500 Subject: [PATCH 15/22] fix: apply suggestions from code review --- 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: 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: na - task: lint_license_headers status: passed --- --- lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js index 56e4bf43ae79..7140fd4fbf8f 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js @@ -24,7 +24,6 @@ var isPlainObject = require( '@stdlib/assert/is-plain-object' ); var isFunction = require( '@stdlib/assert/is-function' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var hasOwnProp = require( '@stdlib/assert/has-own-property' ); -var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ); var isOrder = require( '@stdlib/ndarray/base/assert/is-order' ); var getShape = require( '@stdlib/ndarray/shape' ); @@ -40,6 +39,7 @@ var emptyLike = require( '@stdlib/ndarray/empty-like' ); var ndarray = require( '@stdlib/ndarray/ctor' ); var format = require( '@stdlib/string/format' ); + // VARIABLES // var ROW_MAJOR = 'row-major'; From 14302a6ca18a8b97fe65bc33292dfce6b611653e Mon Sep 17 00:00:00 2001 From: headlessNode Date: Tue, 16 Sep 2025 14:51:07 +0500 Subject: [PATCH 16/22] fix: apply suggestions from code review --- 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: passed - 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 --- --- .../@stdlib/ndarray/flatten-by/docs/repl.txt | 2 +- .../ndarray/flatten-by/docs/types/test.ts | 78 +++++++++---------- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/repl.txt index 9db607b8d160..f6cae4264419 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/repl.txt @@ -28,7 +28,7 @@ fcn: Function Callback function. - + thisArg: any (optional) Callback execution context. diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts index 0c2812f0f983..0cf72a5fd386 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts @@ -39,55 +39,55 @@ function identity( x: any ): any { flattenBy( zeros( 'float64', sh, ord ), identity ); // $ExpectType float64ndarray flattenBy( zeros( 'float64', sh, ord ), {}, identity ); // $ExpectType float64ndarray - flattenBy( zeros( 'float64', sh, ord ), identity, {} ); // $ExpectType float64ndarray - flattenBy( zeros( 'float64', sh, ord ), {}, identity, {} ); // $ExpectType float64ndarray + flattenBy( zeros( 'float64', sh, ord ), identity, {} ); // $ExpectType float64ndarray + flattenBy( zeros( 'float64', sh, ord ), {}, identity, {} ); // $ExpectType float64ndarray flattenBy( zeros( 'float32', sh, ord ), identity ); // $ExpectType float32ndarray flattenBy( zeros( 'float32', sh, ord ), {}, identity ); // $ExpectType float32ndarray - flattenBy( zeros( 'float32', sh, ord ), identity, {} ); // $ExpectType float32ndarray - flattenBy( zeros( 'float32', sh, ord ), {}, identity, {} ); // $ExpectType float32ndarray - flattenBy( zeros( 'complex64', sh, ord ), identity ); // $ExpectType complex64ndarray - flattenBy( zeros( 'complex64', sh, ord ), {}, identity ); // $ExpectType complex64ndarray - flattenBy( zeros( 'complex64', sh, ord ), identity, {} ); // $ExpectType complex64ndarray - flattenBy( zeros( 'complex64', sh, ord ), {}, identity, {} ); // $ExpectType complex64ndarray - flattenBy( zeros( 'complex128', sh, ord ), identity ); // $ExpectType complex128ndarray - flattenBy( zeros( 'complex128', sh, ord ), {}, identity ); // $ExpectType complex128ndarray - flattenBy( zeros( 'complex128', sh, ord ), identity, {} ); // $ExpectType complex128ndarray - flattenBy( zeros( 'complex128', sh, ord ), {}, identity, {} ); // $ExpectType complex128ndarray - flattenBy( zeros( 'int32', sh, ord ), identity ); // $ExpectType int32ndarray - flattenBy( zeros( 'int32', sh, ord ), {}, identity ); // $ExpectType int32ndarray - flattenBy( zeros( 'int32', sh, ord ), identity, {} ); // $ExpectType int32ndarray - flattenBy( zeros( 'int32', sh, ord ), {}, identity, {} ); // $ExpectType int32ndarray - flattenBy( zeros( 'int16', sh, ord ), identity ); // $ExpectType int16ndarray - flattenBy( zeros( 'int16', sh, ord ), {}, identity ); // $ExpectType int16ndarray + flattenBy( zeros( 'float32', sh, ord ), identity, {} ); // $ExpectType float32ndarray + flattenBy( zeros( 'float32', sh, ord ), {}, identity, {} ); // $ExpectType float32ndarray + flattenBy( zeros( 'complex64', sh, ord ), identity ); // $ExpectType complex64ndarray + flattenBy( zeros( 'complex64', sh, ord ), {}, identity ); // $ExpectType complex64ndarray + flattenBy( zeros( 'complex64', sh, ord ), identity, {} ); // $ExpectType complex64ndarray + flattenBy( zeros( 'complex64', sh, ord ), {}, identity, {} ); // $ExpectType complex64ndarray + flattenBy( zeros( 'complex128', sh, ord ), identity ); // $ExpectType complex128ndarray + flattenBy( zeros( 'complex128', sh, ord ), {}, identity ); // $ExpectType complex128ndarray + flattenBy( zeros( 'complex128', sh, ord ), identity, {} ); // $ExpectType complex128ndarray + flattenBy( zeros( 'complex128', sh, ord ), {}, identity, {} ); // $ExpectType complex128ndarray + flattenBy( zeros( 'int32', sh, ord ), identity ); // $ExpectType int32ndarray + flattenBy( zeros( 'int32', sh, ord ), {}, identity ); // $ExpectType int32ndarray + flattenBy( zeros( 'int32', sh, ord ), identity, {} ); // $ExpectType int32ndarray + flattenBy( zeros( 'int32', sh, ord ), {}, identity, {} ); // $ExpectType int32ndarray + flattenBy( zeros( 'int16', sh, ord ), identity ); // $ExpectType int16ndarray + flattenBy( zeros( 'int16', sh, ord ), {}, identity ); // $ExpectType int16ndarray flattenBy( zeros( 'int16', sh, ord ), identity, {} ); // $ExpectType int16ndarray flattenBy( zeros( 'int16', sh, ord ), {}, identity, {} ); // $ExpectType int16ndarray - flattenBy( zeros( 'int8', sh, ord ), identity ); // $ExpectType int8ndarray - flattenBy( zeros( 'int8', sh, ord ), {}, identity ); // $ExpectType int8ndarray + flattenBy( zeros( 'int8', sh, ord ), identity ); // $ExpectType int8ndarray + flattenBy( zeros( 'int8', sh, ord ), {}, identity ); // $ExpectType int8ndarray flattenBy( zeros( 'int8', sh, ord ), identity, {} ); // $ExpectType int8ndarray flattenBy( zeros( 'int8', sh, ord ), {}, identity, {} ); // $ExpectType int8ndarray flattenBy( zeros( 'uint32', sh, ord ), identity ); // $ExpectType uint32ndarray flattenBy( zeros( 'uint32', sh, ord ), {}, identity ); // $ExpectType uint32ndarray - flattenBy( zeros( 'uint32', sh, ord ), identity, {} ); // $ExpectType uint32ndarray + flattenBy( zeros( 'uint32', sh, ord ), identity, {} ); // $ExpectType uint32ndarray flattenBy( zeros( 'uint16', sh, ord ), identity ); // $ExpectType uint16ndarray flattenBy( zeros( 'uint16', sh, ord ), {}, identity ); // $ExpectType uint16ndarray - flattenBy( zeros( 'uint16', sh, ord ), identity, {} ); // $ExpectType uint16ndarray - flattenBy( zeros( 'uint16', sh, ord ), {}, identity, {} ); // $ExpectType uint16ndarray - flattenBy( zeros( 'uint8', sh, ord ), identity ); // $ExpectType uint8ndarray - flattenBy( zeros( 'uint8', sh, ord ), {}, identity ); // $ExpectType uint8ndarray - flattenBy( zeros( 'uint8', sh, ord ), identity, {} ); // $ExpectType uint8ndarray - flattenBy( zeros( 'uint8', sh, ord ), {}, identity, {} ); // $ExpectType uint8ndarray - flattenBy( zeros( 'uint8c', sh, ord ), identity ); // $ExpectType uint8cndarray - flattenBy( zeros( 'uint8c', sh, ord ), {}, identity ); // $ExpectType uint8cndarray - flattenBy( zeros( 'uint8c', sh, ord ), identity, {} ); // $ExpectType uint8cndarray - flattenBy( zeros( 'uint8c', sh, ord ), {}, identity, {} ); // $ExpectType uint8cndarray - flattenBy( empty( 'bool', sh, ord ), identity ); // $ExpectType boolndarray - flattenBy( empty( 'bool', sh, ord ), {}, identity ); // $ExpectType boolndarray - flattenBy( empty( 'bool', sh, ord ), identity, {} ); // $ExpectType boolndarray - flattenBy( empty( 'bool', sh, ord ), {}, identity, {} ); // $ExpectType boolndarray - flattenBy( zeros( 'generic', sh, ord ), identity ); // $ExpectType genericndarray - flattenBy( zeros( 'generic', sh, ord ), {}, identity ); // $ExpectType genericndarray - flattenBy( zeros( 'generic', sh, ord ), identity, {} ); // $ExpectType genericndarray - flattenBy( zeros( 'generic', sh, ord ), {}, identity, {} ); // $ExpectType genericndarray + flattenBy( zeros( 'uint16', sh, ord ), identity, {} ); // $ExpectType uint16ndarray + flattenBy( zeros( 'uint16', sh, ord ), {}, identity, {} ); // $ExpectType uint16ndarray + flattenBy( zeros( 'uint8', sh, ord ), identity ); // $ExpectType uint8ndarray + flattenBy( zeros( 'uint8', sh, ord ), {}, identity ); // $ExpectType uint8ndarray + flattenBy( zeros( 'uint8', sh, ord ), identity, {} ); // $ExpectType uint8ndarray + flattenBy( zeros( 'uint8', sh, ord ), {}, identity, {} ); // $ExpectType uint8ndarray + flattenBy( zeros( 'uint8c', sh, ord ), identity ); // $ExpectType uint8cndarray + flattenBy( zeros( 'uint8c', sh, ord ), {}, identity ); // $ExpectType uint8cndarray + flattenBy( zeros( 'uint8c', sh, ord ), identity, {} ); // $ExpectType uint8cndarray + flattenBy( zeros( 'uint8c', sh, ord ), {}, identity, {} ); // $ExpectType uint8cndarray + flattenBy( empty( 'bool', sh, ord ), identity ); // $ExpectType boolndarray + flattenBy( empty( 'bool', sh, ord ), {}, identity ); // $ExpectType boolndarray + flattenBy( empty( 'bool', sh, ord ), identity, {} ); // $ExpectType boolndarray + flattenBy( empty( 'bool', sh, ord ), {}, identity, {} ); // $ExpectType boolndarray + flattenBy( zeros( 'generic', sh, ord ), identity ); // $ExpectType genericndarray + flattenBy( zeros( 'generic', sh, ord ), {}, identity ); // $ExpectType genericndarray + flattenBy( zeros( 'generic', sh, ord ), identity, {} ); // $ExpectType genericndarray + flattenBy( zeros( 'generic', sh, ord ), {}, identity, {} ); // $ExpectType genericndarray } // The compiler throws an error if the function is provided a first argument which is not an ndarray... From 129673d345d0703b1f26cb516e2d5d495e6de5f9 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Tue, 16 Sep 2025 15:07:11 +0500 Subject: [PATCH 17/22] fix: lint error --- 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: 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: na - task: lint_license_headers status: passed --- --- lib/node_modules/@stdlib/ndarray/flatten-by/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md index cad827797a26..3ae48f5cb542 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md @@ -36,7 +36,7 @@ limitations under the License. var flattenBy = require( '@stdlib/ndarray/flatten-by' ); ``` -#### flattenBy( x\[, options], fcn[, thisArg] ) +#### flattenBy( x\[, options], fcn\[, thisArg] ) Flattens an [ndarray][@stdlib/ndarray/ctor] according to a callback function. From 355f2678f61b9072df78f5a7009c5dab30930bc1 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Tue, 16 Sep 2025 23:13:44 +0500 Subject: [PATCH 18/22] test: add 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 --- --- .../@stdlib/ndarray/flatten-by/lib/main.js | 56 +- .../@stdlib/ndarray/flatten-by/test/test.js | 732 +++++++++++++++--- 2 files changed, 670 insertions(+), 118 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js index 7140fd4fbf8f..4aaa2a6803d4 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js @@ -81,6 +81,7 @@ var COL_MAJOR = 'column-major'; * // returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] */ function flattenBy( x, options, fcn, thisArg ) { + var hasOpts; var nargs; var view; var opts; @@ -96,6 +97,7 @@ function flattenBy( x, options, fcn, thisArg ) { } nargs = arguments.length; xsh = getShape( x ); + hasOpts = false; // Define default options: opts = { @@ -113,46 +115,50 @@ function flattenBy( x, options, fcn, thisArg ) { if ( !isPlainObject( options ) ) { throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); } + hasOpts = true; cb = fcn; } } else { // Case: flattenBy( x, options, fcn, thisArg ) if ( !isPlainObject( options ) ) { throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); } + hasOpts = true; cb = fcn; ctx = thisArg; } if ( !isFunction( cb ) ) { throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) ); } - if ( hasOwnProp( options, 'depth' ) ) { - if ( !isNonNegativeInteger( options.depth ) ) { - throw new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', options.depth ) ); + if ( hasOpts ) { + if ( hasOwnProp( options, 'depth' ) ) { + if ( !isNonNegativeInteger( options.depth ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', options.depth ) ); + } + opts.depth = options.depth; } - opts.depth = options.depth; - } - if ( hasOwnProp( options, 'order' ) ) { - if ( options.order === 'any' ) { - // When 'any', we want to flatten according to the physical layout of the data in memory... - o = strides2order( getStrides( x ) ); - if ( o === 1 ) { - // Data is currently arranged in row-major order: - opts.order = ROW_MAJOR; - } else if ( o === 2 ) { - // Data is currently arranged in column-major order: - opts.order = COL_MAJOR; - } else { // o === 0 || o === 3 (i.e., neither row- nor column-major || both row- and column-major - // When the data is either both row- and column-major (e.g., a one-dimensional ndarray) or neither row- nor column-major (e.g., unordered strides), fallback to flattening according to the stated order of the input ndarray: + if ( hasOwnProp( options, 'order' ) ) { + if ( options.order === 'any' ) { + // When 'any', we want to flatten according to the physical layout of the data in memory... + o = strides2order( getStrides( x ) ); + if ( o === 1 ) { + // Data is currently arranged in row-major order: + opts.order = ROW_MAJOR; + } else if ( o === 2 ) { + // Data is currently arranged in column-major order: + opts.order = COL_MAJOR; + } else { // o === 0 || o === 3 (i.e., neither row- nor column-major || both row- and column-major + // When the data is either both row- and column-major (e.g., a one-dimensional ndarray) or neither row- nor column-major (e.g., unordered strides), fallback to flattening according to the stated order of the input ndarray: + opts.order = getOrder( x ); + } + } else if ( options.order === 'same' ) { + // When 'same', we want to flatten according to the stated order of the input ndarray: opts.order = getOrder( x ); + } else if ( isOrder( options.order ) ) { + // When provided a specific order, flatten according to that order regardless of the order of the input ndarray: + opts.order = options.order; + } else { + throw new TypeError( format( 'invalid option. `%s` option must be a recognized order. Option: `%s`.', 'order', options.order ) ); } - } else if ( options.order === 'same' ) { - // When 'same', we want to flatten according to the stated order of the input ndarray: - opts.order = getOrder( x ); - } else if ( isOrder( options.order ) ) { - // When provided a specific order, flatten according to that order regardless of the order of the input ndarray: - opts.order = options.order; - } else { - throw new TypeError( format( 'invalid option. `%s` option must be a recognized order. Option: `%s`.', 'order', options.order ) ); } } // Create an output ndarray having contiguous memory: diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js index f21f1bb730cd..cc3b71e55c42 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js @@ -27,29 +27,18 @@ var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); var zeros = require( '@stdlib/ndarray/zeros' ); var ndarray = require( '@stdlib/ndarray/ctor' ); var Float64Array = require( '@stdlib/array/float64' ); +var identity = require( '@stdlib/number/float64/base/identity' ); var getDType = require( '@stdlib/ndarray/dtype' ); var getShape = require( '@stdlib/ndarray/shape' ); var getOrder = require( '@stdlib/ndarray/order' ); var getData = require( '@stdlib/ndarray/data-buffer' ); var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var flattenBy = require( './../lib' ); -// FUNCTIONS // - -/** -* Callback function. -* -* @param {number} value - current value -* @returns {number} output value -*/ -function clbk( value ) { - return value + 1; -} - - // TESTS // tape( 'main export is a function', function test( t ) { @@ -81,7 +70,7 @@ tape( 'the function throws an error if provided a first argument which is not an function badValue( value ) { return function badValue() { - flattenBy( value, clbk ); + flattenBy( value, identity ); }; } }); @@ -109,7 +98,7 @@ tape( 'the function throws an error if provided a first argument which is not an function badValue( value ) { return function badValue() { - flattenBy( value, {}, clbk ); + flattenBy( value, {}, identity ); }; } }); @@ -125,7 +114,8 @@ tape( 'the function throws an error if provided an options argument which is not true, false, null, - void 0 + void 0, + function noop() {} ]; for ( i = 0; i < values.length; i++ ) { t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); @@ -134,7 +124,7 @@ tape( 'the function throws an error if provided an options argument which is not function badValue( value ) { return function badValue() { - flattenBy( zeros( [ 2, 2, 2 ] ), value, clbk ); + flattenBy( zeros( [ 2, 2, 2 ] ), value, identity ); }; } }); @@ -166,7 +156,7 @@ tape( 'the function throws an error if provided an invalid `depth` option', func opts = { 'depth': value }; - flattenBy( zeros( [ 2 ] ), opts, clbk ); + flattenBy( zeros( [ 2 ] ), opts, identity ); }; } }); @@ -197,7 +187,7 @@ tape( 'the function throws an error if provided an invalid `order` option', func opts = { 'order': value }; - flattenBy( zeros( [ 2 ] ), opts, clbk ); + flattenBy( zeros( [ 2 ] ), opts, identity ); }; } }); @@ -234,8 +224,8 @@ tape( 'by default, the function flattens all dimensions of a provided input ndar xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); x = new ndarray( dt, xbuf, sh, st, o, ord ); - y = flattenBy( x, clbk ); - expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + y = flattenBy( x, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); t.notEqual( y, x, 'returns expected value' ); t.notEqual( getData( y ), xbuf, 'returns expected value' ); @@ -247,6 +237,61 @@ tape( 'by default, the function flattens all dimensions of a provided input ndar t.end(); }); +tape( 'the function supports specifying the callback execution context (row-major, contiguous)', function test( t ) { + var expected; + var xbuf; + var ord; + var ctx; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + ctx = { + 'count': 1 + }; + + y = flattenBy( x, clbk, ctx ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.strictEqual( ctx.count, 8, 'returns expected value' ); + + t.end(); + + function clbk( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v; + } +}); + tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, non-contiguous)', function test( t ) { var expected; var xbuf; @@ -279,8 +324,8 @@ tape( 'by default, the function flattens all dimensions of a provided input ndar xbuf = new Float64Array( [ 1.0, NaN, 2.0, NaN, 3.0, NaN, 4.0, NaN, 5.0, NaN, 6.0, NaN, 7.0, NaN, 8.0, NaN ] ); x = new ndarray( dt, xbuf, sh, st, o, ord ); - y = flattenBy( x, clbk ); - expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + y = flattenBy( x, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); t.notEqual( y, x, 'returns expected value' ); t.notEqual( getData( y ), xbuf, 'returns expected value' ); @@ -324,8 +369,8 @@ tape( 'by default, the function flattens all dimensions of a provided input ndar xbuf = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); x = new ndarray( dt, xbuf, sh, st, o, ord ); - y = flattenBy( x, clbk ); - expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + y = flattenBy( x, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); t.notEqual( y, x, 'returns expected value' ); t.notEqual( getData( y ), xbuf, 'returns expected value' ); @@ -337,6 +382,61 @@ tape( 'by default, the function flattens all dimensions of a provided input ndar t.end(); }); +tape( 'the function supports specifying the callback execution context (column-major, contiguous)', function test( t ) { + var expected; + var xbuf; + var ord; + var ctx; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + ctx = { + 'count': 1 + }; + + y = flattenBy( x, clbk, ctx ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.strictEqual( ctx.count, 8, 'returns expected value' ); + + t.end(); + + function clbk( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v; + } +}); + tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (column-major, non-contiguous)', function test( t ) { var expected; var xbuf; @@ -369,8 +469,8 @@ tape( 'by default, the function flattens all dimensions of a provided input ndar xbuf = new Float64Array( [ 1.0, NaN, 5.0, NaN, 3.0, NaN, 7.0, NaN, 2.0, NaN, 6.0, NaN, 4.0, NaN, 8.0, NaN ] ); x = new ndarray( dt, xbuf, sh, st, o, ord ); - y = flattenBy( x, clbk ); - expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + y = flattenBy( x, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); t.notEqual( y, x, 'returns expected value' ); t.notEqual( getData( y ), xbuf, 'returns expected value' ); @@ -418,15 +518,15 @@ tape( 'the function supports specifying the maximum number of dimensions to flat opts = { 'depth': 0 }; - y = flattenBy( x, opts, clbk ); + y = flattenBy( x, opts, identity ); expected = [ [ - [ 2.0, 3.0 ], - [ 4.0, 5.0 ] + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] ], [ - [ 6.0, 7.0 ], - [ 8.0, 9.0 ] + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] ] ]; @@ -440,12 +540,12 @@ tape( 'the function supports specifying the maximum number of dimensions to flat opts = { 'depth': 1 }; - y = flattenBy( x, opts, clbk ); + y = flattenBy( x, opts, identity ); expected = [ - [ 2.0, 3.0 ], - [ 4.0, 5.0 ], - [ 6.0, 7.0 ], - [ 8.0, 9.0 ] + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] ]; t.notEqual( y, x, 'returns expected value' ); @@ -458,8 +558,8 @@ tape( 'the function supports specifying the maximum number of dimensions to flat opts = { 'depth': 2 }; - y = flattenBy( x, opts, clbk ); - expected = [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]; + y = flattenBy( x, opts, identity ); + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; t.notEqual( y, x, 'returns expected value' ); t.notEqual( getData( y ), xbuf, 'returns expected value' ); @@ -507,15 +607,15 @@ tape( 'the function supports specifying the maximum number of dimensions to flat opts = { 'depth': 0 }; - y = flattenBy( x, opts, clbk ); + y = flattenBy( x, opts, identity ); expected = [ [ - [ 2.0, 3.0 ], - [ 4.0, 5.0 ] + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] ], [ - [ 6.0, 7.0 ], - [ 8.0, 9.0 ] + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] ] ]; @@ -529,12 +629,12 @@ tape( 'the function supports specifying the maximum number of dimensions to flat opts = { 'depth': 1 }; - y = flattenBy( x, opts, clbk ); + y = flattenBy( x, opts, identity ); expected = [ - [ 2.0, 3.0 ], - [ 4.0, 5.0 ], - [ 6.0, 7.0 ], - [ 8.0, 9.0 ] + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] ]; t.notEqual( y, x, 'returns expected value' ); @@ -547,8 +647,8 @@ tape( 'the function supports specifying the maximum number of dimensions to flat opts = { 'depth': 2 }; - y = flattenBy( x, opts, clbk ); - expected = [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]; + y = flattenBy( x, opts, identity ); + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; t.notEqual( y, x, 'returns expected value' ); t.notEqual( getData( y ), xbuf, 'returns expected value' ); @@ -596,8 +696,8 @@ tape( 'the function supports flattening a provided input ndarray in lexicographi opts = { 'order': 'row-major' }; - y = flattenBy( x, opts, clbk ); - expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); t.notEqual( y, x, 'returns expected value' ); t.notEqual( getData( y ), xbuf, 'returns expected value' ); @@ -610,12 +710,12 @@ tape( 'the function supports flattening a provided input ndarray in lexicographi 'depth': 1, 'order': 'row-major' }; - y = flattenBy( x, opts, clbk ); + y = flattenBy( x, opts, identity ); expected = [ - [ 2.0, 3.0 ], - [ 4.0, 5.0 ], - [ 6.0, 7.0 ], - [ 8.0, 9.0 ] + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] ]; t.notEqual( y, x, 'returns expected value' ); @@ -664,8 +764,8 @@ tape( 'the function supports flattening a provided input ndarray in lexicographi opts = { 'order': 'row-major' }; - y = flattenBy( x, opts, clbk ); - expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); t.notEqual( y, x, 'returns expected value' ); t.notEqual( getData( y ), xbuf, 'returns expected value' ); @@ -678,12 +778,12 @@ tape( 'the function supports flattening a provided input ndarray in lexicographi 'depth': 1, 'order': 'row-major' }; - y = flattenBy( x, opts, clbk ); + y = flattenBy( x, opts, identity ); expected = [ - [ 2.0, 3.0 ], - [ 4.0, 5.0 ], - [ 6.0, 7.0 ], - [ 8.0, 9.0 ] + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] ]; t.notEqual( y, x, 'returns expected value' ); @@ -732,8 +832,8 @@ tape( 'the function supports flattening a provided input ndarray in colexicograp opts = { 'order': 'column-major' }; - y = flattenBy( x, opts, clbk ); - expected = new Float64Array( [ 2.0, 6.0, 4.0, 8.0, 3.0, 7.0, 5.0, 9.0 ] ); + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); t.notEqual( y, x, 'returns expected value' ); t.notEqual( getData( y ), xbuf, 'returns expected value' ); @@ -746,12 +846,12 @@ tape( 'the function supports flattening a provided input ndarray in colexicograp 'depth': 1, 'order': 'column-major' }; - y = flattenBy( x, opts, clbk ); + y = flattenBy( x, opts, identity ); expected = [ - [ 2.0, 3.0 ], - [ 6.0, 7.0 ], - [ 4.0, 5.0 ], - [ 8.0, 9.0 ] + [ 1.0, 2.0 ], + [ 5.0, 6.0 ], + [ 3.0, 4.0 ], + [ 7.0, 8.0 ] ]; t.notEqual( y, x, 'returns expected value' ); @@ -800,8 +900,8 @@ tape( 'the function supports flattening a provided input ndarray in colexicograp opts = { 'order': 'column-major' }; - y = flattenBy( x, opts, clbk ); - expected = new Float64Array( [ 2.0, 6.0, 4.0, 8.0, 3.0, 7.0, 5.0, 9.0 ] ); + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); t.notEqual( y, x, 'returns expected value' ); t.notEqual( getData( y ), xbuf, 'returns expected value' ); @@ -814,12 +914,12 @@ tape( 'the function supports flattening a provided input ndarray in colexicograp 'depth': 1, 'order': 'column-major' }; - y = flattenBy( x, opts, clbk ); + y = flattenBy( x, opts, identity ); expected = [ - [ 2.0, 3.0 ], - [ 6.0, 7.0 ], - [ 4.0, 5.0 ], - [ 8.0, 9.0 ] + [ 1.0, 2.0 ], + [ 5.0, 6.0 ], + [ 3.0, 4.0 ], + [ 7.0, 8.0 ] ]; t.notEqual( y, x, 'returns expected value' ); @@ -868,8 +968,8 @@ tape( 'the function supports flattening a provided input ndarray in same order a opts = { 'order': 'same' }; - y = flattenBy( x, opts, clbk ); - expected = new Float64Array( [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); t.notEqual( y, x, 'returns expected value' ); t.notEqual( getData( y ), xbuf, 'returns expected value' ); @@ -882,12 +982,12 @@ tape( 'the function supports flattening a provided input ndarray in same order a 'depth': 1, 'order': 'same' }; - y = flattenBy( x, opts, clbk ); + y = flattenBy( x, opts, identity ); expected = [ - [ 2.0, 3.0 ], - [ 4.0, 5.0 ], - [ 6.0, 7.0 ], - [ 8.0, 9.0 ] + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] ]; t.notEqual( y, x, 'returns expected value' ); @@ -936,8 +1036,8 @@ tape( 'the function supports flattening a provided input ndarray in same order a opts = { 'order': 'same' }; - y = flattenBy( x, opts, clbk ); - expected = new Float64Array( [ 2.0, 6.0, 4.0, 8.0, 3.0, 7.0, 5.0, 9.0 ] ); + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); t.notEqual( y, x, 'returns expected value' ); t.notEqual( getData( y ), xbuf, 'returns expected value' ); @@ -950,12 +1050,12 @@ tape( 'the function supports flattening a provided input ndarray in same order a 'depth': 1, 'order': 'same' }; - y = flattenBy( x, opts, clbk ); + y = flattenBy( x, opts, identity ); expected = [ - [ 2.0, 3.0 ], - [ 6.0, 7.0 ], - [ 4.0, 5.0 ], - [ 8.0, 9.0 ] + [ 1.0, 2.0 ], + [ 5.0, 6.0 ], + [ 3.0, 4.0 ], + [ 7.0, 8.0 ] ]; t.notEqual( y, x, 'returns expected value' ); @@ -967,3 +1067,449 @@ tape( 'the function supports flattening a provided input ndarray in same order a t.end(); }); + +tape( 'the function supports flattening a provided input ndarray according to the physical layout of the input ndarray (row-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = [ -1, -2, -4 ]; // reversing and negating the strides simulates a flipped and reversed view + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 8.0, 4.0 ], + * [ 6.0, 2.0 ] + * ], + * [ + * [ 7.0, 3.0 ], + * [ 5.0, 1.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'any' + }; + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + opts = { + 'depth': 1, + 'order': 'any' + }; + y = flattenBy( x, opts, identity ); + expected = [ + [ 8.0, 4.0 ], + [ 7.0, 3.0 ], + [ 6.0, 2.0 ], + [ 5.0, 1.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a provided input ndarray according to the physical layout of the input ndarray (column-major)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = [ -4, -2, -1 ]; // reversing and negating the strides simulates a flipped and reversed view + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 8.0, 7.0 ], + * [ 6.0, 5.0 ] + * ], + * [ + * [ 4.0, 3.0 ], + * [ 2.0, 1.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'any' + }; + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + opts = { + 'depth': 1, + 'order': 'any' + }; + y = flattenBy( x, opts, identity ); + expected = [ + [ 8.0, 7.0 ], + [ 6.0, 5.0 ], + [ 4.0, 3.0 ], + [ 2.0, 1.0 ] + ]; + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 4, 2 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a zero-dimensional input ndarray', function test( t ) { + var expected; + var xbuf; + var dt; + var x; + var y; + + dt = 'float64'; + x = scalar2ndarray( 3.0, { + 'dtype': dt, + 'order': 'row-major' + }); + + y = flattenBy( x, identity ); + expected = new Float64Array( [ 3.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + dt = 'float64'; + x = scalar2ndarray( 3.0, { + 'dtype': dt, + 'order': 'column-major' + }); + + y = flattenBy( x, identity ); + expected = new Float64Array( [ 3.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a zero-dimensional input ndarray (order=same)', function test( t ) { + var expected; + var xbuf; + var opts; + var dt; + var x; + var y; + + dt = 'float64'; + x = scalar2ndarray( 3.0, { + 'dtype': dt, + 'order': 'row-major' + }); + + opts = { + 'order': 'same' + }; + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 3.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + dt = 'float64'; + x = scalar2ndarray( 3.0, { + 'dtype': dt, + 'order': 'column-major' + }); + + opts = { + 'order': 'same' + }; + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 3.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a zero-dimensional input ndarray (order=any)', function test( t ) { + var expected; + var xbuf; + var opts; + var dt; + var x; + var y; + + dt = 'float64'; + x = scalar2ndarray( 3.0, { + 'dtype': dt, + 'order': 'row-major' + }); + + opts = { + 'order': 'any' + }; + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 3.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + dt = 'float64'; + x = scalar2ndarray( 3.0, { + 'dtype': dt, + 'order': 'column-major' + }); + + opts = { + 'order': 'any' + }; + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 3.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a one-dimensional input ndarray', function test( t ) { + var expected; + var xbuf; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 8 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flattenBy( x, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + dt = 'float64'; + ord = 'column-major'; + sh = [ 8 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + y = flattenBy( x, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a one-dimensional input ndarray (order=same)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 8 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'same' + }; + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + dt = 'float64'; + ord = 'column-major'; + sh = [ 8 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'same' + }; + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports flattening a one-dimensional input ndarray (order=any)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 8 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'any' + }; + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + + dt = 'float64'; + ord = 'column-major'; + sh = [ 8 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + + opts = { + 'order': 'any' + }; + y = flattenBy( x, opts, identity ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + + t.end(); +}); From c047e283f848fdc401912ea24629c73fd266bde8 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Tue, 16 Sep 2025 23:17:14 +0500 Subject: [PATCH 19/22] test: add 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: 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 --- --- lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js index cc3b71e55c42..98dc8bf4a3a4 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js @@ -114,8 +114,7 @@ tape( 'the function throws an error if provided an options argument which is not true, false, null, - void 0, - function noop() {} + void 0 ]; for ( i = 0; i < values.length; i++ ) { t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); @@ -282,7 +281,7 @@ tape( 'the function supports specifying the callback execution context (row-majo t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - t.strictEqual( ctx.count, 8, 'returns expected value' ); + t.strictEqual( ctx.count, 9, 'returns expected value' ); t.end(); @@ -427,7 +426,7 @@ tape( 'the function supports specifying the callback execution context (column-m t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - t.strictEqual( ctx.count, 8, 'returns expected value' ); + t.strictEqual( ctx.count, 9, 'returns expected value' ); t.end(); From e6a090b76d890a8e4e9fdf66bb0ca31904c7bceb Mon Sep 17 00:00:00 2001 From: headlessNode Date: Tue, 16 Sep 2025 23:20:02 +0500 Subject: [PATCH 20/22] test: add 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: 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 --- --- lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js index 98dc8bf4a3a4..0e4c0c9ed521 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js @@ -418,7 +418,7 @@ tape( 'the function supports specifying the callback execution context (column-m }; y = flattenBy( x, clbk, ctx ); - expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + expected = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); t.notEqual( y, x, 'returns expected value' ); t.notEqual( getData( y ), xbuf, 'returns expected value' ); From 48f6e8b9049462556d2dcd05684c9d11949d5e66 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Tue, 16 Sep 2025 23:30:17 +0500 Subject: [PATCH 21/22] test: add test case --- 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: 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 --- --- .../@stdlib/ndarray/flatten-by/test/test.js | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js index 0e4c0c9ed521..6599c949ebc1 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js @@ -191,6 +191,31 @@ tape( 'the function throws an error if provided an invalid `order` option', func } }); +tape( 'the function throws an error if provided a callback argument which is not a function', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenBy( zeros( [ 2, 2, 2 ] ), {}, value ); + }; + } +}); + tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, contiguous)', function test( t ) { var expected; var xbuf; @@ -291,6 +316,65 @@ tape( 'the function supports specifying the callback execution context (row-majo } }); +tape( 'the function supports specifying the callback execution context (row-major, contiguous, options)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var ctx; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + ctx = { + 'count': 1 + }; + opts = { + 'depth': 2 + }; + + y = flattenBy( x, opts, clbk, ctx ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.strictEqual( ctx.count, 9, 'returns expected value' ); + + t.end(); + + function clbk( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v; + } +}); + tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, non-contiguous)', function test( t ) { var expected; var xbuf; @@ -436,6 +520,65 @@ tape( 'the function supports specifying the callback execution context (column-m } }); +tape( 'the function supports specifying the callback execution context (column-major, contiguous, options)', function test( t ) { + var expected; + var xbuf; + var opts; + var ord; + var ctx; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + ctx = { + 'count': 1 + }; + opts = { + 'depth': 2 + }; + + y = flattenBy( x, opts, clbk, ctx ); + expected = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.strictEqual( ctx.count, 9, 'returns expected value' ); + + t.end(); + + function clbk( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v; + } +}); + tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (column-major, non-contiguous)', function test( t ) { var expected; var xbuf; From 86cc6a4d8929c0f49b23822611edb286d3e35436 Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 17 Sep 2025 00:07:47 -0700 Subject: [PATCH 22/22] chore: clean-up --- 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: 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/flatten-by/README.md | 8 +- .../ndarray/flatten-by/docs/types/index.d.ts | 770 ++--------------- .../ndarray/flatten-by/docs/types/test.ts | 105 ++- .../@stdlib/ndarray/flatten-by/lib/index.js | 1 - .../@stdlib/ndarray/flatten-by/lib/main.js | 29 +- .../@stdlib/ndarray/flatten-by/test/test.js | 786 +++++++++++++----- 6 files changed, 720 insertions(+), 979 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md index 3ae48f5cb542..f4121b083525 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/README.md +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/README.md @@ -63,7 +63,7 @@ The function accepts the following arguments: - **x**: input [ndarray][@stdlib/ndarray/ctor]. - **options**: function options (_optional_). - **fcn**: callback function. -- **thisArg**: callback execution context. +- **thisArg**: callback execution context (_optional_). The function accepts the following options: @@ -172,7 +172,7 @@ var count = ctx.count; - **indices**: current array element indices. - **arr**: the input [ndarray][@stdlib/ndarray/ctor]. -- The traversal [order][@stdlib/ndarray/orders] of array elements depends on the memory layout of both the input and output ndarrays. As a result, the callback is not guaranteed to receive elements in the view [order][@stdlib/ndarray/orders]. +- The order in which array elements are traversed and passed to a provided callback function is **not** guaranteed to match the order of array elements in an [ndarray][@stdlib/ndarray/ctor] view. Accordingly, a provided callback should avoid making assumptions regarding the order of provided elements. @@ -226,10 +226,6 @@ console.log( ndarray2array( y ) ); [@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders - - - - diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts index b83f67605b81..a97d0924d204 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts @@ -20,17 +20,15 @@ /// -/* eslint-disable max-lines */ - -import { float64ndarray, float32ndarray, complex128ndarray, complex64ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, boolndarray, genericndarray, Order } from '@stdlib/types/ndarray'; -import { Complex64, Complex128, ComplexLike } from '@stdlib/types/complex'; +import { typedndarray, genericndarray, Order } from '@stdlib/types/ndarray'; +import { ComplexLike } from '@stdlib/types/complex'; /** * Callback invoked for each ndarray element. * * @returns output value */ -type Nullary = ( this: W ) => V; +type Nullary = ( this: ThisArg ) => V; /** * Callback invoked for each ndarray element. @@ -38,7 +36,7 @@ type Nullary = ( this: W ) => V; * @param value - current array element * @returns output value */ -type Unary = ( this: W, value: T ) => V; +type Unary = ( this: ThisArg, value: T ) => V; /** * Callback invoked for each ndarray element. @@ -47,7 +45,7 @@ type Unary = ( this: W, value: T ) => V; * @param indices - current array element indices * @returns output value */ -type Binary = ( this: W, value: T, indices: Array ) => V; +type Binary = ( this: ThisArg, value: T, indices: Array ) => V; /** * Callback invoked for each ndarray element. @@ -57,7 +55,7 @@ type Binary = ( this: W, value: T, indices: Array ) => V; * @param arr - input array * @returns output value */ -type Ternary = ( this: W, value: T, indices: Array, arr: U ) => V; +type Ternary = ( this: ThisArg, value: T, indices: Array, arr: U ) => V; /** * Callback invoked for each ndarray element. @@ -67,7 +65,7 @@ type Ternary = ( this: W, value: T, indices: Array, arr: U ) * @param arr - input array * @returns output value */ -type Callback = Nullary | Unary | Binary | Ternary; +type Callback = Nullary | Unary | Binary | Ternary; /** * Interface defining function options. @@ -130,40 +128,7 @@ interface Options { * var arr = ndarray2array( y ); * // returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] */ -declare function flattenBy( x: float64ndarray, fcn: Callback, thisArg?: ThisParameterType> ): float64ndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var Float32Array = require( '@stdlib/array/float32' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function scale( value ) { -* return value * 2.0; -* } -* -* var buffer = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'float32', buffer, shape, strides, offset, 'row-major' ); -* // return -* -* var y = flattenBy( x, scale ); -* // returns -* -* var arr = ndarray2array( y ); -* // returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] -*/ -declare function flattenBy( x: float32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): float32ndarray; +declare function flattenBy = typedndarray, ThisArg = unknown>( x: T, fcn: Callback, thisArg?: ThisParameterType> ): T; /** * Flattens an ndarray according to a callback function. @@ -183,7 +148,7 @@ declare function flattenBy( x: float32ndarray, fcn: Callback( x: float32ndarray, fcn: Callback */ -declare function flattenBy( x: complex64ndarray, fcn: Callback, thisArg?: ThisParameterType> ): complex64ndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var Complex128Array = require( '@stdlib/array/complex128' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function identity( value ) { -* return value; -* } -* -* var buffer = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); -* var shape = [ 1 , 3 ]; -* var strides = [ 3, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'complex128', buffer, shape, strides, offset, 'row-major' ); -* // return -* -* var y = flattenBy( x, identity ); -* // returns -*/ -declare function flattenBy( x: complex128ndarray, fcn: Callback, thisArg?: ThisParameterType> ): complex128ndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var Int32Array = require( '@stdlib/array/int32' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function scale( value ) { -* return value * 2.0; -* } -* -* var buffer = new Int32Array( [ 1, 2, 3, 4, 5, 6 ] ); -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'int32', buffer, shape, strides, offset, 'row-major' ); -* // return -* -* var y = flattenBy( x, scale ); -* // returns -* -* var arr = ndarray2array( y ); -* // returns [ 2, 4, 6, 8, 10, 12 ] -*/ -declare function flattenBy( x: int32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): int32ndarray; +declare function flattenBy = typedndarray, ThisArg = unknown>( x: U, fcn: Callback, thisArg?: ThisParameterType> ): U; /** * Flattens an ndarray according to a callback function. @@ -267,29 +169,29 @@ declare function flattenBy( x: int32ndarray, fcn: Callback * * var y = flattenBy( x, scale ); * // returns * * var arr = ndarray2array( y ); -* // returns [ 2, 4, 6, 8, 10, 12 ] +* // returns [ false, true, false, true, false, true ] */ -declare function flattenBy( x: int16ndarray, fcn: Callback, thisArg?: ThisParameterType> ): int16ndarray; +declare function flattenBy = typedndarray, ThisArg = unknown>( x: T, fcn: Callback, thisArg?: ThisParameterType> ): T; /** * Flattens an ndarray according to a callback function. @@ -300,7 +202,6 @@ declare function flattenBy( x: int16ndarray, fcn: Callback( x: int16ndarray, fcn: Callback * * var y = flattenBy( x, scale ); * // returns * * var arr = ndarray2array( y ); -* // returns [ 2, 4, 6, 8, 10, 12 ] +* // returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] */ -declare function flattenBy( x: int8ndarray, fcn: Callback, thisArg?: ThisParameterType> ): int8ndarray; +declare function flattenBy = genericndarray, V = unknown, W extends genericndarray = genericndarray, ThisArg = unknown>( x: U, fcn: Callback, thisArg?: ThisParameterType> ): W; /** * Flattens an ndarray according to a callback function. * * @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened * @param fcn - callback function * @param thisArg - callback execution context * @returns output ndarray * * @example -* var Uint32Array = require( '@stdlib/array/uint32' ); +* var Float64Array = require( '@stdlib/array/float64' ); * var ndarray = require( '@stdlib/ndarray/ctor' ); * var ndarray2array = require( '@stdlib/ndarray/to-array' ); * @@ -341,125 +245,70 @@ declare function flattenBy( x: int8ndarray, fcn: Callback * -* var y = flattenBy( x, scale ); -* // returns -* -* var arr = ndarray2array( y ); -* // returns [ 2, 4, 6, 8, 10, 12 ] -*/ -declare function flattenBy( x: uint32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint32ndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var Uint16Array = require( '@stdlib/array/uint16' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function scale( value ) { -* return value * 2.0; -* } -* -* var buffer = new Uint16Array( [ 1, 2, 3, 4, 5, 6 ] ); -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'uint16', buffer, shape, strides, offset, 'row-major' ); -* // return +* var opts = { +* 'depth': 2 +* }; * -* var y = flattenBy( x, scale ); +* var y = flattenBy( x, opts, scale ); * // returns * * var arr = ndarray2array( y ); -* // returns [ 2, 4, 6, 8, 10, 12 ] +* // returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] */ -declare function flattenBy( x: uint16ndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint16ndarray; +declare function flattenBy = typedndarray, ThisArg = unknown>( x: T, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): T; /** * Flattens an ndarray according to a callback function. * * @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened * @param fcn - callback function * @param thisArg - callback execution context * @returns output ndarray * * @example -* var Uint8Array = require( '@stdlib/array/uint8' ); +* var Complex64Array = require( '@stdlib/array/complex64' ); * var ndarray = require( '@stdlib/ndarray/ctor' ); * var ndarray2array = require( '@stdlib/ndarray/to-array' ); * -* function scale( value ) { -* return value * 2.0; +* function identity( value ) { +* return value; * } * -* var buffer = new Uint8Array( [ 1, 2, 3, 4, 5, 6 ] ); -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; +* var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 1, 3 ]; +* var strides = [ 3, 1 ]; * var offset = 0; * -* var x = ndarray( 'uint8', buffer, shape, strides, offset, 'row-major' ); +* var x = ndarray( 'complex64', buffer, shape, strides, offset, 'row-major' ); * // return * -* var y = flattenBy( x, scale ); -* // returns -* -* var arr = ndarray2array( y ); -* // returns [ 2, 4, 6, 8, 10, 12 ] -*/ -declare function flattenBy( x: uint8ndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint8ndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function scale( value ) { -* return value * 2.0; -* } -* -* var buffer = new Uint8ClampedArray( [ 1, 2, 3, 4, 5, 6 ] ); -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); -* // return +* var opts = { +* 'depth': 1 +* }; * -* var y = flattenBy( x, scale ); +* var y = flattenBy( x, opts, identity ); * // returns -* -* var arr = ndarray2array( y ); -* // returns [ 2, 4, 6, 8, 10, 12 ] */ -declare function flattenBy( x: uint8cndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint8cndarray; +declare function flattenBy = typedndarray, ThisArg = unknown>( x: U, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): U; /** * Flattens an ndarray according to a callback function. * * @param x - input ndarray +* @param options - function options +* @param options.depth - maximum number of dimensions to flatten +* @param options.order - order in which input ndarray elements should be flattened * @param fcn - callback function * @param thisArg - callback execution context * @returns output ndarray @@ -481,85 +330,17 @@ declare function flattenBy( x: uint8cndarray, fcn: Callback * -* var y = flattenBy( x, scale ); -* // returns -* -* var arr = ndarray2array( y ); -* // returns [ false, true, false, true, false, true ] -*/ -declare function flattenBy( x: boolndarray, fcn: Callback, thisArg?: ThisParameterType> ): boolndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function scale( value ) { -* return value * 2.0; -* } -* -* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); -* // return -* -* var y = flattenBy( x, scale ); -* // returns -* -* var arr = ndarray2array( y ); -* // returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] -*/ -declare function flattenBy( x: genericndarray, fcn: Callback, V, W>, thisArg?: ThisParameterType, V, W>> ): genericndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param options - function options -* @param options.depth - maximum number of dimensions to flatten -* @param options.order - order in which input ndarray elements should be flattened -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var Float64Array = require( '@stdlib/array/float64' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function scale( value ) { -* return value * 2.0; -* } -* -* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); -* // return -* * var opts = { -* 'depth': 1 +* 'depth': 2 * }; * * var y = flattenBy( x, opts, scale ); * // returns * * var arr = ndarray2array( y ); -* // returns [ [ 2.0, 4.0, 6.0 ], [ 8.0, 10.0, 12.0 ] ] +* // returns [ false, true, false, true, false, true ] */ -declare function flattenBy( x: float64ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): float64ndarray; +declare function flattenBy = typedndarray, ThisArg = unknown>( x: T, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): T; /** * Flattens an ndarray according to a callback function. @@ -573,7 +354,6 @@ declare function flattenBy( x: float64ndarray, options: Options, fc * @returns output ndarray * * @example -* var Float32Array = require( '@stdlib/array/float32' ); * var ndarray = require( '@stdlib/ndarray/ctor' ); * var ndarray2array = require( '@stdlib/ndarray/to-array' ); * @@ -581,458 +361,26 @@ declare function flattenBy( x: float64ndarray, options: Options, fc * return value * 2.0; * } * -* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; * var shape = [ 3, 1, 2 ]; * var strides = [ 2, 2, 1 ]; * var offset = 0; * -* var x = ndarray( 'float32', buffer, shape, strides, offset, 'row-major' ); +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); * // return * * var opts = { -* 'depth': 1 +* 'depth': 2 * }; * * var y = flattenBy( x, opts, scale ); * // returns * * var arr = ndarray2array( y ); -* // returns [ [ 2.0, 4.0, 6.0 ], [ 8.0, 10.0, 12.0 ] ] +* // returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] */ -declare function flattenBy( x: float32ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): float32ndarray; +declare function flattenBy = genericndarray, V = unknown, W extends genericndarray = genericndarray, ThisArg = unknown>( x: U, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): W; -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param options - function options -* @param options.depth - maximum number of dimensions to flatten -* @param options.order - order in which input ndarray elements should be flattened -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var Complex64Array = require( '@stdlib/array/complex64' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function identity( value ) { -* return value; -* } -* -* var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); -* var shape = [ 1 , 3 ]; -* var strides = [ 3, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'complex64', buffer, shape, strides, offset, 'row-major' ); -* // return -* -* var opts = { -* 'depth': 1 -* }; -* -* var y = flattenBy( x, opts, identity ); -* // returns -*/ -declare function flattenBy( x: complex64ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): complex64ndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param options - function options -* @param options.depth - maximum number of dimensions to flatten -* @param options.order - order in which input ndarray elements should be flattened -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var Complex128Array = require( '@stdlib/array/complex128' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function identity( value ) { -* return value; -* } -* -* var buffer = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); -* var shape = [ 1 , 3 ]; -* var strides = [ 3, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'complex128', buffer, shape, strides, offset, 'row-major' ); -* // return -* -* var opts = { -* 'depth': 1 -* }; -* -* var y = flattenBy( x, opts, identity ); -* // returns -*/ -declare function flattenBy( x: complex128ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): complex128ndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param options - function options -* @param options.depth - maximum number of dimensions to flatten -* @param options.order - order in which input ndarray elements should be flattened -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var Int32Array = require( '@stdlib/array/int32' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function scale( value ) { -* return value * 2.0; -* } -* -* var buffer = new Int32Array( [ 1, 2, 3, 4, 5, 6 ] ); -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'int32', buffer, shape, strides, offset, 'row-major' ); -* // return -* -* var opts = { -* 'depth': 1 -* }; -* -* var y = flattenBy( x, opts, scale ); -* // returns -* -* var arr = ndarray2array( y ); -* // returns [ [ 2, 4, 6 ], [ 8, 10, 12 ] ] -*/ -declare function flattenBy( x: int32ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): int32ndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param options - function options -* @param options.depth - maximum number of dimensions to flatten -* @param options.order - order in which input ndarray elements should be flattened -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var Int16Array = require( '@stdlib/array/int32' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function scale( value ) { -* return value * 2.0; -* } -* -* var buffer = new Int16Array( [ 1, 2, 3, 4, 5, 6 ] ); -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'int16', buffer, shape, strides, offset, 'row-major' ); -* // return -* -* var opts = { -* 'depth': 1 -* }; -* -* var y = flattenBy( x, opts, scale ); -* // returns -* -* var arr = ndarray2array( y ); -* // returns [ [ 2, 4, 6 ], [ 8, 10, 12 ] ] -*/ -declare function flattenBy( x: int16ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): int16ndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param options - function options -* @param options.depth - maximum number of dimensions to flatten -* @param options.order - order in which input ndarray elements should be flattened -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var Int8Array = require( '@stdlib/array/int8' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function scale( value ) { -* return value * 2.0; -* } -* -* var buffer = new Int8Array( [ 1, 2, 3, 4, 5, 6 ] ); -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'int8', buffer, shape, strides, offset, 'row-major' ); -* // return -* -* var opts = { -* 'depth': 1 -* }; -* -* var y = flattenBy( x, opts, scale ); -* // returns -* -* var arr = ndarray2array( y ); -* // returns [ [ 2, 4, 6 ], [ 8, 10, 12 ] ] -*/ -declare function flattenBy( x: int8ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): int8ndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param options - function options -* @param options.depth - maximum number of dimensions to flatten -* @param options.order - order in which input ndarray elements should be flattened -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var Uint32Array = require( '@stdlib/array/uint32' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function scale( value ) { -* return value * 2.0; -* } -* -* var buffer = new Uint32Array( [ 1, 2, 3, 4, 5, 6 ] ); -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'uint32', buffer, shape, strides, offset, 'row-major' ); -* // return -* -* var opts = { -* 'depth': 1 -* }; -* -* var y = flattenBy( x, opts, scale ); -* // returns -* -* var arr = ndarray2array( y ); -* // returns [ [ 2, 4, 6 ], [ 8, 10, 12 ] ] -*/ -declare function flattenBy( x: uint32ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): uint32ndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param options - function options -* @param options.depth - maximum number of dimensions to flatten -* @param options.order - order in which input ndarray elements should be flattened -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var Uint16Array = require( '@stdlib/array/uint16' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function scale( value ) { -* return value * 2.0; -* } -* -* var buffer = new Uint16Array( [ 1, 2, 3, 4, 5, 6 ] ); -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'uint16', buffer, shape, strides, offset, 'row-major' ); -* // return -* -* var opts = { -* 'depth': 1 -* }; -* -* var y = flattenBy( x, opts, scale ); -* // returns -* -* var arr = ndarray2array( y ); -* // returns [ [ 2, 4, 6 ], [ 8, 10, 12 ] ] -*/ -declare function flattenBy( x: uint16ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): uint16ndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param options - function options -* @param options.depth - maximum number of dimensions to flatten -* @param options.order - order in which input ndarray elements should be flattened -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var Uint8Array = require( '@stdlib/array/uint8' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function scale( value ) { -* return value * 2.0; -* } -* -* var buffer = new Uint8Array( [ 1, 2, 3, 4, 5, 6 ] ); -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'uint8', buffer, shape, strides, offset, 'row-major' ); -* // return -* -* var opts = { -* 'depth': 1 -* }; -* -* var y = flattenBy( x, opts, scale ); -* // returns -* -* var arr = ndarray2array( y ); -* // returns [ [ 2, 4, 6 ], [ 8, 10, 12 ] ] -*/ -declare function flattenBy( x: uint8ndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): uint8ndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param options - function options -* @param options.depth - maximum number of dimensions to flatten -* @param options.order - order in which input ndarray elements should be flattened -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function scale( value ) { -* return value * 2.0; -* } -* -* var buffer = new Uint8ClampedArray( [ 1, 2, 3, 4, 5, 6 ] ); -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); -* // return -* -* var opts = { -* 'depth': 1 -* }; -* -* var y = flattenBy( x, opts, scale ); -* // returns -* -* var arr = ndarray2array( y ); -* // returns [ [ 2, 4, 6 ], [ 8, 10, 12 ] ] -*/ -declare function flattenBy( x: uint8cndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): uint8cndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param options - function options -* @param options.depth - maximum number of dimensions to flatten -* @param options.order - order in which input ndarray elements should be flattened -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var BooleanArray = require( '@stdlib/array/bool' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function invert( value ) { -* return !value; -* } -* -* var buffer = new BooleanArray( [ true, false, true, false, true, false ] ); -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); -* // return -* -* var opts = { -* 'depth': 1 -* }; -* -* var y = flattenBy( x, opts, scale ); -* // returns -* -* var arr = ndarray2array( y ); -* // returns [ false, true, false, true, false, true ] -*/ -declare function flattenBy( x: boolndarray, options: Options, fcn: Callback, thisArg?: ThisParameterType> ): boolndarray; - -/** -* Flattens an ndarray according to a callback function. -* -* @param x - input ndarray -* @param options - function options -* @param options.depth - maximum number of dimensions to flatten -* @param options.order - order in which input ndarray elements should be flattened -* @param fcn - callback function -* @param thisArg - callback execution context -* @returns output ndarray -* -* @example -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* function scale( value ) { -* return value * 2.0; -* } -* -* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; -* var shape = [ 3, 1, 2 ]; -* var strides = [ 2, 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); -* // return -* -* var opts = { -* 'depth': 1 -* }; -* -* var y = flattenBy( x, opts, scale ); -* // returns -* -* var arr = ndarray2array( y ); -* // returns [ [ 2.0, 4.0, 6.0 ], [ 8.0, 10.0, 12.0 ] ] -*/ -declare function flattenBy( x: genericndarray, options: Options, fcn: Callback, V, W>, thisArg?: ThisParameterType, V, W>> ): genericndarray; // EXPORTS // diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts index 0cf72a5fd386..d0eec5992d8c 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts @@ -16,8 +16,6 @@ * limitations under the License. */ -/// - import empty = require( '@stdlib/ndarray/base/empty' ); import zeros = require( '@stdlib/ndarray/base/zeros' ); import flattenBy = require( './index' ); @@ -84,10 +82,10 @@ function identity( x: any ): any { flattenBy( empty( 'bool', sh, ord ), {}, identity ); // $ExpectType boolndarray flattenBy( empty( 'bool', sh, ord ), identity, {} ); // $ExpectType boolndarray flattenBy( empty( 'bool', sh, ord ), {}, identity, {} ); // $ExpectType boolndarray - flattenBy( zeros( 'generic', sh, ord ), identity ); // $ExpectType genericndarray - flattenBy( zeros( 'generic', sh, ord ), {}, identity ); // $ExpectType genericndarray - flattenBy( zeros( 'generic', sh, ord ), identity, {} ); // $ExpectType genericndarray - flattenBy( zeros( 'generic', sh, ord ), {}, identity, {} ); // $ExpectType genericndarray + flattenBy( zeros( 'generic', sh, ord ), identity ); // $ExpectType genericndarray + flattenBy( zeros( 'generic', sh, ord ), {}, identity ); // $ExpectType genericndarray + flattenBy( zeros( 'generic', sh, ord ), identity, {} ); // $ExpectType genericndarray + flattenBy( zeros( 'generic', sh, ord ), {}, identity, {} ); // $ExpectType genericndarray } // The compiler throws an error if the function is provided a first argument which is not an ndarray... @@ -100,33 +98,84 @@ function identity( x: any ): any { flattenBy( {}, identity ); // $ExpectError flattenBy( [ 1 ], identity ); // $ExpectError flattenBy( ( x: number ): number => x, identity ); // $ExpectError + + flattenBy( 5, {}, identity ); // $ExpectError + flattenBy( true, {}, identity ); // $ExpectError + flattenBy( false, {}, identity ); // $ExpectError + flattenBy( null, {}, identity ); // $ExpectError + flattenBy( undefined, {}, identity ); // $ExpectError + flattenBy( {}, {}, identity ); // $ExpectError + flattenBy( [ 1 ], {}, identity ); // $ExpectError + flattenBy( ( x: number ): number => x, {}, identity ); // $ExpectError + + flattenBy( 5, identity, {} ); // $ExpectError + flattenBy( true, identity, {} ); // $ExpectError + flattenBy( false, identity, {} ); // $ExpectError + flattenBy( null, identity, {} ); // $ExpectError + flattenBy( undefined, identity, {} ); // $ExpectError + flattenBy( {}, identity, {} ); // $ExpectError + flattenBy( [ 1 ], identity, {} ); // $ExpectError + flattenBy( ( x: number ): number => x, identity, {} ); // $ExpectError + + flattenBy( 5, {}, identity, {} ); // $ExpectError + flattenBy( true, {}, identity, {} ); // $ExpectError + flattenBy( false, {}, identity, {} ); // $ExpectError + flattenBy( null, {}, identity, {} ); // $ExpectError + flattenBy( undefined, {}, identity, {} ); // $ExpectError + flattenBy( {}, {}, identity, {} ); // $ExpectError + flattenBy( [ 1 ], {}, identity, {} ); // $ExpectError + flattenBy( ( x: number ): number => x, {}, identity, {} ); // $ExpectError } // The compiler throws an error if the function is provided an options argument which is not an object... { - flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), '5', identity ); // $ExpectError - flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), true, identity ); // $ExpectError - flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), false, identity ); // $ExpectError - flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), null, identity ); // $ExpectError - flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), [ 1 ], identity ); // $ExpectError + const x = zeros( 'generic', [ 2, 2, 2 ], 'row-major' ); + + flattenBy( x, '5', identity ); // $ExpectError + flattenBy( x, true, identity ); // $ExpectError + flattenBy( x, false, identity ); // $ExpectError + flattenBy( x, null, identity ); // $ExpectError + flattenBy( x, [ 1 ], identity ); // $ExpectError + + flattenBy( x, '5', identity, {} ); // $ExpectError + flattenBy( x, true, identity, {} ); // $ExpectError + flattenBy( x, false, identity, {} ); // $ExpectError + flattenBy( x, null, identity, {} ); // $ExpectError + flattenBy( x, [ 1 ], identity, {} ); // $ExpectError } // The compiler throws an error if the function is provided a second argument with invalid `depth` option... { - flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': '5' }, identity ); // $ExpectError - flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': true }, identity ); // $ExpectError - flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': false }, identity ); // $ExpectError - flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': null }, identity ); // $ExpectError - flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'depth': [ 1 ] }, identity ); // $ExpectError + const x = zeros( 'generic', [ 2, 2, 2 ], 'row-major' ); + + flattenBy( x, { 'depth': '5' }, identity ); // $ExpectError + flattenBy( x, { 'depth': true }, identity ); // $ExpectError + flattenBy( x, { 'depth': false }, identity ); // $ExpectError + flattenBy( x, { 'depth': null }, identity ); // $ExpectError + flattenBy( x, { 'depth': [ 1 ] }, identity ); // $ExpectError + + flattenBy( x, { 'depth': '5' }, identity, {} ); // $ExpectError + flattenBy( x, { 'depth': true }, identity, {} ); // $ExpectError + flattenBy( x, { 'depth': false }, identity, {} ); // $ExpectError + flattenBy( x, { 'depth': null }, identity, {} ); // $ExpectError + flattenBy( x, { 'depth': [ 1 ] }, identity, {} ); // $ExpectError } // The compiler throws an error if the function is provided a second argument with invalid `order` option... { - flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': '5' }, identity ); // $ExpectError - flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': true }, identity ); // $ExpectError - flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': false }, identity ); // $ExpectError - flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': null }, identity ); // $ExpectError - flattenBy( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'order': [ 1 ] }, identity ); // $ExpectError + const x = zeros( 'generic', [ 2, 2, 2 ], 'row-major' ); + + flattenBy( x, { 'order': '5' }, identity ); // $ExpectError + flattenBy( x, { 'order': true }, identity ); // $ExpectError + flattenBy( x, { 'order': false }, identity ); // $ExpectError + flattenBy( x, { 'order': null }, identity ); // $ExpectError + flattenBy( x, { 'order': [ 1 ] }, identity ); // $ExpectError + + flattenBy( x, { 'order': '5' }, identity, {} ); // $ExpectError + flattenBy( x, { 'order': true }, identity, {} ); // $ExpectError + flattenBy( x, { 'order': false }, identity, {} ); // $ExpectError + flattenBy( x, { 'order': null }, identity, {} ); // $ExpectError + flattenBy( x, { 'order': [ 1 ] }, identity, {} ); // $ExpectError } // The compiler throws an error if the function is provided a callback which is not a function... @@ -140,11 +189,21 @@ function identity( x: any ): any { flattenBy( x, {}, undefined ); // $ExpectError flattenBy( x, {}, {} ); // $ExpectError flattenBy( x, {}, [ 1 ] ); // $ExpectError + + flattenBy( x, {}, '5', {} ); // $ExpectError + flattenBy( x, {}, true, {} ); // $ExpectError + flattenBy( x, {}, false, {} ); // $ExpectError + flattenBy( x, {}, null, {} ); // $ExpectError + flattenBy( x, {}, undefined, {} ); // $ExpectError + flattenBy( x, {}, {}, {} ); // $ExpectError + flattenBy( x, {}, [ 1 ], {} ); // $ExpectError } // The compiler throws an error if the function is provided an unsupported number of arguments... { + const x = zeros( 'generic', [ 2, 2, 2 ], 'row-major' ); + flattenBy(); // $ExpectError - flattenBy( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError - flattenBy( zeros( 'float64', [ 2, 2 ], 'row-major' ), {}, ( x: number ): number => x, {}, {} ); // $ExpectError + flattenBy( x ); // $ExpectError + flattenBy( x, {}, ( x: number ): number => x, {}, {} ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js index 0ed4e631e02b..204d867de17b 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js @@ -32,7 +32,6 @@ * return value * 2.0; * } * -* // Create an input ndarray: * var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); * // return * diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js index 4aaa2a6803d4..b06674f4df5a 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js @@ -56,7 +56,7 @@ var COL_MAJOR = 'column-major'; * @param {NonNegativeInteger} [options.depth] - maximum number of dimensions to flatten * @param {string} [options.order='row-major'] - order in which input ndarray elements should be flattened * @param {Function} fcn - callback function -* @param {*} thisArg - callback execution context +* @param {*} [thisArg] - callback execution context * @throws {TypeError} first argument must be an ndarray-like object * @throws {TypeError} options argument must be an object * @throws {TypeError} callback argument must be a function @@ -105,23 +105,25 @@ function flattenBy( x, options, fcn, thisArg ) { 'order': ROW_MAJOR // by default, flatten in lexicographic order (i.e., trailing dimensions first; e.g., if `x` is a matrix, flatten row-by-row) }; - if ( nargs <= 2 ) { // Case: flattenBy( x, fcn ) + // Case: flattenBy( x, fcn ) + if ( nargs <= 2 ) { cb = options; - } else if ( nargs <= 3 ) { - if ( isFunction( options ) ) { // Case: flattenBy( x, fcn, thisArg ) + } + // Case: flattenBy( x, ???, ??? ) + else if ( nargs === 3 ) { + // Case: flattenBy( x, fcn, thisArg ) + if ( isFunction( options ) ) { cb = options; ctx = fcn; - } else { // Case: flattenBy( x, options, fcn ) - if ( !isPlainObject( options ) ) { - throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); - } + } + // Case: flattenBy( x, options, fcn ) + else { hasOpts = true; cb = fcn; } - } else { // Case: flattenBy( x, options, fcn, thisArg ) - if ( !isPlainObject( options ) ) { - throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); - } + } + // Case: flattenBy( x, options, fcn, thisArg ) + else { hasOpts = true; cb = fcn; ctx = thisArg; @@ -130,6 +132,9 @@ function flattenBy( x, options, fcn, thisArg ) { throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) ); } if ( hasOpts ) { + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } if ( hasOwnProp( options, 'depth' ) ) { if ( !isNonNegativeInteger( options.depth ) ) { throw new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', options.depth ) ); diff --git a/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js index 6599c949ebc1..9b306deb95f9 100644 --- a/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js @@ -103,6 +103,62 @@ tape( 'the function throws an error if provided a first argument which is not an } }); +tape( 'the function throws an error if provided a first argument which is not an ndarray (thisArg)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenBy( value, identity, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray (options, thisArg)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenBy( value, {}, identity, {} ); + }; + } +}); + tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { var values; var i; @@ -128,6 +184,31 @@ tape( 'the function throws an error if provided an options argument which is not } }); +tape( 'the function throws an error if provided an options argument which is not an object (thisArg)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + flattenBy( zeros( [ 2, 2, 2 ] ), value, identity, {} ); + }; + } +}); + tape( 'the function throws an error if provided an invalid `depth` option', function test( t ) { var values; var opts; @@ -216,112 +297,35 @@ tape( 'the function throws an error if provided a callback argument which is not } }); -tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, contiguous)', function test( t ) { - var expected; - var xbuf; - var ord; - var sh; - var st; - var dt; - var o; - var x; - var y; - - dt = 'float64'; - ord = 'row-major'; - sh = [ 2, 2, 2 ]; - st = shape2strides( sh, ord ); - o = strides2offset( sh, st ); - - /* - * [ - * [ - * [ 1.0, 2.0 ], - * [ 3.0, 4.0 ] - * ], - * [ - * [ 5.0, 6.0 ], - * [ 7.0, 8.0 ] - * ] - * ] - */ - xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); - x = new ndarray( dt, xbuf, sh, st, o, ord ); - - y = flattenBy( x, identity ); - expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports specifying the callback execution context (row-major, contiguous)', function test( t ) { - var expected; - var xbuf; - var ord; - var ctx; - var sh; - var st; - var dt; - var o; - var x; - var y; - - dt = 'float64'; - ord = 'row-major'; - sh = [ 2, 2, 2 ]; - st = shape2strides( sh, ord ); - o = strides2offset( sh, st ); - - /* - * [ - * [ - * [ 1.0, 2.0 ], - * [ 3.0, 4.0 ] - * ], - * [ - * [ 5.0, 6.0 ], - * [ 7.0, 8.0 ] - * ] - * ] - */ - xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); - x = new ndarray( dt, xbuf, sh, st, o, ord ); - ctx = { - 'count': 1 - }; - - y = flattenBy( x, clbk, ctx ); - expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - t.strictEqual( ctx.count, 9, 'returns expected value' ); +tape( 'the function throws an error if provided a callback argument which is not a function (thisArg)', function test( t ) { + var values; + var i; + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } t.end(); - function clbk( v ) { - this.count += 1; // eslint-disable-line no-invalid-this - return v; + function badValue( value ) { + return function badValue() { + flattenBy( zeros( [ 2, 2, 2 ] ), {}, value, {} ); + }; } }); -tape( 'the function supports specifying the callback execution context (row-major, contiguous, options)', function test( t ) { +tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, contiguous)', function test( t ) { var expected; var xbuf; - var opts; var ord; - var ctx; var sh; var st; var dt; @@ -349,14 +353,8 @@ tape( 'the function supports specifying the callback execution context (row-majo */ xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); x = new ndarray( dt, xbuf, sh, st, o, ord ); - ctx = { - 'count': 1 - }; - opts = { - 'depth': 2 - }; - y = flattenBy( x, opts, clbk, ctx ); + y = flattenBy( x, identity ); expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); t.notEqual( y, x, 'returns expected value' ); @@ -365,14 +363,8 @@ tape( 'the function supports specifying the callback execution context (row-majo t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - t.strictEqual( ctx.count, 9, 'returns expected value' ); t.end(); - - function clbk( v ) { - this.count += 1; // eslint-disable-line no-invalid-this - return v; - } }); tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, non-contiguous)', function test( t ) { @@ -461,122 +453,8 @@ tape( 'by default, the function flattens all dimensions of a provided input ndar t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); t.strictEqual( getDType( y ), dt, 'returns expected value' ); t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports specifying the callback execution context (column-major, contiguous)', function test( t ) { - var expected; - var xbuf; - var ord; - var ctx; - var sh; - var st; - var dt; - var o; - var x; - var y; - - dt = 'float64'; - ord = 'column-major'; - sh = [ 2, 2, 2 ]; - st = shape2strides( sh, ord ); - o = strides2offset( sh, st ); - - /* - * [ - * [ - * [ 1.0, 2.0 ], - * [ 3.0, 4.0 ] - * ], - * [ - * [ 5.0, 6.0 ], - * [ 7.0, 8.0 ] - * ] - * ] - */ - xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); - x = new ndarray( dt, xbuf, sh, st, o, ord ); - ctx = { - 'count': 1 - }; - - y = flattenBy( x, clbk, ctx ); - expected = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - t.strictEqual( ctx.count, 9, 'returns expected value' ); - - t.end(); - - function clbk( v ) { - this.count += 1; // eslint-disable-line no-invalid-this - return v; - } -}); - -tape( 'the function supports specifying the callback execution context (column-major, contiguous, options)', function test( t ) { - var expected; - var xbuf; - var opts; - var ord; - var ctx; - var sh; - var st; - var dt; - var o; - var x; - var y; - - dt = 'float64'; - ord = 'column-major'; - sh = [ 2, 2, 2 ]; - st = shape2strides( sh, ord ); - o = strides2offset( sh, st ); - - /* - * [ - * [ - * [ 1.0, 2.0 ], - * [ 3.0, 4.0 ] - * ], - * [ - * [ 5.0, 6.0 ], - * [ 7.0, 8.0 ] - * ] - * ] - */ - xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); - x = new ndarray( dt, xbuf, sh, st, o, ord ); - ctx = { - 'count': 1 - }; - opts = { - 'depth': 2 - }; - - y = flattenBy( x, opts, clbk, ctx ); - expected = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); - - t.notEqual( y, x, 'returns expected value' ); - t.notEqual( getData( y ), xbuf, 'returns expected value' ); - t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); - t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); - t.strictEqual( getDType( y ), dt, 'returns expected value' ); - t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); - t.strictEqual( ctx.count, 9, 'returns expected value' ); - - t.end(); - - function clbk( v ) { - this.count += 1; // eslint-disable-line no-invalid-this - return v; - } + + t.end(); }); tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (column-major, non-contiguous)', function test( t ) { @@ -1655,3 +1533,459 @@ tape( 'the function supports flattening a one-dimensional input ndarray (order=a t.end(); }); + +tape( 'the function supports specifying the callback execution context (row-major)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var xbuf; + var ord; + var ctx; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + ctx = { + 'count': 1 + }; + + values = []; + indices = []; + arrays = []; + y = flattenBy( x, clbk, ctx ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.strictEqual( ctx.count, 9, 'returns expected value' ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 0, 0, 1 ], + [ 0, 1, 0 ], + [ 0, 1, 1 ], + [ 1, 0, 0 ], + [ 1, 0, 1 ], + [ 1, 1, 0 ], + [ 1, 1, 1 ] + ]; + indices.sort( ascending ); // index order is not guaranteed + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x, + x, + x, + x, + x + ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function clbk( v, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( v ); + indices.push( idx ); + arrays.push( arr ); + return v; + } + + function ascending( a, b ) { + if ( a[ 0 ] > b[ 0 ] ) { + return 1; + } + if ( a[ 0 ] < b[ 0 ] ) { + return -1; + } + if ( a[ 1 ] > b[ 1 ] ) { + return 1; + } + if ( a[ 1 ] < b[ 1 ] ) { + return -1; + } + if ( a[ 2 ] > b[ 2 ] ) { + return 1; + } + if ( a[ 2 ] < b[ 2 ] ) { + return -1; + } + return 0; + } +}); + +tape( 'the function supports specifying the callback execution context (row-major, options)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var xbuf; + var ord; + var ctx; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + ctx = { + 'count': 1 + }; + + values = []; + indices = []; + arrays = []; + y = flattenBy( x, {}, clbk, ctx ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.strictEqual( ctx.count, 9, 'returns expected value' ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 0, 0, 1 ], + [ 0, 1, 0 ], + [ 0, 1, 1 ], + [ 1, 0, 0 ], + [ 1, 0, 1 ], + [ 1, 1, 0 ], + [ 1, 1, 1 ] + ]; + indices.sort( ascending ); // index order is not guaranteed + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x, + x, + x, + x, + x + ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function clbk( v, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( v ); + indices.push( idx ); + arrays.push( arr ); + return v; + } + + function ascending( a, b ) { + if ( a[ 0 ] > b[ 0 ] ) { + return 1; + } + if ( a[ 0 ] < b[ 0 ] ) { + return -1; + } + if ( a[ 1 ] > b[ 1 ] ) { + return 1; + } + if ( a[ 1 ] < b[ 1 ] ) { + return -1; + } + if ( a[ 2 ] > b[ 2 ] ) { + return 1; + } + if ( a[ 2 ] < b[ 2 ] ) { + return -1; + } + return 0; + } +}); + +tape( 'the function supports specifying the callback execution context (column-major)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var xbuf; + var ord; + var ctx; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + ctx = { + 'count': 1 + }; + + values = []; + indices = []; + arrays = []; + y = flattenBy( x, clbk, ctx ); + expected = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.strictEqual( ctx.count, 9, 'returns expected value' ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 0, 0, 1 ], + [ 0, 1, 0 ], + [ 0, 1, 1 ], + [ 1, 0, 0 ], + [ 1, 0, 1 ], + [ 1, 1, 0 ], + [ 1, 1, 1 ] + ]; + indices.sort( ascending ); // index order is not guaranteed + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x, + x, + x, + x, + x + ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function clbk( v, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( v ); + indices.push( idx ); + arrays.push( arr ); + return v; + } + + function ascending( a, b ) { + if ( a[ 0 ] > b[ 0 ] ) { + return 1; + } + if ( a[ 0 ] < b[ 0 ] ) { + return -1; + } + if ( a[ 1 ] > b[ 1 ] ) { + return 1; + } + if ( a[ 1 ] < b[ 1 ] ) { + return -1; + } + if ( a[ 2 ] > b[ 2 ] ) { + return 1; + } + if ( a[ 2 ] < b[ 2 ] ) { + return -1; + } + return 0; + } +}); + +tape( 'the function supports specifying the callback execution context (column-major, options)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var xbuf; + var ord; + var ctx; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 2, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + /* + * [ + * [ + * [ 1.0, 2.0 ], + * [ 3.0, 4.0 ] + * ], + * [ + * [ 5.0, 6.0 ], + * [ 7.0, 8.0 ] + * ] + * ] + */ + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( dt, xbuf, sh, st, o, ord ); + ctx = { + 'count': 1 + }; + + values = []; + indices = []; + arrays = []; + y = flattenBy( x, {}, clbk, ctx ); + expected = new Float64Array( [ 1.0, 5.0, 3.0, 7.0, 2.0, 6.0, 4.0, 8.0 ] ); + + t.notEqual( y, x, 'returns expected value' ); + t.notEqual( getData( y ), xbuf, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' ); + t.strictEqual( getDType( y ), dt, 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.strictEqual( ctx.count, 9, 'returns expected value' ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 0, 0, 1 ], + [ 0, 1, 0 ], + [ 0, 1, 1 ], + [ 1, 0, 0 ], + [ 1, 0, 1 ], + [ 1, 1, 0 ], + [ 1, 1, 1 ] + ]; + indices.sort( ascending ); // index order is not guaranteed + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x, + x, + x, + x, + x + ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function clbk( v, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( v ); + indices.push( idx ); + arrays.push( arr ); + return v; + } + + function ascending( a, b ) { + if ( a[ 0 ] > b[ 0 ] ) { + return 1; + } + if ( a[ 0 ] < b[ 0 ] ) { + return -1; + } + if ( a[ 1 ] > b[ 1 ] ) { + return 1; + } + if ( a[ 1 ] < b[ 1 ] ) { + return -1; + } + if ( a[ 2 ] > b[ 2 ] ) { + return 1; + } + if ( a[ 2 ] < b[ 2 ] ) { + return -1; + } + return 0; + } +});