Skip to content

Commit 30d8ebe

Browse files
committed
Auto-generated commit
1 parent 05c6535 commit 30d8ebe

12 files changed

Lines changed: 983 additions & 2 deletions

File tree

‎.github/.keepalive‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎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-22)
7+
## Unreleased (2026-11-29)
88

99
<section class="features">
1010

1111
### Features
1212

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

5253
<details>
5354

55+
- [`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)_
5456
- [`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)_
5557
- [`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)_
5658
- [`4ce7668`](https://github.com/stdlib-js/stdlib/commit/4ce7668d098bf469feb7d67026e2480fc26de84a) - **docs:** update throws annotations [(#16597)](https://github.com/stdlib-js/stdlib/pull/16597) _(by Philipp Burckhardt, Athan Reines)_

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

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
# set
22+
23+
> Immutably set a value at a path in a document 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 sets a value 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).
30+
31+
</section>
32+
33+
<!-- /.intro -->
34+
35+
<!-- Package usage documentation. -->
36+
37+
<section class="usage">
38+
39+
## Usage
40+
41+
```javascript
42+
var set = require( '@stdlib/utils/json-patch/base/set' );
43+
```
44+
45+
#### set( document, tokens, value )
46+
47+
Immutably sets a value at a path in a document using structural sharing.
48+
49+
```javascript
50+
var doc = {
51+
'a': {
52+
'b': {
53+
'c': 1
54+
}
55+
}
56+
};
57+
var out = set( doc, [ 'a', 'b', 'c' ], 99 );
58+
// returns { 'a': { 'b': { 'c': 99 } } }
59+
60+
var bool = ( doc.a.b === out.a.b );
61+
// returns false
62+
63+
bool = ( doc === out );
64+
// returns false
65+
```
66+
67+
If `tokens` is an empty array (i.e., a root pointer), the function returns `value` directly (i.e., root replacement).
68+
69+
```javascript
70+
var doc = {
71+
'x': 1
72+
};
73+
var out = set( doc, [], [ 1, 2 ] );
74+
// returns [ 1, 2 ]
75+
```
76+
77+
</section>
78+
79+
<!-- /.usage -->
80+
81+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
82+
83+
<section class="notes">
84+
85+
## Notes
86+
87+
- Only objects and arrays along the path are shallow-cloned. All other subtrees keep their original references (structural sharing).
88+
- If `tokens` is empty (i.e., a root pointer), returns `value` directly (i.e., root replacement).
89+
- The function throws an `Error` if any token is an unsafe value (e.g., `__proto__`, `constructor`).
90+
- The function throws a `TypeError` if an intermediate path segment references a non-traversable value (e.g., a string or number).
91+
92+
</section>
93+
94+
<!-- /.notes -->
95+
96+
<!-- Package usage examples. -->
97+
98+
<section class="examples">
99+
100+
## Examples
101+
102+
<!-- eslint no-undef: "error" -->
103+
104+
```javascript
105+
var set = require( '@stdlib/utils/json-patch/base/set' );
106+
107+
// Set a value at a shallow path:
108+
var doc = {
109+
'a': 1,
110+
'b': 2
111+
};
112+
var out = set( doc, [ 'a' ], 99 );
113+
console.log( out );
114+
/* =>
115+
{
116+
'a': 99,
117+
'b': 2
118+
}
119+
*/
120+
121+
// Set a value at a deep path:
122+
doc = {
123+
'a': {
124+
'b': {
125+
'c': 1
126+
}
127+
}
128+
};
129+
out = set( doc, [ 'a', 'b', 'c' ], 42 );
130+
console.log( out );
131+
/* =>
132+
{
133+
'a': {
134+
'b': {
135+
'c': 42
136+
}
137+
}
138+
}
139+
*/
140+
141+
// Root replacement (empty tokens):
142+
doc = {
143+
'x': 1
144+
};
145+
out = set( doc, [], [ 42 ] );
146+
console.log( out );
147+
// => [ 42 ]
148+
149+
// Structural sharing (unchanged subtrees keep references):
150+
doc = {
151+
'a': {
152+
'b': 1
153+
},
154+
'c': {
155+
'd': 2
156+
}
157+
};
158+
out = set( doc, [ 'a', 'b' ], 99 );
159+
console.log( out.c === doc.c );
160+
// => true
161+
```
162+
163+
</section>
164+
165+
<!-- /.examples -->
166+
167+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
168+
169+
<section class="related">
170+
171+
</section>
172+
173+
<!-- /.related -->
174+
175+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
176+
177+
<section class="links">
178+
179+
</section>
180+
181+
<!-- /.links -->
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 isPlainObject = require( '@stdlib/assert/is-plain-object' );
26+
var pkg = require( './../package.json' ).name;
27+
var set = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( format( '%s::shallow_path', pkg ), function benchmark( b ) {
33+
var doc;
34+
var out;
35+
var i;
36+
37+
doc = {
38+
'a': 1,
39+
'b': 2,
40+
'c': 3
41+
};
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
out = set( doc, [ 'a' ], i );
46+
if ( !isPlainObject( out ) ) {
47+
b.fail( 'should return an object' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isPlainObject( out ) ) {
52+
b.fail( 'should return a plain object' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
57+
58+
bench( format( '%s::deep_path', pkg ), function benchmark( b ) {
59+
var doc;
60+
var out;
61+
var i;
62+
63+
doc = {
64+
'a': {
65+
'b': {
66+
'c': {
67+
'd': 1
68+
}
69+
}
70+
}
71+
};
72+
73+
b.tic();
74+
for ( i = 0; i < b.iterations; i++ ) {
75+
out = set( doc, [ 'a', 'b', 'c', 'd' ], i );
76+
if ( !isPlainObject( out ) ) {
77+
b.fail( 'should return an object' );
78+
}
79+
}
80+
b.toc();
81+
if ( !isPlainObject( out ) ) {
82+
b.fail( 'should return a plain object' );
83+
}
84+
b.pass( 'benchmark finished' );
85+
b.end();
86+
});
87+
88+
bench( format( '%s::root_replacement', pkg ), function benchmark( b ) {
89+
var doc;
90+
var out;
91+
var i;
92+
93+
doc = {
94+
'a': 1
95+
};
96+
97+
b.tic();
98+
for ( i = 0; i < b.iterations; i++ ) {
99+
out = set( doc, [], {
100+
'x': i
101+
});
102+
if ( !isPlainObject( out ) ) {
103+
b.fail( 'should return an object' );
104+
}
105+
}
106+
b.toc();
107+
if ( !isPlainObject( out ) ) {
108+
b.fail( 'should return a plain object' );
109+
}
110+
b.pass( 'benchmark finished' );
111+
b.end();
112+
});

‎json-patch/base/set/docs/repl.txt‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
{{alias}}( document, tokens, value )
3+
Immutably sets a value at a path in a document using structural sharing.
4+
5+
Only objects and arrays along the path are shallow-cloned. All other
6+
subtrees keep their original references.
7+
8+
If `tokens` is empty (i.e., a root pointer), the function returns `value`
9+
directly (i.e., root replacement).
10+
11+
Parameters
12+
----------
13+
document: Object|Array
14+
Source document.
15+
16+
tokens: Array<string>
17+
Array of reference tokens.
18+
19+
value: any
20+
The value to set.
21+
22+
Returns
23+
-------
24+
out: Object|Array
25+
New document with the value set at the specified path.
26+
27+
Examples
28+
--------
29+
> var doc = { 'a': { 'b': { 'c': 1 } } };
30+
> var out = {{alias}}( doc, [ 'a', 'b', 'c' ], 99 )
31+
{ 'a': { 'b': { 'c': 99 } } }
32+
> var bool = ( doc === out )
33+
false
34+
35+
See Also
36+
--------
37+

0 commit comments

Comments
 (0)