diff --git a/lib/node_modules/@stdlib/utils/json-patch/base/insert/README.md b/lib/node_modules/@stdlib/utils/json-patch/base/insert/README.md new file mode 100644 index 000000000000..f570cdd5de99 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-patch/base/insert/README.md @@ -0,0 +1,162 @@ + + +# insert + +> Immutably insert a value into an array at a path in a document using structural sharing. + + + +
+ +This function immutably inserts a value into an array at a specified path in a document. Only objects and arrays along the path are shallow-cloned; all other subtrees retain their original references (structural sharing). + +
+ + + + + +
+ +## Usage + +```javascript +var insert = require( '@stdlib/utils/json-patch/base/insert' ); +``` + +#### insert( document, tokens, value ) + +Immutably inserts a value into an array at a path in a document using structural sharing. + +```javascript +var doc = { + 'a': [ 1, 2, 3 ] +}; +var out = insert( doc, [ 'a', '1' ], 99 ); +// returns { 'a': [ 1, 99, 2, 3 ] } + +var bool = ( doc.a === out.a ); +// returns false + +bool = ( doc === out ); +// returns false +``` + +The `-` token is resolved to the array length, appending the value to the end of the array. + +```javascript +var doc = [ 1, 2, 3 ]; +var out = insert( doc, [ '-' ], 4 ); +// returns [ 1, 2, 3, 4 ] +``` + +
+ + + + + +
+ +## Notes + +- Only objects and arrays along the path are shallow-cloned. All other subtrees keep their original references (structural sharing). +- The final container must be an array. The value is inserted before the given index via `splice( index, 0, value )`, shifting existing elements right. +- The `-` token is resolved to `array.length` (append). +- The function throws an `Error` if any token is an unsafe value (e.g., `__proto__`, `constructor`). +- The function throws a `TypeError` if an intermediate path segment references a non-traversable value (e.g., a string or number). + +
+ + + + + +
+ +## Examples + + + +```javascript +var insert = require( '@stdlib/utils/json-patch/base/insert' ); + +// Insert an element into an array: +var doc = [ 1, 2, 3 ]; +var out = insert( doc, [ '1' ], 99 ); +console.log( out ); +// => [ 1, 99, 2, 3 ] + +// Append an element to an array: +doc = [ 1, 2, 3 ]; +out = insert( doc, [ '-' ], 4 ); +console.log( out ); +// => [ 1, 2, 3, 4 ] + +// Insert into a nested array: +doc = { + 'a': { + 'b': [ 1, 2 ] + } +}; +out = insert( doc, [ 'a', 'b', '1' ], 42 ); +console.log( out ); +/* => + { + 'a': { + 'b': [ 1, 42, 2 ] + } + } +*/ + +// Structural sharing (unchanged subtrees keep references): +doc = { + 'a': { + 'b': [ 1, 2 ] + }, + 'c': { + 'd': 3 + } +}; +out = insert( doc, [ 'a', 'b', '0' ], 99 ); +console.log( out.c === doc.c ); +// => true +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/utils/json-patch/base/insert/benchmark/benchmark.js b/lib/node_modules/@stdlib/utils/json-patch/base/insert/benchmark/benchmark.js new file mode 100644 index 000000000000..9339744662fa --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-patch/base/insert/benchmark/benchmark.js @@ -0,0 +1,101 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 format = require( '@stdlib/string/format' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var pkg = require( './../package.json' ).name; +var insert = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::shallow_path', pkg ), function benchmark( b ) { + var doc; + var out; + var i; + + doc = [ 1, 2, 3 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = insert( doc, [ '1' ], i ); + if ( !isArray( out ) ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( out ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::append', pkg ), function benchmark( b ) { + var doc; + var out; + var i; + + doc = [ 1, 2, 3 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = insert( doc, [ '-' ], i ); + if ( !isArray( out ) ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( out ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::deep_path', pkg ), function benchmark( b ) { + var doc; + var out; + var i; + + doc = { + 'a': { + 'b': [ 1, 2, 3 ] + } + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = insert( doc, [ 'a', 'b', '1' ], i ); + if ( !isPlainObject( out ) ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isPlainObject( out ) ) { + b.fail( 'should return a plain object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/utils/json-patch/base/insert/docs/repl.txt b/lib/node_modules/@stdlib/utils/json-patch/base/insert/docs/repl.txt new file mode 100644 index 000000000000..f7e447e6b420 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-patch/base/insert/docs/repl.txt @@ -0,0 +1,39 @@ + +{{alias}}( document, tokens, value ) + Immutably inserts a value into an array at a path in a document using + structural sharing. + + Only objects and arrays along the path are shallow-cloned. All other + subtrees keep their original references. + + The final container must be an array. The value is inserted before the + given index, shifting existing elements right. The `-` token is resolved + to `array.length` (append). + + Parameters + ---------- + document: Object|Array + Source document. + + tokens: Array + Array of reference tokens. + + value: any + The value to insert. + + Returns + ------- + out: Object|Array + New document with the value inserted. + + Examples + -------- + > var doc = { 'a': [ 1, 2, 3 ] }; + > var out = {{alias}}( doc, [ 'a', '1' ], 99 ) + { 'a': [ 1, 99, 2, 3 ] } + > var bool = ( doc === out ) + false + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/utils/json-patch/base/insert/docs/types/index.d.ts b/lib/node_modules/@stdlib/utils/json-patch/base/insert/docs/types/index.d.ts new file mode 100644 index 000000000000..b88081fcad23 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-patch/base/insert/docs/types/index.d.ts @@ -0,0 +1,52 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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 + +/** +* Immutably inserts a value into an array at a path in a document using structural sharing. +* +* @param document - source document +* @param tokens - array of reference tokens +* @param value - value to insert +* @throws tokens must not be empty +* @throws tokens must not contain unsafe tokens +* @throws intermediate path segments must reference objects or arrays +* @throws target container must be an array +* @throws array index must be within bounds +* @returns new document with the value inserted +* +* @example +* var doc = { +* 'a': [ 1, 2, 3 ] +* }; +* var out = insert( doc, [ 'a', '1' ], 99 ); +* // returns { 'a': [ 1, 99, 2, 3 ] } +* +* var bool = ( doc.a === out.a ); +* // returns false +* +* bool = ( doc === out ); +* // returns false +*/ +declare function insert( document: Array | Record, tokens: Array, value: any ): any; + + +// EXPORTS // + +export = insert; diff --git a/lib/node_modules/@stdlib/utils/json-patch/base/insert/docs/types/test.ts b/lib/node_modules/@stdlib/utils/json-patch/base/insert/docs/types/test.ts new file mode 100644 index 000000000000..23f2c2b5b494 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-patch/base/insert/docs/types/test.ts @@ -0,0 +1,36 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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 insert = require( './index' ); + + +// TESTS // + +// The function returns any... +{ + insert( [ 1, 2, 3 ], [ '0' ], 99 ); // $ExpectType any + insert( { 'a': [ 1, 2 ] }, [ 'a', '1' ], 99 ); // $ExpectType any +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + insert(); // $ExpectError + insert( [ 1, 2 ] ); // $ExpectError + insert( [ 1, 2 ], [ '0' ] ); // $ExpectError + insert( [ 1, 2 ], [ '0' ], 99, 42 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/utils/json-patch/base/insert/examples/index.js b/lib/node_modules/@stdlib/utils/json-patch/base/insert/examples/index.js new file mode 100644 index 000000000000..a7ca7747ce09 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-patch/base/insert/examples/index.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 insert = require( './../lib' ); + +// Insert an element into an array: +var doc = [ 1, 2, 3 ]; +var out = insert( doc, [ '1' ], 99 ); +console.log( out ); +// => [ 1, 99, 2, 3 ] + +// Append an element to an array: +doc = [ 1, 2, 3 ]; +out = insert( doc, [ '-' ], 4 ); +console.log( out ); +// => [ 1, 2, 3, 4 ] + +// Insert into a nested array: +doc = { + 'a': { + 'b': [ 1, 2 ] + } +}; +out = insert( doc, [ 'a', 'b', '1' ], 42 ); +console.log( out ); +/* => + { + 'a': { + 'b': [ 1, 42, 2 ] + } + } +*/ + +// Structural sharing (unchanged subtrees keep references): +doc = { + 'a': { + 'b': [ 1, 2 ] + }, + 'c': { + 'd': 3 + } +}; +out = insert( doc, [ 'a', 'b', '0' ], 99 ); +console.log( out.c === doc.c ); +// => true diff --git a/lib/node_modules/@stdlib/utils/json-patch/base/insert/lib/index.js b/lib/node_modules/@stdlib/utils/json-patch/base/insert/lib/index.js new file mode 100644 index 000000000000..d15991762371 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-patch/base/insert/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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'; + +/** +* Immutably insert a value into an array at a path in a document using structural sharing. +* +* @module @stdlib/utils/json-patch/base/insert +* +* @example +* var insert = require( '@stdlib/utils/json-patch/base/insert' ); +* +* var doc = { +* 'a': [ 1, 2, 3 ] +* }; +* var out = insert( doc, [ 'a', '1' ], 99 ); +* // returns { 'a': [ 1, 99, 2, 3 ] } +* +* var bool = ( doc.a === out.a ); +* // returns false +* +* bool = ( doc === out ); +* // returns false +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/utils/json-patch/base/insert/lib/main.js b/lib/node_modules/@stdlib/utils/json-patch/base/insert/lib/main.js new file mode 100644 index 000000000000..c0d49ac3f6c3 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-patch/base/insert/lib/main.js @@ -0,0 +1,108 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isArray = require( '@stdlib/assert/is-array' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var format = require( '@stdlib/string/format' ); +var copy = require( '@stdlib/utils/copy' ); +var isSafeToken = require( '@stdlib/utils/json-pointer/base/assert/is-safe-token' ); +var resolveToken = require( '@stdlib/utils/json-pointer/base/resolve-token' ); + + +// MAIN // + +/** +* Immutably inserts a value into an array at a path in a document using structural sharing. +* +* @param {(Object|Array)} document - source document +* @param {Array} tokens - array of reference tokens +* @param {*} value - value to insert +* @throws {Error} tokens must not be empty +* @throws {Error} tokens must not contain unsafe tokens +* @throws {TypeError} intermediate path segments must reference objects or arrays +* @throws {TypeError} target container must be an array +* @throws {RangeError} index must be within bounds (0 to array.length) +* @returns {(Object|Array)} new document with the value inserted +* +* @example +* var doc = { +* 'a': [ 1, 2, 3 ] +* }; +* var out = insert( doc, [ 'a', '1' ], 99 ); +* // returns { 'a': [ 1, 99, 2, 3 ] } +* +* var bool = ( doc.a === out.a ); +* // returns false +* +* bool = ( doc === out ); +* // returns false +*/ +function insert( document, tokens, value ) { + var current; + var child; + var root; + var key; + var i; + + if ( tokens.length === 0 ) { + throw new Error( 'invalid argument. Cannot insert at the root of a document.' ); + } + + root = copy( document, 1 ); + current = root; + + // Walk all tokens except the last, cloning each level: + for ( i = 0; i < tokens.length - 1; i++ ) { + if ( !isSafeToken( tokens[ i ] ) ) { + throw new Error( format( 'invalid argument. Token contains an unsafe value. Value: `%s`.', tokens[ i ] ) ); + } + key = resolveToken( current, tokens[ i ] ); + + if ( !isPlainObject( current[ key ] ) && !isArray( current[ key ] ) ) { + throw new TypeError( format( 'invalid argument. Path references a non-traversable value at token `%s`.', tokens[ i ] ) ); + } + child = copy( current[ key ], 1 ); + current[ key ] = child; + current = child; + } + + // The final container must be an array for insert operations: + if ( !isArray( current ) ) { + throw new TypeError( format( 'invalid argument. Insert target must be an array. Token: `%s`.', tokens[ tokens.length - 1 ] ) ); + } + if ( !isSafeToken( tokens[ tokens.length - 1 ] ) ) { + throw new Error( format( 'invalid argument. Token contains an unsafe value. Value: `%s`.', tokens[ tokens.length - 1 ] ) ); + } + + key = resolveToken( current, tokens[ tokens.length - 1 ] ); + if ( key < 0 || key > current.length ) { + throw new RangeError( format( 'invalid argument. Array index out of bounds for insert. Index: `%d`. Length: `%d`.', key, current.length ) ); + } + current.splice( key, 0, value ); + + return root; +} + + +// EXPORTS // + +module.exports = insert; diff --git a/lib/node_modules/@stdlib/utils/json-patch/base/insert/package.json b/lib/node_modules/@stdlib/utils/json-patch/base/insert/package.json new file mode 100644 index 000000000000..040464a18602 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-patch/base/insert/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/utils/json-patch/base/insert", + "version": "0.0.0", + "description": "Immutably insert a value into an array at a path in a document using structural sharing.", + "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", + "stdutils", + "utilities", + "utility", + "utils", + "util", + "base", + "json", + "patch", + "json-patch", + "rfc6902", + "insert", + "array", + "immutable", + "structural-sharing" + ] +} diff --git a/lib/node_modules/@stdlib/utils/json-patch/base/insert/test/test.js b/lib/node_modules/@stdlib/utils/json-patch/base/insert/test/test.js new file mode 100644 index 000000000000..0212a33b0fe8 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-patch/base/insert/test/test.js @@ -0,0 +1,231 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 insert = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof insert, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided an empty tokens array (cannot insert at root)', function test( t ) { + var doc; + + doc = [ 1, 2 ]; + t.throws( badValue, Error, 'throws an error when provided empty tokens' ); + t.end(); + + function badValue() { + insert( doc, [], 99 ); + } +}); + +tape( 'the function immutably inserts a value at the beginning of an array', function test( t ) { + var expected; + var doc; + var out; + + doc = [ 1, 2, 3 ]; + expected = [ 99, 1, 2, 3 ]; + + out = insert( doc, [ '0' ], 99 ); + t.deepEqual( out, expected, 'returns expected value' ); + t.notEqual( out, doc, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function immutably inserts a value into the middle of an array', function test( t ) { + var expected; + var doc; + var out; + + doc = [ 1, 2, 3 ]; + expected = [ 1, 99, 2, 3 ]; + + out = insert( doc, [ '1' ], 99 ); + t.deepEqual( out, expected, 'returns expected value' ); + t.notEqual( out, doc, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function immutably appends a value to an array when provided the `-` token', function test( t ) { + var expected; + var doc; + var out; + + doc = [ 1, 2, 3 ]; + expected = [ 1, 2, 3, 99 ]; + + out = insert( doc, [ '-' ], 99 ); + t.deepEqual( out, expected, 'returns expected value' ); + t.notEqual( out, doc, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function immutably inserts a value into a nested array', function test( t ) { + var expected; + var doc; + var out; + + doc = { + 'a': { + 'b': [ 1, 2, 3 ] + } + }; + expected = { + 'a': { + 'b': [ 1, 99, 2, 3 ] + } + }; + + out = insert( doc, [ 'a', 'b', '1' ], 99 ); + t.deepEqual( out, expected, 'returns expected value' ); + t.notEqual( out, doc, 'returns expected value' ); + t.notEqual( out.a, doc.a, 'returns expected value' ); + t.notEqual( out.a.b, doc.a.b, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function uses structural sharing for unchanged subtrees', function test( t ) { + var doc; + var out; + + doc = { + 'a': { + 'b': [ 1, 2, 3 ] + }, + 'c': { + 'd': 42 + } + }; + out = insert( doc, [ 'a', 'b', '1' ], 99 ); + + t.strictEqual( out.c, doc.c, 'returns expected value' ); + t.notEqual( out.a, doc.a, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function throws a type error if the target container is not an array', function test( t ) { + var doc; + + doc = { + 'a': { + 'b': 1 + } + }; + t.throws( badValue, TypeError, 'throws a type error when target is an object' ); + t.end(); + + function badValue() { + insert( doc, [ 'a', 'b' ], 99 ); + } +}); + +tape( 'the function throws a range error if the array index is out of bounds', function test( t ) { + var values; + var doc; + var i; + + doc = [ 1, 2, 3 ]; + values = [ + '4', + '10' + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + insert( doc, [ value ], 99 ); + }; + } +}); + +tape( 'the function throws an error if a token is an unsafe value (prototype pollution vector)', function test( t ) { + var values; + var i; + + values = [ + '__proto__', + 'constructor', + 'prototype' + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + insert( [ 1, 2 ], [ value ], 99 ); + }; + } +}); + +tape( 'the function throws a type error if an intermediate path segment references a non-traversable value', function test( t ) { + var values; + var i; + + values = [ + '', + 42, + true, + null + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws when provided ' + ( typeof values[ i ] ) ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + insert({ + 'a': value + }, [ 'a', '0' ], 99 ); + }; + } +}); + +tape( 'the function does not mutate the original document', function test( t ) { + var doc; + var out; + + doc = [ 1, 2, 3 ]; + out = insert( doc, [ '1' ], 99 ); + + t.deepEqual( doc, [ 1, 2, 3 ], 'returns expected value' ); + t.deepEqual( out, [ 1, 99, 2, 3 ], 'returns expected value' ); + + t.end(); +});