jupyter-ijavascript-utils 1.35.0 → 1.37.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/DOCS.md +34 -0
- package/README.md +34 -0
- package/package.json +1 -1
- package/src/format.js +86 -3
- package/src/random.js +6 -3
package/DOCS.md
CHANGED
|
@@ -75,6 +75,7 @@ Give it a try here:
|
|
|
75
75
|
|
|
76
76
|
## What's New
|
|
77
77
|
|
|
78
|
+
* 1.36 - {@link module:format.replaceStrings|format.replaceStrings} to allow for replacement dictionaries and tuplets
|
|
78
79
|
* 1.35 - {@link module:object.extractObjectProperties|extractObjectProperties} / {@link module:object.extractObjectProperty|extractObjectProperty} - to do horizontal transposes on objects
|
|
79
80
|
* 1.34 - {@link module:format.mapArrayDomain|format.mapArrayDomain} and add notes in the header of {@link module:random|random} on using non-uniform distributions.
|
|
80
81
|
* 1.33 - Object.augmentInherit and Object.union
|
|
@@ -369,3 +370,36 @@ Depends on:
|
|
|
369
370
|
* [n-riesco/ijavascript jupyter kernel](https://github.com/n-riesco/ijavascript#installation)
|
|
370
371
|
|
|
371
372
|
See the [How to Use]{@tutorial howToUse} section for more.
|
|
373
|
+
|
|
374
|
+
## ESM Modules + D3
|
|
375
|
+
|
|
376
|
+
Note that we strongly recommend using this with other modules like D3 - that only support ESM modules now.
|
|
377
|
+
|
|
378
|
+
There is a [known issue #210](https://github.com/n-riesco/ijavascript/issues/210) in the iJavaScript kernel.
|
|
379
|
+
|
|
380
|
+
So if you try to import libraries like d3 [and get comments like this](https://github.com/n-riesco/ijavascript/issues/279)
|
|
381
|
+
|
|
382
|
+
```
|
|
383
|
+
$ node -e "import defaultExport from './test.mjs'"
|
|
384
|
+
[eval]:1
|
|
385
|
+
import defaultExport from './test.mjs'
|
|
386
|
+
^^^^^^
|
|
387
|
+
|
|
388
|
+
SyntaxError: Cannot use import statement outside a module
|
|
389
|
+
at new Script (vm.js:88:7)
|
|
390
|
+
at createScript (vm.js:263:10)
|
|
391
|
+
at Object.runInThisContext (vm.js:311:10)
|
|
392
|
+
at Object.<anonymous> ([eval]-wrapper:10:26)
|
|
393
|
+
at Module._compile (internal/modules/cjs/loader.js:1151:30)
|
|
394
|
+
at evalScript (internal/process/execution.js:94:25)
|
|
395
|
+
at internal/main/eval_string.js:23:3
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
### Use [esm-hook](https://github.com/alex-kinokon/esm-hook) as a workaround for now.
|
|
399
|
+
|
|
400
|
+
```
|
|
401
|
+
require("esm-hook"); // must come before requiring esm modules
|
|
402
|
+
d3 = require('d3'); // import esm modules
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
More is found on the documentation for [issue #210](require("esm-hook"))
|
package/README.md
CHANGED
|
@@ -55,6 +55,7 @@ This is not intended to be the only way to accomplish many of these tasks, and a
|
|
|
55
55
|
|
|
56
56
|
# What's New
|
|
57
57
|
|
|
58
|
+
* 1.36 - replaceStrings to allow for replacement dictionaries and tuplets
|
|
58
59
|
* 1.35 - object.extractObjectProperties / object.extractObjectProperty - to do horizontal transposes on objects
|
|
59
60
|
* 1.34 - format.mapArrayDomain and add notes in to random on using non-uniform distributions.
|
|
60
61
|
* 1.33 - Object.augmentInherit and Object.union
|
|
@@ -98,6 +99,39 @@ found under the [docResources/notebooks](https://github.com/paulroth3d/jupyter-i
|
|
|
98
99
|
simply create a package in the folder you will run the `jupyter lab` command
|
|
99
100
|
- such as the sample one under [docResources/notebooks/package.json](https://github.com/paulroth3d/jupyter-ijavascript-utils/blob/main/docResources/notebooks/package.json))
|
|
100
101
|
|
|
102
|
+
## ESM Modules + D3
|
|
103
|
+
|
|
104
|
+
Note that we strongly recommend using this with other modules like D3 - that only support ESM modules now.
|
|
105
|
+
|
|
106
|
+
There is a [known issue #210](https://github.com/n-riesco/ijavascript/issues/210) in the iJavaScript kernel.
|
|
107
|
+
|
|
108
|
+
So if you try to import libraries like d3 [and get comments like this](https://github.com/n-riesco/ijavascript/issues/279)
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
$ node -e "import defaultExport from './test.mjs'"
|
|
112
|
+
[eval]:1
|
|
113
|
+
import defaultExport from './test.mjs'
|
|
114
|
+
^^^^^^
|
|
115
|
+
|
|
116
|
+
SyntaxError: Cannot use import statement outside a module
|
|
117
|
+
at new Script (vm.js:88:7)
|
|
118
|
+
at createScript (vm.js:263:10)
|
|
119
|
+
at Object.runInThisContext (vm.js:311:10)
|
|
120
|
+
at Object.<anonymous> ([eval]-wrapper:10:26)
|
|
121
|
+
at Module._compile (internal/modules/cjs/loader.js:1151:30)
|
|
122
|
+
at evalScript (internal/process/execution.js:94:25)
|
|
123
|
+
at internal/main/eval_string.js:23:3
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Use [esm-hook](https://github.com/alex-kinokon/esm-hook) as a workaround for now.
|
|
127
|
+
|
|
128
|
+
```
|
|
129
|
+
require("esm-hook"); // must come before requiring esm modules
|
|
130
|
+
d3 = require('d3'); // import esm modules
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
More is found on the documentation for [issue #210](require("esm-hook"))
|
|
134
|
+
|
|
101
135
|
## Google Collab
|
|
102
136
|
|
|
103
137
|
You can very easily use iJavaScript and the jupyter-ijavascript-utils within Google Collab.
|
package/package.json
CHANGED
package/src/format.js
CHANGED
|
@@ -397,6 +397,7 @@ module.exports.mapDomain = function mapDomain(val, [domainMin, domainMax], [rang
|
|
|
397
397
|
* ```
|
|
398
398
|
* require('esm-hook');
|
|
399
399
|
* d3 = require('d3');
|
|
400
|
+
* utils = require('jupyter-ijavascript-utils');
|
|
400
401
|
*
|
|
401
402
|
* //-- create a number generator using Normal / Gaussian distribution
|
|
402
403
|
* randomGenerator = d3.randomNormal(
|
|
@@ -409,12 +410,14 @@ module.exports.mapDomain = function mapDomain(val, [domainMin, domainMax], [rang
|
|
|
409
410
|
*
|
|
410
411
|
* randomDataset = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
|
|
411
412
|
*
|
|
413
|
+
* numPicks = 3; // increase to a larger number (ex: 1000) to better see distributions
|
|
414
|
+
*
|
|
412
415
|
* //-- create an array of 3 items, each with the results from randomGenerator
|
|
413
|
-
* results = utils.array.size(
|
|
416
|
+
* results = utils.array.size(numPicks, () => randomGenerator());
|
|
414
417
|
* // [ 0.6235937672428706, 0.4991359903898883, 0.4279365561645624 ]
|
|
415
418
|
*
|
|
416
419
|
* //-- map those values to the randomDataset
|
|
417
|
-
* results.map(val => ({ pick: utils.format.mapArrayDomain(val, randomDataset) }));
|
|
420
|
+
* resultPicks = results.map(val => ({ pick: utils.format.mapArrayDomain(val, randomDataset) }));
|
|
418
421
|
* // [ { pick: 'g' }, { pick: 'e' }, { pick: 'e' } ]
|
|
419
422
|
*
|
|
420
423
|
* //-- group them by the pick field
|
|
@@ -430,7 +433,7 @@ module.exports.mapDomain = function mapDomain(val, [domainMin, domainMax], [rang
|
|
|
430
433
|
* .title('Distribution')
|
|
431
434
|
* .data(groupedResults)
|
|
432
435
|
* .encode(
|
|
433
|
-
* vl.x().fieldN('
|
|
436
|
+
* vl.x().fieldN('pick'),
|
|
434
437
|
* vl.y().fieldQ('count').scale({type: 'log'})
|
|
435
438
|
* );
|
|
436
439
|
* });
|
|
@@ -1184,3 +1187,83 @@ module.exports.lineCount = function lineCount(str, newlineCharacter = '\n') {
|
|
|
1184
1187
|
const match = str.match(rex);
|
|
1185
1188
|
return match ? match.length + 1 : 1;
|
|
1186
1189
|
};
|
|
1190
|
+
|
|
1191
|
+
/**
|
|
1192
|
+
* Performs a set of replacement against a string or list of strings.
|
|
1193
|
+
*
|
|
1194
|
+
* The string replacements can either be [[search,replace],[search,replace]]
|
|
1195
|
+
* or a map of Map([[search, replace], [search, replace]])
|
|
1196
|
+
*
|
|
1197
|
+
* This is meant to provide a way to apply consistent string cleanups across projects.
|
|
1198
|
+
*
|
|
1199
|
+
* example:
|
|
1200
|
+
*
|
|
1201
|
+
* ```
|
|
1202
|
+
* targetStrings = [
|
|
1203
|
+
* 'jack and jill went up the hill',
|
|
1204
|
+
* 'to fetch the pail of water',
|
|
1205
|
+
* 'jack fell down and broke his crown',
|
|
1206
|
+
* 'and jill came tumbling after'
|
|
1207
|
+
* ];
|
|
1208
|
+
*
|
|
1209
|
+
* //-- include an array of strings to all remove out
|
|
1210
|
+
* utils.format.replaceStrings(targetStrings, ['down ', ' of water']);
|
|
1211
|
+
* // [ 'jack and jill went up the hill',
|
|
1212
|
+
* // 'to fetch the pail',
|
|
1213
|
+
* // 'jack fell and broke his crown',
|
|
1214
|
+
* // 'and jill came tumbling after' ]
|
|
1215
|
+
*
|
|
1216
|
+
* //-- or use tuplets of [find, replace] with regular expressions
|
|
1217
|
+
* replaceValues = [['jack', 'john'], [/\s+jill/i, ' ringo'], ' down'];
|
|
1218
|
+
* utils.format.replaceStrings(targetStrings, replaceValues);
|
|
1219
|
+
* expected = [
|
|
1220
|
+
* 'john and ringo went up the hill',
|
|
1221
|
+
* 'to fetch the pail of water',
|
|
1222
|
+
* 'john fell and broke his crown',
|
|
1223
|
+
* 'and ringo came tumbling after'
|
|
1224
|
+
* ];
|
|
1225
|
+
*
|
|
1226
|
+
* //-- a map will do the same, but will not support regular expressions for keys
|
|
1227
|
+
* replaceValues = new Map();
|
|
1228
|
+
* replaceValues.set('jack', 'john');
|
|
1229
|
+
* replaceValues.set('jill', 'ringo');
|
|
1230
|
+
* utils.format.replaceStrings(targetStrings, replaceValues);
|
|
1231
|
+
* expected = [
|
|
1232
|
+
* 'john and ringo went up the hill',
|
|
1233
|
+
* 'to fetch the pail of water',
|
|
1234
|
+
* 'john fell down and broke his crown',
|
|
1235
|
+
* 'and ringo came tumbling after'
|
|
1236
|
+
* ];
|
|
1237
|
+
* ```
|
|
1238
|
+
*
|
|
1239
|
+
* @param {string|string[]} targetStr - the string to search for with the tuplets
|
|
1240
|
+
* @param {Array|Map<string|RegExp,string>} stringTupletsOrMap - [[search, replace]] or Map<String|RegExp,String>
|
|
1241
|
+
* @returns {string[]} - the resulting list of strings
|
|
1242
|
+
*/
|
|
1243
|
+
module.exports.replaceStrings = function replaceStrings(targetStr, stringTupletsOrMap) {
|
|
1244
|
+
const cleanStrings = !targetStr ? [] : Array.isArray(targetStr) ? targetStr : [targetStr];
|
|
1245
|
+
// const signature = 'replaceStrings(targetStrings, stringTupletsOrMap)';
|
|
1246
|
+
const replacementEntries = [];
|
|
1247
|
+
|
|
1248
|
+
if (Array.isArray(stringTupletsOrMap)) {
|
|
1249
|
+
stringTupletsOrMap.forEach((possibleReplacement) => {
|
|
1250
|
+
if (typeof possibleReplacement === 'string') {
|
|
1251
|
+
replacementEntries.push([possibleReplacement, '']);
|
|
1252
|
+
} else if (Array.isArray(possibleReplacement)) {
|
|
1253
|
+
const [replaceSearch, replaceWith] = possibleReplacement;
|
|
1254
|
+
replacementEntries.push([replaceSearch, replaceWith || '']);
|
|
1255
|
+
}
|
|
1256
|
+
});
|
|
1257
|
+
} else if (stringTupletsOrMap instanceof Map) {
|
|
1258
|
+
[...stringTupletsOrMap.entries()]
|
|
1259
|
+
.forEach(([replaceSearch, replaceWith]) => {
|
|
1260
|
+
replacementEntries.push([replaceSearch, replaceWith || '']);
|
|
1261
|
+
});
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
return cleanStrings.map((stringToClean) => !stringToClean
|
|
1265
|
+
? stringToClean
|
|
1266
|
+
: replacementEntries.reduce((result, [replaceSearch, replaceWith]) => !result
|
|
1267
|
+
? result
|
|
1268
|
+
: result.replace(replaceSearch, replaceWith), stringToClean));
|
|
1269
|
+
};
|
package/src/random.js
CHANGED
|
@@ -55,6 +55,7 @@ const SimplexModule = require('./random_simplex');
|
|
|
55
55
|
* ```
|
|
56
56
|
* require('esm-hook');
|
|
57
57
|
* d3 = require('d3');
|
|
58
|
+
* utils = require('jupyter-ijavascript-utils');
|
|
58
59
|
*
|
|
59
60
|
* //-- create a number generator using Normal / Gaussian distribution
|
|
60
61
|
* randomGenerator = d3.randomNormal(
|
|
@@ -67,12 +68,14 @@ const SimplexModule = require('./random_simplex');
|
|
|
67
68
|
*
|
|
68
69
|
* randomDataset = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
|
|
69
70
|
*
|
|
71
|
+
* numPicks = 3; // increase to a larger number (ex: 1000) to better see distributions
|
|
72
|
+
*
|
|
70
73
|
* //-- create an array of 3 items, each with the results from randomGenerator
|
|
71
|
-
* results = utils.array.size(
|
|
74
|
+
* results = utils.array.size(numPicks, () => randomGenerator());
|
|
72
75
|
* // [ 0.6235937672428706, 0.4991359903898883, 0.4279365561645624 ]
|
|
73
76
|
*
|
|
74
77
|
* //-- map those values to the randomDataset
|
|
75
|
-
* results.map(val => ({ pick: utils.format.mapArrayDomain(val, randomDataset) }));
|
|
78
|
+
* resultPicks = results.map(val => ({ pick: utils.format.mapArrayDomain(val, randomDataset) }));
|
|
76
79
|
* // [ { pick: 'g' }, { pick: 'e' }, { pick: 'e' } ]
|
|
77
80
|
*
|
|
78
81
|
* //-- group them by the pick field
|
|
@@ -88,7 +91,7 @@ const SimplexModule = require('./random_simplex');
|
|
|
88
91
|
* .title('Distribution')
|
|
89
92
|
* .data(groupedResults)
|
|
90
93
|
* .encode(
|
|
91
|
-
* vl.x().fieldN('
|
|
94
|
+
* vl.x().fieldN('pick'),
|
|
92
95
|
* vl.y().fieldQ('count').scale({type: 'log'})
|
|
93
96
|
* );
|
|
94
97
|
* });
|