jupyter-ijavascript-utils 1.35.0 → 1.36.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 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
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jupyter-ijavascript-utils",
3
- "version": "1.35.0",
3
+ "version": "1.36.0",
4
4
  "description": "Utilities for working with iJavaScript - a Jupyter Kernel",
5
5
  "homepage": "https://jupyter-ijavascript-utils.onrender.com/",
6
6
  "license": "MIT",
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(3, () => randomGenerator());
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('value'),
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(3, () => randomGenerator());
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('value'),
94
+ * vl.x().fieldN('pick'),
92
95
  * vl.y().fieldQ('count').scale({type: 'log'})
93
96
  * );
94
97
  * });