Skip to content

Commit f5d2c96

Browse files
steff456kgryte
andauthored
Add support for iteratively inserting array elements into a formatted message and printing the result (stdlib-js#526)
* Add README * Update readme * Add implementation * Add package.json, examples, benchmarks and tests * Update test descriptions * Add docs * Add review changes * Add trailing newline * Lowercase type * Update description * Move package to console namespace and rename to log-each * Apply suggestions from code review * Fix require path Co-authored-by: Athan Reines <kgryte@gmail.com>
1 parent d38cd90 commit f5d2c96

12 files changed

Lines changed: 1131 additions & 0 deletions

File tree

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
# logEach
22+
23+
> Insert array element values into a format string and print the result.
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 logEach = require( '@stdlib/console/log-each' );
41+
```
42+
43+
#### logEach( str\[, ...args] )
44+
45+
Inserts array element values into a format string and prints the result.
46+
47+
```javascript
48+
var x = [ 1, 2, 3 ];
49+
var y = [ 4, 5, 6 ];
50+
51+
logEach( '%d < %d ', x, y );
52+
// e.g., => '1 < 4\n2 < 5\n3 < 6\n'
53+
```
54+
55+
If an interpolated argument is not a collection, the argument is broadcasted for each iteration.
56+
57+
```javascript
58+
var x = [ 1, 2, 3 ];
59+
var y = 4;
60+
61+
logEach( '%d < %d', x, y );
62+
// e.g., => '1 < 4\n2 < 4\n3 < 4\n'
63+
```
64+
65+
</section>
66+
67+
<!-- /.usage -->
68+
69+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
70+
71+
<section class="notes">
72+
73+
## Notes
74+
75+
- If the function is provided collections of unequal lengths, the function throws an error.
76+
77+
</section>
78+
79+
<!-- /.notes -->
80+
81+
<!-- Package usage examples. -->
82+
83+
<section class="examples">
84+
85+
## Examples
86+
87+
<!-- eslint no-undef: "error" -->
88+
89+
```javascript
90+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
91+
var naryFunction = require( '@stdlib/utils/nary-function' );
92+
var filledBy = require( '@stdlib/array/filled-by' );
93+
var map = require( '@stdlib/utils/map' );
94+
var abs = require( '@stdlib/math/base/special/abs' );
95+
var logEach = require( '@stdlib/console/log-each' );
96+
97+
var rand = discreteUniform( -50, 50 );
98+
var x = filledBy( 10, 'float64', rand );
99+
100+
var y = map( x, naryFunction( abs, 1 ) );
101+
logEach( 'abs(%d) = %d', x, y );
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- 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. -->
109+
110+
<section class="references">
111+
112+
</section>
113+
114+
<!-- /.references -->
115+
116+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
117+
118+
<section class="related">
119+
120+
</section>
121+
122+
<!-- /.related -->
123+
124+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
125+
126+
<section class="links">
127+
128+
</section>
129+
130+
<!-- /.links -->
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 proxyquire = require( 'proxyquire' );
24+
var bench = require( '@stdlib/bench' );
25+
var pkg = require( './../package.json' ).name;
26+
27+
28+
// MAIN //
29+
30+
bench( pkg+'::no_collections', function benchmark( b ) {
31+
var logEach;
32+
var i;
33+
34+
logEach = proxyquire( './../lib/main.js', {
35+
'./logger.js': logger
36+
});
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
logEach( '%d, %d', i, i + 1 );
41+
}
42+
b.toc();
43+
b.pass( 'benchmark finished' );
44+
b.end();
45+
46+
function logger( str ) {
47+
if ( typeof str !== 'string' ) {
48+
b.fail( 'should return a string' );
49+
}
50+
}
51+
});
52+
53+
bench( pkg+'::collections:len=1', function benchmark( b ) {
54+
var logEach;
55+
var i;
56+
57+
logEach = proxyquire( './../lib/main.js', {
58+
'./logger.js': logger
59+
});
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
logEach( '%d, %d', [ i ], [ i + 1 ] );
64+
}
65+
b.toc();
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
69+
function logger( str ) {
70+
if ( typeof str !== 'string' ) {
71+
b.fail( 'should return a string' );
72+
}
73+
}
74+
});
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 proxyquire = require( 'proxyquire' );
24+
var bench = require( '@stdlib/bench' );
25+
var zeros = require( '@stdlib/array/zeros' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {PositiveInteger} len - array length
37+
* @returns {Function} benchmark function
38+
*/
39+
function createBenchmark( len ) {
40+
var x = zeros( len, 'float64' );
41+
return benchmark;
42+
43+
/**
44+
* Benchmark function.
45+
*
46+
* @private
47+
* @param {Benchmark} b - benchmark instance
48+
*/
49+
function benchmark( b ) {
50+
var logEach;
51+
var i;
52+
53+
logEach = proxyquire( './../lib/main.js', {
54+
'./logger.js': logger
55+
});
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
logEach( '%d, %d', x, x );
60+
}
61+
b.toc();
62+
b.pass( 'benchmark finished' );
63+
b.end();
64+
65+
function logger( str ) {
66+
if ( typeof str !== 'string' ) {
67+
b.fail( 'should return a string' );
68+
}
69+
}
70+
}
71+
}
72+
73+
74+
// MAIN //
75+
76+
/**
77+
* Main execution sequence.
78+
*
79+
* @private
80+
*/
81+
function main() {
82+
var len;
83+
var min;
84+
var max;
85+
var f;
86+
var i;
87+
88+
min = 1; // 10^min
89+
max = 6; // 10^max
90+
91+
for ( i = min; i <= max; i++ ) {
92+
len = pow( 10, i );
93+
f = createBenchmark( len );
94+
bench( pkg+'::collections:len='+len, f );
95+
}
96+
}
97+
98+
main();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
{{alias}}( str[, ...args] )
3+
Inserts array element values into a format string and prints the result.
4+
5+
Parameters
6+
----------
7+
str: String
8+
Format string.
9+
10+
args: ...any (optional)
11+
Collections or values.
12+
13+
Examples
14+
--------
15+
> var x = [ 1, 2, 3 ];
16+
> var y = [ 4, 5, 6 ];
17+
> {{alias}}( '%d < %d ', x, y );
18+
19+
See Also
20+
--------
21+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
* Inserts array element values into a format string and prints the result.
23+
*
24+
* @param str - format string
25+
* @param args - collections or values
26+
* @throws {RangeError} provided collections must have the same length
27+
*
28+
* @example
29+
* var logEach = require( `@stdlib/console/log-each` );
30+
*
31+
* var x = [ 1, 2, 3 ];
32+
* var y = [ 4, 5, 6 ];
33+
*
34+
* logEach( '%d < %d ', x, y );
35+
* // e.g., => '1 < 4\n2 < 5\n3 < 6\n'
36+
*/
37+
declare function logEach( str: string, ...args: any ): void;
38+
39+
40+
// EXPORTS //
41+
42+
export = logEach;

0 commit comments

Comments
 (0)