From 332f1384d412ef6cd84df83c2ecedb359549ed2a Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 26 Feb 2024 15:26:20 +0000 Subject: [PATCH] Auto-generated commit --- .npmignore | 3 +- CONTRIBUTORS | 2 + base/with/README.md | 151 +++++++++++++++++++++++++++ base/with/benchmark/benchmark.js | 51 +++++++++ base/with/docs/repl.txt | 30 ++++++ base/with/docs/types/index.d.ts | 91 ++++++++++++++++ base/with/docs/types/test.ts | 68 ++++++++++++ base/with/examples/index.js | 45 ++++++++ base/with/lib/index.js | 45 ++++++++ base/with/lib/main.js | 106 +++++++++++++++++++ base/with/package.json | 66 ++++++++++++ base/with/test/test.js | 173 +++++++++++++++++++++++++++++++ 12 files changed, 830 insertions(+), 1 deletion(-) create mode 100644 base/with/README.md create mode 100644 base/with/benchmark/benchmark.js create mode 100644 base/with/docs/repl.txt create mode 100644 base/with/docs/types/index.d.ts create mode 100644 base/with/docs/types/test.ts create mode 100644 base/with/examples/index.js create mode 100644 base/with/lib/index.js create mode 100644 base/with/lib/main.js create mode 100644 base/with/package.json create mode 100644 base/with/test/test.js diff --git a/.npmignore b/.npmignore index 5c7f09d7..8eea7dc2 100644 --- a/.npmignore +++ b/.npmignore @@ -29,8 +29,9 @@ branches.md .postinstall.json Makefile -# Ignore `binding.gyp` file to avoid compilation of native addon when installing package: +# Ignore files to avoid compilation of native addon when installing package: binding.gyp +include.gypi # Directories # ############### diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 72675add..26c41fe0 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -15,6 +15,7 @@ Dominik Moritz Dorrin Sotoudeh Frank Kovacs GUNJ JOSHI +Golden <103646877+AuenKr@users.noreply.github.com> Harshita Kalani James Gelok Jaysukh Makvana @@ -24,6 +25,7 @@ Jordan Gallivan <115050475+Jordan-Gallivan@users.noreply.github.com> Joris Labie Justin Dennison Karthik Prakash <116057817+skoriop@users.noreply.github.com> +Khaldon Marcus Fantham Matt Cochrane Milan Raj diff --git a/base/with/README.md b/base/with/README.md new file mode 100644 index 00000000..73d360ad --- /dev/null +++ b/base/with/README.md @@ -0,0 +1,151 @@ + + +# withArray + +> Return a new array after replacing an index with a given value. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var withArray = require( '@stdlib/array/base/with' ); +``` + +#### withArray( x, index, value ) + +Return a new array after updating an index into the input array. + +```javascript +var x = [ 1, 2, 3, 4 ]; + +var out = withArray( x, 0, 5 ); +// returns [5, 2, 3, 4] + +out = withArray( x, -1, 6 ); +// returns [1, 2, 3, 6] + +``` + +The function accepts the following arguments: + +- **x**: an input array. +- **index**: element index. +- **value**: replacement value. + +
+ + + + + +
+ +## 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: + + ```text + x.with( index, value ) + ``` + + If provided an array-like object without a `with` method, the function manually shallow copied that object and assign provided value to that index. + +- Negative indices are resolved relative to the last array element, with the last element corresponding to `-1`. + +- If provided out-of-bounds indices, the function always returns `undefined`. + +
+ + + + + +
+ +## Examples + + + +```javascript +var discreteuniform = require( '@stdlib/random/array/discrete-uniform' ); +var withArray = require( '@stdlib/array/base/with' ); +var rand = require( '@stdlib/random/base/randu' ); +var x; +var indices; + +// Define an array: +x = discreteuniform( 10, -100, 100 ); + +// Define an array containing random index values: +indices = discreteuniform( 100, -x.length, x.length-1 ); + +// Randomly selected values from the input array: +var i; +var index; +var newvalue; +var updatedarray; +for (i = 0; i < indices.length; i++) { + index = indices[i]; + newvalue = rand(); // Random value between -100 and 100 + updatedarray = withArray(x, index, newvalue); // Update the value at the given index + console.log('Updated x[%d] to %d', index, newvalue); + console.log('Updated array:', updatedarray); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/base/with/benchmark/benchmark.js b/base/with/benchmark/benchmark.js new file mode 100644 index 00000000..9b99fd77 --- /dev/null +++ b/base/with/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @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 withArray = require( './../../../base/with/lib' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var bench = require( '@stdlib/bench' ); +var rand = require( '@stdlib/random/base/randu' ); +var pkg = require( './../../../base/with/package.json' ).name; + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var value; + var x; + var v; + var i; + var j; + + x = uniform( 100, 0.0, 10.0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = ( i%20 ); + value = rand(); + v = withArray( x, j, value ); + b.equal(v[j], value, 'index ' + j + ' should be updated to ' + value); + } + b.toc(); + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/base/with/docs/repl.txt b/base/with/docs/repl.txt new file mode 100644 index 00000000..32da2ed8 --- /dev/null +++ b/base/with/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( x, index, value ) + Return a new array after replacing an index with a given value. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + index: integer + Index of the element to be replaced. + + value: any + Value to replace the element at the specified index with. + + Returns + ------- + out: ArrayLikeObject + Updated array. + + Examples + -------- + > var x = [ 1, 2, 3, 4 ]; + > {{alias}}( x, 0, 5 ) + [ 5, 2, 3, 4 ] + > {{alias}}( x, -1, 6 ) + [ 1, 2, 3, 6 ] + + See Also + -------- diff --git a/base/with/docs/types/index.d.ts b/base/with/docs/types/index.d.ts new file mode 100644 index 00000000..4149c510 --- /dev/null +++ b/base/with/docs/types/index.d.ts @@ -0,0 +1,91 @@ +/* +* @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, AccessorArrayLike, Complex128Array, Complex64Array } from '@stdlib/types/array'; + +/** +* Returns a new array after updating an index into the input array. +* +* 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: +* +* x.with( index, value ) +* +* If provided an array-like object without a `with` method, the function manually +* shallow copied that object and assign provided value to that index. +* +* Negative indices are resolved relative to the last array element, with the last +* element corresponding to `-1`. +* +* If provided out-of-bounds indices, the function always returns `undefined`. +* +* @param x - input array +* @param index - element index +* @param value - replacement value +* @returns updated array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* var out = with( x, 0, 5 ); +* // returns [ 5, 2, 3, 4 ] + +* @example +* var out = with( x, -1, 6 ); +* // returns [ 1, 2, 3, 6 ] +*/ + +/** + * Sets the value at the specified index in a Complex128Array. + * + * @param x - Complex128Array to modify + * @param index - index at which to set the value + * @param value - new value to set + * @returns modified Complex128Array if successful; otherwise, throws a range error. + */ +declare function withArray( x: Complex128Array, index: number, value: any ): Complex128Array | void; + +/** + * Sets the value at the specified index in a Complex64Array. + * + * @param x - Complex64Array to modify + * @param index - index at which to set the value + * @param value - new value to set + * @returns modified Complex64Array if successful; otherwise, throws a range error + */ +declare function withArray( x: Complex64Array, index: number, value: any ): Complex64Array | void; + +/** + * Sets the value at the specified index in an array and returns the modified array. + * + * @template T - type of elements in the array + * @param x - array to modify, which can be either a Collection or an AccessorArrayLike + * @param index - index at which to set the value + * @param value - new value to set + * @returns modified array if successful; otherwise, throws range error + */ +declare function withArray< T = unknown >( x: Collection | AccessorArrayLike, index: number, value: any ): Collection | AccessorArrayLike | void; + + +// EXPORTS // + +export = withArray; diff --git a/base/with/docs/types/test.ts b/base/with/docs/types/test.ts new file mode 100644 index 00000000..73a465c6 --- /dev/null +++ b/base/with/docs/types/test.ts @@ -0,0 +1,68 @@ +/* +* @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 withArray = require( './index' ); + + +// TESTS // + +// The function returns an updated array... +{ + withArray( [ 1, 2, 3, 4 ], 0, 5 ); // $ExpectType void | Collection | AccessorArrayLike + withArray( new Complex128Array( 5 ), 0, { 're': 1.0, 'im': 1.0 } ); // $ExpectType void | Complex128Array + withArray( new Complex64Array( 5 ), 0, { 're': 1.0, 'im': 1.0 } ); // $ExpectType void | Complex64Array + withArray( toAccessorArray( [ 1, 2, 3, 4 ] ), 0, 5 ); // $ExpectType void | Collection | AccessorArrayLike +} + +// The compiler throws an error if the function is provided a first argument which is not a collection... +{ + withArray( 5, 0, 5 ); // $ExpectError + withArray( true, 0, 5 ); // $ExpectError + withArray( false, 0, 5 ); // $ExpectError + withArray( null, 0, 5 ); // $ExpectError + withArray( void 0, 0, 5 ); // $ExpectError + withArray( {}, 0, 5 ); // $ExpectError + withArray( ( x: number ): number => x, 0, 5 ); // $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 ]; + + withArray( x, 'abc', 5 ); // $ExpectError + withArray( x, true, 5 ); // $ExpectError + withArray( x, false, 5 ); // $ExpectError + withArray( x, null, 5 ); // $ExpectError + withArray( x, void 0, 5 ); // $ExpectError + withArray( x, [ '1' ], 5 ); // $ExpectError + withArray( x, {}, 5 ); // $ExpectError + withArray( x, ( x: number ): number => x, 5 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = [ 1, 2, 3, 4 ]; + + withArray(); // $ExpectError + withArray( x ); // $ExpectError + withArray( x, 0 ); // $ExpectError + withArray( x, 0, 0, 5 ); // $ExpectError +} diff --git a/base/with/examples/index.js b/base/with/examples/index.js new file mode 100644 index 00000000..6457e36b --- /dev/null +++ b/base/with/examples/index.js @@ -0,0 +1,45 @@ +/** +* @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 rand = require( '@stdlib/random/base/randu' ); +var withArray = require( './../lib' ); +var indices; +var x; +var i; + +// Define an array: +x = discreteUniform( 10, -100, 100 ); + +// Define an array containing random index values: +indices = discreteUniform( 100, -x.length, x.length-1 ); + +// Randomly selected values from the input array: +var index; +var newValue; +var updatedArray; +for ( i = 0; i < indices.length; i++ ) { + index = indices[i]; + newValue = rand(); // Random value between -100 and 100 + + updatedArray = withArray(x, index, newValue); // Update the value at the random index + console.log('Updated x[%d] to %d', index, newValue); + console.log('Updated array:', updatedArray); +} diff --git a/base/with/lib/index.js b/base/with/lib/index.js new file mode 100644 index 00000000..e8476844 --- /dev/null +++ b/base/with/lib/index.js @@ -0,0 +1,45 @@ +/** +* @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 an updated array after replacing an index with a given value. +* +* @module @stdlib/array/base/with +* +* @example +* var withArray = require( '@stdlib/array/base/with' ); +* +* var x = [ 1, 2, 3, 4 ]; +* +* var v = withArray( x, 0 ,5 ); +* // returns [ 5, 2, 3, 4 ] +* +* v = withArray( x, -2, -1 ); +* // returns [ 1, 2, -1, 4 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/base/with/lib/main.js b/base/with/lib/main.js new file mode 100644 index 00000000..82f85a7b --- /dev/null +++ b/base/with/lib/main.js @@ -0,0 +1,106 @@ +/** +* @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 resolveGetter = require( './../../../base/resolve-getter' ); + + +// FUNCTIONS // + +/** +* Tests whether an object has a specified method. +* +* @private +* @param {Object} obj - input object +* @param {string} method - method name +* @returns {boolean} boolean indicating whether an object has a specified method +* +* @example +* var bool = hasMethod( [], 'map' ); +* // returns true +* +* @example +* var bool = hasMethod( [], 'beep' ); +* // returns false +*/ +function hasMethod( obj, method ) { + return ( typeof obj[ method ] === 'function' ); +} + + +// MAIN // + +/** +* Returns a new array after replacing an index with a given value. +* +* @param {Collection} x - input array +* @param {integer} index - element index +* @param {any} value - value assigned to that particular index +* @throws {RangeError} if the index is out of bounds +* @returns {Collection} modified array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* +* var v = withArray( x, 0 ,5 ); +* // returns [ 5, 2, 3, 4 ] +* +* v = withArray( x, 1 , 6 ); +* // returns [ 1, 6, 3, 4 ] +* +* v = withArray(x, -2 , 7); +* // returns [ 1, 2, 7, 4 ] +*/ +function withArray( x, index, value ) { + var modifiedArray= []; + var temp; + var get; + var k; + + if ( index < 0 ) { + index += x.length; + } + + if ( index < 0 || index >= x.length ) { + throw new RangeError( 'Index out of bounds' ); + } + + if ( hasMethod( x, 'with' ) ) { + return x.with( index, value ); + } + + for ( k = 0; k < x.length; k++ ) { + if ( k === index ) { + modifiedArray.push( value ); + } + else { + get= resolveGetter( x ); + temp = get( x, k ); + modifiedArray.push( temp ); + } + } + return modifiedArray; +} + + +// EXPORTS // + +module.exports = withArray; diff --git a/base/with/package.json b/base/with/package.json new file mode 100644 index 00000000..1b624221 --- /dev/null +++ b/base/with/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/array/base/with", + "version": "0.0.0", + "description": "Return a new array after replacing an index with a given value.", + "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", + "with", + "get", + "getter", + "accessor", + "access", + "retrieve" + ], + "__stdlib__": {} + } \ No newline at end of file diff --git a/base/with/test/test.js b/base/with/test/test.js new file mode 100644 index 00000000..661f95fd --- /dev/null +++ b/base/with/test/test.js @@ -0,0 +1,173 @@ +/** +* @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 Complex128Array = require( './../../../complex128' ); +var toAccessorArray = require( './../../../base/to-accessor-array' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var withArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof withArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a new array with the specified index modified to the provided value (generic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ 1, 2, 3, 4, 5, 6 ]; + + expected = [ 1, 2, 3, 4, 10, 6 ]; + actual = withArray( x, -2, 10 ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.notEqual( x, actual, 'returns a new array' ); + t.end(); +}); + +tape('the function throws a RangeError if provided an out-of-bounds index (generic)', function test( t ) { + var actual; + var x; + + x = [ 1, 2, 3, 4, 5, 6 ]; + + actual = function outofbounds() { + withArray( x, 10, 10 ); + }; + t.throws( actual, RangeError, 'throws RangeError for positive out-of-bounds index' ); + + actual = function outofbounds() { + withArray( x, -10, 10 ); + }; + t.throws( actual, RangeError, 'throws RangeError for negative out-of-bounds index' ); + + t.end(); +}); + +tape( 'the function returns a new array with the specified index modified to the provided value (typed array)', function test( t ) { + var expected; + var actual; + var x; + + x = new Int32Array( [ 1, 2, 3, 4, 5, 6 ] ); + + expected = new Int32Array( [ 1, 2, 10, 4, 5, 6 ] ); + actual = withArray( x, 2, 10 ); + t.deepEqual( actual, expected, 'returns expected value' ); + + // // Ensure input array is not mutated: + t.notEqual( x, actual, 'returns a new array' ); + + t.end(); +}); + +tape('the function throws a RangeError if provided an out-of-bounds index (typed array)', function test( t ) { + var actual; + var x = new Int32Array( [ 1, 2, 3, 4, 5, 6 ] ); + + actual = function outofbounds() { + withArray( x, 10, 10 ); + }; + t.throws( actual, RangeError, 'throws RangeError for positive out-of-bounds index' ); + + actual = function outofbounds() { + withArray( x, -10, 10 ); + }; + t.throws( actual, RangeError, 'throws RangeError for negative out-of-bounds index' ); + + t.end(); +}); + +tape( 'the function returns a new array with the specified index modified to the provided value (complex typed array)', function test( t ) { + var expected; + var actual; + var x; + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + expected = new Complex128Array([ 1.0, 2.0, 10.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]); + actual = withArray( x, 2, new Complex128( 10.0, 0.0 ) ); + t.deepEqual( actual, expected, 'returns expected value' ); + + // Ensure input array is not mutated: + t.notEqual( x, actual, 'returns a new array' ); + + t.end(); +}); + +tape('the function throws a RangeError if provided an out-of-bounds index (complex typed array)', function test( t ) { + var actual; + var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + actual = function outofbounds() { + withArray( x, 10, new Complex128( 10.0, 0.0 ) ); + }; + t.throws( actual, RangeError, 'throws RangeError for positive out-of-bounds index' ); + + actual = function outofbounds() { + withArray( x, -10, new Complex128( 10.0, 0.0 ) ); + }; + t.throws( actual, RangeError, 'throws RangeError for negative out-of-bounds index' ); + + t.end(); +}); + +tape( 'the function returns a new array with the specified index modified to the provided value (accessors)', function test( t ) { + var expected; + var actual; + var x; + + x = toAccessorArray( [ 1, 2, 3, 4, 5, 6 ] ); + + expected = [ 1, 2, 10, 4, 5, 6 ]; + actual = withArray( x, 2, 10 ); + t.deepEqual( actual, expected, 'returns expected value' ); + + // Ensure input array is not mutated: + t.notEqual( toAccessorArray(x), actual, 'returns a new array' ); + + t.end(); +}); + +tape('the function throws a RangeError if provided an out-of-bounds index (accessors)', function test(t) { + var actual; + var x = toAccessorArray([ 1, 2, 3, 4, 5, 6 ]); + + actual = function outofbounds() { + withArray( x, 10, 10 ); + }; + t.throws( actual, RangeError, 'throws RangeError for positive out-of-bounds index' ); + + actual = function outofbounds() { + withArray( x, -10, 10 ); + }; + t.throws( actual, RangeError, 'throws RangeError for negative out-of-bounds index' ); + + t.end(); +});