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
150 changes: 150 additions & 0 deletions lib/node_modules/@stdlib/utils/json-pointer/has/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<!--

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

-->

# has

> Test if a [JSON Pointer][json-pointer] ([RFC 6901][rfc-6901]) resolves to a value in a document.

<section class="usage">

## Usage

```javascript
var has = require( '@stdlib/utils/json-pointer/has' );
```

#### has( document, pointer )

Tests if a [JSON Pointer][json-pointer] resolves to a value in a document.

```javascript
var obj = {
'foo': [ 'bar', 'baz' ]
};
var bool = has( obj, '/foo/0' );
// returns true

obj = {
'foo': 'bar'
};
bool = has( obj, '/foo' );
// returns true

bool = has( obj, '/missing' );
// returns false

bool = has( obj, '' );
// returns true
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- Per [RFC 6901][rfc-6901], an empty string refers to the root of the document and returns `true` for any valid object or array.
- The `-` token on an array references the position after the last element (nonexistent element), so it returns `false`.

</section>

<!-- /.notes -->

<section class="examples">

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

## Examples

```javascript
var has = require( '@stdlib/utils/json-pointer/has' );

var doc = {
'foo': [ 'bar', 'baz' ],
'': 0,
'a/b': 1,
'm~n': 8
};

var bool = has( doc, '/foo' );
// returns true

bool = has( doc, '/foo/0' );
// returns true

bool = has( doc, '/foo/99' );
// returns false

bool = has( doc, '/foo/-' );
// returns false

bool = has( doc, '/' );
// returns true

bool = has( doc, '/a~1b' );
// returns true

bool = has( doc, '/m~0n' );
// returns true

bool = has( doc, '/nonexistent' );
// returns false

bool = has( doc, '' );
// returns true
```

</section>

<!-- /.examples -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">

## References

- [RFC 6901 - JavaScript Object Notation (JSON) Pointer][rfc-6901]

</section>

<!-- /.references -->

<!-- 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">

[json-pointer]: https://datatracker.ietf.org/doc/html/rfc6901

[rfc-6901]: https://datatracker.ietf.org/doc/html/rfc6901

</section>

<!-- /.links -->
163 changes: 163 additions & 0 deletions lib/node_modules/@stdlib/utils/json-pointer/has/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/**
* @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 pkg = require( './../package.json' ).name;
var has = require( './../lib' );


// MAIN //

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

doc = {
'foo': 'bar',
'baz': 'qux'
};

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = has( doc, '/foo' );
if ( typeof out !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( out !== true ) {
b.fail( 'should return expected value' );
}
b.pass( 'benchmark finished' );
b.end();
});

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

doc = {
'a': {
'b': {
'c': {
'd': {
'e': {
'f': 42
}
}
}
}
}
};

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = has( doc, '/a/b/c/d/e/f' );
if ( typeof out !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( out !== true ) {
b.fail( 'should return expected value' );
}
b.pass( 'benchmark finished' );
b.end();
});

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

doc = {
'arr': [
[ 10, 20 ],
[ 30, 40 ]
]
};

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = has( doc, '/arr/0/1' );
if ( typeof out !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( out !== true ) {
b.fail( 'should return expected value' );
}
b.pass( 'benchmark finished' );
b.end();
});

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

doc = {
'foo': 'bar'
};

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = has( doc, '/missing' );
if ( typeof out !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( out !== false ) {
b.fail( 'should return expected value' );
}
b.pass( 'benchmark finished' );
b.end();
});

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

doc = {
'foo': 'bar'
};

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = has( doc, '' );
if ( typeof out !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( out !== true ) {
b.fail( 'should return expected value' );
}
b.pass( 'benchmark finished' );
b.end();
});
37 changes: 37 additions & 0 deletions lib/node_modules/@stdlib/utils/json-pointer/has/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

{{alias}}( document, pointer )
Tests if a JSON Pointer resolves to a value in a document.

Per RFC 6901, an empty string refers to the root of the document and
returns `true` for any valid object or array.

The `-` token on an array references the position after the last element
(nonexistent element), so it returns `false`.

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

pointer: string
JSON Pointer string.

Returns
-------
bool: boolean
Boolean indicating if pointer resolves.

Examples
--------
> var bool = {{alias}}( { 'foo': [ 'bar', 'baz' ] }, '/foo/0' )
true
> bool = {{alias}}( { 'foo': 'bar' }, '/foo' )
true
> bool = {{alias}}( { 'foo': 'bar' }, '/missing' )
false
> bool = {{alias}}( { 'foo': 'bar' }, '' )
true

See Also
--------

Loading
Loading