@stdlib/string-to-grapheme-cluster-iterator 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,177 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
package/NOTICE ADDED
@@ -0,0 +1 @@
1
+ Copyright (c) 2016-2023 The Stdlib Authors.
package/README.md ADDED
@@ -0,0 +1,289 @@
1
+ <!--
2
+
3
+ @license Apache-2.0
4
+
5
+ Copyright (c) 2022 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
+ # graphemeClusters2iterator
22
+
23
+ [![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
24
+
25
+ > Create an iterator which iterates over [grapheme clusters][unicode-text-segmentation].
26
+
27
+ <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
28
+
29
+ <section class="intro">
30
+
31
+ </section>
32
+
33
+ <!-- /.intro -->
34
+
35
+ <!-- Package usage documentation. -->
36
+
37
+ <section class="installation">
38
+
39
+ ## Installation
40
+
41
+ ```bash
42
+ npm install @stdlib/string-to-grapheme-cluster-iterator
43
+ ```
44
+
45
+ </section>
46
+
47
+ <section class="usage">
48
+
49
+ ## Usage
50
+
51
+ ```javascript
52
+ var graphemeClusters2iterator = require( '@stdlib/string-to-grapheme-cluster-iterator' );
53
+ ```
54
+
55
+ #### graphemeClusters2iterator( src\[, mapFcn\[, thisArg]] )
56
+
57
+ Returns an iterator which iterates over each [grapheme cluster][unicode-text-segmentation] in a `string`.
58
+
59
+ ```javascript
60
+ var iter = graphemeClusters2iterator( '🌷🍕' );
61
+
62
+ var v = iter.next().value;
63
+ // returns '🌷'
64
+
65
+ v = iter.next().value;
66
+ // returns '🍕'
67
+
68
+ var bool = iter.next().done;
69
+ // returns true
70
+ ```
71
+
72
+ The returned iterator protocol-compliant object has the following properties:
73
+
74
+ - **next**: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the iterator is finished.
75
+ - **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.
76
+
77
+ To invoke a function for each `src` value, provide a callback function.
78
+
79
+ ```javascript
80
+ function fcn( v ) {
81
+ return v + v;
82
+ }
83
+
84
+ var it = graphemeClusters2iterator( 'beep', fcn );
85
+ // returns <Object>
86
+
87
+ var v = it.next().value;
88
+ // returns 'bb'
89
+
90
+ v = it.next().value;
91
+ // returns 'ee'
92
+
93
+ v = it.next().value;
94
+ // returns 'ee'
95
+
96
+ // ...
97
+ ```
98
+
99
+ The invoked function is provided three arguments:
100
+
101
+ - **value**: [grapheme cluster][unicode-text-segmentation].
102
+ - **index**: iterated value index.
103
+ - **src**: source string.
104
+
105
+ ```javascript
106
+ function fcn( v, i ) {
107
+ return v + i;
108
+ }
109
+
110
+ var it = graphemeClusters2iterator( 'bar', fcn );
111
+ // returns <Object>
112
+
113
+ var v = it.next().value;
114
+ // returns 'b0'
115
+
116
+ v = it.next().value;
117
+ // returns 'a1'
118
+
119
+ v = it.next().value;
120
+ // returns 'r2'
121
+
122
+ // ...
123
+ ```
124
+
125
+ To set the callback function execution context, provide a `thisArg`.
126
+
127
+ ```javascript
128
+ function fcn( v ) {
129
+ this.count += 1;
130
+ return v;
131
+ }
132
+
133
+ var ctx = {
134
+ 'count': 0
135
+ };
136
+
137
+ var it = graphemeClusters2iterator( '🌷🍕', fcn, ctx );
138
+ // returns <Object>
139
+
140
+ var v = it.next().value;
141
+ // returns '🌷'
142
+
143
+ v = it.next().value;
144
+ // returns '🍕'
145
+
146
+ var count = ctx.count;
147
+ // returns 2
148
+ ```
149
+
150
+ </section>
151
+
152
+ <!-- /.usage -->
153
+
154
+ <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
155
+
156
+ <section class="notes">
157
+
158
+ ## Notes
159
+
160
+ - If an environment supports `Symbol.iterator`, the returned iterator is iterable.
161
+ - In environments supporting `Symbol.iterator`, the function **explicitly** does **not** invoke a string's `@@iterator` method, regardless of whether this method is defined. To convert a string to an implementation defined iterator, invoke this method directly.
162
+
163
+ </section>
164
+
165
+ <!-- /.notes -->
166
+
167
+ <!-- Package usage examples. -->
168
+
169
+ <section class="examples">
170
+
171
+ ## Examples
172
+
173
+ <!-- eslint no-undef: "error" -->
174
+
175
+ ```javascript
176
+ var graphemeClusters2iterator = require( '@stdlib/string-to-grapheme-cluster-iterator' );
177
+
178
+ function repeat( str ) {
179
+ return str + str;
180
+ }
181
+
182
+ // Create an iterator which iterates over grapheme clusters:
183
+ var it = graphemeClusters2iterator( 'Iñtërnâtiônàlizætiøn', repeat );
184
+
185
+ // Perform manual iteration...
186
+ var v;
187
+ while ( true ) {
188
+ v = it.next();
189
+ if ( v.done ) {
190
+ break;
191
+ }
192
+ console.log( v.value );
193
+ }
194
+ ```
195
+
196
+ </section>
197
+
198
+ <!-- /.examples -->
199
+
200
+ <!-- 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. -->
201
+
202
+ <section class="references">
203
+
204
+ </section>
205
+
206
+ <!-- /.references -->
207
+
208
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
209
+
210
+ <section class="related">
211
+
212
+ </section>
213
+
214
+ <!-- /.related -->
215
+
216
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
217
+
218
+
219
+ <section class="main-repo" >
220
+
221
+ * * *
222
+
223
+ ## Notice
224
+
225
+ This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
226
+
227
+ For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib].
228
+
229
+ #### Community
230
+
231
+ [![Chat][chat-image]][chat-url]
232
+
233
+ ---
234
+
235
+ ## License
236
+
237
+ See [LICENSE][stdlib-license].
238
+
239
+
240
+ ## Copyright
241
+
242
+ Copyright &copy; 2016-2023. The Stdlib [Authors][stdlib-authors].
243
+
244
+ </section>
245
+
246
+ <!-- /.stdlib -->
247
+
248
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
249
+
250
+ <section class="links">
251
+
252
+ [npm-image]: http://img.shields.io/npm/v/@stdlib/string-to-grapheme-cluster-iterator.svg
253
+ [npm-url]: https://npmjs.org/package/@stdlib/string-to-grapheme-cluster-iterator
254
+
255
+ [test-image]: https://github.com/stdlib-js/string-to-grapheme-cluster-iterator/actions/workflows/test.yml/badge.svg?branch=v0.0.1
256
+ [test-url]: https://github.com/stdlib-js/string-to-grapheme-cluster-iterator/actions/workflows/test.yml?query=branch:v0.0.1
257
+
258
+ [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/string-to-grapheme-cluster-iterator/main.svg
259
+ [coverage-url]: https://codecov.io/github/stdlib-js/string-to-grapheme-cluster-iterator?branch=main
260
+
261
+ <!--
262
+
263
+ [dependencies-image]: https://img.shields.io/david/stdlib-js/string-to-grapheme-cluster-iterator.svg
264
+ [dependencies-url]: https://david-dm.org/stdlib-js/string-to-grapheme-cluster-iterator/main
265
+
266
+ -->
267
+
268
+ [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
269
+ [chat-url]: https://gitter.im/stdlib-js/stdlib/
270
+
271
+ [stdlib]: https://github.com/stdlib-js/stdlib
272
+
273
+ [stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
274
+
275
+ [umd]: https://github.com/umdjs/umd
276
+ [es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
277
+
278
+ [deno-url]: https://github.com/stdlib-js/string-to-grapheme-cluster-iterator/tree/deno
279
+ [umd-url]: https://github.com/stdlib-js/string-to-grapheme-cluster-iterator/tree/umd
280
+ [esm-url]: https://github.com/stdlib-js/string-to-grapheme-cluster-iterator/tree/esm
281
+ [branches-url]: https://github.com/stdlib-js/string-to-grapheme-cluster-iterator/blob/main/branches.md
282
+
283
+ [stdlib-license]: https://raw.githubusercontent.com/stdlib-js/string-to-grapheme-cluster-iterator/main/LICENSE
284
+
285
+ [unicode-text-segmentation]: http://www.unicode.org/reports/tr29/
286
+
287
+ </section>
288
+
289
+ <!-- /.links -->
package/docs/repl.txt ADDED
@@ -0,0 +1,55 @@
1
+
2
+ {{alias}}( src[, mapFcn[, thisArg]] )
3
+ Returns an iterator which iterates over each grapheme cluster in a string.
4
+
5
+ When invoked, an input function is provided three arguments:
6
+
7
+ - value: grapheme cluster
8
+ - index: iterated value index
9
+ - src: source string
10
+
11
+ If an environment supports Symbol.iterator, the returned iterator is
12
+ iterable.
13
+
14
+ If an environment supports Symbol.iterator, the function explicitly does not
15
+ invoke a string's `@@iterator` method, regardless of whether this method is
16
+ defined. To convert a string to an implementation defined iterator, invoke
17
+ this method directly.
18
+
19
+ Parameters
20
+ ----------
21
+ src: string
22
+ String from which to create the iterator.
23
+
24
+ mapFcn: Function (optional)
25
+ Function to invoke for each iterated value.
26
+
27
+ thisArg: any (optional)
28
+ Execution context.
29
+
30
+ Returns
31
+ -------
32
+ iterator: Object
33
+ Iterator.
34
+
35
+ iterator.next(): Function
36
+ Returns an iterator protocol-compliant object containing the next
37
+ iterated value (if one exists) and a boolean flag indicating whether the
38
+ iterator is finished.
39
+
40
+ iterator.return( [value] ): Function
41
+ Finishes an iterator and returns a provided value.
42
+
43
+ Examples
44
+ --------
45
+ > var it = {{alias}}( '🌷🍕' );
46
+ > var v = it.next().value
47
+ '🌷'
48
+ > v = it.next().value
49
+ '🍕'
50
+ > var bool = it.next().done
51
+ true
52
+
53
+ See Also
54
+ --------
55
+
@@ -0,0 +1,97 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2022 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: 2.0
20
+
21
+ /// <reference types="@stdlib/types"/>
22
+
23
+ import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter';
24
+
25
+ // Define a union type representing both iterable and non-iterable iterators:
26
+ type Iterator = Iter | IterableIterator;
27
+
28
+ /**
29
+ * Map function invoked for each iterated value.
30
+ *
31
+ * @returns iterator value
32
+ */
33
+ type Nullary = () => any;
34
+
35
+ /**
36
+ * Map function invoked for each iterated value.
37
+ *
38
+ * @param value - iterated value
39
+ * @returns iterator value
40
+ */
41
+ type Unary = ( value: string ) => any;
42
+
43
+ /**
44
+ * Map function invoked for each iterated value.
45
+ *
46
+ * @param value - iterated value
47
+ * @param index - iterated value index
48
+ * @returns iterator value
49
+ */
50
+ type Binary = ( value: string, index: number ) => any;
51
+
52
+ /**
53
+ * Map function invoked for each iterated value.
54
+ *
55
+ * @param value - iterated value
56
+ * @param index - iterated value index
57
+ * @param src - source string
58
+ * @returns iterator value
59
+ */
60
+ type Ternary = ( value: string, index: number, src: string ) => any;
61
+
62
+ /**
63
+ * Map function invoked for each iterated value.
64
+ *
65
+ * @param value - iterated value
66
+ * @param index - iterated value index
67
+ * @param src - source string
68
+ * @returns iterator value
69
+ */
70
+ type MapFunction = Nullary | Unary | Binary | Ternary;
71
+
72
+ /**
73
+ * Returns an iterator which iterates over each grapheme cluster in a string.
74
+ *
75
+ * @param src - input value
76
+ * @param mapFc - function to invoke for each iterated value
77
+ * @param thisArg - execution context
78
+ * @returns iterator
79
+ *
80
+ * @example
81
+ * var iter = graphemeClusters2iterator( '🌷🍕' );
82
+ *
83
+ * var v = iter.next().value;
84
+ * // returns '🌷'
85
+ *
86
+ * v = iter.next().value;
87
+ * // returns '🍕'
88
+ *
89
+ * var bool = iter.next().done;
90
+ * // returns true
91
+ */
92
+ declare function graphemeClusters2iterator( src: string, mapFcn?: MapFunction, thisArg?: any ): Iterator; // tslint:disable-line:max-line-length
93
+
94
+
95
+ // EXPORTS //
96
+
97
+ export = graphemeClusters2iterator;
@@ -0,0 +1,66 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2022 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
+ import graphemeClusters2iterator = require( './index' );
20
+
21
+ /**
22
+ * Transforms a string.
23
+ *
24
+ * @param v - iterated value
25
+ * @returns new value
26
+ */
27
+ function transform( v: string ): string {
28
+ return v + v;
29
+ }
30
+
31
+
32
+ // TESTS //
33
+
34
+ // The function returns an iterator...
35
+ {
36
+ graphemeClusters2iterator( 'beep' ); // $ExpectType Iterator
37
+ graphemeClusters2iterator( 'beep', transform ); // $ExpectType Iterator
38
+ graphemeClusters2iterator( 'beep', transform, {} ); // $ExpectType Iterator
39
+ }
40
+
41
+ // The compiler throws an error if the function is provided a first argument which is not a string...
42
+ {
43
+ graphemeClusters2iterator( 123 ); // $ExpectError
44
+ graphemeClusters2iterator( true ); // $ExpectError
45
+ graphemeClusters2iterator( false ); // $ExpectError
46
+ graphemeClusters2iterator( {} ); // $ExpectError
47
+ graphemeClusters2iterator( null ); // $ExpectError
48
+ graphemeClusters2iterator( undefined ); // $ExpectError
49
+ }
50
+
51
+ // The compiler throws an error if the function is provided a second argument which is not a map function...
52
+ {
53
+ graphemeClusters2iterator( 'beep', 'abc' ); // $ExpectError
54
+ graphemeClusters2iterator( 'beep', 123 ); // $ExpectError
55
+ graphemeClusters2iterator( 'beep', [] ); // $ExpectError
56
+ graphemeClusters2iterator( 'beep', {} ); // $ExpectError
57
+ graphemeClusters2iterator( 'beep', true ); // $ExpectError
58
+ graphemeClusters2iterator( 'beep', false ); // $ExpectError
59
+ graphemeClusters2iterator( 'beep', null ); // $ExpectError
60
+ }
61
+
62
+ // The compiler throws an error if the function is provided an unsupported number of arguments...
63
+ {
64
+ graphemeClusters2iterator(); // $ExpectError
65
+ graphemeClusters2iterator( 'beep', transform, {}, 123 ); // $ExpectError
66
+ }
package/lib/index.js ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2022 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
+ * Create an iterator which iterates over grapheme clusters.
23
+ *
24
+ * @module @stdlib/string-to-grapheme-cluster-iterator
25
+ *
26
+ * @example
27
+ * var graphemeClusters2iterator = require( '@stdlib/string-to-grapheme-cluster-iterator' );
28
+ *
29
+ * var iter = graphemeClusters2iterator( '🌷🍕' );
30
+ *
31
+ * var v = iter.next().value;
32
+ * // returns '🌷'
33
+ *
34
+ * v = iter.next().value;
35
+ * // returns '🍕'
36
+ *
37
+ * var bool = iter.next().done;
38
+ * // returns true
39
+ */
40
+
41
+ // MODULES //
42
+
43
+ var main = require( './main.js' );
44
+
45
+
46
+ // EXPORTS //
47
+
48
+ module.exports = main;
package/lib/main.js ADDED
@@ -0,0 +1,195 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2022 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 setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
24
+ var isFunction = require( '@stdlib/assert-is-function' );
25
+ var isString = require( '@stdlib/assert-is-string' ).isPrimitive;
26
+ var iteratorSymbol = require( '@stdlib/symbol-iterator' );
27
+ var nextGraphemeClusterBreak = require( '@stdlib/string-next-grapheme-cluster-break' );
28
+ var format = require( '@stdlib/string-format' );
29
+
30
+
31
+ // MAIN //
32
+
33
+ /**
34
+ * Returns an iterator which iterates over each grapheme cluster in a string.
35
+ *
36
+ * @param {string} src - input value
37
+ * @param {Function} [mapFcn] - function to invoke for each iterated value
38
+ * @param {*} [thisArg] - execution context
39
+ * @throws {TypeError} first argument must be a string
40
+ * @throws {TypeError} second argument must be a function
41
+ * @returns {Iterator} iterator
42
+ *
43
+ * @example
44
+ * var iter = graphemeClusters2iterator( '🌷🍕' );
45
+ *
46
+ * var v = iter.next().value;
47
+ * // returns '🌷'
48
+ *
49
+ * v = iter.next().value;
50
+ * // returns '🍕'
51
+ *
52
+ * var bool = iter.next().done;
53
+ * // returns true
54
+ */
55
+ function graphemeClusters2iterator( src ) {
56
+ var thisArg;
57
+ var iter;
58
+ var FLG;
59
+ var fcn;
60
+ var i;
61
+ if ( !isString( src ) ) {
62
+ throw new TypeError( format( 'invalid argument. First argument must be astring. Value: `%s`.', src ) );
63
+ }
64
+ if ( arguments.length > 1 ) {
65
+ fcn = arguments[ 1 ];
66
+ if ( !isFunction( fcn ) ) {
67
+ throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', fcn ) );
68
+ }
69
+ thisArg = arguments[ 2 ];
70
+ }
71
+ i = 0;
72
+
73
+ // Create an iterator protocol-compliant object:
74
+ iter = {};
75
+ if ( fcn ) {
76
+ setReadOnly( iter, 'next', next1 );
77
+ } else {
78
+ setReadOnly( iter, 'next', next2 );
79
+ }
80
+ setReadOnly( iter, 'return', end );
81
+
82
+ // If an environment supports `Symbol.iterator`, make the iterator iterable:
83
+ if ( iteratorSymbol ) {
84
+ setReadOnly( iter, iteratorSymbol, factory );
85
+ }
86
+ return iter;
87
+
88
+ /**
89
+ * Returns an iterator protocol-compliant object containing the next iterated value.
90
+ *
91
+ * @private
92
+ * @returns {Object} iterator protocol-compliant object
93
+ */
94
+ function next1() {
95
+ var v;
96
+ var j;
97
+ if ( FLG ) {
98
+ return {
99
+ 'done': true
100
+ };
101
+ }
102
+ j = nextGraphemeClusterBreak( src, i );
103
+ if ( j === -1 ) {
104
+ FLG = true;
105
+ if ( src.length ) {
106
+ return {
107
+ 'value': fcn.call( thisArg, src.substring( i ), i, src ),
108
+ 'done': false
109
+ };
110
+ }
111
+ return {
112
+ 'done': true
113
+ };
114
+ }
115
+ v = fcn.call( thisArg, src.substring( i, j ), i, src );
116
+ i = j;
117
+ return {
118
+ 'value': v,
119
+ 'done': false
120
+ };
121
+ }
122
+
123
+ /**
124
+ * Returns an iterator protocol-compliant object containing the next iterated value.
125
+ *
126
+ * @private
127
+ * @returns {Object} iterator protocol-compliant object
128
+ */
129
+ function next2() {
130
+ var v;
131
+ var j;
132
+ if ( FLG ) {
133
+ return {
134
+ 'done': true
135
+ };
136
+ }
137
+ j = nextGraphemeClusterBreak( src, i );
138
+ if ( j === -1 ) {
139
+ FLG = true;
140
+ if ( src.length ) {
141
+ return {
142
+ 'value': src.substring( i ),
143
+ 'done': false
144
+ };
145
+ }
146
+ return {
147
+ 'done': true
148
+ };
149
+ }
150
+ v = src.substring( i, j );
151
+ i = j;
152
+ return {
153
+ 'value': v,
154
+ 'done': false
155
+ };
156
+ }
157
+
158
+ /**
159
+ * Finishes an iterator.
160
+ *
161
+ * @private
162
+ * @param {*} [value] - value to return
163
+ * @returns {Object} iterator protocol-compliant object
164
+ */
165
+ function end( value ) {
166
+ FLG = true;
167
+ if ( arguments.length ) {
168
+ return {
169
+ 'value': value,
170
+ 'done': true
171
+ };
172
+ }
173
+ return {
174
+ 'done': true
175
+ };
176
+ }
177
+
178
+ /**
179
+ * Returns a new iterator.
180
+ *
181
+ * @private
182
+ * @returns {Iterator} iterator
183
+ */
184
+ function factory() {
185
+ if ( fcn ) {
186
+ return graphemeClusters2iterator( src, fcn, thisArg );
187
+ }
188
+ return graphemeClusters2iterator( src );
189
+ }
190
+ }
191
+
192
+
193
+ // EXPORTS //
194
+
195
+ module.exports = graphemeClusters2iterator;
package/package.json ADDED
@@ -0,0 +1,99 @@
1
+ {
2
+ "name": "@stdlib/string-to-grapheme-cluster-iterator",
3
+ "version": "0.0.1",
4
+ "description": "Create an iterator which iterates grapheme clusters.",
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
+ "benchmark": "./benchmark",
19
+ "doc": "./docs",
20
+ "example": "./examples",
21
+ "lib": "./lib",
22
+ "test": "./test"
23
+ },
24
+ "types": "./docs/types",
25
+ "scripts": {
26
+ "test": "make test",
27
+ "test-cov": "make test-cov",
28
+ "examples": "make examples",
29
+ "benchmark": "make benchmark"
30
+ },
31
+ "homepage": "https://stdlib.io",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git://github.com/stdlib-js/string-to-grapheme-cluster-iterator.git"
35
+ },
36
+ "bugs": {
37
+ "url": "https://github.com/stdlib-js/stdlib/issues"
38
+ },
39
+ "dependencies": {
40
+ "@stdlib/assert-is-function": "^0.0.x",
41
+ "@stdlib/assert-is-string": "^0.0.x",
42
+ "@stdlib/string-format": "^0.0.x",
43
+ "@stdlib/string-next-grapheme-cluster-break": "^0.0.x",
44
+ "@stdlib/symbol-iterator": "^0.0.x",
45
+ "@stdlib/types": "^0.0.x",
46
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x"
47
+ },
48
+ "devDependencies": {
49
+ "@stdlib/assert-is-iterator-like": "^0.0.x",
50
+ "@stdlib/bench": "^0.0.x",
51
+ "@stdlib/string-num-grapheme-clusters": "^0.0.x",
52
+ "@stdlib/utils-noop": "^0.0.x",
53
+ "proxyquire": "^2.0.0",
54
+ "tape": "git+https://github.com/kgryte/tape.git#fix/globby",
55
+ "istanbul": "^0.4.1",
56
+ "tap-min": "git+https://github.com/Planeshifter/tap-min.git"
57
+ },
58
+ "engines": {
59
+ "node": ">=0.10.0",
60
+ "npm": ">2.7.0"
61
+ },
62
+ "os": [
63
+ "aix",
64
+ "darwin",
65
+ "freebsd",
66
+ "linux",
67
+ "macos",
68
+ "openbsd",
69
+ "sunos",
70
+ "win32",
71
+ "windows"
72
+ ],
73
+ "keywords": [
74
+ "stdlib",
75
+ "stdstring",
76
+ "utilities",
77
+ "utility",
78
+ "utils",
79
+ "util",
80
+ "string",
81
+ "str",
82
+ "unicode",
83
+ "grapheme",
84
+ "segmentation",
85
+ "surrogate",
86
+ "astral",
87
+ "emojis",
88
+ "iterator",
89
+ "iterate",
90
+ "iteration",
91
+ "iter",
92
+ "to",
93
+ "convert"
94
+ ],
95
+ "funding": {
96
+ "type": "patreon",
97
+ "url": "https://www.patreon.com/athan"
98
+ }
99
+ }