jupyter-ijavascript-utils 1.15.2 → 1.16.1

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
@@ -46,6 +46,7 @@ This is not intended to be the only way to accomplish many of these tasks, and a
46
46
 
47
47
  ## What's New
48
48
 
49
+ * 1.16 - provide file.matchFiles - as a way to find files or directories
49
50
  * 1.15 - provide {@link module:object.formatProperties|object.formatProperties} - as a way to quickly convert to string, number, etc.
50
51
  * 1.14 - provide {@link module:object.mapProperties|object.mapProperties()} and {@link module:format.compactNumber|format.compactNumber()}
51
52
  * 1.13 - provide {@link module:random|utils.random()} to genrate random values
package/README.md CHANGED
@@ -46,6 +46,7 @@ This is not intended to be the only way to accomplish many of these tasks, and a
46
46
 
47
47
  # What's New
48
48
 
49
+ * 1.16 - provide file.matchFiles - as a way to find files or directories
49
50
  * 1.15 - provide object.formatProperties - as a way to quickly convert to string, number, etc.
50
51
  * 1.14 - provide format.compactNumber and object.mapProperties
51
52
  * 1.13 - provide utils.random() to genrate random values
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jupyter-ijavascript-utils",
3
- "version": "1.15.2",
3
+ "version": "1.16.1",
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/file.js CHANGED
@@ -26,6 +26,7 @@ const logger = require('./logger');
26
26
  * * listing directory
27
27
  * * {@link module:file.pwd|pwd()} - list the current path
28
28
  * * {@link module:file.listFiles|listFiles(path)} - list files in a diven path
29
+ * * {@link module:file.matchFiles|matchFiles(path, matchingFn)} - find files or directories with a matching fn
29
30
  * * checking files exist
30
31
  * * {@link module:file.checkFile|checkFile(...paths)} - check if a file at a path exists
31
32
  *
@@ -279,12 +280,13 @@ module.exports.pwd = function pwd() {
279
280
  * List files in a directory
280
281
  *
281
282
  * @param {String} directoryPath - path of the directory to list
283
+ * @param {Object} [readdirOptions=null] - object with options to pass to fs readdir
282
284
  * @see {@link module:file.pwd|pwd()} - to get the current working directory
283
285
  * @example
284
286
  * utils.file.listFiles('./');
285
287
  * // ['.gitignore', 'data', ... ];
286
288
  */
287
- module.exports.listFiles = function listFiles(directoryPath) {
289
+ module.exports.listFiles = function listFiles(directoryPath, readdirOptions = null) {
288
290
  const resolvedPath = path.resolve(directoryPath);
289
291
  if (!fs.existsSync(resolvedPath)) {
290
292
  logger.error('Path does not exist: %s', resolvedPath);
@@ -295,13 +297,65 @@ module.exports.listFiles = function listFiles(directoryPath) {
295
297
  }
296
298
 
297
299
  try {
298
- const results = fs.readdirSync(resolvedPath);
300
+ const results = fs.readdirSync(resolvedPath, readdirOptions);
299
301
  return results;
300
302
  } catch (err) {
301
303
  logger.error(`unable to read directory: ${resolvedPath}`);
302
304
  }
303
305
  };
304
306
 
307
+ /**
308
+ * Finds files in a directory, returning only the file names and paths of those that match a function.
309
+ *
310
+ * Note the matching function passes both fileNames and {@link https://nodejs.org/api/fs.html#class-fsdirent|DirEnt} objects<br />
311
+ * {(fileName:String, file:{@link https://nodejs.org/api/fs.html#class-fsdirent|DirEnt}) => Boolean}<br />
312
+ * allowing for checking for files:`.isFile()`, directories:`.isDirectory()`, symbolic links:`.isSymbolicLink()`, etc.
313
+ *
314
+ * For example, if there is a `./tmp` folder, with:
315
+ *
316
+ * * ./tmp/fileA (file)
317
+ * * ./tmp/fileB (file)
318
+ * * ./tmp/dirA (directory)
319
+ * * ./tmp/dirB (directory)
320
+ *
321
+ * You could find only files like the following:
322
+ *
323
+ * ```
324
+ * utils.file.matchFiles('./tmp', (fileName, file) => file.isFile());
325
+ * // ['./tmp/fileA', './tmp/fileB'];
326
+ * ```
327
+ *
328
+ * or find directories ending with the letter B:
329
+ *
330
+ * ```
331
+ * utils.file.matchFiles('./tmp',
332
+ * (fileName, file) => file.isDirectory() && fileName.endsWith('B')
333
+ * );
334
+ * // ['./tmp/dirB'];
335
+ * ```
336
+ *
337
+ * Note: passing false as the last parameter will only return file names
338
+ *
339
+ * ```
340
+ * utils.file.matchFiles('./tmp', (fileName) => fileName.startsWith('file'), false);
341
+ * // ['fileA', 'fileB']
342
+ * ```
343
+ *
344
+ * @param {String} directoryPath - path of the directory to match within
345
+ * @param {Function} matchingFunction - (DirEnt) => Boolean function to determine
346
+ * if the path should be returned or not
347
+ * @param {Boolean} [returnFullPath=true] - whether the full path should be returned
348
+ * @returns {String[]} - list of the files that match
349
+
350
+ */
351
+ module.exports.matchFiles = function matchFiles(directoryPath, matchingFunction, returnFullPath = true) {
352
+ return FileUtil.listFiles(directoryPath, { withFileTypes: true })
353
+ .filter((dirExt) => matchingFunction(dirExt.name, dirExt))
354
+ .map((dirExt) => returnFullPath
355
+ ? path.resolve(directoryPath, dirExt.name)
356
+ : dirExt.name);
357
+ };
358
+
305
359
  /**
306
360
  * Synchronously checks if any of the files provided do not exist.
307
361
  *
package/src/object.js CHANGED
@@ -206,10 +206,12 @@ module.exports.augment = function augment(objCollection, mappingFn, inPlace = fa
206
206
  */
207
207
  module.exports.mapByProperty = function mapByProperty(collection, propertyName) {
208
208
  if (!propertyName) throw new Error('object.mapByProperty: expects a propertyName');
209
+
210
+ const cleanedFunc = ObjectUtils.evaluateFunctionOrProperty(propertyName);
209
211
 
210
212
  return (collection || []).reduce(
211
213
  (result, entry) => {
212
- result.set(entry[propertyName], entry);
214
+ result.set(cleanedFunc(entry), entry);
213
215
  return result;
214
216
  }, new Map()
215
217
  );
package/src/plantuml.js CHANGED
@@ -1,5 +1,4 @@
1
- /* global fetch */
2
- /* eslint-disable no-empty, class-methods-use-this */
1
+ /* eslint-disable no-empty, class-methods-use-this, no-redeclare */
3
2
 
4
3
  //-- encode the strings into the format needed for the plantuml server
5
4
  const plantUMLEncoder = require('plantuml-encoder');