Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ limitations under the License.

-->

# hasReplaceSymbolSupport
# Replace Symbol Support

> Detect native [`Symbol.replace`][mdn-replace-symbol] support.
> Detect native [`Symbol.replace`][mdn-symbol] support.

<section class="usage">

Expand All @@ -34,7 +34,7 @@ var hasReplaceSymbolSupport = require( '@stdlib/assert/has-replace-symbol-suppor

#### hasReplaceSymbolSupport()

Detects if a runtime environment supports [`Symbol.replace`][mdn-replace-symbol].
Detects if a runtime environment supports ES2018 [`Symbol.asyncIterator`][mdn-symbol].

<!-- eslint-disable id-length -->

Expand Down Expand Up @@ -114,6 +114,13 @@ $ has-replace-symbol-support

<section class="related">

* * *

## See Also

- <span class="package-name">[`@stdlib/assert/has-iterator-symbol-support`][@stdlib/assert/has-iterator-symbol-support]</span><span class="delimiter">: </span><span class="description">detect native Symbol.iterator support.</span>
- <span class="package-name">[`@stdlib/assert/has-symbol-support`][@stdlib/assert/has-symbol-support]</span><span class="delimiter">: </span><span class="description">detect native Symbol support.</span>

</section>

<!-- /.related -->
Expand All @@ -122,7 +129,15 @@ $ has-replace-symbol-support

<section class="links">

[mdn-replace-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/replace
[mdn-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol

<!-- <related-links> -->

[@stdlib/assert/has-iterator-symbol-support]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/has-iterator-symbol-support

[@stdlib/assert/has-symbol-support]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/has-symbol-support

<!-- </related-links> -->

</section>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
var bench = require( '@stdlib/bench' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var hasReplaceSymbolSupport = require( './../lib' );
var hasReplaceSymbolSupport = require( './../lib' ); // eslint-disable-line id-length


// MAIN //
Expand All @@ -34,7 +34,7 @@ bench( pkg, function benchmark( b ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
// Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear.
// Note: this *could* be optimized by a JS engine. If so, the loop may be removed.
bool = hasReplaceSymbolSupport();
if ( typeof bool !== 'boolean' ) {
b.fail( 'should return a boolean' );
Expand Down
4 changes: 2 additions & 2 deletions lib/node_modules/@stdlib/assert/has-replace-symbol-support/bin/cli
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
var resolve = require( 'path' ).resolve;
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
var CLI = require( '@stdlib/cli/ctor' );
var detect = require( './../lib' );
var hasReplaceSymbolSupport = require( './../lib' ); // eslint-disable-line id-length


// MAIN //
Expand Down Expand Up @@ -54,7 +54,7 @@ function main() {
return;
}

console.log( detect() ); // eslint-disable-line no-console
console.log( hasReplaceSymbolSupport() ); // eslint-disable-line no-console
}

main();
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@

{{alias}}()
Tests for native `Symbol.replace` support.

Returns
-------
bool: boolean
Boolean indicating if an environment has native `Symbol.replace`
support.
Boolean indicating if an environment supports native `Symbol.replace`.

Examples
--------
Expand All @@ -15,4 +13,3 @@

See Also
--------

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* Tests for native `Symbol.replace` support.
*
* @returns boolean indicating if an environment has `Symbol.replace` support
* @returns boolean indicating if an environment supports `Symbol.replace`
*
* @example
* var bool = hasReplaceSymbolSupport();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

import hasReplaceSymbolSupport = require( './index' );
import hasReplaceSymbolSupport from './index';


// TESTS //
Expand All @@ -28,6 +28,11 @@ import hasReplaceSymbolSupport = require( './index' );

// The compiler throws an error if the function is provided arguments...
{
// @ts-expect-error: intentionally testing invalid args
hasReplaceSymbolSupport( true ); // $ExpectError

// @ts-expect-error: intentionally testing invalid args
hasReplaceSymbolSupport( [], 123 ); // $ExpectError
}


Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

'use strict';

var hasReplaceSymbolSupport = require( './../lib' );
var hasReplaceSymbolSupport = require( './../lib' ); // eslint-disable-line id-length

var bool = hasReplaceSymbolSupport();
if ( bool ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
* Copyright (c) 2018 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.
Expand All @@ -19,14 +19,14 @@
'use strict';

/**
* Test for native `Symbol.replace` support.
* Test for native `Symbol.asyncIterator` support.
*
* @module @stdlib/assert/has-replace-symbol-support
* @module @stdlib/assert/has-async-iterator-symbol-support
*
* @example
* var hasReplaceSymbolSupport = require( '@stdlib/assert/has-replace-symbol-support' );
* var hasAsyncIteratorSymbolSupport = require( '@stdlib/assert/has-async-iterator-symbol-support' );
*
* var bool = hasReplaceSymbolSupport();
* var bool = hasAsyncIteratorSymbolSupport();
* // returns <boolean>
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,27 @@
* limitations under the License.
*/

/* eslint-disable stdlib/require-globals */

'use strict';

// MODULES //

var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var Symbol = require( '@stdlib/symbol/ctor' );


// MAIN //

/**
* Tests for native `Symbol.replace` support.
*
* @returns {boolean} boolean indicating if an environment has `Symbol.replace` support
* @returns {boolean} boolean indicating if an environment supports `Symbol.replace`
*
* @example
* var bool = hasReplaceSymbolSupport();
* // returns <boolean>
*/
function hasReplaceSymbolSupport() {
function hasReplaceSymbolSupport() { // eslint-disable-line id-length
return (
typeof Symbol === 'function' &&
typeof Symbol( 'foo' ) === 'symbol' &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,18 @@
],
"keywords": [
"stdlib",
"stdutils",
"stdutil",
"utilities",
"utility",
"utils",
"util",
"assert",
"detect",
"feature",
"symbol",
"replace",
"string",
"regexp",
"es2015",
"es6",
"support",
"has",
"native",
"issupported",
"cli"
]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
* Copyright (c) 2018 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@
* limitations under the License.
*/

/* eslint-disable stdlib/require-globals */

'use strict';

// MODULES //

var tape = require( 'tape' );
var proxyquire = require( 'proxyquire' );
var Symbol = require( '@stdlib/symbol/ctor' );
var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' );
var detect = require( './../lib' );


// VARIABLES //

var hasSymbols = hasSymbolSupport();
var hasReplaceSymbol = ( typeof Symbol === 'function' && typeof Symbol.replace === 'symbol' );


Expand All @@ -40,27 +42,32 @@ tape( 'main export is a function', function test( t ) {
});

tape( 'feature detection result is a boolean', function test( t ) {
t.strictEqual( typeof detect(), 'boolean', 'returns expected value' );
t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' );
t.end();
});

tape( 'if `Symbol.replace` is supported, detection result is `true`', function test( t ) {
if ( hasReplaceSymbol ) {
t.strictEqual( detect(), true, 'returns expected value' );
t.strictEqual( detect(), true, 'detection result is `true`' );
} else {
t.strictEqual( detect(), false, 'returns expected value' );
t.strictEqual( detect(), false, 'detection result is `false`' );
}
t.end();
});

tape( 'the function guards against a `Symbol` global variable which does not produce `symbols`', function test( t ) {
var detect = proxyquire( './../lib/main.js', {
'@stdlib/symbol/ctor': mock
});
t.strictEqual( detect(), false, 'returns expected value' );
t.end();

function mock() {
return {};
var tmp;
if ( hasSymbols ) {
tmp = Symbol;
Symbol = {}; // eslint-disable-line no-global-assign
} else {
global.Symbol = {};
}
t.strictEqual( detect(), false, 'detection result is `false`' );
if ( hasSymbols ) {
Symbol = tmp; // eslint-disable-line no-global-assign
} else {
delete global.Symbol;
}
t.end();
});
Loading