Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions lib/node_modules/@stdlib/utils/json-patch/base/unset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<!--

@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.

-->

# unset

> Immutably unset a value at a path in a document using structural sharing.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

This function immutably unsets 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).

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var unset = require( '@stdlib/utils/json-patch/base/unset' );
```

#### unset( document, tokens )

Immutably unsets a value at a path in a document using structural sharing.

```javascript
var doc = {
'a': {
'b': 1,
'c': 2
}
};
var out = unset( doc, [ 'a', 'b' ] );
// returns { 'a': { 'c': 2 } }

var bool = ( doc.a === out.a );
// returns false

bool = ( doc === out );
// returns false
```

```javascript
var doc = {
'a': [ 1, 2, 3 ]
};
var out = unset( doc, [ 'a', '1' ] );
// returns { 'a': [ 1, 3 ] }
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- Only objects and arrays along the path are shallow-cloned. All other subtrees keep their original references (structural sharing).
- The function throws an `Error` if `tokens` is empty (cannot unset the root of a document).
- 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).

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var unset = require( '@stdlib/utils/json-patch/base/unset' );

// Unset a value at a shallow path:
var doc = {
'a': 1,
'b': 2
};
var out = unset( doc, [ 'a' ] );
console.log( out );
/* =>
{
'b': 2
}
*/

// Unset a value at a deep path:
doc = {
'a': {
'b': {
'c': 1,
'd': 2
}
}
};
out = unset( doc, [ 'a', 'b', 'c' ] );
console.log( out );
/* =>
{
'a': {
'b': {
'd': 2
}
}
}
*/

// Unset an element from an array:
doc = {
'x': [ 'a', 'b', 'c' ]
};
out = unset( doc, [ 'x', '1' ] );
console.log( out );
/* =>
{
'x': [ 'a', 'c' ]
}
*/

// Structural sharing (unchanged subtrees keep references):
doc = {
'a': {
'b': 1,
'c': 2
},
'd': {
'e': 3
}
};
out = unset( doc, [ 'a', 'b' ] );
console.log( out.d === doc.d );
// => true
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -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 bench = require( '@stdlib/bench' );
var format = require( '@stdlib/string/format' );
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
var pkg = require( './../package.json' ).name;
var unset = require( './../lib' );


// MAIN //

bench( format( '%s::shallow_path', pkg ), function benchmark( b ) {
var doc;
var out;
var i;

doc = {
'a': 1,
'b': 2,
'c': 3
};

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = unset( doc, [ 'a' ] );
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();
});

bench( format( '%s::deep_path', pkg ), function benchmark( b ) {
var doc;
var out;
var i;

doc = {
'a': {
'b': {
'c': {
'd': 1
}
}
}
};

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = unset( doc, [ 'a', 'b', 'c', 'd' ] );
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();
});

bench( format( '%s::array', pkg ), function benchmark( b ) {
var doc;
var out;
var i;

doc = [ 1, 2, 3, 4, 5 ];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = unset( doc, [ '2' ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object/array' );
}
}
b.toc();
if ( typeof out !== 'object' ) {
b.fail( 'should return an object/array' );
}
b.pass( 'benchmark finished' );
b.end();
});
31 changes: 31 additions & 0 deletions lib/node_modules/@stdlib/utils/json-patch/base/unset/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

{{alias}}( document, tokens )
Immutably unsets a value 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.

Parameters
----------
document: Object|Array
Source document.

tokens: Array<string>
Array of reference tokens.

Returns
-------
out: Object|Array
New document with the value at the specified path removed.

Examples
--------
> var doc = { 'a': { 'b': 1, 'c': 2 } };
> var out = {{alias}}( doc, [ 'a', 'b' ] )
{ 'a': { 'c': 2 } }
> var bool = ( doc === out )
false

See Also
--------

Original file line number Diff line number Diff line change
@@ -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 unsets a value at a path in a document using structural sharing.
*
* @param document - source document
* @param tokens - array of reference tokens
* @throws tokens must not be empty
* @throws tokens must not contain unsafe tokens
* @throws intermediate path segments must reference objects or arrays
* @returns new document with the value at the specified path removed
*
* @example
* var doc = {
* 'a': {
* 'b': 1,
* 'c': 2
* }
* };
* var out = unset( doc, [ 'a', 'b' ] );
* // returns { 'a': { 'c': 2 } }
*
* var bool = ( doc.a === out.a );
* // returns false
*
* bool = ( doc === out );
* // returns false
*/
declare function unset( document: Array<any> | Record<string, any>, tokens: Array<string> ): any;


// EXPORTS //

export = unset;
Loading
Loading