@ohos-ports/stdlib-symbol 0.0.0-stage → 0.4.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +205 -2
- package/SECURITY.md +5 -0
- package/async-iterator/README.md +129 -0
- package/async-iterator/docs/repl.txt +16 -0
- package/async-iterator/docs/types/index.d.ts +31 -0
- package/async-iterator/docs/types/test.ts +29 -0
- package/async-iterator/lib/index.js +43 -0
- package/async-iterator/lib/main.js +48 -0
- package/async-iterator/package.json +61 -0
- package/ctor/README.md +143 -0
- package/ctor/docs/repl.txt +27 -0
- package/ctor/docs/types/index.d.ts +34 -0
- package/ctor/docs/types/test.ts +34 -0
- package/ctor/lib/index.js +40 -0
- package/ctor/lib/main.js +28 -0
- package/ctor/package.json +60 -0
- package/docs/types/index.d.ts +116 -0
- package/has-instance/README.md +143 -0
- package/has-instance/docs/repl.txt +18 -0
- package/has-instance/docs/types/index.d.ts +31 -0
- package/has-instance/docs/types/test.ts +29 -0
- package/has-instance/lib/index.js +49 -0
- package/has-instance/lib/main.js +54 -0
- package/has-instance/package.json +57 -0
- package/is-concat-spreadable/README.md +122 -0
- package/is-concat-spreadable/docs/repl.txt +18 -0
- package/is-concat-spreadable/docs/types/index.d.ts +31 -0
- package/is-concat-spreadable/docs/types/test.ts +29 -0
- package/is-concat-spreadable/lib/index.js +46 -0
- package/is-concat-spreadable/lib/main.js +50 -0
- package/is-concat-spreadable/package.json +58 -0
- package/iterator/README.md +164 -0
- package/iterator/docs/repl.txt +16 -0
- package/iterator/docs/types/index.d.ts +31 -0
- package/iterator/docs/types/test.ts +29 -0
- package/iterator/lib/index.js +75 -0
- package/iterator/lib/main.js +79 -0
- package/iterator/package.json +58 -0
- package/lib/index.js +105 -0
- package/package.json +58 -4
- package/replace/README.md +130 -0
- package/replace/docs/repl.txt +18 -0
- package/replace/docs/types/index.d.ts +31 -0
- package/replace/docs/types/test.ts +29 -0
- package/replace/lib/index.js +44 -0
- package/replace/lib/main.js +48 -0
- package/replace/package.json +58 -0
- package/to-primitive/README.md +158 -0
- package/to-primitive/docs/repl.txt +16 -0
- package/to-primitive/docs/types/index.d.ts +30 -0
- package/to-primitive/docs/types/test.ts +29 -0
- package/to-primitive/lib/index.js +57 -0
- package/to-primitive/lib/main.js +63 -0
- package/to-primitive/package.json +61 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
|
|
3
|
+
@license Apache-2.0
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2025 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
|
+
# ReplaceSymbol
|
|
22
|
+
|
|
23
|
+
> [Symbol][mdn-symbol] which provides a method for replacing substrings matched by the current object.
|
|
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
|
+
</section>
|
|
30
|
+
|
|
31
|
+
<!-- /.intro -->
|
|
32
|
+
|
|
33
|
+
<!-- Package usage documentation. -->
|
|
34
|
+
|
|
35
|
+
<section class="usage">
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
var ReplaceSymbol = require( '@stdlib/symbol/replace' );
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### ReplaceSymbol
|
|
44
|
+
|
|
45
|
+
[`symbol`][mdn-symbol] which provides a method for replacing substrings matched by the current object.
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
var s = typeof ReplaceSymbol;
|
|
49
|
+
// e.g., returns 'symbol'
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
</section>
|
|
53
|
+
|
|
54
|
+
<!-- /.usage -->
|
|
55
|
+
|
|
56
|
+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
57
|
+
|
|
58
|
+
<section class="notes">
|
|
59
|
+
|
|
60
|
+
## Notes
|
|
61
|
+
|
|
62
|
+
- The [symbol][mdn-symbol] is only supported in environments which support [symbols][mdn-symbol]. In non-supporting environments, the value is `null`.
|
|
63
|
+
- When calling `String.prototype.replace` and `String.prototype.replaceAll` and the `pattern` argument is an object with a `[ReplaceSymbol]()` method, this method is called with the target string and replacement as arguments.
|
|
64
|
+
|
|
65
|
+
</section>
|
|
66
|
+
|
|
67
|
+
<!-- /.notes -->
|
|
68
|
+
|
|
69
|
+
<!-- Package usage examples. -->
|
|
70
|
+
|
|
71
|
+
<section class="examples">
|
|
72
|
+
|
|
73
|
+
## Examples
|
|
74
|
+
|
|
75
|
+
<!-- eslint no-undef: "error" -->
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
var defineProperty = require( '@stdlib/utils/define-property' );
|
|
79
|
+
var ReplaceSymbol = require( '@stdlib/symbol/replace' );
|
|
80
|
+
|
|
81
|
+
function replace( str, replacement ) {
|
|
82
|
+
return replacement;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
var obj = {};
|
|
86
|
+
|
|
87
|
+
defineProperty( obj, ReplaceSymbol, {
|
|
88
|
+
'configurable': true,
|
|
89
|
+
'value': null
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
var str = 'beep';
|
|
93
|
+
console.log( str.replace( obj, 'boop' ) );
|
|
94
|
+
|
|
95
|
+
defineProperty( obj, ReplaceSymbol, {
|
|
96
|
+
'configurable': true,
|
|
97
|
+
'value': replace
|
|
98
|
+
});
|
|
99
|
+
console.log( str.replace( obj, 'boop' ) );
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
</section>
|
|
103
|
+
|
|
104
|
+
<!-- /.examples -->
|
|
105
|
+
|
|
106
|
+
<!-- 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. -->
|
|
107
|
+
|
|
108
|
+
<section class="references">
|
|
109
|
+
|
|
110
|
+
</section>
|
|
111
|
+
|
|
112
|
+
<!-- /.references -->
|
|
113
|
+
|
|
114
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
115
|
+
|
|
116
|
+
<section class="related">
|
|
117
|
+
|
|
118
|
+
</section>
|
|
119
|
+
|
|
120
|
+
<!-- /.related -->
|
|
121
|
+
|
|
122
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
123
|
+
|
|
124
|
+
<section class="links">
|
|
125
|
+
|
|
126
|
+
[mdn-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol
|
|
127
|
+
|
|
128
|
+
</section>
|
|
129
|
+
|
|
130
|
+
<!-- /.links -->
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
{{alias}}
|
|
3
|
+
Replace symbol.
|
|
4
|
+
|
|
5
|
+
This symbol provides a method for replacing substrings matched by the
|
|
6
|
+
current object.
|
|
7
|
+
|
|
8
|
+
The symbol is only supported in ES6/ES2015+ environments. For non-supporting
|
|
9
|
+
environments, the value is `null`.
|
|
10
|
+
|
|
11
|
+
Examples
|
|
12
|
+
--------
|
|
13
|
+
> var s = {{alias}}
|
|
14
|
+
e.g., <symbol>
|
|
15
|
+
|
|
16
|
+
See Also
|
|
17
|
+
--------
|
|
18
|
+
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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
|
+
// EXPORTS //
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Replace symbol.
|
|
25
|
+
*
|
|
26
|
+
* ## Notes
|
|
27
|
+
*
|
|
28
|
+
* - This symbol provides a method for replacing substrings matched by the current object.
|
|
29
|
+
* - The symbol is only supported in ES6/ES2015+ environments. For non-supporting environments, the value is `null`.
|
|
30
|
+
*/
|
|
31
|
+
export = Symbol.replace;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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
|
+
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
20
|
+
|
|
21
|
+
import ReplaceSymbol = require( './index' );
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
// TESTS //
|
|
25
|
+
|
|
26
|
+
// The exported value is the `replace` symbol...
|
|
27
|
+
{
|
|
28
|
+
ReplaceSymbol;
|
|
29
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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
|
+
/**
|
|
22
|
+
* Symbol which provides a method for replacing substrings matched by the current object.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/symbol/replace
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var ReplaceSymbol = require( '@stdlib/symbol/replace' );
|
|
28
|
+
*
|
|
29
|
+
* function replace( str, replacement ) {
|
|
30
|
+
* return replacement;
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* var obj = {};
|
|
34
|
+
* obj[ ReplaceSymbol ] = replace;
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
// MAIN //
|
|
38
|
+
|
|
39
|
+
var main = require( './main.js' );
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
// EXPORTS //
|
|
43
|
+
|
|
44
|
+
module.exports = main;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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 hasReplaceSymbolSupport = require( '@ohos-ports/stdlib-assert/has-replace-symbol-support' );
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// MAIN //
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Replace symbol.
|
|
30
|
+
*
|
|
31
|
+
* @name ReplaceSymbol
|
|
32
|
+
* @constant
|
|
33
|
+
* @type {(symbol|null)}
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* function replace( str, replacement ) {
|
|
37
|
+
* return replacement;
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* var obj = {};
|
|
41
|
+
* obj[ ReplaceSymbol ] = replace;
|
|
42
|
+
*/
|
|
43
|
+
var ReplaceSymbol = ( hasReplaceSymbolSupport() ) ? Symbol.replace : null;
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
// EXPORTS //
|
|
47
|
+
|
|
48
|
+
module.exports = ReplaceSymbol;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/symbol/replace",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Replace symbol.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "The Stdlib Authors",
|
|
8
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
9
|
+
},
|
|
10
|
+
"contributors": [
|
|
11
|
+
{
|
|
12
|
+
"name": "The Stdlib Authors",
|
|
13
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"main": "./lib",
|
|
17
|
+
"directories": {
|
|
18
|
+
"doc": "./docs",
|
|
19
|
+
"example": "./examples",
|
|
20
|
+
"lib": "./lib",
|
|
21
|
+
"test": "./test"
|
|
22
|
+
},
|
|
23
|
+
"types": "./docs/types",
|
|
24
|
+
"scripts": {},
|
|
25
|
+
"homepage": "https://github.com/stdlib-js/stdlib",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git://github.com/stdlib-js/stdlib.git"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {},
|
|
34
|
+
"devDependencies": {},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=0.10.0",
|
|
37
|
+
"npm": ">2.7.0"
|
|
38
|
+
},
|
|
39
|
+
"os": [
|
|
40
|
+
"aix",
|
|
41
|
+
"darwin",
|
|
42
|
+
"freebsd",
|
|
43
|
+
"linux",
|
|
44
|
+
"macos",
|
|
45
|
+
"openbsd",
|
|
46
|
+
"sunos",
|
|
47
|
+
"win32",
|
|
48
|
+
"windows"
|
|
49
|
+
],
|
|
50
|
+
"keywords": [
|
|
51
|
+
"stdlib",
|
|
52
|
+
"symbol",
|
|
53
|
+
"sym",
|
|
54
|
+
"replace",
|
|
55
|
+
"replacer",
|
|
56
|
+
"string"
|
|
57
|
+
]
|
|
58
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
|
|
3
|
+
@license Apache-2.0
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2025 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
|
+
# ToPrimitiveSymbol
|
|
22
|
+
|
|
23
|
+
> [Symbol][mdn-symbol] which specifies a method for converting an object to a primitive value.
|
|
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
|
+
</section>
|
|
30
|
+
|
|
31
|
+
<!-- /.intro -->
|
|
32
|
+
|
|
33
|
+
<!-- Package usage documentation. -->
|
|
34
|
+
|
|
35
|
+
<section class="usage">
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
var ToPrimitiveSymbol = require( '@stdlib/symbol/to-primitive' );
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### ToPrimitiveSymbol
|
|
44
|
+
|
|
45
|
+
[Symbol][mdn-symbol] which specifies a method for converting an object to a primitive value.
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
var s = typeof ToPrimitiveSymbol;
|
|
49
|
+
// e.g., returns 'symbol'
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
</section>
|
|
53
|
+
|
|
54
|
+
<!-- /.usage -->
|
|
55
|
+
|
|
56
|
+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
57
|
+
|
|
58
|
+
<section class="notes">
|
|
59
|
+
|
|
60
|
+
## Notes
|
|
61
|
+
|
|
62
|
+
- The [symbol][mdn-symbol] is only supported in environments which support [symbols][mdn-symbol]. In non-supporting environments, the value is `null`.
|
|
63
|
+
|
|
64
|
+
- When an object needs to be converted to a primitive value, JavaScript runtimes look for a `[ToPrimitiveSymbol]()` method on the object. If the method exists, JavaScript runtimes call the method with a `hint` string argument (one of `'number'`, `'string'`, or `'default'`) indicating the preferred type of the primitive value to be returned.
|
|
65
|
+
|
|
66
|
+
- If the hint is `'number'`, the method should return a number.
|
|
67
|
+
- If the hint is `'string'`, the method should return a string.
|
|
68
|
+
- If the hint is `'default'`, the method can return either a number or a string.
|
|
69
|
+
|
|
70
|
+
- If an object does not have a `[ToPrimitiveSymbol]()` method, JavaScript runtimes use the default type conversion algorithm by calling `valueOf()` and/or `toString()` methods.
|
|
71
|
+
|
|
72
|
+
</section>
|
|
73
|
+
|
|
74
|
+
<!-- /.notes -->
|
|
75
|
+
|
|
76
|
+
<!-- Package usage examples. -->
|
|
77
|
+
|
|
78
|
+
<section class="examples">
|
|
79
|
+
|
|
80
|
+
## Examples
|
|
81
|
+
|
|
82
|
+
<!-- eslint-disable no-invalid-this -->
|
|
83
|
+
|
|
84
|
+
<!-- eslint no-undef: "error" -->
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
var defineProperty = require( '@stdlib/utils/define-property' );
|
|
88
|
+
var Number = require( '@stdlib/number/ctor' );
|
|
89
|
+
var ToPrimitiveSymbol = require( '@stdlib/symbol/to-primitive' );
|
|
90
|
+
|
|
91
|
+
function CustomObject( value ) {
|
|
92
|
+
if ( !(this instanceof CustomObject) ) {
|
|
93
|
+
return new CustomObject( value );
|
|
94
|
+
}
|
|
95
|
+
this._value = value;
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function toPrimitive( hint ) {
|
|
100
|
+
var value = this._value;
|
|
101
|
+
if ( hint === 'string' ) {
|
|
102
|
+
return 'CustomObject: ' + value;
|
|
103
|
+
}
|
|
104
|
+
if ( hint === 'number' ) {
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
// Default hint:
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
defineProperty( CustomObject.prototype, ToPrimitiveSymbol, {
|
|
112
|
+
'configurable': false,
|
|
113
|
+
'enumerable': false,
|
|
114
|
+
'writable': false,
|
|
115
|
+
'value': toPrimitive
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
var obj = new CustomObject( 42 );
|
|
119
|
+
|
|
120
|
+
console.log( String( obj ) );
|
|
121
|
+
// => 'CustomObject: 42'
|
|
122
|
+
|
|
123
|
+
console.log( Number( obj ) );
|
|
124
|
+
// => 42
|
|
125
|
+
|
|
126
|
+
console.log( obj + 10 );
|
|
127
|
+
// => 52
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
</section>
|
|
131
|
+
|
|
132
|
+
<!-- /.examples -->
|
|
133
|
+
|
|
134
|
+
<!-- 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. -->
|
|
135
|
+
|
|
136
|
+
<section class="references">
|
|
137
|
+
|
|
138
|
+
</section>
|
|
139
|
+
|
|
140
|
+
<!-- /.references -->
|
|
141
|
+
|
|
142
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
143
|
+
|
|
144
|
+
<section class="related">
|
|
145
|
+
|
|
146
|
+
</section>
|
|
147
|
+
|
|
148
|
+
<!-- /.related -->
|
|
149
|
+
|
|
150
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
151
|
+
|
|
152
|
+
<section class="links">
|
|
153
|
+
|
|
154
|
+
[mdn-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol
|
|
155
|
+
|
|
156
|
+
</section>
|
|
157
|
+
|
|
158
|
+
<!-- /.links -->
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
{{alias}}
|
|
3
|
+
Symbol which specifies a method for converting an object to a primitive
|
|
4
|
+
value.
|
|
5
|
+
|
|
6
|
+
The symbol is only supported in ES6/ES2015+ environments. For non-supporting
|
|
7
|
+
environments, the value is `null`.
|
|
8
|
+
|
|
9
|
+
Examples
|
|
10
|
+
--------
|
|
11
|
+
> var s = {{alias}}
|
|
12
|
+
e.g., <symbol>
|
|
13
|
+
|
|
14
|
+
See Also
|
|
15
|
+
--------
|
|
16
|
+
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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
|
+
// EXPORTS //
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Symbol which specifies a method for converting an object to a primitive value.
|
|
25
|
+
*
|
|
26
|
+
* ## Notes
|
|
27
|
+
*
|
|
28
|
+
* - The symbol is only supported in ES6/ES2015+ environments. For non-supporting environments, the value is `null`.
|
|
29
|
+
*/
|
|
30
|
+
export = Symbol.toPrimitive;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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
|
+
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
20
|
+
|
|
21
|
+
import ToPrimitive = require( './index' );
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
// TESTS //
|
|
25
|
+
|
|
26
|
+
// The exported value is the `toPrimitive` symbol...
|
|
27
|
+
{
|
|
28
|
+
ToPrimitive;
|
|
29
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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
|
+
/**
|
|
22
|
+
* Symbol which specifies a method for converting an object to a primitive value.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/symbol/to-primitive
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var Number = require( '@stdlib/number/ctor' );
|
|
28
|
+
* var ToPrimitiveSymbol = require( '@stdlib/symbol/to-primitive' );
|
|
29
|
+
*
|
|
30
|
+
* function valueOf( hint ) {
|
|
31
|
+
* if ( hint === 'string' ) {
|
|
32
|
+
* return 'CustomObject';
|
|
33
|
+
* }
|
|
34
|
+
* if ( hint === 'number' ) {
|
|
35
|
+
* return 123;
|
|
36
|
+
* }
|
|
37
|
+
* return null;
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* var obj = {};
|
|
41
|
+
* obj[ ToPrimitiveSymbol ] = valueOf;
|
|
42
|
+
*
|
|
43
|
+
* var str = String( obj );
|
|
44
|
+
* // returns 'CustomObject'
|
|
45
|
+
*
|
|
46
|
+
* var num = Number( obj );
|
|
47
|
+
* // returns 123
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
// MODULES //
|
|
51
|
+
|
|
52
|
+
var main = require( './main.js' );
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
// EXPORTS //
|
|
56
|
+
|
|
57
|
+
module.exports = main;
|