Skip to content

Commit ab070ca

Browse files
committed
Auto-generated commit
1 parent 30d8ebe commit ab070ca

11 files changed

Lines changed: 912 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
55
<section class="release" id="unreleased">
66

7-
## Unreleased (2026-11-29)
7+
## Unreleased (2026-11-30)
88

99
<section class="features">
1010

1111
### Features
1212

13+
- [`008bd34`](https://github.com/stdlib-js/stdlib/commit/008bd34951ff5893bbc21eea4ff902b397d53dc9) - add `utils/json-patch/base/insert` [(#16389)](https://github.com/stdlib-js/stdlib/pull/16389)
1314
- [`96e3245`](https://github.com/stdlib-js/stdlib/commit/96e324534b246ff813560e2cb2a95589cd740082) - add `utils/json-patch/base/set` [(#16380)](https://github.com/stdlib-js/stdlib/pull/16380)
1415
- [`a6aa6d9`](https://github.com/stdlib-js/stdlib/commit/a6aa6d97df92d15c0262526367502ebd9d367036) - add `utils/json-pointer/has` [(#16246)](https://github.com/stdlib-js/stdlib/pull/16246)
1516
- [`6514036`](https://github.com/stdlib-js/stdlib/commit/6514036b042522b853e8c77111fc8b7c2eaf42cc) - add `utils/json-pointer/get` [(#16245)](https://github.com/stdlib-js/stdlib/pull/16245)
@@ -52,6 +53,7 @@ A total of 10 issues were closed in this release:
5253

5354
<details>
5455

56+
- [`008bd34`](https://github.com/stdlib-js/stdlib/commit/008bd34951ff5893bbc21eea4ff902b397d53dc9) - **feat:** add `utils/json-patch/base/insert` [(#16389)](https://github.com/stdlib-js/stdlib/pull/16389) _(by Gururaj Gurram, Athan Reines)_
5557
- [`96e3245`](https://github.com/stdlib-js/stdlib/commit/96e324534b246ff813560e2cb2a95589cd740082) - **feat:** add `utils/json-patch/base/set` [(#16380)](https://github.com/stdlib-js/stdlib/pull/16380) _(by Gururaj Gurram, Athan Reines)_
5658
- [`83652c1`](https://github.com/stdlib-js/stdlib/commit/83652c1fc6effe086806e0fc9cefcf2a7d96ff82) - **refactor:** return `undefined` for nonexistent document locations [(#16676)](https://github.com/stdlib-js/stdlib/pull/16676) _(by Gururaj Gurram, Athan Reines)_
5759
- [`d00c253`](https://github.com/stdlib-js/stdlib/commit/d00c25388d3c73d6fe3a272540e6686a4a86bb19) - **refactor:** add error handling for invalid escape sequences [(#16677)](https://github.com/stdlib-js/stdlib/pull/16677) _(by Gururaj Gurram)_

‎json-patch/base/insert/README.md‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# insert
22+
23+
> Immutably insert a value into an array according to a specified document path using structural sharing.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
This function immutably inserts a value into an array according to a specified document path. Only objects and arrays along the path are shallow-cloned; all other subtrees retain their original references (structural sharing).
30+
31+
</section>
32+
33+
<!-- /.intro -->
34+
35+
<!-- Package usage documentation. -->
36+
37+
<section class="usage">
38+
39+
## Usage
40+
41+
```javascript
42+
var insert = require( '@stdlib/utils/json-patch/base/insert' );
43+
```
44+
45+
#### insert( document, tokens, value )
46+
47+
Immutably inserts a value into an array according to a specified document path using structural sharing.
48+
49+
```javascript
50+
var doc = {
51+
'a': [ 1, 2, 3 ]
52+
};
53+
var out = insert( doc, [ 'a', '1' ], 99 );
54+
// returns { 'a': [ 1, 99, 2, 3 ] }
55+
56+
var bool = ( doc.a === out.a );
57+
// returns false
58+
59+
bool = ( doc === out );
60+
// returns false
61+
```
62+
63+
The `-` token is resolved to the array length, appending the value to the end of the array.
64+
65+
```javascript
66+
var doc = [ 1, 2, 3 ];
67+
var out = insert( doc, [ '-' ], 4 );
68+
// returns [ 1, 2, 3, 4 ]
69+
```
70+
71+
</section>
72+
73+
<!-- /.usage -->
74+
75+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
76+
77+
<section class="notes">
78+
79+
## Notes
80+
81+
- Only objects and arrays along the path are shallow-cloned. All other subtrees keep their original references (structural sharing).
82+
- The final container must be an array. The value is inserted before the given index via `splice( index, 0, value )`, shifting existing elements right.
83+
- The `-` token is resolved to `array.length` (i.e., append).
84+
- The function throws an `Error` if any token is an unsafe value (e.g., `__proto__`, `constructor`).
85+
- The function throws a `TypeError` if an intermediate path segment references a non-traversable value (e.g., a string or number).
86+
87+
</section>
88+
89+
<!-- /.notes -->
90+
91+
<!-- Package usage examples. -->
92+
93+
<section class="examples">
94+
95+
## Examples
96+
97+
<!-- eslint no-undef: "error" -->
98+
99+
```javascript
100+
var insert = require( '@stdlib/utils/json-patch/base/insert' );
101+
102+
// Insert an element into an array:
103+
var doc = [ 1, 2, 3 ];
104+
var out = insert( doc, [ '1' ], 99 );
105+
console.log( out );
106+
// => [ 1, 99, 2, 3 ]
107+
108+
// Append an element to an array:
109+
doc = [ 1, 2, 3 ];
110+
out = insert( doc, [ '-' ], 4 );
111+
console.log( out );
112+
// => [ 1, 2, 3, 4 ]
113+
114+
// Insert into a nested array:
115+
doc = {
116+
'a': {
117+
'b': [ 1, 2 ]
118+
}
119+
};
120+
out = insert( doc, [ 'a', 'b', '1' ], 42 );
121+
console.log( out );
122+
/* =>
123+
{
124+
'a': {
125+
'b': [ 1, 42, 2 ]
126+
}
127+
}
128+
*/
129+
130+
// Structural sharing (unchanged subtrees keep references):
131+
doc = {
132+
'a': {
133+
'b': [ 1, 2 ]
134+
},
135+
'c': {
136+
'd': 3
137+
}
138+
};
139+
out = insert( doc, [ 'a', 'b', '0' ], 99 );
140+
console.log( out.c === doc.c );
141+
// => true
142+
```
143+
144+
</section>
145+
146+
<!-- /.examples -->
147+
148+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
149+
150+
<section class="related">
151+
152+
</section>
153+
154+
<!-- /.related -->
155+
156+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
157+
158+
<section class="links">
159+
160+
</section>
161+
162+
<!-- /.links -->
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var format = require( '@stdlib/string/format' );
25+
var isArray = require( '@stdlib/assert/is-array' );
26+
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
27+
var pkg = require( './../package.json' ).name;
28+
var insert = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( format( '%s::shallow_path', pkg ), function benchmark( b ) {
34+
var doc;
35+
var out;
36+
var i;
37+
38+
doc = [ 1, 2, 3 ];
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
out = insert( doc, [ '1' ], i );
43+
if ( !isArray( out ) ) {
44+
b.fail( 'should return an array' );
45+
}
46+
}
47+
b.toc();
48+
if ( !isArray( out ) ) {
49+
b.fail( 'should return an array' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( format( '%s::append', pkg ), function benchmark( b ) {
56+
var doc;
57+
var out;
58+
var i;
59+
60+
doc = [ 1, 2, 3 ];
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
out = insert( doc, [ '-' ], i );
65+
if ( !isArray( out ) ) {
66+
b.fail( 'should return an array' );
67+
}
68+
}
69+
b.toc();
70+
if ( !isArray( out ) ) {
71+
b.fail( 'should return an array' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
});
76+
77+
bench( format( '%s::deep_path', pkg ), function benchmark( b ) {
78+
var doc;
79+
var out;
80+
var i;
81+
82+
doc = {
83+
'a': {
84+
'b': [ 1, 2, 3 ]
85+
}
86+
};
87+
88+
b.tic();
89+
for ( i = 0; i < b.iterations; i++ ) {
90+
out = insert( doc, [ 'a', 'b', '1' ], i );
91+
if ( !isPlainObject( out ) ) {
92+
b.fail( 'should return an object' );
93+
}
94+
}
95+
b.toc();
96+
if ( !isPlainObject( out ) ) {
97+
b.fail( 'should return a plain object' );
98+
}
99+
b.pass( 'benchmark finished' );
100+
b.end();
101+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
{{alias}}( document, tokens, value )
3+
Immutably inserts a value into an array according to a specified document
4+
path using structural sharing.
5+
6+
Only objects and arrays along the path are shallow-cloned. All other
7+
subtrees keep their original references.
8+
9+
The final container must be an array. The value is inserted before the
10+
given index, shifting existing elements right. The `-` token is resolved
11+
to `array.length` (i.e., append).
12+
13+
Parameters
14+
----------
15+
document: Object|Array
16+
Source document.
17+
18+
tokens: Array<string>
19+
Array of reference tokens.
20+
21+
value: any
22+
The value to insert.
23+
24+
Returns
25+
-------
26+
out: Object|Array
27+
New document with the value inserted.
28+
29+
Examples
30+
--------
31+
> var doc = { 'a': [ 1, 2, 3 ] };
32+
> var out = {{alias}}( doc, [ 'a', '1' ], 99 )
33+
{ 'a': [ 1, 99, 2, 3 ] }
34+
> var bool = ( doc === out )
35+
false
36+
37+
See Also
38+
--------
39+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Immutably inserts a value into an array according to a specified document path using structural sharing.
23+
*
24+
* @param document - source document
25+
* @param tokens - array of reference tokens
26+
* @param value - value to insert
27+
* @throws tokens must not be empty
28+
* @throws tokens must not contain unsafe tokens
29+
* @throws intermediate path segments must reference objects or arrays
30+
* @throws target container must be an array
31+
* @throws array index must be within bounds
32+
* @returns new document with the value inserted
33+
*
34+
* @example
35+
* var doc = {
36+
* 'a': [ 1, 2, 3 ]
37+
* };
38+
* var out = insert( doc, [ 'a', '1' ], 99 );
39+
* // returns { 'a': [ 1, 99, 2, 3 ] }
40+
*
41+
* var bool = ( doc.a === out.a );
42+
* // returns false
43+
*
44+
* bool = ( doc === out );
45+
* // returns false
46+
*/
47+
declare function insert( document: Array<any> | Record<string, any>, tokens: Array<string>, value: any ): any;
48+
49+
50+
// EXPORTS //
51+
52+
export = insert;

0 commit comments

Comments
 (0)