jupyter-ijavascript-utils 1.23.0 → 1.24.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.24 - format.stripHtmlTags, TableGenerator.offset, chain.chainFlatMap, chain.chainFilter
78
79
  * 1.23 - add format.parseNumber and TableGenerator.styleColumn, align group.separateByFields to vega-lite fold transform
79
80
  * 1.22 - make chain iJavaScript aware, but still able to work outside of Jupyter
80
81
  * 1.21 - include {@link module:chain|chain} - simple monoid
package/Dockerfile CHANGED
@@ -1,3 +1,3 @@
1
1
  # syntax=docker/dockerfile:1
2
2
 
3
- FROM darkbluestudios/jupyter-ijavascript-utils:binder_1.23.0
3
+ FROM darkbluestudios/jupyter-ijavascript-utils:binder_1.24.0
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.24 - format.stripHtmlTags, TableGenerator.offset, chain.chainFlatMap, chain.chainFilter
58
59
  * 1.23 - add format.parseNumber and TableGenerator.styleColumn, align group.separateByFields to vega-lite fold transform
59
60
  * 1.22 - make chain iJavaScript aware, but still able to work outside of Jupyter
60
61
  * 1.21 - include chain - simple monoid
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jupyter-ijavascript-utils",
3
- "version": "1.23.0",
3
+ "version": "1.24.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",
@@ -105,6 +105,7 @@ const { createSort } = require('./array');
105
105
  * * sort and limit the output
