Skip to content

Commit 4260bf7

Browse files
Faraz GhaniFaraz Ghani
authored andcommitted
feat: add symbol/to-string-tag package
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 6267d64 commit 4260bf7

File tree

9 files changed

+464
-0
lines changed

9 files changed

+464
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# ToStringTagSymbol
22+
23+
> [`Symbol.toStringTag`][mdn-symbol] which specifies the default string tag for an 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 ToStringTagSymbol = require( '@stdlib/symbol/to-string-tag' );
41+
```
42+
43+
#### ToStringTagSymbol
44+
45+
toStringTag [`symbol`][mdn-symbol] which specifies the default string tag used by Object.prototype.toString.
46+
47+
```javascript
48+
var s = typeof ToStringTagSymbol;
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+
</section>
65+
66+
<!-- /.notes -->
67+
68+
<!-- Package usage examples. -->
69+
70+
<section class="examples">
71+
72+
## Examples
73+
74+
<!-- eslint no-undef: "error" -->
75+
76+
```javascript
77+
var ToStringTagSymbol = require( '@stdlib/symbol/to-string-tag' );
78+
79+
function Foo() {
80+
// Assign a property to avoid an empty function:
81+
this.x = 1;
82+
}
83+
84+
if ( ToStringTagSymbol ) {
85+
Foo.prototype[ ToStringTagSymbol ] = 'FooObject';
86+
}
87+
88+
var obj = new Foo();
89+
90+
// When using Object.prototype.toString:
91+
console.log( Object.prototype.toString.call( obj ) );
92+
// => '[object FooObject]'
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- 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. -->
100+
101+
<section class="references">
102+
103+
</section>
104+
105+
<!-- /.references -->
106+
107+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
108+
109+
<section class="related">
110+
111+
* * *
112+
113+
## See Also
114+
115+
- <span class="package-name">[`@stdlib/symbol/ctor`][@stdlib/symbol/ctor]</span><span class="delimiter">: </span><span class="description">symbols.</span>
116+
117+
</section>
118+
119+
<!-- /.related -->
120+
121+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
122+
123+
<section class="links">
124+
125+
[mdn-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol
126+
127+
<!-- <related-links> -->
128+
129+
[@stdlib/symbol/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/symbol/ctor
130+
131+
<!-- </related-links> -->
132+
133+
</section>
134+
135+
<!-- /.links -->
136+
137+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{{alias}}
2+
toStringTag symbol.
3+
4+
This symbol specifies the default tag used by Object.prototype.toString.
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+
13+
See Also
14+
--------
15+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
/**
22+
* toStringTag symbol.
23+
*
24+
* ## Notes
25+
*
26+
* - This symbol specifies the default tag used by `Object.prototype.toString`.
27+
* - The symbol is only supported in ES6/ES2015+ environments. For non-supporting environments, the value is `null`.
28+
*/
29+
export = Symbol.toStringTag;
30+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 ToStringTagSymbol = require( './index' );
22+
23+
24+
// TESTS //
25+
26+
// The exported value is the toStringTag symbol...
27+
{
28+
ToStringTagSymbol;
29+
}
30+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
var ToStringTagSymbol = require( './../lib' );
22+
23+
function Person( name ) {
24+
this.name = name;
25+
26+
// Set a custom tag used by Object.prototype.toString:
27+
if ( ToStringTagSymbol ) {
28+
this[ ToStringTagSymbol ] = 'Person';
29+
}
30+
}
31+
32+
var p = new Person( 'Alice' );
33+
34+
console.log( Object.prototype.toString.call( p ) );
35+
// => '[object Person]'
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
* ToStringTag symbol.
23+
*
24+
* @module @stdlib/symbol/to-string-tag
25+
*
26+
* @example
27+
* var ToStringTagSymbol = require( '@stdlib/symbol/to-string-tag' );
28+
*
29+
* function Person( name ) {
30+
* this.name = name;
31+
*
32+
* // Provide a custom `toString` tag:
33+
* if ( ToStringTagSymbol ) {
34+
* this[ ToStringTagSymbol ] = 'Person';
35+
* }
36+
* }
37+
*
38+
* var p = new Person( 'Alice' );
39+
* console.log( Object.prototype.toString.call( p ) );
40+
* // => '[object Person]'
41+
*/
42+
43+
// MAIN //
44+
45+
var main = require( './main.js' );
46+
47+
48+
// EXPORTS //
49+
50+
module.exports = main;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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 hasToStringTagSupport = require( '@stdlib/assert/has-tostringtag-support' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* ToStringTag symbol.
30+
*
31+
* @name ToStringTagSymbol
32+
* @constant
33+
* @type {(symbol|null)}
34+
*
35+
* @example
36+
* function Person( name ) {
37+
* this.name = name;
38+
*
39+
* // Provide a custom `toString` tag:
40+
* if ( ToStringTagSymbol ) {
41+
* this[ ToStringTagSymbol ] = 'Person';
42+
* }
43+
* }
44+
*
45+
* var p = new Person( 'Alice' );
46+
* console.log( Object.prototype.toString.call( p ) );
47+
* // => '[object Person]'
48+
*/
49+
var ToStringTagSymbol = ( hasToStringTagSupport() ) ?
50+
Symbol.toStringTag :
51+
null;
52+
53+
54+
// EXPORTS //
55+
56+
module.exports = ToStringTagSymbol;

0 commit comments

Comments
 (0)