Skip to content

Commit adbbff0

Browse files
committed
feat:added symbol/split
1 parent 06188d1 commit adbbff0

9 files changed

Lines changed: 457 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
> Split [symbol][mdn-split-symbol] which is used to split a string at the indices that match 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 SplitSymbol = require( '@stdlib/symbol/split' );
41+
```
42+
43+
#### SplitSymbol
44+
45+
Split [`symbol`][mdn-split-symbol] which is used to split a string at the indices that match the current object.
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+
- This [symbol][mdn-split-symbol] is only supported in environments which support [symbols][mdn-symbol]. In non-supporting environments, the value is `null`.
63+
64+
- The `split` operator uses the following algorithm to determine the return value of `string.split( object )`:
65+
66+
- If `object` has `[SplitSymbol]()` method then the same method is called.
67+
- Otherwise, if `object` does not have a `[SplitSymbol]()` method (i.e., `object[SplitSymbol]` is `null` or `undefined`), the `String.prototype.split()` method determines the result .
68+
69+
</section>
70+
71+
<!-- /.notes -->
72+
73+
<!-- Package usage examples. -->
74+
75+
<section class="examples">
76+
77+
## Examples
78+
79+
<!-- eslint no-undef: "error" -->
80+
81+
```javascript
82+
var defineProperty = require( '@stdlib/utils/define-property' );
83+
var SplitSymbol = require( './../lib' );
84+
85+
function split( str) {
86+
return str.split(" ");>
87+
}
88+
89+
var obj = {};
90+
91+
defineProperty( obj, SplitSymbol, {
92+
'configurable': true,
93+
'value': null
94+
});
95+
96+
var str = 'hello how';
97+
console.log( str.split( obj ) );
98+
// => [ 'hello how' ]
99+
100+
defineProperty( obj, SplitSymbol, {
101+
'configurable': true,
102+
'value': split
103+
});
104+
console.log( str.split( obj ) );
105+
// => [ 'hello', 'how' ]
106+
```
107+
108+
</section>
109+
110+
<!-- /.examples -->
111+
112+
<!-- 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. -->
113+
114+
<section class="references">
115+
116+
</section>
117+
118+
<!-- /.references -->
119+
120+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
121+
122+
<section class="related">
123+
124+
</section>
125+
126+
<!-- /.related -->
127+
128+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
129+
130+
<section class="links">
131+
132+
[mdn-split-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/split
133+
134+
[mdn-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symvol
135+
136+
</section>
137+
138+
<!-- /.links -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
{{alias}}
3+
Split symbol.
4+
5+
This symbol splits a string at the indices that match the current object.
6+
7+
The symbol is only supported in ES6/ES2015+ environments. For non-supporting
8+
environments, the value is `null`.
9+
10+
Examples
11+
--------
12+
> var s = {{alias}}
13+
e.g., <symbol>
14+
15+
See Also
16+
--------
17+
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 splits a string at the indices that match 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.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: 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+
var defineProperty = require( '@stdlib/utils/define-property' );
22+
var SplitSymbol = require( './../lib' );
23+
24+
function split( 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 = 'hello how';
36+
console.log( str.split( obj ) );
37+
// => [ 'hello how' ]
38+
39+
defineProperty( obj, SplitSymbol, {
40+
'configurable': true,
41+
'value': split
42+
});
43+
console.log( str.split( obj ) );
44+
// => [ 'hello', 'how' ]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
* Split Symbol
23+
*
24+
* @module @stdlib/symbol/split
25+
*
26+
* @example
27+
* var SplitSymbol = require( '@stdlib/symbol/split' );
28+
*
29+
* if ( SplitSymbol === null ) {
30+
* console.log( 'Environment does not support Symbol.split.' );
31+
* } else {
32+
* console.log( 'Environment does support Symbol.split.' );
33+
* }
34+
*/
35+
36+
// MAIN //
37+
38+
var main = require( './main.js' );
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = main;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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' ); // eslint-disable-line id-length
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Split symbol.
30+
*
31+
* @name SplitSymbol
32+
* @constant
33+
* @type {(symbol|null)}
34+
*
35+
* @example
36+
* if ( SplitSymbol === null ) {
37+
* console.log( 'Environment does not support Symbol.split.' );
38+
* } else {
39+
* console.log( 'Environment does support Symbol.split.' );
40+
* }
41+
*/
42+
var SplitSymbol = ( hasSplitSymbolSupport() ) ? Symbol.split : null; // eslint-disable-line max-len
43+
44+
45+
// EXPORTS //
46+
47+
module.exports = SplitSymbol;

0 commit comments

Comments
 (0)