106
106
  * * {@link TableGenerator#filter|filter(fn)} - determine which rows to include or not
107
107
  * * {@link TableGenerator#limit|limit(number)} - limit only specific # of rows
108
+ * * {@link TableGenerator#offset|offset(number)} - starts results only after an offset number of rows
108
109
  * * {@link TableGenerator#sortFn|sortFn(fn)} - Standard Array sort function
109
110
  * * {@link TableGenerator#sort|sort(field, field, ...)} - sorts by fields, or descending with '-'
110
111
  * * transpose the output
@@ -197,6 +198,17 @@ class TableGenerator {
197
198
  */
198
199
  #limit = 0;
199
200
 
201
+ /**
202
+ * The number of rows to skip before showing results.
203
+ *
204
+ * 10 : means start showing results only after the first 10 records
205
+ *
206
+ * -10 : means only show the last 10
207
+ *
208
+ * @type {Number}
209
+ */
210
+ #offset = 0;
211
+
200
212
  /**
201
213
  * PrintValue options to use when rendering the table values
202
214
  * @type {PrintOptions}
@@ -289,6 +301,7 @@ class TableGenerator {
289
301
  this.#formatterFn = null;
290
302
  this.#labels = {};
291
303
  this.#limit = 0;
304
+ this.#offset = 0;
292
305
  this.#printOptions = null;
293
306
  this.#sortFn = null;
294
307
  this.#styleTable = '';
@@ -677,6 +690,21 @@ class TableGenerator {
677
690
  return this;
678
691
  }
679
692
 
693
+ /**
694
+ * The number of rows to skip before showing any records.
695
+ *
696
+ * 10 : means start showing results only after the first 10 records
697
+ *
698
+ * -10 : means only show the last 10
699
+ *
700
+ * @param {Number} offsetRecords - the number of rows to skip
701
+ * @returns {TableGenerator} - chainable interface
702
+ */
703
+ offset(offsetRecords) {
704
+ this.#offset = offsetRecords;
705
+ return this;
706
+ }
707
+
680
708
  /**
681
709
  * Sets the alternative labels to be used for specific fields.
682
710
  *
@@ -1130,8 +1158,12 @@ class TableGenerator {
1130
1158
 
1131
1159
  if (this.#limit < 0) {
1132
1160
  data = data.reverse().slice(0, -this.#limit);
1161
+ } else if (this.#offset < 0) {
1162
+ data = data.slice(this.#offset);
1133
1163
  } else if (this.#limit > 0) {
1134
- data = data.slice(0, this.#limit);
1164
+ data = data.slice(this.#offset, this.#offset + this.#limit);
1165
+ } else if (this.#offset > 0) {
1166
+ data = data.slice(this.#offset);
1135
1167
  }
1136
1168
 
1137
1169
  if (this.#isTransposed) {
package/src/chain.js CHANGED
@@ -13,6 +13,9 @@
13
13
  * * {@link ChainContainer#close|.close()} - gets the value of the current chain
14
14
  * * {@link ChainContainer#chain|.chain(function)} - where it is passed the value, and returns a new Chain with that value.
15
15
  * * {@link ChainContainer#chainMap|.chainMap(function)} - where it treats value as an array, and maps function on every item in the array
16
+ * * {@link ChainContainer#chainFlatMap|.chainFlatMap(function)} - where it treats value as an array, and maps function on every item in the array,
17
+ * flattening the results
18
+ * * {@link ChainContainer#chainFilter|.chainFilter(function)} - where it treats the value as an array, and filters values based on the result
16
19
  * * {@link ChainContainer#chainReduce|.chainReduce(function, initialValue)} - where it treats value as an array, and reduces the value array
17
20
  * * {@link ChainContainer#errorHandler|.errorHandler(fn)} - custom function called if an error is ever thrown
18
21
  * * {@link ChainContainer#debug|.debug()} - console.logs the current value, and continues the chain with that value
@@ -158,6 +161,69 @@ class ChainContainer {
158
161
  return this.chain((value) => value.map(fn));
159
162
  }
160
163
 
164
+ /**
165
+ * Assuming that value is an array, performs a
166
+ * [javaScript flatMap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap)
167
+ * against the results.
168
+ *
169
+ * This can be very helpful in expanding the list of items in an array, or removing items from an array.
170
+ *
171
+ * ```
172
+ * // expanding size of the array
173
+ * initializeArray = (size) => Array.from(Array(size)).map((val, index) => index);
174
+ * initializeArray(3); // [0, 1, 2]
175
+ *
176
+ * utils.chain([1, 2, 3, 4])
177
+ * .chainFlatMap(initializeArray)
178
+ * .close();
179
+ *
180
+ * // [1, 1, 2, 1, 2, 3, 1, 2, 3, 4];
181
+ * ```
182
+ *
183
+ * or similar to {@link ChainContainer#chainFilter|chainFilter}
184
+ *
185
+ * ```
186
+ * // reducing the size of the array
187
+ * filterOdd = (value) => value % 2 === 0 ? [value] : [];
188
+ * filterOdd(2); // [2]
189
+ * filterOdd(1); // []
190
+ *
191
+ * chain([1, 2, 3, 4, 5])
192
+ * .chainFlatMap(filterOdd)
193
+ * .close();
194
+ *
195
+ * // [2, 4];
196
+ * ```
197
+ *
198
+ * @param {function(any):any} fn - function that can either return a value or array of values.
199
+ * @returns {ChainContainer}
200
+ * @see {@link ChainContainer#chainFilter} - for other options in filtering
201
+ */
202
+ chainFlatMap(fn) {
203
+ if (!Array.isArray(this.value)) throw Error(`chainFlatMap expects an array, but was passed:${this.value}`);
204
+
205
+ return this.chain((value) => value.flatMap(fn));
206
+ }
207
+
208
+ /**
209
+ * Assuming that value is an array, this maps fn to filter the results in the array.
210
+ *
211
+ * ```
212
+ * chain([1,2,3,4])
213
+ * .chainFilter((value) => value < 3)
214
+ * .close();
215
+ * // [1, 2]
216
+ * ```
217
+ *
218
+ * @param {function(any):Boolean} fn - Function accepting a value and returning whether it should be included (true) or not (false)
219
+ * @returns {ChainContainer}
220
+ */
221
+ chainFilter(fn) {
222
+ if (!Array.isArray(this.value)) throw Error(`chainFilter expects an array, but was passed:${this.value}`);
223
+
224
+ return this.chain((value) => value.filter(fn));
225
+ }
226
+
161
227
  /**
162
228
  * Assuming that the value is an array, performs a reduce using fn and initialValue
163
229
  *
@@ -361,6 +427,9 @@ class ChainContainer {
361
427
  * * {@link ChainContainer#close|.close()} - gets the value of the current chain
362
428
  * * {@link ChainContainer#chain|.chain(function)} - where it is passed the value, and returns a new Chain with that value.
363
429
  * * {@link ChainContainer#chainMap|.chainMap(function)} - where it treats value as an array, and maps function on every item in the array
430
+ * * {@link ChainContainer#chainFlatMap|.chainFlatMap(function)} - where it treats value as an array, and maps function on every item in the array,
431
+ * flattening the results
432
+ * * {@link ChainContainer#chainFilter|.chainFilter(function)} - where it treats the value as an array, and filters values based on the result
364
433
  * * {@link ChainContainer#chainReduce|.chainReduce(function, initialValue)} - where it treats value as an array, and reduces the value array
365
434
  * * {@link ChainContainer#errorHandler|.errorHandler(fn)} - custom function called if an error is ever thrown
366
435
  * * {@link ChainContainer#debug|.debug()} - console.logs the current value, and continues the chain with that value
package/src/format.js CHANGED
@@ -15,6 +15,7 @@
15
15
  * * {@link module:format.capitalize|format.capitalize} - Capitalizes only the first character in the string (ex: 'John paul');
16
16
  * * {@link module:format.capitalizeAll|format.capitalizeAll} - Capitalizes all the words in a string (ex: 'John Paul')
17
17
  * * {@link module:format.ellipsify|format.ellipsify} - Truncates a string if the length is 'too long'
18
+ * * {@link module:format.stripHtmlTags|format.stripHtmlTags} - removes html / xml tags from strings.
18
19
  * * {@link module:format.limitLines|format.limitLines(string, toLine, fromLine, lineSeparator)} - selects only a subset of lines in a string
19
20
  * * {@link module:format.consoleLines|format.consoleLines(...)} - same as limit lines, only console.logs the string out.
20
21
  * * Formatting Time
@@ -30,7 +31,7 @@
30
31
  * * {@link module:format.safeConvertInteger|format.safeConvertInteger} - converts a value to a Number (123), or uses a default for any error or NaN
31
32
  * * {@link module:format.safeConvertBoolean|format.safeConvertBoolean} - converts a value to a boolean
32
33
  * * Parsing values
33
- * * {@link module:format.parseNumber|format.parseBoolean(val)} - converts a value to a boolean value
34
+ * * {@link module:format.parseBoolean|format.parseBoolean(val)} - converts a value to a boolean value
34
35
  * * {@link module:format.parseNumber|format.parseNumber(val, locale)} - converts a value to a number
35
36
  * * Identifying values
36
37
  * * {@link module:format.isEmptyValue|format.isEmptyValue} - determine if a value is not 'empty'
@@ -977,3 +978,21 @@ module.exports.limitLines = function limitLines(str, toLine, fromLine, lineSepar
977
978
  module.exports.consoleLines = function consoleLines(str, toLine, fromLine, lineSeparator) {
978
979
  console.log(FormatUtils.limitLines(str, toLine, fromLine, lineSeparator));
979
980
  };
981
+
982
+ /**
983
+ * Strips any html or xml tags from a string.
984
+ *
985
+ * Note, if you want to remove html entities (ex: `&nbsp;` or `&#8209;`), please consider [other libraries](https://www.npmjs.com/search?q=html%20entities)
986
+ *
987
+ * @param {String} str - string to strip html / xml entities from
988
+ * @returns {String}
989
+ * @example
990
+ *
991
+ * utils.format.stripHtmlTags('Hello <br />Nice to see <b>you</b>'); // 'Hello Nice to see you'
992
+ * utils.format.stripHtmlTags('example string'); // 'example string' -- untouched
993
+ */
994
+ module.exports.stripHtmlTags = function stripHtmlTags(str) {
995
+ if (!str) return str;
996
+
997
+ return str.replace(/<[^>]+>/g, '');
998
+ };
package/src/vega.js CHANGED
@@ -293,8 +293,11 @@ const IJSUtils = require('./ijs');
293
293
  *
294
294
  * If your data is at the series level, you can:
295
295
  *
296
- * * Transform the data with the {@link group#separateByFields|group.separateByFields}
296
+ * * Transform the data with the {@link module:group.separateByFields|group.separateByFields}
297
+ * * utils.group.separateByFields(fruitSeriesData, 'apples', 'bananas', 'pears');
298
+ * * this makes a new field called 'key' that will have values of either apples, bananas or pears
297
299
  * * or using the [vega-lite fold transform](https://vega.github.io/vega-lite/docs/fold.html)
300
+ * * by adding in the `.transform()` into the spec - like below
298
301
  *
299
302
  * ```
300
303
  * fruitSeriesData = [{ year:'2020', apples:20, bananas:15, pears:18 },