@stdlib/console-log-each-map 0.1.0
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 +177 -0
- package/NOTICE +1 -0
- package/README.md +280 -0
- package/SECURITY.md +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +7 -0
- package/docs/types/index.d.ts +599 -0
- package/lib/index.js +67 -0
- package/lib/main.js +188 -0
- package/package.json +79 -0
package/lib/main.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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 isString = require( '@stdlib/assert-is-string' ).isPrimitive;
|
|
24
|
+
var isFunction = require( '@stdlib/assert-is-function' );
|
|
25
|
+
var isCollection = require( '@stdlib/assert-is-collection' );
|
|
26
|
+
var resolveGetter = require( '@stdlib/array-base-resolve-getter' );
|
|
27
|
+
var nulls = require( '@stdlib/array-base-nulls' );
|
|
28
|
+
var zeros = require( '@stdlib/array-base-zeros' );
|
|
29
|
+
var format = require( '@stdlib/string-format' );
|
|
30
|
+
var logger = require( '@stdlib/console-log' );
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
// MAIN //
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Inserts array element values and the result of a callback function into a format string and prints the result.
|
|
37
|
+
*
|
|
38
|
+
* @param {string} str - format string
|
|
39
|
+
* @param {...(Collection|*)} [args] - collections or values
|
|
40
|
+
* @param {Function} clbk - callback function
|
|
41
|
+
* @param {*} [thisArg] - callback execution context
|
|
42
|
+
* @throws {TypeError} first argument must be a string
|
|
43
|
+
* @throws {TypeError} callback argument must be a function
|
|
44
|
+
* @throws {RangeError} provided collections must have the same length
|
|
45
|
+
* @returns {void}
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* function add( x, y ) {
|
|
49
|
+
* return x + y;
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* var x = [ 1, 2, 3 ];
|
|
53
|
+
* var y = [ 4, 5, 6 ];
|
|
54
|
+
*
|
|
55
|
+
* logEachMap( '%d + %d = %d', x, y, add );
|
|
56
|
+
* // e.g., => '1 + 4 = 5\n2 + 5 = 7\n3 + 6 = 9\n'
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* function multiply( x, y ) {
|
|
60
|
+
* return x * y;
|
|
61
|
+
* }
|
|
62
|
+
*
|
|
63
|
+
* var x = [ 0.5, 1.0, 1.5 ];
|
|
64
|
+
* var y = [ 0.5, 0.75, 1.0 ];
|
|
65
|
+
*
|
|
66
|
+
* logEachMap( '%0.2f * %0.2f = %0.2f', x, y, multiply );
|
|
67
|
+
* // e.g., => '0.50 * 0.50 = 0.25\n1.00 * 0.75 = 0.75\n1.50 * 1.00 = 1.50\n'
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* function append( x, y ) {
|
|
71
|
+
* return x + y;
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* var x = [ 'foo', 'bar' ];
|
|
75
|
+
* var y = [ 'baz', 'beep' ];
|
|
76
|
+
*
|
|
77
|
+
* logEachMap( '%s+%s = %s', x, y, append );
|
|
78
|
+
* // e.g., => 'foo+baz = foobaz\nbar+beep = barbeep\n'
|
|
79
|
+
*/
|
|
80
|
+
function logEachMap( str ) {
|
|
81
|
+
var strides;
|
|
82
|
+
var offsets;
|
|
83
|
+
var getters;
|
|
84
|
+
var thisArg;
|
|
85
|
+
var cbArgs;
|
|
86
|
+
var values;
|
|
87
|
+
var nargs;
|
|
88
|
+
var args;
|
|
89
|
+
var clbk;
|
|
90
|
+
var len;
|
|
91
|
+
var v;
|
|
92
|
+
var s;
|
|
93
|
+
var i;
|
|
94
|
+
var j;
|
|
95
|
+
|
|
96
|
+
nargs = arguments.length;
|
|
97
|
+
if ( !isString( str ) ) {
|
|
98
|
+
throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );
|
|
99
|
+
}
|
|
100
|
+
nargs -= 1;
|
|
101
|
+
if ( isFunction( arguments[ nargs ] ) ) {
|
|
102
|
+
clbk = arguments[ nargs ];
|
|
103
|
+
nargs -= 1;
|
|
104
|
+
} else {
|
|
105
|
+
clbk = arguments[ nargs-1 ];
|
|
106
|
+
if ( !isFunction( clbk ) ) {
|
|
107
|
+
throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', clbk ) );
|
|
108
|
+
}
|
|
109
|
+
thisArg = arguments[ nargs ];
|
|
110
|
+
nargs -= 2;
|
|
111
|
+
}
|
|
112
|
+
getters = [];
|
|
113
|
+
strides = [];
|
|
114
|
+
args = [];
|
|
115
|
+
|
|
116
|
+
// Find the first argument which is a collection...
|
|
117
|
+
for ( i = 1; i < nargs+1; i++ ) {
|
|
118
|
+
v = arguments[ i ];
|
|
119
|
+
if ( isCollection( v ) ) {
|
|
120
|
+
getters.push( resolveGetter( v ) );
|
|
121
|
+
args.push( v );
|
|
122
|
+
strides.push( 1 );
|
|
123
|
+
len = v.length;
|
|
124
|
+
i += 1;
|
|
125
|
+
break;
|
|
126
|
+
} else {
|
|
127
|
+
v = [ v ];
|
|
128
|
+
getters.push( resolveGetter( v ) );
|
|
129
|
+
args.push( v );
|
|
130
|
+
strides.push( 0 );
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// If weren't provided a collection argument, all arguments are "broadcasted"...
|
|
134
|
+
if ( len === void 0 ) {
|
|
135
|
+
len = 1;
|
|
136
|
+
}
|
|
137
|
+
// For the remaining arguments, resolve each argument to a collection...
|
|
138
|
+
for ( ; i < nargs+1; i++ ) {
|
|
139
|
+
v = arguments[ i ];
|
|
140
|
+
if ( isCollection( v ) ) {
|
|
141
|
+
if ( v.length !== len ) {
|
|
142
|
+
throw new RangeError( 'invalid argument. Provided collections must have the same length.' );
|
|
143
|
+
}
|
|
144
|
+
s = 1;
|
|
145
|
+
} else {
|
|
146
|
+
v = [ v ];
|
|
147
|
+
s = 0;
|
|
148
|
+
}
|
|
149
|
+
getters.push( resolveGetter( v ) );
|
|
150
|
+
args.push( v );
|
|
151
|
+
strides.push( s );
|
|
152
|
+
}
|
|
153
|
+
// Initialize an array containing values for generating an interpolated format string:
|
|
154
|
+
values = nulls( nargs+2 ); // [ str, v0, v1, ..., vN, result ]
|
|
155
|
+
values[ 0 ] = str;
|
|
156
|
+
|
|
157
|
+
// Initialize an array containing index offsets, which are "pointers" to the current set of array elements when calling the provided callback function:
|
|
158
|
+
offsets = zeros( nargs ); // [ o0, o1, ..., oN ]
|
|
159
|
+
|
|
160
|
+
// Initialize an array containing arguments to be provided to the callback function:
|
|
161
|
+
cbArgs = nulls( nargs+2 ); // [ v0, v1, ..., vN, index, arrays ]
|
|
162
|
+
|
|
163
|
+
// The last argument provided to the callback function should be the list of input arrays/broadcasted values:
|
|
164
|
+
cbArgs[ nargs+1 ] = args;
|
|
165
|
+
|
|
166
|
+
// Print an interpolated format string for each set of broadcasted array values...
|
|
167
|
+
for ( i = 0; i < len; i++ ) {
|
|
168
|
+
// Resolve the set of broadcasted array values...
|
|
169
|
+
for ( j = 0; j < nargs; j++ ) {
|
|
170
|
+
cbArgs[ j ] = getters[ j ]( args[ j ], offsets[ j ] );
|
|
171
|
+
values[ j+1 ] = cbArgs[ j ];
|
|
172
|
+
offsets[ j ] += strides[ j ];
|
|
173
|
+
}
|
|
174
|
+
// The second-to-last callback argument should be the current array element index:
|
|
175
|
+
cbArgs[ nargs ] = i;
|
|
176
|
+
|
|
177
|
+
// Compute the result of passing the current set of array elements to the provided callback function:
|
|
178
|
+
values[ nargs+1 ] = clbk.apply( thisArg, cbArgs );
|
|
179
|
+
|
|
180
|
+
// Print an interpolated string:
|
|
181
|
+
logger( format.apply( null, values ) );
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
// EXPORTS //
|
|
187
|
+
|
|
188
|
+
module.exports = logEachMap;
|
package/package.json
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/console-log-each-map",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Insert array element values and the result of a callback function into a format string and print the result.",
|
|
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
|
+
"doc": "./docs",
|
|
19
|
+
"lib": "./lib",
|
|
20
|
+
"dist": "./dist"
|
|
21
|
+
},
|
|
22
|
+
"types": "./docs/types",
|
|
23
|
+
"scripts": {},
|
|
24
|
+
"homepage": "https://stdlib.io",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git://github.com/stdlib-js/console-log-each-map.git"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@stdlib/array-base-nulls": "github:stdlib-js/array-base-nulls#main",
|
|
34
|
+
"@stdlib/array-base-resolve-getter": "^0.2.2",
|
|
35
|
+
"@stdlib/array-base-zeros": "^0.2.2",
|
|
36
|
+
"@stdlib/assert-is-collection": "^0.2.2",
|
|
37
|
+
"@stdlib/assert-is-function": "^0.2.2",
|
|
38
|
+
"@stdlib/assert-is-string": "^0.2.2",
|
|
39
|
+
"@stdlib/console-log": "^0.2.2",
|
|
40
|
+
"@stdlib/string-format": "^0.2.2",
|
|
41
|
+
"@stdlib/error-tools-fmtprodmsg": "^0.2.2"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=0.10.0",
|
|
46
|
+
"npm": ">2.7.0"
|
|
47
|
+
},
|
|
48
|
+
"os": [
|
|
49
|
+
"aix",
|
|
50
|
+
"darwin",
|
|
51
|
+
"freebsd",
|
|
52
|
+
"linux",
|
|
53
|
+
"macos",
|
|
54
|
+
"openbsd",
|
|
55
|
+
"sunos",
|
|
56
|
+
"win32",
|
|
57
|
+
"windows"
|
|
58
|
+
],
|
|
59
|
+
"keywords": [
|
|
60
|
+
"stdlib",
|
|
61
|
+
"console",
|
|
62
|
+
"log",
|
|
63
|
+
"logger",
|
|
64
|
+
"debug",
|
|
65
|
+
"debugging",
|
|
66
|
+
"print",
|
|
67
|
+
"map",
|
|
68
|
+
"each",
|
|
69
|
+
"foreach",
|
|
70
|
+
"array",
|
|
71
|
+
"element",
|
|
72
|
+
"format",
|
|
73
|
+
"string"
|
|
74
|
+
],
|
|
75
|
+
"funding": {
|
|
76
|
+
"type": "opencollective",
|
|
77
|
+
"url": "https://opencollective.com/stdlib"
|
|
78
|
+
}
|
|
79
|
+
}
|