Skip to content

Commit 8a39b32

Browse files
committed
Move interpolation logic to own package
1 parent aa54b59 commit 8a39b32

22 files changed

Lines changed: 2050 additions & 134 deletions

File tree

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

lib/node_modules/@stdlib/string/format/lib/format_double.js renamed to lib/node_modules/@stdlib/string/base/format-interpolate/lib/format_double.js

File renamed without changes.

lib/node_modules/@stdlib/string/format/lib/format_integer.js renamed to lib/node_modules/@stdlib/string/base/format-interpolate/lib/format_integer.js

File renamed without changes.

0 commit comments

Comments
 (0)