diff --git a/CHANGELOG.md b/CHANGELOG.md index d6c354e0..58c306eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@
-## Unreleased (2024-08-16) +## Unreleased (2024-08-17)
@@ -77,6 +77,7 @@ ##### Features +- [`297e4d5`](https://github.com/stdlib-js/stdlib/commit/297e4d5c9c1b015ccf8c7bb77c672e9ac1328898) - add `without` to namespace - [`29641e1`](https://github.com/stdlib-js/stdlib/commit/29641e155d07e3aa7e71340d47ada3e43d1edaa5) - add `cunone` to namespace - [`a596d0c`](https://github.com/stdlib-js/stdlib/commit/a596d0c68b97049ec249e9c51caf7e2d34190f4a) - add `removeAt` to namespace - [`00fd68d`](https://github.com/stdlib-js/stdlib/commit/00fd68ddf80a71b08e5353c63c297fca4daaf873) - update namespace TypeScript declarations [(#2415)](https://github.com/stdlib-js/stdlib/pull/2415) @@ -1308,12 +1309,45 @@ This release closes the following issue: ##### Features +- [`15c9b0e`](https://github.com/stdlib-js/stdlib/commit/15c9b0e711e44c7e06ba768ab577d8cffdc5647d) - add support for assigning to an output array - [`4253607`](https://github.com/stdlib-js/stdlib/commit/425360790cca3ef7b1747b3c1828ab5825306441) - add `array/base/with` [(#1374)](https://github.com/stdlib-js/stdlib/pull/1374)
+
+ +##### Bug Fixes + +- [`45bfdb6`](https://github.com/stdlib-js/stdlib/commit/45bfdb698f5b5b31e1782efe846b76ee64ab032a) - update error messages to accommodate signed integers + +
+ + + + + +
+ + + +
+ +#### [@stdlib/array/base/without](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/without) + +
+ +
+ +##### Features + +- [`f6df528`](https://github.com/stdlib-js/stdlib/commit/f6df52818f504fd55987a80f586ad55b40405d58) - add `array/base/without` + +
+ + +
@@ -2435,6 +2469,12 @@ A total of 10 people contributed to this release. Thank you to the following con
+- [`297e4d5`](https://github.com/stdlib-js/stdlib/commit/297e4d5c9c1b015ccf8c7bb77c672e9ac1328898) - **feat:** add `without` to namespace _(by Athan Reines)_ +- [`f6df528`](https://github.com/stdlib-js/stdlib/commit/f6df52818f504fd55987a80f586ad55b40405d58) - **feat:** add `array/base/without` _(by Athan Reines)_ +- [`e22b37a`](https://github.com/stdlib-js/stdlib/commit/e22b37aaf13a81aa9c8536d54b8a43871e4bdd16) - **docs:** add example _(by Athan Reines)_ +- [`1f1c2df`](https://github.com/stdlib-js/stdlib/commit/1f1c2dfc7314a343282c8d4904a8ad05c500f80d) - **docs:** fix example _(by Athan Reines)_ +- [`15c9b0e`](https://github.com/stdlib-js/stdlib/commit/15c9b0e711e44c7e06ba768ab577d8cffdc5647d) - **feat:** add support for assigning to an output array _(by Athan Reines)_ +- [`45bfdb6`](https://github.com/stdlib-js/stdlib/commit/45bfdb698f5b5b31e1782efe846b76ee64ab032a) - **fix:** update error messages to accommodate signed integers _(by Athan Reines)_ - [`29641e1`](https://github.com/stdlib-js/stdlib/commit/29641e155d07e3aa7e71340d47ada3e43d1edaa5) - **feat:** add `cunone` to namespace _(by Athan Reines)_ - [`a596d0c`](https://github.com/stdlib-js/stdlib/commit/a596d0c68b97049ec249e9c51caf7e2d34190f4a) - **feat:** add `removeAt` to namespace _(by Athan Reines)_ - [`400fcbb`](https://github.com/stdlib-js/stdlib/commit/400fcbb91753cc2a47d428a65a29ee455a208768) - **refactor:** avoid unnecessary copies, update examples, and refactor benchmarks _(by Athan Reines)_ diff --git a/base/lib/index.js b/base/lib/index.js index 1a96c6e8..ecc37636 100644 --- a/base/lib/index.js +++ b/base/lib/index.js @@ -1548,6 +1548,15 @@ setReadOnly( ns, 'where', require( './../../base/where' ) ); */ setReadOnly( ns, 'arrayWith', require( './../../base/with' ) ); +/** +* @name without +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/array/base/without} +*/ +setReadOnly( ns, 'without', require( './../../base/without' ) ); + /** * @name zeroTo * @memberof ns diff --git a/base/with/README.md b/base/with/README.md index 93dabeb7..ac7b68b9 100644 --- a/base/with/README.md +++ b/base/with/README.md @@ -52,7 +52,27 @@ var out = arrayWith( x, 0, 5 ); out = arrayWith( x, -1, 6 ); // returns [ 1, 2, 3, 6 ] +``` + +The function accepts the following arguments: + +- **x**: an input array. +- **index**: element index. +- **value**: replacement value. + +### arrayWith.assign( x, index, value, out, stride, offset ) + +Copies elements from one array to another array and sets the element at the specified index to a provided value. + +```javascript +var x = [ 1, 2, 3, 4 ]; + +var out = [ 0, 0, 0, 0 ]; +var arr = arrayWith.assign( x, 0, 5, out, 1, 0 ); +// returns [ 5, 2, 3, 4 ] +var bool = ( arr === out ); +// returns true ``` The function accepts the following arguments: @@ -60,6 +80,9 @@ The function accepts the following arguments: - **x**: an input array. - **index**: element index. - **value**: replacement value. +- **out**: output array. +- **stride**: output array stride. +- **offset**: output array offset. @@ -71,13 +94,13 @@ The function accepts the following arguments: ## Notes -- If provided an array-like object having a `with` method, the function defers execution to that method and assumes that the method has the following signature: +- If provided an array-like object having a `with` method, the `arrayWith` function defers execution to that method and assumes that the method has the following signature: ```text x.with( index, value ) ``` - If provided an array-like object without a `with` method, the function shallow copies input array data to a new generic array, normalizes a provided index, and sets a specified element. + If provided an array-like object without a `with` method, the `arrayWith` function shallow copies input array data to a new generic array, normalizes a provided index, and sets a specified element. - Negative indices are resolved relative to the last array element, with the last element corresponding to `-1`. diff --git a/base/with/benchmark/benchmark.assign.length.js b/base/with/benchmark/benchmark.assign.length.js new file mode 100644 index 00000000..4522ded5 --- /dev/null +++ b/base/with/benchmark/benchmark.assign.length.js @@ -0,0 +1,98 @@ +/** +* @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. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isArray = require( '@stdlib/assert/is-array' ); +var ones = require( './../../../base/ones' ); +var zeros = require( './../../../base/zeros' ); +var pkg = require( './../package.json' ).name; +var arrayWith = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out = zeros( len ); + var x = ones( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arrayWith.assign( x, i%len, i, out, 1, 0 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( len ); + bench( pkg+':assign:dtype=generic,len='+len, f ); + } +} + +main(); diff --git a/base/with/benchmark/benchmark.js b/base/with/benchmark/benchmark.length.js similarity index 100% rename from base/with/benchmark/benchmark.js rename to base/with/benchmark/benchmark.length.js diff --git a/base/with/docs/repl.txt b/base/with/docs/repl.txt index 730214e3..2002a558 100644 --- a/base/with/docs/repl.txt +++ b/base/with/docs/repl.txt @@ -42,6 +42,48 @@ > x [ 1, 2, 3, 4 ] + +{{alias}}.assign( x, index, value, out, stride, offset ) + Copies elements from one array to another array and sets the element at the + specified index to a provided value. + + Negative indices are resolved relative to the last array element, with the + last element corresponding to `-1`. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + index: integer + Index of the element to be replaced. + + value: any + Replacement value. + + out: ArrayLikeObject + Output array. + + stride: integer + Output array stride. + + offset: integer + Output array offset. + + Returns + ------- + out: ArrayLikeObject + Output array. + + Examples + -------- + > var x = [ 1, 2, 3, 4 ]; + > var out = [ 0, 0, 0, 0 ]; + > var arr = {{alias}}.assign( x, 0, 5, out, 1, 0 ) + [ 5, 2, 3, 4 ] + > var bool = ( arr === out ) + true + See Also -------- diff --git a/base/with/docs/types/index.d.ts b/base/with/docs/types/index.d.ts index e73909ef..ae3b511b 100644 --- a/base/with/docs/types/index.d.ts +++ b/base/with/docs/types/index.d.ts @@ -20,45 +20,196 @@ /// -import { Collection, RealTypedArray, ComplexTypedArray } from '@stdlib/types/array'; +import { Collection, RealTypedArray, ComplexTypedArray, AccessorArrayLike } from '@stdlib/types/array'; import { ComplexLike } from '@stdlib/types/complex'; /** -* Returns a new array with the element at the specified index replaced with a provided value. -* -* @param x - input array -* @param index - index at which to set a provided value -* @param value - replacement value -* @returns output array -* -* @example -* var Complex128Array = require( '@stdlib/array/complex128' ); -* var Complex128 = require( '@stdlib/complex/float64/ctor' ); -* -* var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); -* -* var out = withArray( x, 0, new Complex128( 7.0, 8.0 ) ); -* // returns [ 7.0, 8.0, 3.0, 4.0, 5.0, 6.0 ] +* Interface describing `arrayWith`. */ -declare function withArray( x: T, index: number, value: ComplexLike ): T; +interface ArrayWith { + /** + * Returns a new array with the element at the specified index replaced with a provided value. + * + * @param x - input array + * @param index - index at which to set a provided value + * @param value - replacement value + * @returns output array + * + * @example + * var Complex128Array = require( './../../../../complex128' ); + * var Complex128 = require( '@stdlib/complex/float64/ctor' ); + * + * var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + * + * var out = arrayWith( x, 0, new Complex128( 7.0, 8.0 ) ); + * // returns [ 7.0, 8.0, 3.0, 4.0, 5.0, 6.0 ] + */ + ( x: T, index: number, value: ComplexLike ): T; -/** -* Returns a new array with the element at the specified index replaced with a provided value. -* -* @param x - input array -* @param index - index at which to set a provided value -* @param value - replacement value -* @returns output array -* -* @example -* var Float64Array = require( '@stdlib/array/float64' ); -* -* var x = new Float64Array( [ 1.0, 2.0, 3.0 ] ); -* -* var out = withArray( x, 0, 5.0 ); -* // returns [ 5.0, 2.0, 3.0 ] -*/ -declare function withArray( x: T, index: number, value: number ): T; // eslint-disable-line @typescript-eslint/unified-signatures + /** + * Returns a new array with the element at the specified index replaced with a provided value. + * + * @param x - input array + * @param index - index at which to set a provided value + * @param value - replacement value + * @returns output array + * + * @example + * var Float64Array = require( './../../../../float64' ); + * + * var x = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + * + * var out = arrayWith( x, 0, 5.0 ); + * // returns [ 5.0, 2.0, 3.0 ] + */ + ( x: T, index: number, value: number ): T; // eslint-disable-line @typescript-eslint/unified-signatures + + /** + * Returns a new array with the element at the specified index replaced with a provided value. + * + * @param x - input array + * @param index - index at which to set a provided value + * @param value - replacement value + * @returns output array + * + * @example + * var x = [ 1, 2, 3 ]; + * + * var out = arrayWith( x, 0, 7 ); + * // returns [ 7, 2, 3 ] + * + * @example + * var x = [ 1, 2, 3, 4, 5, 6 ]; + * + * var out = arrayWith( x, 1, 8 ); + * // returns [ 1, 8, 3, 4, 5, 6 ] + */ + ( x: Collection, index: number, value: T ): Array; + + /** + * Copies elements from one array to another array and sets the element at the specified index to a provided value. + * + * @param x - input array + * @param index - index at which to set a provided value + * @param value - replacement value + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @returns output array + * + * @example + * var Float64Array = require( './../../../../float64' ); + * + * var x = [ 1, 2, 3, 4 ]; + * + * var out = new Float64Array( [ 0, 0, 0, 0 ] ); + * var arr = arrayWith.assign( x, 0, 5, out, 1, 0 ); + * // returns [ 5, 2, 3, 4 ] + * + * var bool = ( arr === out ); + * // returns true + */ + assign( x: Collection | AccessorArrayLike, index: number, value: number, out: T, stride: number, offset: number ): T; + + /** + * Copies elements from one array to another array and sets the element at the specified index to a provided value. + * + * @param x - input array + * @param index - index at which to set a provided value + * @param value - replacement value + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @returns output array + * + * @example + * var Complex128Array = require( './../../../../complex128' ); + * var Complex128 = require( '@stdlib/complex/float64/ctor' ); + * + * var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + * + * var out = new Complex128Array( x.length ); + * var arr = arrayWith.assign( x, 0, new Complex128( 7.0, 8.0 ), out, 1, 0 ); + * // returns [ 7.0, 8.0, 3.0, 4.0, 5.0, 6.0 ] + * + * var bool = ( arr === out ); + * // returns true + */ + assign( x: Collection | AccessorArrayLike, index: number, value: ComplexLike, out: T, stride: number, offset: number ): T; // eslint-disable-line @typescript-eslint/unified-signatures + + /** + * Copies elements from one array to another array and sets the element at the specified index to a provided value. + * + * @param x - input array + * @param index - index at which to set a provided value + * @param value - replacement value + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @returns output array + * + * @example + * var x = [ 1, 2, 3, 4 ]; + * + * var out = [ 0, 0, 0, 0 ]; + * var arr = arrayWith.assign( x, 0, 5, out, 1, 0 ); + * // returns [ 5, 2, 3, 4 ] + * + * var bool = ( arr === out ); + * // returns true + */ + assign( x: Collection | AccessorArrayLike, index: number, value: U, out: Array, stride: number, offset: number ): Array; + + /** + * Copies elements from one array to another array and sets the element at the specified index to a provided value. + * + * @param x - input array + * @param index - index at which to set a provided value + * @param value - replacement value + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @returns output array + * + * @example + * var toAccessorArray = require( './../../../../base/to-accessor-array' ); + * + * var x = toAccessorArray( [ 1, 2, 3, 4 ] ); + * + * var out = toAccessorArray( [ 0, 0, 0, 0 ] ); + * var arr = arrayWith.assign( x, 0, 5, out, 1, 0 ); + * + * var v = out[ 0 ]; + * // returns 5 + * + * var bool = ( arr === out ); + * // returns true + */ + assign( x: Collection | AccessorArrayLike, index: number, value: U, out: AccessorArrayLike, stride: number, offset: number ): AccessorArrayLike; + + /** + * Copies elements from one array to another array and sets the element at the specified index to a provided value. + * + * @param x - input array + * @param index - index at which to set a provided value + * @param value - replacement value + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @returns output array + * + * @example + * var x = [ 1, 2, 3, 4 ]; + * + * var out = [ 0, 0, 0, 0 ]; + * var arr = arrayWith.assign( x, 0, 5, out, 1, 0 ); + * // returns [ 5, 2, 3, 4 ] + * + * var bool = ( arr === out ); + * // returns true + */ + assign( x: Collection | AccessorArrayLike, index: number, value: U, out: Collection, stride: number, offset: number ): Collection; +} /** * Returns a new array with the element at the specified index replaced with a provided value. @@ -71,18 +222,30 @@ declare function withArray( x: T, index: number, value * @example * var x = [ 1, 2, 3 ]; * -* var out = withArray( x, 0, 7 ); +* var out = arrayWith( x, 0, 7 ); * // returns [ 7, 2, 3 ] * * @example * var x = [ 1, 2, 3, 4, 5, 6 ]; * -* var out = withArray( x, 2, 8 ); +* var out = arrayWith( x, 1, 8 ); * // returns [ 1, 8, 3, 4, 5, 6 ] +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var x = [ 1, 2, 3, 4 ]; +* +* var out = new Float64Array( [ 0, 0, 0, 0 ] ); +* var arr = arrayWith.assign( x, 0, 5, out, 1, 0 ); +* // returns [ 5, 2, 3, 4 ] +* +* var bool = ( arr === out ); +* // returns true */ -declare function withArray( x: Collection, index: number, value: T ): Array; +declare var arrayWith: ArrayWith; // EXPORTS // -export = withArray; +export = arrayWith; diff --git a/base/with/docs/types/test.ts b/base/with/docs/types/test.ts index fbd4ce69..52dce400 100644 --- a/base/with/docs/types/test.ts +++ b/base/with/docs/types/test.ts @@ -66,3 +66,105 @@ import arrayWith = require( './index' ); arrayWith( x, 0 ); // $ExpectError arrayWith( x, 0, 0, 5 ); // $ExpectError } + +// Attached to the main export is an `assign` method which returns a collection... +{ + const x = [ 1, 2, 3, 4 ]; + const y = new Complex128Array( 4 ); + + arrayWith.assign( x, 0, 5, [ 0, 0, 0, 0 ], 1, 0 ); // $ExpectType number[] + arrayWith.assign( x, 0, 5, new Float64Array( 4 ), 1, 0 ); // $ExpectType Float64Array + arrayWith.assign( x, 0, 5, new Float32Array( 4 ), 1, 0 ); // $ExpectType Float32Array + arrayWith.assign( x, 0, 5, new Int32Array( 4 ), 1, 0 ); // $ExpectType Int32Array + arrayWith.assign( x, 0, 5, new Int16Array( 4 ), 1, 0 ); // $ExpectType Int16Array + arrayWith.assign( x, 0, 5, new Int8Array( 4 ), 1, 0 ); // $ExpectType Int8Array + arrayWith.assign( x, 0, 5, new Uint32Array( 4 ), 1, 0 ); // $ExpectType Uint32Array + arrayWith.assign( x, 0, 5, new Uint16Array( 4 ), 1, 0 ); // $ExpectType Uint16Array + arrayWith.assign( x, 0, 5, new Uint8Array( 4 ), 1, 0 ); // $ExpectType Uint8Array + arrayWith.assign( x, 0, 5, new Uint8ClampedArray( 4 ), 1, 0 ); // $ExpectType Uint8ClampedArray + arrayWith.assign( y, 0, { 're': 1.0, 'im': 1.0 }, new Complex128Array( 4 ), 1, 0 ); // $ExpectType Complex128Array + arrayWith.assign( y, 0, { 're': 1.0, 'im': 1.0 }, new Complex64Array( 4 ), 1, 0 ); // $ExpectType Complex64Array +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an array-like object... +{ + const out = [ 0, 0, 0, 0 ]; + + arrayWith.assign( 1, 0, 5, out, 1, 0 ); // $ExpectError + arrayWith.assign( true, 0, 5, out, 1, 0 ); // $ExpectError + arrayWith.assign( false, 0, 5, out, 1, 0 ); // $ExpectError + arrayWith.assign( null, 0, 5, out, 1, 0 ); // $ExpectError + arrayWith.assign( void 0, 0, 5, out, 1, 0 ); // $ExpectError + arrayWith.assign( {}, 0, 5, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not a number... +{ + const x = [ 1, 2, 3, 4 ]; + const out = [ 0, 0, 0, 0 ]; + + arrayWith.assign( x, '1', 5, out, 1, 0 ); // $ExpectError + arrayWith.assign( x, true, 5, out, 1, 0 ); // $ExpectError + arrayWith.assign( x, false, 5, out, 1, 0 ); // $ExpectError + arrayWith.assign( x, null, 5, out, 1, 0 ); // $ExpectError + arrayWith.assign( x, void 0, 5, out, 1, 0 ); // $ExpectError + arrayWith.assign( x, [], 5, out, 1, 0 ); // $ExpectError + arrayWith.assign( x, {}, 5, out, 1, 0 ); // $ExpectError + arrayWith.assign( x, ( x: number ): number => x, 5, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a fourth argument which is not an array-like object... +{ + const x = [ 1, 2, 3, 4 ]; + + arrayWith.assign( x, 0, 5, 1, 1, 0 ); // $ExpectError + arrayWith.assign( x, 0, 5, true, 1, 0 ); // $ExpectError + arrayWith.assign( x, 0, 5, false, 1, 0 ); // $ExpectError + arrayWith.assign( x, 0, 5, null, 1, 0 ); // $ExpectError + arrayWith.assign( x, 0, 5, void 0, 1, 0 ); // $ExpectError + arrayWith.assign( x, 0, 5, {}, 1, 0 ); // $ExpectError + arrayWith.assign( x, 0, 5, ( x: number ): number => x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a fifth argument which is not a number... +{ + const x = [ 1, 2, 3, 4 ]; + const out = [ 0, 0, 0, 0 ]; + + arrayWith.assign( x, 0, 1, out, '1', 0 ); // $ExpectError + arrayWith.assign( x, 0, 1, out, true, 0 ); // $ExpectError + arrayWith.assign( x, 0, 1, out, false, 0 ); // $ExpectError + arrayWith.assign( x, 0, 1, out, null, 0 ); // $ExpectError + arrayWith.assign( x, 0, 1, out, void 0, 0 ); // $ExpectError + arrayWith.assign( x, 0, 1, out, [], 0 ); // $ExpectError + arrayWith.assign( x, 0, 1, out, {}, 0 ); // $ExpectError + arrayWith.assign( x, 0, 1, out, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a sixth argument which is not a number... +{ + const x = [ 1, 2, 3, 4 ]; + const out = [ 0, 0, 0, 0 ]; + + arrayWith.assign( x, 0, 1, out, 1, '1' ); // $ExpectError + arrayWith.assign( x, 0, 1, out, 1, true ); // $ExpectError + arrayWith.assign( x, 0, 1, out, 1, false ); // $ExpectError + arrayWith.assign( x, 0, 1, out, 1, null ); // $ExpectError + arrayWith.assign( x, 0, 1, out, 1, void 0 ); // $ExpectError + arrayWith.assign( x, 0, 1, out, 1, [] ); // $ExpectError + arrayWith.assign( x, 0, 1, out, 1, {} ); // $ExpectError + arrayWith.assign( x, 0, 1, out, 1, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = [ 1, 2, 3, 4 ]; + const out = [ 0, 0, 0, 0 ]; + + arrayWith.assign(); // $ExpectError + arrayWith.assign( x ); // $ExpectError + arrayWith.assign( x, 0 ); // $ExpectError + arrayWith.assign( x, 0, 0, out ); // $ExpectError + arrayWith.assign( x, 0, 0, out, 1 ); // $ExpectError + arrayWith.assign( x, 0, 0, out, 1, 0, {} ); // $ExpectError +} diff --git a/base/with/lib/assign.js b/base/with/lib/assign.js new file mode 100644 index 00000000..74ad9f36 --- /dev/null +++ b/base/with/lib/assign.js @@ -0,0 +1,225 @@ +/** +* @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. +*/ + +'use strict'; + +// MODULES // + +var isComplexDataType = require( './../../../base/assert/is-complex-floating-point-data-type' ); +var isBooleanDataType = require( './../../../base/assert/is-boolean-data-type' ); +var isComplexLike = require( '@stdlib/assert/is-complex-like' ); +var Boolean = require( '@stdlib/boolean/ctor' ); +var arraylike2object = require( './../../../base/arraylike2object' ); +var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); +var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var real = require( '@stdlib/complex/float64/real' ); +var imag = require( '@stdlib/complex/float64/imag' ); +var format = require( '@stdlib/string/format' ); + + +// FUNCTIONS // + +/** +* Copies elements from one array to another array and sets the element at the specified index to a provided value. +* +* @private +* @param {Collection} x - input array +* @param {integer} index - element index +* @param {*} value - replacement value +* @param {Collection} out - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @returns {Collection} output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* +* var out = [ 0, 0, 0, 0 ]; +* var arr = indexed( x, 0, 5, out, 1, 0 ); +* // returns [ 5, 2, 3, 4 ] +* +* var bool = ( arr === out ); +* // returns true +*/ +function indexed( x, index, value, out, stride, offset ) { + var io; + var i; + + io = offset; + for ( i = 0; i < x.length; i++ ) { + out[ io ] = x[ i ]; + io += stride; + } + out[ offset+(index*stride) ] = value; + return out; +} + +/** +* Copies elements from one accessor array to another accessor array and sets the element at the specified index to a provided value. +* +* @private +* @param {Object} x - input array object +* @param {integer} index - element index +* @param {*} value - replacement value +* @param {Object} out - output array object +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @returns {Collection} output array +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = toAccessorArray( [ 1, 2, 3, 4 ] ); +* +* var out = toAccessorArray( [ 0, 0, 0, 0 ] ); +* var arr = accessors( arraylike2object( x ), 0, 5, arraylike2object( out ), 1, 0 ); +* +* var v = arr.get( 0 ); +* // returns 5 +*/ +function accessors( x, index, value, out, stride, offset ) { + var xdata; + var odata; + var xget; + var oset; + var io; + var i; + + xdata = x.data; + odata = out.data; + + xget = x.accessors[ 0 ]; + oset = out.accessors[ 1 ]; + + io = offset; + for ( i = 0; i < xdata.length; i++ ) { + oset( odata, io, xget( xdata, i ) ); + io += stride; + } + oset( odata, offset+(index*stride), value ); + return odata; +} + +/** +* Copies elements from one complex array to another complex array and sets the element at the specified index to a provided value. +* +* @private +* @param {Collection} x - real-valued floating-point input array view +* @param {integer} index - element index +* @param {ComplexLike} value - replacement value +* @param {Collection} out - real-valued floating-point output array view +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @returns {Collection} output array view +* +* @example +* var Complex128 = require( '@stdlib/complex/float64/ctor' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* +* var out = new Float64Array( 4 ); +* var arr = complex( x, 0, new Complex128( 5.0, 6.0 ), out, 1, 0 ); +* // returns [ 5.0, 6.0, 3.0, 4.0 ] +*/ +function complex( x, index, value, out, stride, offset ) { + var so; + var io; + var i; + var j; + + so = stride * 2; // multiply by 2, as real-valued array consists of interleaved real and imaginary components + io = offset * 2; + for ( i = 0; i < x.length/2; i++ ) { + j = i * 2; + out[ io ] = x[ j ]; + out[ io+1 ] = x[ j+1 ]; + io += so; + } + io = ( offset+(index*stride) ) * 2; + out[ io ] = real( value ); + out[ io+1 ] = imag( value ); + return out; +} + + +// MAIN // + +/** +* Copies elements from one array to another array and sets the element at the specified index to a provided value. +* +* @param {Collection} x - input array +* @param {integer} index - element index +* @param {*} value - replacement value +* @param {Collection} out - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @throws {RangeError} second argument must not exceed array bounds +* @returns {Collection} output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* +* var out = [ 0, 0, 0, 0 ]; +* var arr = assign( x, 0, 5, out, 1, 0 ); +* // returns [ 5, 2, 3, 4 ] +* +* var bool = ( arr === out ); +* // returns true +*/ +function assign( x, index, value, out, stride, offset ) { + var xo; + var oo; + + index = normalizeIndex( index, x.length-1 ); + if ( index < 0 ) { + throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%d`.', index ) ); + } + xo = arraylike2object( x ); + oo = arraylike2object( out ); + if ( xo.accessorProtocol || oo.accessorProtocol ) { + // Note: we only explicitly support a limited set of dtype-to-dtype pairs, as this function should not be concerned with casting rules, etc. That is left to userland... + if ( + isComplexDataType( xo.dtype ) && + isComplexDataType( oo.dtype ) && + isComplexLike( value ) + ) { + complex( reinterpret( x, 0 ), index, value, reinterpret( out, 0 ), stride, offset ); // eslint-disable-line max-len + return out; + } + if ( + isBooleanDataType( xo.dtype ) && + isBooleanDataType( oo.dtype ) + ) { + indexed( reinterpretBoolean( x, 0 ), index, Boolean( value ), reinterpretBoolean( out, 0 ), stride, offset ); // eslint-disable-line max-len + return out; + } + accessors( xo, index, value, oo, stride, offset ); + return out; + } + indexed( x, index, value, out, stride, offset ); + return out; +} + + +// EXPORTS // + +module.exports = assign; diff --git a/base/with/lib/index.js b/base/with/lib/index.js index 37245a37..8953ca3d 100644 --- a/base/with/lib/index.js +++ b/base/with/lib/index.js @@ -33,11 +33,30 @@ * * v = arrayWith( x, -2, -1 ); * // returns [ 1, 2, -1, 4 ] +* +* @example +* var arrayWith = require( '@stdlib/array/base/with' ); +* +* var x = [ 1, 2, 3, 4 ]; +* +* var out = [ 0, 0, 0, 0 ]; +* var arr = arrayWith.assign( x, 0, 5, out, 1, 0 ); +* // returns [ 5, 2, 3, 4 ] +* +* var bool = ( arr === out ); +* // returns true */ // MODULES // +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); // EXPORTS // diff --git a/base/with/lib/main.js b/base/with/lib/main.js index c285b8f2..dc7591c7 100644 --- a/base/with/lib/main.js +++ b/base/with/lib/main.js @@ -22,6 +22,7 @@ var slice = require( './../../../base/slice' ); var resolveSetter = require( './../../../base/resolve-setter' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); var format = require( '@stdlib/string/format' ); @@ -77,13 +78,9 @@ function arrayWith( x, index, value ) { if ( hasMethod( x, 'with' ) ) { return x.with( index, value ); } + index = normalizeIndex( index, x.length-1 ); if ( index < 0 ) { - index += x.length; - if ( index < 0 ) { - throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%u`.', index ) ); - } - } else if ( index >= x.length ) { - throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%u`.', index ) ); + throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%d`.', index ) ); } out = slice( x, 0, x.length ); set = resolveSetter( out ); diff --git a/base/with/test/test.assign.js b/base/with/test/test.assign.js new file mode 100644 index 00000000..7254572a --- /dev/null +++ b/base/with/test/test.assign.js @@ -0,0 +1,457 @@ +/** +* @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. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int32Array = require( './../../../int32' ); +var Float64Array = require( './../../../float64' ); +var Complex128Array = require( './../../../complex128' ); +var AccessorArray = require( './../../../base/accessor' ); +var Complex128 = require( '@stdlib/complex/float64/ctor' ); +var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' ); +var zeros = require( './../../../zeros' ); +var arrayWith = require( './../lib/assign.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof arrayWith, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (generic)', function test( t ) { + var values; + var out; + var x; + var i; + + x = [ 1, 2, 3 ]; + out = zeros( x.length, 'generic' ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + arrayWith( x, value, 0, out, 1, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (float64)', function test( t ) { + var values; + var out; + var x; + var i; + + x = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + out = zeros( x.length, 'float64' ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + arrayWith( x, value, 0.0, out, 1, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (int32)', function test( t ) { + var values; + var out; + var x; + var i; + + x = new Int32Array( [ 1, 2, 3 ] ); + out = zeros( x.length, 'int32' ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + arrayWith( x, value, 0, out, 1, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (complex128)', function test( t ) { + var values; + var out; + var x; + var i; + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + out = zeros( x.length, 'complex128' ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + arrayWith( x, value, new Complex128( 0.0, 0.0 ), out, 1, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (accessors)', function test( t ) { + var values; + var out; + var x; + var i; + + x = new AccessorArray( [ 1, 2, 3 ] ); + out = new AccessorArray( zeros( x.length, 'generic' ) ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + arrayWith( x, value, 0, out, 1, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (array-like)', function test( t ) { + var values; + var out; + var x; + var i; + + x = { + 'length': 3, + '0': 1, + '1': 2, + '2': 3 + }; + + out = { + 'length': 3, + '0': 0, + '1': 0, + '2': 0 + }; + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + arrayWith( x, value, 0, out, 1, 0 ); + }; + } +}); + +tape( 'the function copies elements to another array and sets an element at a specified index to a provided value (generic)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ 1, 2, 3 ]; + + out = zeros( x.length, 'generic' ); + expected = [ 5, 2, 3 ]; + actual = arrayWith( x, 0, 5, out, 1, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + out = zeros( x.length*2, 'generic' ); + expected = [ 1, 0, 5, 0, 3, 0 ]; + actual = arrayWith( x, 1, 5, out, 2, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + out = zeros( x.length*2, 'generic' ); + expected = [ 0, 5, 0, 2, 0, 1 ]; + actual = arrayWith( x, 2, 5, out, -2, out.length-1 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies elements to another array and sets an element at a specified index to a provided value (float64)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + + out = zeros( x.length, 'float64' ); + expected = new Float64Array( [ 5.0, 2.0, 3.0 ] ); + actual = arrayWith( x, 0, 5.0, out, 1, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + out = zeros( x.length*2, 'float64' ); + expected = new Float64Array( [ 1.0, 0.0, 5.0, 0.0, 3.0, 0.0 ] ); + actual = arrayWith( x, 1, 5.0, out, 2, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + out = zeros( x.length*2, 'float64' ); + expected = new Float64Array( [ 0.0, 5.0, 0.0, 2.0, 0.0, 1.0 ] ); + actual = arrayWith( x, 2, 5.0, out, -2, out.length-1 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies elements to another array and sets an element at a specified index to a provided value (int32)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = new Int32Array( [ 1, 2, 3 ] ); + + out = zeros( x.length, 'int32' ); + expected = new Int32Array( [ 5, 2, 3 ] ); + actual = arrayWith( x, 0, 5, out, 1, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + out = zeros( x.length*2, 'int32' ); + expected = new Int32Array( [ 1, 0, 5, 0, 3, 0 ] ); + actual = arrayWith( x, 1, 5, out, 2, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + out = zeros( x.length*2, 'int32' ); + expected = new Int32Array( [ 0, 5, 0, 2, 0, 1 ] ); + actual = arrayWith( x, 2, 5, out, -2, out.length-1 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies elements to another array and sets an element at a specified index to a provided value (complex128)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + out = zeros( x.length, 'complex128' ); + expected = new Complex128Array( [ 7.0, 8.0, 3.0, 4.0, 5.0, 6.0 ] ); + actual = arrayWith( x, 0, new Complex128( 7.0, 8.0 ), out, 1, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + out = zeros( x.length*2, 'complex128' ); + expected = new Complex128Array( [ 1.0, 2.0, 0.0, 0.0, 7.0, 8.0, 0.0, 0.0, 5.0, 6.0, 0.0, 0.0 ] ); // eslint-disable-line max-len + actual = arrayWith( x, 1, new Complex128( 7.0, 8.0 ), out, 2, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + out = zeros( x.length*2, 'complex128' ); + expected = new Complex128Array( [ 0.0, 0.0, 7.0, 8.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0, 1.0, 2.0 ] ); // eslint-disable-line max-len + actual = arrayWith( x, 2, new Complex128( 7.0, 8.0 ), out, -2, out.length-1 ); // eslint-disable-line max-len + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies elements to another array and sets an element at a specified index to a provided value (accessors)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = new AccessorArray( [ 1, 2, 3 ] ); + + out = new AccessorArray( zeros( x.length, 'generic' ) ); + expected = [ 5, 2, 3 ]; + actual = arrayWith( x, 0, 5, out, 1, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + out = new AccessorArray( zeros( x.length*2, 'generic' ) ); + expected = [ 1, 0, 5, 0, 3, 0 ]; + actual = arrayWith( x, 1, 5, out, 2, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + out = new AccessorArray( zeros( x.length*2, 'generic' ) ); + expected = [ 0, 5, 0, 2, 0, 1 ]; + actual = arrayWith( x, 2, 5, out, -2, out.length-1 ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + t.end(); + + function isEqual( actual, expected ) { + var i; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual.get( i ), expected[ i ], 'returns expected value' ); + } + } +}); + +tape( 'the function copies elements to another array and sets an element at a specified index to a provided value (array-like)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = { + 'length': 3, + '0': 1, + '1': 2, + '2': 3 + }; + + out = { + 'length': 3, + '0': 0, + '1': 0, + '2': 0 + }; + expected = [ 5, 2, 3 ]; + actual = arrayWith( x, 0, 5, out, 1, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + out = { + 'length': 6, + '0': 0, + '1': 0, + '2': 0, + '3': 0, + '4': 0, + '5': 0 + }; + expected = [ 1, 0, 5, 0, 3, 0 ]; + actual = arrayWith( x, 1, 5, out, 2, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + out = { + 'length': 6, + '0': 0, + '1': 0, + '2': 0, + '3': 0, + '4': 0, + '5': 0 + }; + expected = [ 0, 5, 0, 2, 0, 1 ]; + actual = arrayWith( x, 2, 5, out, -2, out.length-1 ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + t.end(); + + function isEqual( actual, expected ) { + var i; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' ); + } + } +}); diff --git a/base/with/test/test.js b/base/with/test/test.js index 08048378..3d03f52f 100644 --- a/base/with/test/test.js +++ b/base/with/test/test.js @@ -21,16 +21,8 @@ // MODULES // var tape = require( 'tape' ); -var Int32Array = require( './../../../int32' ); -var Float64Array = require( './../../../float64' ); -var Complex128Array = require( './../../../complex128' ); -var AccessorArray = require( './../../../base/accessor' ); -var Complex128 = require( '@stdlib/complex/float64/ctor' ); -var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' ); -var isArray = require( '@stdlib/assert/is-array' ); -var isFloat64Array = require( '@stdlib/assert/is-float64array' ); -var isInt32Array = require( '@stdlib/assert/is-int32array' ); -var isComplex128Array = require( '@stdlib/assert/is-complex128array' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var hasMethod = require( '@stdlib/assert/is-method' ); var arrayWith = require( './../lib' ); @@ -42,360 +34,8 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'the function throws an error if provided a second argument which is out-of-bounds (generic)', function test( t ) { - var values; - var x; - var i; - - x = [ 1, 2, 3 ]; - - values = [ - 10, - 100, - 1000, - -10, - -100, - -1000 - ]; - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - arrayWith( x, value, 0 ); - }; - } -}); - -tape( 'the function throws an error if provided a second argument which is out-of-bounds (float64)', function test( t ) { - var values; - var x; - var i; - - x = new Float64Array( [ 1.0, 2.0, 3.0 ] ); - - values = [ - 10, - 100, - 1000, - -10, - -100, - -1000 - ]; - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - arrayWith( x, value, 0.0 ); - }; - } -}); - -tape( 'the function throws an error if provided a second argument which is out-of-bounds (int32)', function test( t ) { - var values; - var x; - var i; - - x = new Int32Array( [ 1, 2, 3 ] ); - - values = [ - 10, - 100, - 1000, - -10, - -100, - -1000 - ]; - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - arrayWith( x, value, 0 ); - }; - } -}); - -tape( 'the function throws an error if provided a second argument which is out-of-bounds (complex128)', function test( t ) { - var values; - var x; - var i; - - x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); - - values = [ - 10, - 100, - 1000, - -10, - -100, - -1000 - ]; - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - arrayWith( x, value, new Complex128( 0.0, 0.0 ) ); - }; - } -}); - -tape( 'the function throws an error if provided a second argument which is out-of-bounds (accessors)', function test( t ) { - var values; - var x; - var i; - - x = new AccessorArray( [ 1, 2, 3 ] ); - - values = [ - 10, - 100, - 1000, - -10, - -100, - -1000 - ]; - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - arrayWith( x, value, 0 ); - }; - } -}); - -tape( 'the function throws an error if provided a second argument which is out-of-bounds (array-like)', function test( t ) { - var values; - var x; - var i; - - x = { - 'length': 3, - '0': 1, - '1': 2, - '2': 3 - }; - - values = [ - 10, - 100, - 1000, - -10, - -100, - -1000 - ]; - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - arrayWith( x, value, 0 ); - }; - } -}); - -tape( 'the function returns a new array with an element at a specified index containing a provided value (generic)', function test( t ) { - var expected; - var actual; - var x; - - x = [ 1, 2, 3 ]; - - expected = [ 5, 2, 3 ]; - actual = arrayWith( x, 0, 5 ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isArray( actual ), true, 'returns expected value' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - expected = [ 1, 5, 3 ]; - actual = arrayWith( x, 1, 5 ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isArray( actual ), true, 'returns expected value' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - expected = [ 1, 2, 5 ]; - actual = arrayWith( x, 2, 5 ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isArray( actual ), true, 'returns expected value' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function returns a new array with an element at a specified index containing a provided value (float64)', function test( t ) { - var expected; - var actual; - var x; - - x = new Float64Array( [ 1.0, 2.0, 3.0 ] ); - - expected = [ 5.0, 2.0, 3.0 ]; - actual = arrayWith( x, 0, 5.0 ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isFloat64Array( actual ), true, 'returns expected value' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - expected = [ 1.0, 5.0, 3.0 ]; - actual = arrayWith( x, 1, 5.0 ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isFloat64Array( actual ), true, 'returns expected value' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - expected = [ 1.0, 2.0, 5.0 ]; - actual = arrayWith( x, 2, 5.0 ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isFloat64Array( actual ), true, 'returns expected value' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function returns a new array with an element at a specified index containing a provided value (int32)', function test( t ) { - var expected; - var actual; - var x; - - x = new Int32Array( [ 1, 2, 3 ] ); - - expected = new Int32Array( [ 5, 2, 3 ] ); - actual = arrayWith( x, 0, 5 ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isInt32Array( actual ), true, 'returns expected value' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - expected = new Int32Array( [ 1, 5, 3 ] ); - actual = arrayWith( x, 1, 5 ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isInt32Array( actual ), true, 'returns expected value' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - expected = new Int32Array( [ 1, 2, 5 ] ); - actual = arrayWith( x, 2, 5 ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isInt32Array( actual ), true, 'returns expected value' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function returns a new array with an element at a specified index containing a provided value (complex128)', function test( t ) { - var expected; - var actual; - var x; - - x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); - - expected = new Complex128Array( [ 7.0, 8.0, 3.0, 4.0, 5.0, 6.0 ] ); - actual = arrayWith( x, 0, new Complex128( 7.0, 8.0 ) ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isComplex128Array( actual ), true, 'returns expected value' ); - t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); - - expected = new Complex128Array( [ 1.0, 2.0, 7.0, 8.0, 5.0, 6.0 ] ); - actual = arrayWith( x, 1, new Complex128( 7.0, 8.0 ) ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isComplex128Array( actual ), true, 'returns expected value' ); - t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); - - expected = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 7.0, 8.0 ] ); - actual = arrayWith( x, 2, new Complex128( 7.0, 8.0 ) ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isComplex128Array( actual ), true, 'returns expected value' ); - t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function returns a new array with an element at a specified index containing a provided value (accessors)', function test( t ) { - var expected; - var actual; - var x; - - x = new AccessorArray( [ 1, 2, 3 ] ); - - expected = [ 5, 2, 3 ]; - actual = arrayWith( x, 0, 5 ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isArray( actual ), true, 'returns expected value' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - expected = [ 1, 5, 3 ]; - actual = arrayWith( x, 1, 5 ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isArray( actual ), true, 'returns expected value' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - expected = [ 1, 2, 5 ]; - actual = arrayWith( x, 2, 5 ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isArray( actual ), true, 'returns expected value' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function returns a new array with an element at a specified index containing a provided value (array-like)', function test( t ) { - var expected; - var actual; - var x; - - x = { - 'length': 3, - '0': 1, - '1': 2, - '2': 3 - }; - - expected = [ 5, 2, 3 ]; - actual = arrayWith( x, 0, 5 ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isArray( actual ), true, 'returns expected value' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - expected = [ 1, 5, 3 ]; - actual = arrayWith( x, 1, 5 ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isArray( actual ), true, 'returns expected value' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - expected = [ 1, 2, 5 ]; - actual = arrayWith( x, 2, 5 ); - - t.notEqual( actual, x, 'returns different reference' ); - t.strictEqual( isArray( actual ), true, 'returns expected value' ); - t.deepEqual( actual, expected, 'returns expected value' ); - +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( hasOwnProp( arrayWith, 'assign' ), true, 'returns expected value' ); + t.strictEqual( hasMethod( arrayWith, 'assign' ), true, 'returns expected value' ); t.end(); }); diff --git a/base/with/test/test.main.js b/base/with/test/test.main.js new file mode 100644 index 00000000..08048378 --- /dev/null +++ b/base/with/test/test.main.js @@ -0,0 +1,401 @@ +/** +* @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. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int32Array = require( './../../../int32' ); +var Float64Array = require( './../../../float64' ); +var Complex128Array = require( './../../../complex128' ); +var AccessorArray = require( './../../../base/accessor' ); +var Complex128 = require( '@stdlib/complex/float64/ctor' ); +var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isFloat64Array = require( '@stdlib/assert/is-float64array' ); +var isInt32Array = require( '@stdlib/assert/is-int32array' ); +var isComplex128Array = require( '@stdlib/assert/is-complex128array' ); +var arrayWith = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof arrayWith, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (generic)', function test( t ) { + var values; + var x; + var i; + + x = [ 1, 2, 3 ]; + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + arrayWith( x, value, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (float64)', function test( t ) { + var values; + var x; + var i; + + x = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + arrayWith( x, value, 0.0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (int32)', function test( t ) { + var values; + var x; + var i; + + x = new Int32Array( [ 1, 2, 3 ] ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + arrayWith( x, value, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (complex128)', function test( t ) { + var values; + var x; + var i; + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + arrayWith( x, value, new Complex128( 0.0, 0.0 ) ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (accessors)', function test( t ) { + var values; + var x; + var i; + + x = new AccessorArray( [ 1, 2, 3 ] ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + arrayWith( x, value, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (array-like)', function test( t ) { + var values; + var x; + var i; + + x = { + 'length': 3, + '0': 1, + '1': 2, + '2': 3 + }; + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + arrayWith( x, value, 0 ); + }; + } +}); + +tape( 'the function returns a new array with an element at a specified index containing a provided value (generic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ 1, 2, 3 ]; + + expected = [ 5, 2, 3 ]; + actual = arrayWith( x, 0, 5 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 5, 3 ]; + actual = arrayWith( x, 1, 5 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 2, 5 ]; + actual = arrayWith( x, 2, 5 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new array with an element at a specified index containing a provided value (float64)', function test( t ) { + var expected; + var actual; + var x; + + x = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + + expected = [ 5.0, 2.0, 3.0 ]; + actual = arrayWith( x, 0, 5.0 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isFloat64Array( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1.0, 5.0, 3.0 ]; + actual = arrayWith( x, 1, 5.0 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isFloat64Array( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1.0, 2.0, 5.0 ]; + actual = arrayWith( x, 2, 5.0 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isFloat64Array( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new array with an element at a specified index containing a provided value (int32)', function test( t ) { + var expected; + var actual; + var x; + + x = new Int32Array( [ 1, 2, 3 ] ); + + expected = new Int32Array( [ 5, 2, 3 ] ); + actual = arrayWith( x, 0, 5 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isInt32Array( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Int32Array( [ 1, 5, 3 ] ); + actual = arrayWith( x, 1, 5 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isInt32Array( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Int32Array( [ 1, 2, 5 ] ); + actual = arrayWith( x, 2, 5 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isInt32Array( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new array with an element at a specified index containing a provided value (complex128)', function test( t ) { + var expected; + var actual; + var x; + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + expected = new Complex128Array( [ 7.0, 8.0, 3.0, 4.0, 5.0, 6.0 ] ); + actual = arrayWith( x, 0, new Complex128( 7.0, 8.0 ) ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isComplex128Array( actual ), true, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + expected = new Complex128Array( [ 1.0, 2.0, 7.0, 8.0, 5.0, 6.0 ] ); + actual = arrayWith( x, 1, new Complex128( 7.0, 8.0 ) ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isComplex128Array( actual ), true, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + expected = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 7.0, 8.0 ] ); + actual = arrayWith( x, 2, new Complex128( 7.0, 8.0 ) ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isComplex128Array( actual ), true, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new array with an element at a specified index containing a provided value (accessors)', function test( t ) { + var expected; + var actual; + var x; + + x = new AccessorArray( [ 1, 2, 3 ] ); + + expected = [ 5, 2, 3 ]; + actual = arrayWith( x, 0, 5 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 5, 3 ]; + actual = arrayWith( x, 1, 5 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 2, 5 ]; + actual = arrayWith( x, 2, 5 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new array with an element at a specified index containing a provided value (array-like)', function test( t ) { + var expected; + var actual; + var x; + + x = { + 'length': 3, + '0': 1, + '1': 2, + '2': 3 + }; + + expected = [ 5, 2, 3 ]; + actual = arrayWith( x, 0, 5 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 5, 3 ]; + actual = arrayWith( x, 1, 5 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 2, 5 ]; + actual = arrayWith( x, 2, 5 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); diff --git a/base/without/README.md b/base/without/README.md new file mode 100644 index 00000000..634900d0 --- /dev/null +++ b/base/without/README.md @@ -0,0 +1,155 @@ + + +# without + +> Return a new array containing every element from an input array, except for the element at a specified index. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var without = require( '@stdlib/array/base/without' ); +``` + +#### without( x, index ) + +Returns a new array containing every element from an input array, except for the element at a specified index. + +```javascript +var x = [ 1, 2, 3, 4 ]; + +var out = without( x, 0 ); +// returns [ 2, 3, 4 ] + +out = without( x, -1 ); +// returns [ 1, 2, 3 ] +``` + +The function accepts the following arguments: + +- **x**: an input array. +- **index**: element index. + +### without.assign( x, index, out, stride, offset ) + +Copies every element from one array to another array, except for the element at a specified index. + +```javascript +var x = [ 1, 2, 3, 4 ]; + +var out = [ 0, 0, 0 ]; +var arr = without.assign( x, 0, out, 1, 0 ); +// returns [ 2, 3, 4 ] + +var bool = ( arr === out ); +// returns true +``` + +The function accepts the following arguments: + +- **x**: an input array. +- **index**: element index. +- **out**: output array. +- **stride**: output array stride. +- **offset**: output array offset. + +
+ + + + + +
+ +## Notes + +- Negative indices are resolved relative to the last array element, with the last element corresponding to `-1`. + +
+ + + + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var without = require( '@stdlib/array/base/without' ); + +// Define an array: +var opts = { + 'dtype': 'generic' +}; +var x = discreteUniform( 5, -100, 100, opts ); + +// Define an array containing random index values: +var indices = discreteUniform( 100, -x.length, x.length-1, opts ); + +// Randomly omit elements from the input array: +var i; +for ( i = 0; i < indices.length; i++ ) { + console.log( 'x = [%s]', without( x, indices[ i ] ).join( ',' ) ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/base/without/benchmark/benchmark.assign.length.js b/base/without/benchmark/benchmark.assign.length.js new file mode 100644 index 00000000..ad7cf999 --- /dev/null +++ b/base/without/benchmark/benchmark.assign.length.js @@ -0,0 +1,98 @@ +/** +* @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. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isArray = require( '@stdlib/assert/is-array' ); +var ones = require( './../../../base/ones' ); +var zeros = require( './../../../base/zeros' ); +var pkg = require( './../package.json' ).name; +var without = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out = zeros( len+1 ); + var x = ones( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = without.assign( x, i%len, out, 1, 0 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( len ); + bench( pkg+':assign:dtype=generic,len='+len, f ); + } +} + +main(); diff --git a/base/without/benchmark/benchmark.length.js b/base/without/benchmark/benchmark.length.js new file mode 100644 index 00000000..b339f695 --- /dev/null +++ b/base/without/benchmark/benchmark.length.js @@ -0,0 +1,96 @@ +/** +* @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. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isArray = require( '@stdlib/assert/is-array' ); +var ones = require( './../../../base/ones' ); +var pkg = require( './../package.json' ).name; +var without = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = ones( len+1 ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = without( x, i%len, i ); + if ( out.length !== len ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isArray( out ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( len ); + bench( pkg+':dtype=generic,len='+len, f ); + } +} + +main(); diff --git a/base/without/docs/repl.txt b/base/without/docs/repl.txt new file mode 100644 index 00000000..73607977 --- /dev/null +++ b/base/without/docs/repl.txt @@ -0,0 +1,73 @@ + +{{alias}}( x, index ) + Returns a new array containing every element from an input array, except for + the element at a specified index. + + Negative indices are resolved relative to the last array element, with the + last element corresponding to `-1`. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + index: integer + Index of the element to be omitted. + + Returns + ------- + out: ArrayLikeObject + Output array. + + Examples + -------- + > var x = [ 1, 2, 3, 4 ]; + > {{alias}}( x, 0 ) + [ 2, 3, 4 ] + > {{alias}}( x, -1 ) + [ 1, 2, 3 ] + > x + [ 1, 2, 3, 4 ] + + +{{alias}}.assign( x, index, out, stride, offset ) + Copies every element from one array to another array, except for the element + at a specified index. + + Negative indices are resolved relative to the last array element, with the + last element corresponding to `-1`. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + index: integer + Index of the element to be omitted. + + out: ArrayLikeObject + Output array. + + stride: integer + Output array stride. + + offset: integer + Output array offset. + + Returns + ------- + out: ArrayLikeObject + Output array. + + Examples + -------- + > var x = [ 1, 2, 3, 4 ]; + > var out = [ 0, 0, 0 ]; + > var arr = {{alias}}.assign( x, 0, out, 1, 0 ) + [ 2, 3, 4 ] + > var bool = ( arr === out ) + true + + See Also + -------- + diff --git a/base/without/docs/types/index.d.ts b/base/without/docs/types/index.d.ts new file mode 100644 index 00000000..e251ec9a --- /dev/null +++ b/base/without/docs/types/index.d.ts @@ -0,0 +1,218 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Collection, RealTypedArray, ComplexTypedArray, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Interface describing `without`. +*/ +interface ArrayWith { + /** + * Returns a new array containing every element from an input array, except for the element at a specified index. + * + * @param x - input array + * @param index - index of the element to exclude + * @returns output array + * + * @example + * var Float64Array = require( './../../../../float64' ); + * + * var x = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + * + * var out = without( x, 0 ); + * // returns [ 2.0, 3.0 ] + * + * @example + * var Complex128Array = require( './../../../../complex128' ); + * + * var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + * + * var out = without( x, 0 ); + * // returns [ 3.0, 4.0, 5.0, 6.0 ] + */ + ( x: T, index: number ): T; + + /** + * Returns a new array containing every element from an input array, except for the element at a specified index. + * + * @param x - input array + * @param index - index of the element to exclude + * @returns output array + * + * @example + * var x = [ 1, 2, 3 ]; + * + * var out = without( x, 0 ); + * // returns [ 2, 3 ] + * + * @example + * var x = [ 1, 2, 3, 4, 5, 6 ]; + * + * var out = without( x, 1 ); + * // returns [ 1, 3, 4, 5, 6 ] + */ + ( x: Collection, index: number ): Array; + + /** + * Copies every element from one array to another array, except for the element at a specified index. + * + * @param x - input array + * @param index - index of the element to exclude + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @returns output array + * + * @example + * var Float64Array = require( './../../../../float64' ); + * + * var x = [ 1, 2, 3, 4 ]; + * + * var out = new Float64Array( [ 0, 0, 0 ] ); + * var arr = without.assign( x, 0, out, 1, 0 ); + * // returns [ 2, 3, 4 ] + * + * var bool = ( arr === out ); + * // returns true + * + * @example + * var Complex128Array = require( './../../../../complex128' ); + * + * var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + * + * var out = new Complex128Array( x.length-1 ); + * var arr = without.assign( x, 0, out, 1, 0 ); + * // returns [ 3.0, 4.0, 5.0, 6.0 ] + * + * var bool = ( arr === out ); + * // returns true + */ + assign( x: Collection | AccessorArrayLike, index: number, out: T, stride: number, offset: number ): T; + + /** + * Copies every element from one array to another array, except for the element at a specified index. + * + * @param x - input array + * @param index - index of the element to exclude + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @returns output array + * + * @example + * var x = [ 1, 2, 3, 4 ]; + * + * var out = [ 0, 0, 0 ]; + * var arr = without.assign( x, 0, out, 1, 0 ); + * // returns [ 2, 3, 4 ] + * + * var bool = ( arr === out ); + * // returns true + */ + assign( x: Collection | AccessorArrayLike, index: number, out: Array, stride: number, offset: number ): Array; + + /** + * Copies every element from one array to another array, except for the element at a specified index. + * + * @param x - input array + * @param index - index of the element to exclude + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @returns output array + * + * @example + * var toAccessorArray = require( './../../../../base/to-accessor-array' ); + * + * var x = toAccessorArray( [ 1, 2, 3, 4 ] ); + * + * var out = toAccessorArray( [ 0, 0, 0 ] ); + * var arr = without.assign( x, 0, out, 1, 0 ); + * + * var v = out[ 0 ]; + * // returns 2 + * + * var bool = ( arr === out ); + * // returns true + */ + assign( x: Collection | AccessorArrayLike, index: number, out: AccessorArrayLike, stride: number, offset: number ): AccessorArrayLike; + + /** + * Copies every element from one array to another array, except for the element at a specified index. + * + * @param x - input array + * @param index - index of the element to exclude + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @returns output array + * + * @example + * var x = [ 1, 2, 3, 4 ]; + * + * var out = [ 0, 0, 0 ]; + * var arr = without.assign( x, 0, out, 1, 0 ); + * // returns [ 2, 3, 4 ] + * + * var bool = ( arr === out ); + * // returns true + */ + assign( x: Collection | AccessorArrayLike, index: number, out: Collection, stride: number, offset: number ): Collection; +} + +/** +* Returns a new array containing every element from an input array, except for the element at a specified index. +* +* @param x - input array +* @param index - index of the element to exclude +* @returns output array +* +* @example +* var x = [ 1, 2, 3 ]; +* +* var out = without( x, 0 ); +* // returns [ 2, 3 ] +* +* @example +* var x = [ 1, 2, 3, 4, 5, 6 ]; +* +* var out = without( x, 1 ); +* // returns [ 1, 3, 4, 5, 6 ] +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var x = [ 1, 2, 3, 4 ]; +* +* var out = new Float64Array( [ 0, 0, 0 ] ); +* var arr = without.assign( x, 0, out, 1, 0 ); +* // returns [ 2, 3, 4 ] +* +* var bool = ( arr === out ); +* // returns true +*/ +declare var without: ArrayWith; + + +// EXPORTS // + +export = without; diff --git a/base/without/docs/types/test.ts b/base/without/docs/types/test.ts new file mode 100644 index 00000000..66989a70 --- /dev/null +++ b/base/without/docs/types/test.ts @@ -0,0 +1,169 @@ +/* +* @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 Complex128Array = require( './../../../../complex128' ); +import Complex64Array = require( './../../../../complex64' ); +import toAccessorArray = require( './../../../../base/to-accessor-array' ); +import without = require( './index' ); + + +// TESTS // + +// The function returns an updated array... +{ + without( [ 1, 2, 3, 4 ], 0 ); // $ExpectType number[] + without( new Complex128Array( 5 ), 0 ); // $ExpectType Complex128Array + without( new Complex64Array( 5 ), 0 ); // $ExpectType Complex64Array + without( toAccessorArray( [ 1, 2, 3, 4 ] ), 0 ); // $ExpectType number[] +} + +// The compiler throws an error if the function is provided a first argument which is not a collection... +{ + without( 5, 0 ); // $ExpectError + without( true, 0 ); // $ExpectError + without( false, 0 ); // $ExpectError + without( null, 0 ); // $ExpectError + without( void 0, 0 ); // $ExpectError + without( {}, 0 ); // $ExpectError + without( ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + const x = [ 1, 2, 3, 4 ]; + + without( x, 'abc' ); // $ExpectError + without( x, true ); // $ExpectError + without( x, false ); // $ExpectError + without( x, null ); // $ExpectError + without( x, void 0 ); // $ExpectError + without( x, [ '1' ] ); // $ExpectError + without( x, {} ); // $ExpectError + without( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = [ 1, 2, 3, 4 ]; + + without(); // $ExpectError + without( x ); // $ExpectError + without( x, 0, 0 ); // $ExpectError +} + +// Attached to the main export is an `assign` method which returns a collection... +{ + const x = [ 1, 2, 3, 4 ]; + const y = new Complex128Array( 4 ); + + without.assign( x, 0, [ 0, 0, 0 ], 1, 0 ); // $ExpectType number[] + without.assign( x, 0, new Float64Array( 3 ), 1, 0 ); // $ExpectType Float64Array + without.assign( x, 0, new Float32Array( 3 ), 1, 0 ); // $ExpectType Float32Array + without.assign( x, 0, new Int32Array( 3 ), 1, 0 ); // $ExpectType Int32Array + without.assign( x, 0, new Int16Array( 3 ), 1, 0 ); // $ExpectType Int16Array + without.assign( x, 0, new Int8Array( 3 ), 1, 0 ); // $ExpectType Int8Array + without.assign( x, 0, new Uint32Array( 3 ), 1, 0 ); // $ExpectType Uint32Array + without.assign( x, 0, new Uint16Array( 3 ), 1, 0 ); // $ExpectType Uint16Array + without.assign( x, 0, new Uint8Array( 3 ), 1, 0 ); // $ExpectType Uint8Array + without.assign( x, 0, new Uint8ClampedArray( 3 ), 1, 0 ); // $ExpectType Uint8ClampedArray + without.assign( y, 0, new Complex128Array( 3 ), 1, 0 ); // $ExpectType Complex128Array + without.assign( y, 0, new Complex64Array( 3 ), 1, 0 ); // $ExpectType Complex64Array +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an array-like object... +{ + const out = [ 0, 0, 0 ]; + + without.assign( 1, 0, out, 1, 0 ); // $ExpectError + without.assign( true, 0, out, 1, 0 ); // $ExpectError + without.assign( false, 0, out, 1, 0 ); // $ExpectError + without.assign( null, 0, out, 1, 0 ); // $ExpectError + without.assign( void 0, 0, out, 1, 0 ); // $ExpectError + without.assign( {}, 0, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not a number... +{ + const x = [ 1, 2, 3, 4 ]; + const out = [ 0, 0, 0 ]; + + without.assign( x, '1', out, 1, 0 ); // $ExpectError + without.assign( x, true, out, 1, 0 ); // $ExpectError + without.assign( x, false, out, 1, 0 ); // $ExpectError + without.assign( x, null, out, 1, 0 ); // $ExpectError + without.assign( x, void 0, out, 1, 0 ); // $ExpectError + without.assign( x, [], out, 1, 0 ); // $ExpectError + without.assign( x, {}, out, 1, 0 ); // $ExpectError + without.assign( x, ( x: number ): number => x, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a third argument which is not an array-like object... +{ + const x = [ 1, 2, 3, 4 ]; + + without.assign( x, 0, 1, 1, 0 ); // $ExpectError + without.assign( x, 0, true, 1, 0 ); // $ExpectError + without.assign( x, 0, false, 1, 0 ); // $ExpectError + without.assign( x, 0, null, 1, 0 ); // $ExpectError + without.assign( x, 0, void 0, 1, 0 ); // $ExpectError + without.assign( x, 0, {}, 1, 0 ); // $ExpectError + without.assign( x, 0, ( x: number ): number => x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a fourth argument which is not a number... +{ + const x = [ 1, 2, 3, 4 ]; + const out = [ 0, 0, 0 ]; + + without.assign( x, 0, out, '1', 0 ); // $ExpectError + without.assign( x, 0, out, true, 0 ); // $ExpectError + without.assign( x, 0, out, false, 0 ); // $ExpectError + without.assign( x, 0, out, null, 0 ); // $ExpectError + without.assign( x, 0, out, void 0, 0 ); // $ExpectError + without.assign( x, 0, out, [], 0 ); // $ExpectError + without.assign( x, 0, out, {}, 0 ); // $ExpectError + without.assign( x, 0, out, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a fifth argument which is not a number... +{ + const x = [ 1, 2, 3, 4 ]; + const out = [ 0, 0, 0 ]; + + without.assign( x, 0, out, 1, '1' ); // $ExpectError + without.assign( x, 0, out, 1, true ); // $ExpectError + without.assign( x, 0, out, 1, false ); // $ExpectError + without.assign( x, 0, out, 1, null ); // $ExpectError + without.assign( x, 0, out, 1, void 0 ); // $ExpectError + without.assign( x, 0, out, 1, [] ); // $ExpectError + without.assign( x, 0, out, 1, {} ); // $ExpectError + without.assign( x, 0, out, 1, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = [ 1, 2, 3, 4 ]; + const out = [ 0, 0, 0 ]; + + without.assign(); // $ExpectError + without.assign( x ); // $ExpectError + without.assign( x, 0 ); // $ExpectError + without.assign( x, 0, out ); // $ExpectError + without.assign( x, 0, out, 1 ); // $ExpectError + without.assign( x, 0, out, 1, 0, {} ); // $ExpectError +} diff --git a/base/without/examples/index.js b/base/without/examples/index.js new file mode 100644 index 00000000..74857452 --- /dev/null +++ b/base/without/examples/index.js @@ -0,0 +1,37 @@ +/** +* @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. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var without = require( './../lib' ); + +// Define an array: +var opts = { + 'dtype': 'generic' +}; +var x = discreteUniform( 5, -100, 100, opts ); + +// Define an array containing random index values: +var indices = discreteUniform( 100, -x.length, x.length-1, opts ); + +// Randomly omit elements from the input array: +var i; +for ( i = 0; i < indices.length; i++ ) { + console.log( 'x = [%s]', without( x, indices[ i ] ).join( ',' ) ); +} diff --git a/base/without/lib/assign.js b/base/without/lib/assign.js new file mode 100644 index 00000000..5fa9c95e --- /dev/null +++ b/base/without/lib/assign.js @@ -0,0 +1,220 @@ +/** +* @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. +*/ + +'use strict'; + +// MODULES // + +var isComplexDataType = require( './../../../base/assert/is-complex-floating-point-data-type' ); +var isBooleanDataType = require( './../../../base/assert/is-boolean-data-type' ); +var arraylike2object = require( './../../../base/arraylike2object' ); +var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); +var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var format = require( '@stdlib/string/format' ); + + +// FUNCTIONS // + +/** +* Copies every element from one array to another array, except for the element at a specified index. +* +* @private +* @param {Collection} x - input array +* @param {integer} index - element index +* @param {Collection} out - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @returns {Collection} output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* +* var out = [ 0, 0, 0 ]; +* var arr = indexed( x, 0, out, 1, 0 ); +* // returns [ 2, 3, 4 ] +* +* var bool = ( arr === out ); +* // returns true +*/ +function indexed( x, index, out, stride, offset ) { + var io; + var i; + + io = offset; + for ( i = 0; i < x.length; i++ ) { + if ( i === index ) { + continue; + } + out[ io ] = x[ i ]; + io += stride; + } + return out; +} + +/** +* Copies every element from one accessor array to another accessor array, except for the element at a specified index. +* +* @private +* @param {Object} x - input array object +* @param {integer} index - element index +* @param {Object} out - output array object +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @returns {Collection} output array +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = toAccessorArray( [ 1, 2, 3, 4 ] ); +* +* var out = toAccessorArray( [ 0, 0, 0 ] ); +* var arr = accessors( arraylike2object( x ), 0, arraylike2object( out ), 1, 0 ); +* +* var v = arr.get( 0 ); +* // returns 2 +*/ +function accessors( x, index, out, stride, offset ) { + var xdata; + var odata; + var xget; + var oset; + var io; + var i; + + xdata = x.data; + odata = out.data; + + xget = x.accessors[ 0 ]; + oset = out.accessors[ 1 ]; + + io = offset; + for ( i = 0; i < xdata.length; i++ ) { + if ( i === index ) { + continue; + } + oset( odata, io, xget( xdata, i ) ); + io += stride; + } + return odata; +} + +/** +* Copies every element from one complex array to another complex array, except for the element at a specified index. +* +* @private +* @param {Collection} x - real-valued floating-point input array view +* @param {integer} index - element index +* @param {Collection} out - real-valued floating-point output array view +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @returns {Collection} output array view +* +* @example +* var Complex128 = require( '@stdlib/complex/float64/ctor' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* +* var out = new Float64Array( 2 ); +* var arr = complex( x, 0, out, 1, 0 ); +* // returns [ 3.0, 4.0 ] +*/ +function complex( x, index, out, stride, offset ) { + var so; + var io; + var i; + var j; + + so = stride * 2; // multiply by 2, as real-valued array consists of interleaved real and imaginary components + io = offset * 2; + for ( i = 0; i < x.length/2; i++ ) { + if ( i === index ) { + continue; + } + j = i * 2; + out[ io ] = x[ j ]; + out[ io+1 ] = x[ j+1 ]; + io += so; + } + return out; +} + + +// MAIN // + +/** +* Copies every element from one array to another array, except for the element at a specified index. +* +* @param {Collection} x - input array +* @param {integer} index - element index +* @param {Collection} out - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @throws {RangeError} second argument must not exceed array bounds +* @returns {Collection} output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* +* var out = [ 0, 0, 0 ]; +* var arr = assign( x, 0, out, 1, 0 ); +* // returns [ 2, 3, 4 ] +* +* var bool = ( arr === out ); +* // returns true +*/ +function assign( x, index, out, stride, offset ) { + var xo; + var oo; + + index = normalizeIndex( index, x.length-1 ); + if ( index < 0 ) { + throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%d`.', index ) ); + } + xo = arraylike2object( x ); + oo = arraylike2object( out ); + if ( xo.accessorProtocol || oo.accessorProtocol ) { + // Note: we only explicitly support a limited set of dtype-to-dtype pairs, as this function should not be concerned with casting rules, etc. That is left to userland... + if ( + isComplexDataType( xo.dtype ) && + isComplexDataType( oo.dtype ) + ) { + complex( reinterpret( x, 0 ), index, reinterpret( out, 0 ), stride, offset ); // eslint-disable-line max-len + return out; + } + if ( + isBooleanDataType( xo.dtype ) && + isBooleanDataType( oo.dtype ) + ) { + indexed( reinterpretBoolean( x, 0 ), index, reinterpretBoolean( out, 0 ), stride, offset ); // eslint-disable-line max-len + return out; + } + accessors( xo, index, oo, stride, offset ); + return out; + } + indexed( x, index, out, stride, offset ); + return out; +} + + +// EXPORTS // + +module.exports = assign; diff --git a/base/without/lib/index.js b/base/without/lib/index.js new file mode 100644 index 00000000..e08a3907 --- /dev/null +++ b/base/without/lib/index.js @@ -0,0 +1,64 @@ +/** +* @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. +*/ + +'use strict'; + +/** +* Return a new array containing every element from an input array, except for the element at a specified index. +* +* @module @stdlib/array/base/without +* +* @example +* var without = require( '@stdlib/array/base/without' ); +* +* var x = [ 1, 2, 3, 4 ]; +* +* var v = without( x, 0 ); +* // returns [ 2, 3, 4 ] +* +* v = without( x, -2 ); +* // returns [ 1, 2, 4 ] +* +* @example +* var without = require( '@stdlib/array/base/without' ); +* +* var x = [ 1, 2, 3, 4 ]; +* +* var out = [ 0, 0, 0 ]; +* var arr = without.assign( x, 0, out, 1, 0 ); +* // returns [ 2, 3, 4 ] +* +* var bool = ( arr === out ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; diff --git a/base/without/lib/main.js b/base/without/lib/main.js new file mode 100644 index 00000000..7fe97454 --- /dev/null +++ b/base/without/lib/main.js @@ -0,0 +1,67 @@ +/** +* @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. +*/ + +'use strict'; + +// MODULES // + +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var zeros = require( './../../../zeros' ); +var dtype = require( './../../../dtype' ); +var format = require( '@stdlib/string/format' ); +var assign = require( './assign.js' ); + + +// MAIN // + +/** +* Returns a new array containing every element from an input array, except for the element at a specified index. +* +* @param {Collection} x - input array +* @param {integer} index - element index +* @throws {RangeError} second argument must not exceed array bounds +* @returns {Collection} output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* +* var v = without( x, 0 ); +* // returns [ 2, 3, 4 ] +* +* v = without( x, 1 ); +* // returns [ 1, 3, 4 ] +* +* v = without( x, -2 ); +* // returns [ 1, 2, 4 ] +*/ +function without( x, index ) { + var out; + + index = normalizeIndex( index, x.length-1 ); + if ( index < 0 ) { + throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%d`.', index ) ); + } + out = zeros( x.length-1, dtype( x ) || 'generic' ); + assign( x, index, out, 1, 0 ); + return out; +} + + +// EXPORTS // + +module.exports = without; diff --git a/base/without/package.json b/base/without/package.json new file mode 100644 index 00000000..563b0d9d --- /dev/null +++ b/base/without/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/array/base/without", + "version": "0.0.0", + "description": "Return a new array containing every element from an input array, except for the element at a specified index.", + "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", + "base", + "array", + "typed", + "collection", + "vector", + "omit", + "exclude", + "skip", + "copy" + ], + "__stdlib__": {} +} diff --git a/base/without/test/test.assign.js b/base/without/test/test.assign.js new file mode 100644 index 00000000..49c9ac49 --- /dev/null +++ b/base/without/test/test.assign.js @@ -0,0 +1,450 @@ +/** +* @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. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int32Array = require( './../../../int32' ); +var Float64Array = require( './../../../float64' ); +var Complex128Array = require( './../../../complex128' ); +var AccessorArray = require( './../../../base/accessor' ); +var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' ); +var zeros = require( './../../../zeros' ); +var without = require( './../lib/assign.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof without, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (generic)', function test( t ) { + var values; + var out; + var x; + var i; + + x = [ 1, 2, 3 ]; + out = zeros( x.length-1, 'generic' ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + without( x, value, out, 1, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (float64)', function test( t ) { + var values; + var out; + var x; + var i; + + x = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + out = zeros( x.length-1, 'float64' ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + without( x, value, out, 1, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (int32)', function test( t ) { + var values; + var out; + var x; + var i; + + x = new Int32Array( [ 1, 2, 3 ] ); + out = zeros( x.length-1, 'int32' ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + without( x, value, out, 1, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (complex128)', function test( t ) { + var values; + var out; + var x; + var i; + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + out = zeros( x.length-1, 'complex128' ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + without( x, value, out, 1, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (accessors)', function test( t ) { + var values; + var out; + var x; + var i; + + x = new AccessorArray( [ 1, 2, 3 ] ); + out = new AccessorArray( zeros( x.length-1, 'generic' ) ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + without( x, value, out, 1, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (array-like)', function test( t ) { + var values; + var out; + var x; + var i; + + x = { + 'length': 3, + '0': 1, + '1': 2, + '2': 3 + }; + + out = { + 'length': 2, + '0': 0, + '1': 0 + }; + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + without( x, value, out, 1, 0 ); + }; + } +}); + +tape( 'the function copies elements to another array and sets an element at a specified index to a provided value (generic)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ 1, 2, 3 ]; + + out = zeros( x.length-1, 'generic' ); + expected = [ 2, 3 ]; + actual = without( x, 0, out, 1, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + out = zeros( (x.length-1)*2, 'generic' ); + expected = [ 1, 0, 3, 0 ]; + actual = without( x, 1, out, 2, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + out = zeros( (x.length-1)*2, 'generic' ); + expected = [ 0, 2, 0, 1 ]; + actual = without( x, 2, out, -2, out.length-1 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies elements to another array and sets an element at a specified index to a provided value (float64)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + + out = zeros( x.length-1, 'float64' ); + expected = new Float64Array( [ 2.0, 3.0 ] ); + actual = without( x, 0, out, 1, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + out = zeros( (x.length-1)*2, 'float64' ); + expected = new Float64Array( [ 1.0, 0.0, 3.0, 0.0 ] ); + actual = without( x, 1, out, 2, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + out = zeros( (x.length-1)*2, 'float64' ); + expected = new Float64Array( [ 0.0, 2.0, 0.0, 1.0 ] ); + actual = without( x, 2, out, -2, out.length-1 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies elements to another array and sets an element at a specified index to a provided value (int32)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = new Int32Array( [ 1, 2, 3 ] ); + + out = zeros( x.length-1, 'int32' ); + expected = new Int32Array( [ 2, 3 ] ); + actual = without( x, 0, out, 1, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + out = zeros( (x.length-1)*2, 'int32' ); + expected = new Int32Array( [ 1, 0, 3, 0 ] ); + actual = without( x, 1, out, 2, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + out = zeros( (x.length-1)*2, 'int32' ); + expected = new Int32Array( [ 0, 2, 0, 1 ] ); + actual = without( x, 2, out, -2, out.length-1 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies elements to another array and sets an element at a specified index to a provided value (complex128)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + out = zeros( x.length-1, 'complex128' ); + expected = new Complex128Array( [ 3.0, 4.0, 5.0, 6.0 ] ); + actual = without( x, 0, out, 1, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + out = zeros( (x.length-1)*2, 'complex128' ); + expected = new Complex128Array( [ 1.0, 2.0, 0.0, 0.0, 5.0, 6.0, 0.0, 0.0 ] ); // eslint-disable-line max-len + actual = without( x, 1, out, 2, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + out = zeros( (x.length-1)*2, 'complex128' ); + expected = new Complex128Array( [ 0.0, 0.0, 3.0, 4.0, 0.0, 0.0, 1.0, 2.0 ] ); // eslint-disable-line max-len + actual = without( x, 2, out, -2, out.length-1 ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies elements to another array and sets an element at a specified index to a provided value (accessors)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = new AccessorArray( [ 1, 2, 3 ] ); + + out = new AccessorArray( zeros( x.length-1, 'generic' ) ); + expected = [ 2, 3 ]; + actual = without( x, 0, out, 1, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + out = new AccessorArray( zeros( (x.length-1)*2, 'generic' ) ); + expected = [ 1, 0, 3, 0 ]; + actual = without( x, 1, out, 2, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + out = new AccessorArray( zeros( (x.length-1)*2, 'generic' ) ); + expected = [ 0, 2, 0, 1 ]; + actual = without( x, 2, out, -2, out.length-1 ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + t.end(); + + function isEqual( actual, expected ) { + var i; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual.get( i ), expected[ i ], 'returns expected value' ); + } + } +}); + +tape( 'the function copies elements to another array and sets an element at a specified index to a provided value (array-like)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = { + 'length': 3, + '0': 1, + '1': 2, + '2': 3 + }; + + out = { + 'length': 2, + '0': 0, + '1': 0 + }; + expected = [ 2, 3 ]; + actual = without( x, 0, out, 1, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + out = { + 'length': 4, + '0': 0, + '1': 0, + '2': 0, + '3': 0 + }; + expected = [ 1, 0, 3, 0 ]; + actual = without( x, 1, out, 2, 0 ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + out = { + 'length': 4, + '0': 0, + '1': 0, + '2': 0, + '3': 0 + }; + expected = [ 0, 2, 0, 1 ]; + actual = without( x, 2, out, -2, out.length-1 ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + t.end(); + + function isEqual( actual, expected ) { + var i; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' ); + } + } +}); diff --git a/base/without/test/test.js b/base/without/test/test.js new file mode 100644 index 00000000..e8736174 --- /dev/null +++ b/base/without/test/test.js @@ -0,0 +1,41 @@ +/** +* @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. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var hasMethod = require( '@stdlib/assert/is-method' ); +var without = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof without, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( hasOwnProp( without, 'assign' ), true, 'returns expected value' ); + t.strictEqual( hasMethod( without, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/base/without/test/test.main.js b/base/without/test/test.main.js new file mode 100644 index 00000000..6b760707 --- /dev/null +++ b/base/without/test/test.main.js @@ -0,0 +1,400 @@ +/** +* @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. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int32Array = require( './../../../int32' ); +var Float64Array = require( './../../../float64' ); +var Complex128Array = require( './../../../complex128' ); +var AccessorArray = require( './../../../base/accessor' ); +var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isFloat64Array = require( '@stdlib/assert/is-float64array' ); +var isInt32Array = require( '@stdlib/assert/is-int32array' ); +var isComplex128Array = require( '@stdlib/assert/is-complex128array' ); +var without = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof without, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (generic)', function test( t ) { + var values; + var x; + var i; + + x = [ 1, 2, 3 ]; + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + without( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (float64)', function test( t ) { + var values; + var x; + var i; + + x = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + without( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (int32)', function test( t ) { + var values; + var x; + var i; + + x = new Int32Array( [ 1, 2, 3 ] ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + without( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (complex128)', function test( t ) { + var values; + var x; + var i; + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + without( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (accessors)', function test( t ) { + var values; + var x; + var i; + + x = new AccessorArray( [ 1, 2, 3 ] ); + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + without( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds (array-like)', function test( t ) { + var values; + var x; + var i; + + x = { + 'length': 3, + '0': 1, + '1': 2, + '2': 3 + }; + + values = [ + 10, + 100, + 1000, + -10, + -100, + -1000 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + without( x, value ); + }; + } +}); + +tape( 'the function returns a new array containing every element in an input array, except for an element at a specified index (generic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ 1, 2, 3 ]; + + expected = [ 2, 3 ]; + actual = without( x, 0 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 3 ]; + actual = without( x, 1 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 2 ]; + actual = without( x, 2 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new array containing every element in an input array, except for an element at a specified index (float64)', function test( t ) { + var expected; + var actual; + var x; + + x = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + + expected = [ 2.0, 3.0 ]; + actual = without( x, 0 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isFloat64Array( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1.0, 3.0 ]; + actual = without( x, 1 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isFloat64Array( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1.0, 2.0 ]; + actual = without( x, 2 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isFloat64Array( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new array containing every element in an input array, except for an element at a specified index (int32)', function test( t ) { + var expected; + var actual; + var x; + + x = new Int32Array( [ 1, 2, 3 ] ); + + expected = new Int32Array( [ 2, 3 ] ); + actual = without( x, 0 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isInt32Array( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Int32Array( [ 1, 3 ] ); + actual = without( x, 1 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isInt32Array( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Int32Array( [ 1, 2 ] ); + actual = without( x, 2 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isInt32Array( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new array containing every element in an input array, except for an element at a specified index (complex128)', function test( t ) { + var expected; + var actual; + var x; + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + expected = new Complex128Array( [ 3.0, 4.0, 5.0, 6.0 ] ); + actual = without( x, 0 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isComplex128Array( actual ), true, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + expected = new Complex128Array( [ 1.0, 2.0, 5.0, 6.0 ] ); + actual = without( x, 1 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isComplex128Array( actual ), true, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + expected = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + actual = without( x, 2 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isComplex128Array( actual ), true, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new array containing every element in an input array, except for an element at a specified index (accessors)', function test( t ) { + var expected; + var actual; + var x; + + x = new AccessorArray( [ 1, 2, 3 ] ); + + expected = [ 2, 3 ]; + actual = without( x, 0 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 3 ]; + actual = without( x, 1 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 2 ]; + actual = without( x, 2 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new array containing every element in an input array, except for an element at a specified index (array-like)', function test( t ) { + var expected; + var actual; + var x; + + x = { + 'length': 3, + '0': 1, + '1': 2, + '2': 3 + }; + + expected = [ 2, 3 ]; + actual = without( x, 0 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 3 ]; + actual = without( x, 1 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 2 ]; + actual = without( x, 2 ); + + t.notEqual( actual, x, 'returns different reference' ); + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +});