jupyter-ijavascript-utils 1.8.3 → 1.8.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jupyter-ijavascript-utils",
3
- "version": "1.8.3",
3
+ "version": "1.8.5",
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/array.js CHANGED
@@ -16,8 +16,14 @@ require('./_types/global');
16
16
  * * {@link module:array.SORT_ASCENDING|array.SORT_ASCENDING} - common ascending sorting function for array.sort()
17
17
  * * {@link module:array.SORT_DESCENDING|array.SORT_DESCENDING} - common descending sorting function for array.sort()
18
18
  * * Rearrange Array
19
- * * {@link module:array.reshape|array.reshape}
20
- * * {@link module:array.transpose|array.transpose}
19
+ * * {@link module:array.reshape|array.reshape} - reshapes an array to a size of rows and columns
20
+ * * {@link module:array.transpose|array.transpose} - transposes (flips - the array along the diagonal)
21
+ * * Picking Values
22
+ * * {@link module:array.peekFirst|array.peekFirst} - peeks at the first value in the list
23
+ * * {@link module:array.peekLast|array.peekLast} - peeks at the last value in the list
24
+ * * {@link module:array.pickRows|array.pickRows} - picks a row from a 2d array
25
+ * * {@link module:array.pickColumns|array.pickColumns} - picks a column from a 2d array
26
+ * * {@link module:array.pick|array.pick} - picks either/or rows and columns
21
27
  *
22
28
  * @module array
23
29
  * @exports array
@@ -101,18 +107,144 @@ module.exports.createSort = (...fields) => {
101
107
  });
102
108
  };
103
109
 
110
+ /**
111
+ * Peek in an array and return the first value in the array.
112
+ *
113
+ * Or return the default value (`defaultVal`) - if the array is empty
114
+ *
115
+ * @param {Array} targetArray - array to be peeked within
116
+ * @param {any} defaultVal - the value to return if the array is empty
117
+ * @returns {any}
118
+ */
104
119
  module.exports.peekFirst = function peekFirst(targetArray, defaultVal = null) {
105
120
  return (Array.isArray(targetArray) && targetArray.length > 0)
106
121
  ? targetArray[0]
107
122
  : defaultVal;
108
123
  };
109
124
 
125
+ /**
126
+ * Peek in an array and return the last value in the array.
127
+ *
128
+ * Or return the default value (`defaultVal`) - if the array is empty
129
+ *
130
+ * @param {Array} targetArray - array to be peeked within
131
+ * @param {any} defaultVal - the value to return if the array is empty
132
+ * @returns {any}
133
+ */
110
134
  module.exports.peekLast = function peekLast(targetArray, defaultVal = null) {
111
135
  return (Array.isArray(targetArray) && targetArray.length > 0)
112
136
  ? targetArray[targetArray.length - 1]
113
137
  : defaultVal;
114
138
  };
115
139
 
