README.md (3406B)
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 | precision | precision (integer or `'*'`) | 68 | mapping | positional mapping from format specifier to argument index | 69 70 </section> 71 72 <!-- /.usage --> 73 74 <section class="examples"> 75 76 ## Examples 77 78 <!-- eslint no-undef: "error" --> 79 80 ```javascript 81 var formatTokenize = require( '@stdlib/string/base/format-tokenize' ); 82 var PI = require( '@stdlib/constants/float64/pi' ); 83 var formatInterpolate = require( '@stdlib/string/base/format-interpolate' ); 84 85 var tokens = formatTokenize( 'Hello %s!' ); 86 var out = formatInterpolate( tokens, 'World' ); 87 // returns 'Hello World!' 88 89 tokens = formatTokenize( 'Pi: ~%.2f' ); 90 out = formatInterpolate( tokens, PI ); 91 // returns 'Pi: ~3.14' 92 93 tokens = formatTokenize( 'Index: %d, Value: %s' ); 94 out = formatInterpolate( tokens, 0, 'foo' ); 95 // returns 'Index: 0, Value: foo' 96 ``` 97 98 </section> 99 100 <!-- /.examples --> 101 102 <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> 103 104 <section class="related"> 105 106 </section> 107 108 <!-- /.related --> 109 110 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 111 112 <section class="links"> 113 114 </section> 115 116 <!-- /.links -->