Skip to content

Commit 09799ea

Browse files
feat: add symbol/split
This commit adds the `@stdlib/symbol/split` package which exports `Symbol.split` if the environment supports it, otherwise `null`. The `Symbol.split` symbol specifies a method that splits a string at the indices that match a regular expression. When calling `String.prototype.split` and the separator argument is an object with a `[Symbol.split]()` method, this method is called with the target string and limit as arguments. The package structure follows the existing convention established by similar packages such as `symbol/replace`, `symbol/has-instance`, and `symbol/is-concat-spreadable`. Closes: #8488
1 parent 1122999 commit 09799ea

File tree

10 files changed

+463
-0
lines changed

10 files changed

+463
-0
lines changed

lib/node_modules/@stdlib/symbol/lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ setReadOnly( ns, 'IteratorSymbol', require( '@stdlib/symbol/iterator' ) );
9090
*/
9191
setReadOnly( ns, 'ReplaceSymbol', require( '@stdlib/symbol/replace' ) );
9292

93+
/**
94+
* @name SplitSymbol
95+
* @memberof ns
96+
* @readonly
97+
* @type {(symbol|null)}
98+
* @see {@link module:@stdlib/symbol/split}
99+
*/
100+
setReadOnly( ns, 'SplitSymbol', require( '@stdlib/symbol/split' ) );
101+
93102
/**
94103
* @name ToPrimitiveSymbol
95104
* @memberof ns
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
# SplitSymbol
22+
23+
> [Symbol][mdn-symbol] which specifies a method that splits a string at the indices that match a regular expression.
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 SplitSymbol = require( '@stdlib/symbol/split' );
41+
```
42+
43+
#### SplitSymbol
44+
45+
[`symbol`][mdn-symbol] which specifies a method that splits a string at the indices that match a regular expression.
46+
47+
```javascript
48+
var s = typeof SplitSymbol;
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.split` and the `separator` argument is an object with a `[SplitSymbol]()` method, this method is called with the target string and limit 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 SplitSymbol = require( '@stdlib/symbol/split' );
80+
81+
function splitter( str ) {
82+
return str.split( '-' );
83+
}
84+
85+
var obj = {};
86+
87+
defineProperty( obj, SplitSymbol, {
88+
'configurable': true,
89+
'value': null
90+
});
91+
92+
var str = 'a-b-c-d-e';
93+
console.log( str.split( obj ) );
94+
95+
defineProperty( obj, SplitSymbol, {
96+
'configurable': true,
97+
'value': splitter
98+
});
99+
console.log( str.split( obj ) );
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 -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
{{alias}}
3+
Split symbol.
4+
5+
This symbol specifies a method that splits a string at the indices that
6+
match a regular expression.
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+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
* Split symbol.
25+
*
26+
* ## Notes
27+
*
28+
* - This symbol specifies a method that splits a string at the indices that match a regular expression.
29+
* - The symbol is only supported in ES6/ES2015+ environments. For non-supporting environments, the value is `null`.
30+
*/
31+
export = Symbol.split;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 Split = require( './index' );
22+
23+
24+
// TESTS //
25+
26+
// The exported value is the `split` symbol...
27+
{
28+
Split;
29+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
var defineProperty = require( '@stdlib/utils/define-property' );
22+
var SplitSymbol = require( './../lib' );
23+
24+
function splitter( str ) {
25+
return str.split( '-' );
26+
}
27+
28+
var obj = {};
29+
30+
defineProperty( obj, SplitSymbol, {
31+
'configurable': true,
32+
'value': null
33+
});
34+
35+
var str = 'a-b-c-d-e';
36+
console.log( str.split( obj ) );
37+
38+
defineProperty( obj, SplitSymbol, {
39+
'configurable': true,
40+
'value': splitter
41+
});
42+
console.log( str.split( obj ) );
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 specifies a method that splits a string at the indices that match a regular expression.
23+
*
24+
* @module @stdlib/symbol/split
25+
*
26+
* @example
27+
* var SplitSymbol = require( '@stdlib/symbol/split' );
28+
*
29+
* function splitter( str, limit ) {
30+
* return str.split( '-', limit );
31+
* }
32+
*
33+
* var obj = {};
34+
* obj[ SplitSymbol ] = splitter;
35+
*/
36+
37+
// MAIN //
38+
39+
var main = require( './main.js' );
40+
41+
42+
// EXPORTS //
43+
44+
module.exports = main;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 hasSplitSymbolSupport = require( '@stdlib/assert/has-split-symbol-support' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Split symbol.
30+
*
31+
* @name SplitSymbol
32+
* @constant
33+
* @type {(symbol|null)}
34+
*
35+
* @example
36+
* function splitter( str, limit ) {
37+
* return str.split( '-', limit );
38+
* }
39+
*
40+
* var obj = {};
41+
* obj[ SplitSymbol ] = splitter;
42+
*/
43+
var SplitSymbol = ( hasSplitSymbolSupport() ) ? Symbol.split : null;
44+
45+
46+
// EXPORTS //
47+
48+
module.exports = SplitSymbol;

0 commit comments

Comments
 (0)