140
+ /**
141
+ * Picks a row (or multiple rows) from a 2d array.
142
+ *
143
+ * Please also see [Danfo.js](https://danfo.jsdata.org/) for working with DataFrames.
144
+ *
145
+ * @param {Array} array2d - 2d array to pick from [row][column]
146
+ * @param {...Number} rowIndices - Indexes of the row to return, [0...length-1]
147
+ * @returns - Array with only those rows
148
+ * @example
149
+ * data = [
150
+ * ['john', 23, 'purple'],
151
+ * ['jane', 32, 'red'],
152
+ * ['ringo', 27, 'green']
153
+ * ];
154
+ *
155
+ * utils.array.pickRows(data, 0);
156
+ * //-- [['john', 23, 'purple']];
157
+ *
158
+ * utils.array.pickRows(data, 0, 1);
159
+ * //-- [['john', 23, 'purple'], ['jane', 32, 'red']];
160
+ */
161
+ module.exports.pickRows = function pickRows(array2d, ...rowIndices) {
162
+ //-- allow passing an array as the first item
163
+ const cleanRowIndices = rowIndices.length > 0 && Array.isArray(rowIndices[0])
164
+ ? rowIndices[0]
165
+ : rowIndices;
166
+ return cleanRowIndices.map((index) => array2d[index]);
167
+ };
168
+
169
+ /**
170
+ * Picks a column (or multiple columns) from a 2d array
171
+ *
172
+ * Please also see [Danfo.js](https://danfo.jsdata.org/) for working with DataFrames.
173
+ *
174
+ * @param {Array} array2d - 2d array to pick from [row][column]
175
+ * @param {...any} columns - Indexes of the columns to pick the values from: [0...row.length-1]
176
+ * @returns - Array with all rows, and only those columns
177
+ * @example
178
+ * data = [
179
+ * ['john', 23, 'purple'],
180
+ * ['jane', 32, 'red'],
181
+ * ['ringo', 27, 'green']
182
+ * ];
183
+ *
184
+ * utils.array.pickColumns(data, 0);
185
+ * //-- [['john'], ['jane'], ['ringo']];
186
+ *
187
+ * utils.array.pickColumns(data, 0, 2);
188
+ * //-- [['john', 'purple'], ['jane', 'red'], ['ringo', 'green']];
189
+ */
190
+ module.exports.pickColumns = function pickColumns(array2d, ...columns) {
191
+ //-- allow passing an array as the first item
192
+ const cleanColumns = columns.length > 0 && Array.isArray(columns[0])
193
+ ? columns[0]
194
+ : columns;
195
+ return array2d.map((row) => cleanColumns.map((columnIndex) => row[columnIndex]));
196
+ };
197
+
198
+ /**
199
+ * Convenience function for picking specific rows and columns from a 2d array.
200
+ *
201
+ * Please also see [Danfo.js](https://danfo.jsdata.org/) for working with DataFrames.
202
+ *
203
+ * @param {Array} array2d - 2d array to pick from [row][column]
204
+ * @param {Object} options - options on which to pick
205
+ * @param {Number[]} [options.rows = null] - indices of the rows to pick
206
+ * @param {Number[]} [options.columns = null] - indices of the columns to pick.
207
+ * @returns {Array} - 2d array of only the rows and columns chosen.
208
+ * @see {@link module:Array.pickRows} - picking rows
209
+ * @see {@link module:Array.pickColumns} - picking columns
210
+ * @returns - 2dArray of the columns and rows requested
211
+ * @example
212
+ * data = [
213
+ * ['john', 23, 'purple'],
214
+ * ['jane', 32, 'red'],
215
+ * ['ringo', 27, 'green']
216
+ * ];
217
+ *
218
+ * utils.array.pick(data, {rows: [0, 1]});
219
+ * //-- [['john', 23, 'purple'], ['jane', 32, 'red']];
220
+ *
221
+ * utils.array.pick(data, {columns: [0, 2]});
222
+ * //-- [['john', 'purple'], ['jane', 'red'], ['ringo', 'green']];
223
+ *
224
+ * utils.array.pick(data, {rows:[0, 1], columns:[0, 2]});
225
+ * //-- [['john', 'purple'], ['jane', 'red']];
226
+ */
227
+ module.exports.pick = function pick(array2d, options) {
228
+ const cleanOptions = options || {};
229
+
230
+ const {
231
+ rows = null,
232
+ columns = null
233
+ } = cleanOptions;
234
+
235
+ let results = array2d;
236
+
237
+ if (rows) {
238
+ results = ArrayUtils.pickRows(results, rows);
239
+ }
240
+
241
+ if (columns) {
242
+ results = ArrayUtils.pickColumns(results, columns);
243
+ }
244
+
245
+ return results;
246
+ };
247
+
116
248
  /**
117
249
  * Creates an array of a specific size and default value
118
250
  *
package/src/datasets.js CHANGED
@@ -9,6 +9,9 @@ const fetch = require('node-fetch');
9
9
  * The data lives at [https://github.com/vega/vega-datasets](https://github.com/vega/vega-datasets)
10
10
  * and [https://cdn.jsdelivr.net/npm/vega-datasets](https://cdn.jsdelivr.net/npm/vega-datasets)
11
11
  *
12
+ * **For those of you familiar with Pandas, please consider looking at [danfo.js](https://danfo.jsdata.org/)
13
+ * and [DataFrame.js](https://gmousse.gitbooks.io/dataframe-js/content/#dataframe-js)**
14
+ *
12
15
  * * {@link module:datasets.list|list()} - retrieves the list of the datasets available
13
16
  * * {@link module:datasets.fetch|fetch(datasetName)} - returns a promise and fetches the dataset
14
17
  *
package/src/latex.js CHANGED
@@ -95,7 +95,7 @@ module.exports.render = function render(body) {
95
95
  * For example, here we can give options on display options, and additional custom macros.
96
96
  *
97
97
  * ```
98
- * utils.katex.render("c = \\pm\\root{a^2 + b^2}\\in\\RR", {
98
+ * utils.latex.katex("c = \\pm\\root{a^2 + b^2}\\in\\RR", {
99
99
  * displayMode: false,
100
100
  * macros: {
101
101
  * "\\RR": "\\mathbb{R}",