Skip to content

Commit 4de5e1c

Browse files
committed
Add symbol/split
1 parent 974a2c4 commit 4de5e1c

File tree

9 files changed

+456
-0
lines changed

9 files changed

+456
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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 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 SplitSymbol = require( '@stdlib/symbol/split' );
41+
```
42+
43+
#### SplitSymbol
44+
45+
[`symbol`][mdn-symbol] which provides a method for replacing substrings matched by 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+
- The [symbol][mdn-symbol] is only supported in environments which support [symbols][mdn-symbol]. In non-supporting environments, the value is `null`.
63+
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 split(str) {
82+
var idx = str.indexOf(this.value);
83+
if (idx === -1) {
84+
return str;
85+
}
86+
return str.substring(0, idx) + '/' + str.substring(idx + this.value.length);
87+
}
88+
89+
var obj = { value: 'foo' };
90+
91+
defineProperty(obj, SplitSymbol, {
92+
'configurable': true,
93+
'value': split
94+
});
95+
96+
var str = 'foobar';
97+
console.log(str.split(obj));
98+
99+
```
100+
101+
</section>
102+
103+
<!-- /.examples -->
104+
105+
<!-- 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. -->
106+
107+
<section class="references">
108+
109+
</section>
110+
111+
<!-- /.references -->
112+
113+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
114+
115+
<section class="related">
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+
</section>
128+
129+
<!-- /.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 provides a method for splitting substrings matched by 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 provides a method for splitting 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.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 SplitSymbol = require( './index' );
22+
23+
24+
// TESTS //
25+
26+
// The exported value is the `split` symbol...
27+
{
28+
SplitSymbol;
29+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
var defineProperty = require('@stdlib/utils/define-property');
21+
var SplitSymbol = require('../../lib');
22+
23+
function split(str) {
24+
var idx = str.indexOf(this.value);
25+
if (idx === -1) {
26+
return str;
27+
}
28+
return str.substring(0, idx) + '/' + str.substring(idx + this.value.length);
29+
}
30+
31+
var obj = { value: 'foo' };
32+
33+
defineProperty(obj, SplitSymbol, {
34+
'configurable': true,
35+
'value': split
36+
});
37+
38+
var str = 'foobar';
39+
console.log(str.split(obj));
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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/split
25+
*
26+
* @example
27+
* var SplitSymbol = require( '@stdlib/symbol/split' );
28+
*
29+
* function split(str) {
30+
* var idx = str.indexOf(this.value);
31+
* if (idx === -1) {
32+
* return str; // nothing to split
33+
* }
34+
* return str.substring(0, idx) + '/' + str.substring(idx + this.value.length);
35+
*}
36+
*
37+
* var obj = {};
38+
* obj[ SplitSymbol ] = split;
39+
*
40+
*/
41+
42+
// MAIN //
43+
44+
var main = require( './main.js' );
45+
46+
47+
// EXPORTS //
48+
49+
module.exports = main;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 split(str) {
37+
* var idx = str.indexOf(this.value);
38+
* if (idx === -1) {
39+
* return str; // nothing to split
40+
* }
41+
* return str.substring(0, idx) + '/' + str.substring(idx + this.value.length);
42+
*}
43+
*
44+
* var obj = {};
45+
* obj[ SplitSymbol ] = split;
46+
*/
47+
var splitSymbol = ( hasSplitSymbolSupport() ) ? Symbol.split : null;
48+
49+
50+
// EXPORTS //
51+
52+
module.exports = splitSymbol;

0 commit comments

Comments
 (0)