Skip to content

Commit ee2eacb

Browse files
committed
Add package to tokenize strings with format identifiers
1 parent 8c4f857 commit ee2eacb

File tree

10 files changed

+818
-0
lines changed

10 files changed

+818
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
# formatTokenize
22+
23+
> Tokenize a string into an array of string parts and format identifier objects.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var formatTokenize = require( '@stdlib/string/base/format-tokenize' );
37+
```
38+
39+
#### formatTokenize( str )
40+
41+
Tokenizes a string into an array of string parts and format identifier objects.
42+
43+
```javascript
44+
var str = 'Hello, %s! My name is %s.';
45+
var out = formatTokenize( str );
46+
// returns [ 'Hello, ', {...}, '! My name is ', {...}, '.' ]
47+
```
48+
49+
The format identifier objects have the following properties:
50+
51+
| property | description |
52+
| --------- | --------------------------------------------------------------------------------------------------- |
53+
| specifier | format specifier (one of 's', 'c', 'd', 'i', 'u', 'b', 'o', 'x', 'X', 'e', 'E', 'f', 'F', 'g', 'G') |
54+
| flags | format flags (string with any of '0', ' ', '+', '-', '#') |
55+
| width | minimum field width (integer or `'*'`) |
56+
| hasPeriod | boolean indicating whether format identifier contains a period (`'.'`) |
57+
| precision | precision (integer or `'*'`) |
58+
| mapping | positional mapping from format specifier to argument index |
59+
60+
</section>
61+
62+
<!-- /.usage -->
63+
64+
<section class="examples">
65+
66+
## Examples
67+
68+
<!-- eslint no-undef: "error" -->
69+
70+
```javascript
71+
var formatTokenize = require( '@stdlib/string/base/format-tokenize' );
72+
73+
var out = formatTokenize( 'Hello %s!' );
74+
// returns [ 'Hello ', {...}, '!' ]
75+
76+
out = formatTokenize( 'Pi: ~%.2f' );
77+
// returns [ 'Pi: ~', {...} ]
78+
79+
out = formatTokenize( 'Multiple flags: %#+s' );
80+
// returns [ 'Foo ', {...} ]
81+
```
82+
83+
</section>
84+
85+
<!-- /.examples -->
86+
87+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
88+
89+
<section class="related">
90+
91+
</section>
92+
93+
<!-- /.related -->
94+
95+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
96+
97+
<section class="links">
98+
99+
</section>
100+
101+
<!-- /.links -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 bench = require( '@stdlib/bench' );
24+
var isArray = require( '@stdlib/assert/is-array' );
25+
var pkg = require( './../package.json' ).name;
26+
var formatTokenize = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var out;
34+
var i;
35+
36+
values = [
37+
'Hello %s!',
38+
'Pi: ~%.2f',
39+
'Multiple flags: %#+s',
40+
'foo bar baz %s',
41+
'foo bar baz %s %s',
42+
'foo bar baz %d %f %u'
43+
];
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
out = formatTokenize( values[ i%values.length ] );
48+
if ( !isArray( out ) ) {
49+
b.fail( 'should return an array' );
50+
}
51+
}
52+
b.toc();
53+
if ( !isArray( out ) ) {
54+
b.fail( 'should return an array' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
{{alias}}( str )
3+
Tokenize a string into an array of string parts and format identifier
4+
objects.
5+
6+
Parameters
7+
----------
8+
str: string
9+
Input string.
10+
11+
Returns
12+
-------
13+
out: Array
14+
Array of string parts and format identifier objects.
15+
16+
Examples
17+
--------
18+
> var out = {{alias}}( 'Hello %s!' )
19+
[ 'Hello ', {...}, '!' ]
20+
> out = {{alias}}( '%s %s %d' )
21+
[ {...}, ' ', {...}, ' ', {...}, ' ' ]
22+
> out = {{alias}}( 'Pi: %.2f' )
23+
[ 'Pi: ', {...} ]
24+
25+
See Also
26+
--------
27+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
/**
22+
* Format identifier object.
23+
*/
24+
interface FormatIdentifier {
25+
/**
26+
* Format specifier (one of 's', 'c', 'd', 'i', 'u', 'b', 'o', 'x', 'X', 'e', 'E', 'f', 'F', 'g', 'G').
27+
*/
28+
specifier: string;
29+
30+
/**
31+
* Flags.
32+
*/
33+
flags: string;
34+
35+
/**
36+
* Minimum field width (integer or `'*'`).
37+
*/
38+
width: string;
39+
40+
/**
41+
* Boolean indicating whether format identifier contains a period (`'.'`).
42+
*/
43+
hasPeriod: boolean;
44+
45+
/**
46+
* Precision (integer or `'*'`).
47+
*/
48+
precision: string;
49+
50+
/**
51+
* Positional mapping from format specifier to argument index.
52+
*/
53+
mapping: number;
54+
}
55+
56+
type StringOrToken = string | FormatIdentifier;
57+
58+
/**
59+
* Tokenizes a string into an array of string parts and format identifier objects.
60+
*
61+
* @param str - input string
62+
* @returns tokens
63+
*
64+
* @example
65+
* var tokens = formatTokenize( 'Hello %s!' );
66+
* // returns [ 'Hello ', {...}, '!' ]
67+
*/
68+
declare function formatTokenize( str: string ): Array<StringOrToken>;
69+
70+
71+
// EXPORTS //
72+
73+
export = formatTokenize;
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) 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 formatTokenize = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array of string parts and format identifier objects...
25+
{
26+
formatTokenize( 'Hello, %s!' ); // $ExpectType string
27+
}
28+
29+
// The function does not compile if provided an argument other than a string...
30+
{
31+
formatTokenize( true ); // $ExpectError
32+
formatTokenize( false ); // $ExpectError
33+
formatTokenize( null ); // $ExpectError
34+
formatTokenize( undefined ); // $ExpectError
35+
formatTokenize( 5 ); // $ExpectError
36+
formatTokenize( [] ); // $ExpectError
37+
formatTokenize( {} ); // $ExpectError
38+
formatTokenize( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The function does not compile if provided insufficient arguments...
42+
{
43+
formatTokenize(); // $ExpectError
44+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
var formatTokenize = require( './../lib' );
22+
23+
var out = formatTokenize( 'Hello %s!' );
24+
console.log( out );
25+
// => [ 'Hello ', {...}, '!' ]
26+
27+
out = formatTokenize( 'Pi: ~%.2f' );
28+
console.log( out );
29+
// => [ 'Pi: ~', {...} ]
30+
31+
out = formatTokenize( 'Multiple flags: %#+s' );
32+
console.log( out );
33+
// => [ 'Foo ', {...} ]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
* Tokenize a string into an array of string parts and format identifier objects.
23+
*
24+
* @module @stdlib/string/base/format-tokenize
25+
*
26+
* @example
27+
* var formatTokenize = require( '@stdlib/string/base/format-tokenize' );
28+
*
29+
* var str = 'Hello %s!';
30+
* var tokens = formatTokenize( str );
31+
* // returns [ 'Hello ', {...}, '!' ]
32+
*/
33+
34+
// MODULES //
35+
36+
var formatTokenize = require( './main.js' );
37+
38+
39+
// EXPORTS //
40+
41+
module.exports = formatTokenize;

0 commit comments

Comments
 (0)