Skip to content

Commit 680ed0e

Browse files
committed
feat: add symbol/split package
Add a new symbol package that exports Symbol.split, used for defining custom string splitting behavior. This follows the same pattern as existing symbol packages (has-instance, iterator, etc.). Includes: - Main implementation in lib/main.js - Unit tests in test/test.js - TypeScript definitions in docs/types/index.d.ts - Usage examples in examples/index.js - Comprehensive README documentation RFC: #8488
1 parent 7e2e27a commit 680ed0e

7 files changed

Lines changed: 415 additions & 0 deletions

File tree

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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-symbol] which is used to define a custom string splitting behavior.
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-symbol] which is used to define a custom string splitting behavior.
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+
</section>
61+
62+
<!-- /.notes -->
63+
64+
<!-- Package usage examples. -->
65+
66+
<section class="examples">
67+
68+
## Examples
69+
70+
<!-- eslint no-undef: "error" -->
71+
72+
```javascript
73+
var SplitSymbol = require( '@stdlib/symbol/split' );
74+
75+
var str;
76+
var obj;
77+
var parts;
78+
79+
function splitHandler() {
80+
return [ 'beep', 'boop' ];
81+
}
82+
83+
if ( SplitSymbol ) {
84+
str = 'beep-boop';
85+
obj = {};
86+
obj[ SplitSymbol ] = splitHandler;
87+
parts = str.split( obj );
88+
console.log( parts );
89+
// => [ 'beep', 'boop' ]
90+
}
91+
```
92+
93+
</section>
94+
95+
<!-- /.examples -->
96+
97+
<!-- Section to include cited references. If references are cited in the above section, this section should be included. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
98+
99+
<section class="references">
100+
101+
</section>
102+
103+
<!-- /.references -->
104+
105+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated based on metadata in the package.json file. -->
106+
107+
<section class="related">
108+
109+
</section>
110+
111+
<!-- /.related -->
112+
113+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
114+
115+
<section class="links">
116+
117+
[mdn-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol
118+
119+
</section>
120+
121+
<!-- /.links -->
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
/**
20+
* Split symbol.
21+
*
22+
* @example
23+
* if ( SplitSymbol ) {
24+
* console.log( 'Split symbol is supported' );
25+
* }
26+
*/
27+
declare const SplitSymbol: symbol | null;
28+
29+
30+
// EXPORTS //
31+
32+
export = SplitSymbol;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 SplitSymbol = require( './../lib' );
22+
23+
var str;
24+
var obj;
25+
var parts;
26+
27+
function splitHandler() {
28+
return [ 'beep', 'boop' ];
29+
}
30+
31+
if ( SplitSymbol ) {
32+
str = 'beep-boop';
33+
obj = {};
34+
obj[ SplitSymbol ] = splitHandler;
35+
parts = str.split( obj );
36+
console.log( parts );
37+
// => [ 'beep', 'boop' ]
38+
} else {
39+
console.log( 'Split symbol is not supported in this environment.' );
40+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 used to define a custom string splitting behavior.
23+
*
24+
* @module @stdlib/symbol/split
25+
*
26+
* @example
27+
* var SplitSymbol = require( '@stdlib/symbol/split' );
28+
*
29+
* var str;
30+
* var obj;
31+
* var parts;
32+
*
33+
* function splitHandler() {
34+
* return [ 'beep', 'boop' ];
35+
* }
36+
*
37+
* if ( SplitSymbol ) {
38+
* str = 'beep-boop';
39+
* obj = {};
40+
* obj[ SplitSymbol ] = splitHandler;
41+
* parts = str.split( obj );
42+
* console.log( parts );
43+
* // => [ 'beep', 'boop' ]
44+
* }
45+
*/
46+
47+
// MAIN //
48+
49+
var main = require( './main.js' );
50+
51+
52+
// EXPORTS //
53+
54+
module.exports = main;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
* @default
35+
*
36+
* @example
37+
* var str;
38+
* var obj;
39+
* var parts;
40+
*
41+
* function splitHandler() {
42+
* return [ 'beep', 'boop' ];
43+
* }
44+
*
45+
* if ( SplitSymbol ) {
46+
* str = 'beep-boop';
47+
* obj = {};
48+
* obj[ SplitSymbol ] = splitHandler;
49+
* parts = str.split( obj );
50+
* console.log( parts );
51+
* // => [ 'beep', 'boop' ]
52+
* }
53+
*/
54+
var SplitSymbol = ( hasSplitSymbolSupport() ) ? Symbol.split : null;
55+
56+
57+
// EXPORTS //
58+
59+
module.exports = SplitSymbol;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"name": "@stdlib/symbol/split",
3+
"version": "0.0.0",
4+
"description": "Split 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+
"split",
55+
"string"
56+
]
57+
}

0 commit comments

Comments
 (0)