@jdeighan/coffee-utils 7.0.75 → 8.0.2

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "7.0.75",
4
+ "version": "8.0.2",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -135,6 +135,16 @@ export isString = (x) ->
135
135
 
136
136
  # ---------------------------------------------------------------------------
137
137
 
138
+ export isNonEmptyString = (x) ->
139
+
140
+ if typeof x != 'string' && x ! instanceof String
141
+ return false
142
+ if x.match(/^\s*$/)
143
+ return false
144
+ return true
145
+
146
+ # ---------------------------------------------------------------------------
147
+
138
148
  export isBoolean = (x) ->
139
149
 
140
150
  return typeof x == 'boolean'
@@ -269,19 +279,43 @@ export isInteger = (x) ->
269
279
 
270
280
  # ---------------------------------------------------------------------------
271
281
 
272
- export isUniqueList = (lItems) ->
282
+ export isUniqueList = (lItems, func=undef) ->
273
283
 
274
284
  if ! lItems?
275
285
  return true # empty list is unique
286
+ if defined(func)
287
+ assert isFunction(func), "Not a function: #{OL(func)}"
276
288
  h = {}
277
289
  for item in lItems
278
- if h[item]
290
+ if defined(func) && !func(item)
291
+ return false
292
+ if defined(h[item])
279
293
  return false
280
294
  h[item] = 1
281
295
  return true
282
296
 
283
297
  # ---------------------------------------------------------------------------
284
298
 
299
+ export isUniqueTree = (lItems, func=undef, hFound={}) ->
300
+
301
+ if isEmpty(lItems)
302
+ return true # empty list is unique
303
+ if defined(func)
304
+ assert isFunction(func), "Not a function: #{OL(func)}"
305
+ for item in lItems
306
+ if isArray(item)
307
+ if ! isUniqueTree(item, func, hFound)
308
+ return false
309
+ else
310
+ if defined(func) && !func(item)
311
+ return false
312
+ if defined(hFound[item])
313
+ return false
314
+ hFound[item] = 1
315
+ return true
316
+
317
+ # ---------------------------------------------------------------------------
318
+
285
319
  export uniq = (lItems) ->
286
320
 
287
321
  return [...new Set(lItems)]
@@ -128,6 +128,17 @@ export var isString = function(x) {
128
128
  return typeof x === 'string' || x instanceof String;
129
129
  };
130
130
 
131
+ // ---------------------------------------------------------------------------
132
+ export var isNonEmptyString = function(x) {
133
+ if (typeof x !== 'string' && !(x instanceof String)) {
134
+ return false;
135
+ }
136
+ if (x.match(/^\s*$/)) {
137
+ return false;
138
+ }
139
+ return true;
140
+ };
141
+
131
142
  // ---------------------------------------------------------------------------
132
143
  export var isBoolean = function(x) {
133
144
  return typeof x === 'boolean';
@@ -270,15 +281,21 @@ export var isInteger = function(x) {
270
281
  };
271
282
 
272
283
  // ---------------------------------------------------------------------------
273
- export var isUniqueList = function(lItems) {
284
+ export var isUniqueList = function(lItems, func = undef) {
274
285
  var h, i, item, len;
275
286
  if (lItems == null) {
276
287
  return true; // empty list is unique
277
288
  }
289
+ if (defined(func)) {
290
+ assert(isFunction(func), `Not a function: ${OL(func)}`);
291
+ }
278
292
  h = {};
279
293
  for (i = 0, len = lItems.length; i < len; i++) {
280
294
  item = lItems[i];
281
- if (h[item]) {
295
+ if (defined(func) && !func(item)) {
296
+ return false;
297
+ }
298
+ if (defined(h[item])) {
282
299
  return false;
283
300
  }
284
301
  h[item] = 1;
@@ -286,6 +303,34 @@ export var isUniqueList = function(lItems) {
286
303
  return true;
287
304
  };
288
305
 
306
+ // ---------------------------------------------------------------------------
307
+ export var isUniqueTree = function(lItems, func = undef, hFound = {}) {
308
+ var i, item, len;
309
+ if (isEmpty(lItems)) {
310
+ return true; // empty list is unique
311
+ }
312
+ if (defined(func)) {
313
+ assert(isFunction(func), `Not a function: ${OL(func)}`);
314
+ }
315
+ for (i = 0, len = lItems.length; i < len; i++) {
316
+ item = lItems[i];
317
+ if (isArray(item)) {
318
+ if (!isUniqueTree(item, func, hFound)) {
319
+ return false;
320
+ }
321
+ } else {
322
+ if (defined(func) && !func(item)) {
323
+ return false;
324
+ }
325
+ if (defined(hFound[item])) {
326
+ return false;
327
+ }
328
+ hFound[item] = 1;
329
+ }
330
+ }
331
+ return true;
332
+ };
333
+
289
334
  // ---------------------------------------------------------------------------
290
335
  export var uniq = function(lItems) {
291
336
  return [...new Set(lItems)];
@@ -7,7 +7,7 @@ import NReadLines from 'n-readlines'
7
7
 
8
8
  import {
9
9
  assert, undef, pass, defined, rtrim, error, isEmpty, nonEmpty,
10
- isString, isArray, isRegExp, isFunction, croak, OL,
10
+ isString, isArray, isHash, isRegExp, isFunction, croak, OL,
11
11
  } from '@jdeighan/coffee-utils'
12
12
  import {log, LOG} from '@jdeighan/coffee-utils/log'
13
13
  import {debug} from '@jdeighan/coffee-utils/debug'
@@ -268,7 +268,13 @@ export forEachFile = (dir, cb, filt=undef, level=0) ->
268
268
 
269
269
  # ---------------------------------------------------------------------------
270
270
 
271
- export pathTo = (fname, searchDir, direction="down") ->
271
+ export pathTo = (fname, searchDir, hOptions={}) ->
272
+
273
+ {direction, relative} = hOptions
274
+ if isEmpty(direction)
275
+ direction = 'down'
276
+ if isEmpty(relative)
277
+ relative = false
272
278
 
273
279
  debug "enter pathTo()", fname, searchDir, direction
274
280
  if ! searchDir
@@ -276,6 +282,8 @@ export pathTo = (fname, searchDir, direction="down") ->
276
282
  assert fs.existsSync(searchDir), "Dir #{searchDir} does not exist"
277
283
  filepath = mkpath(searchDir, fname)
278
284
  if fs.existsSync(filepath)
285
+ if relative
286
+ filepath = "./#{fname}"
279
287
  debug "return from pathTo() - file exists", filepath
280
288
  return filepath
281
289
 
@@ -285,17 +293,26 @@ export pathTo = (fname, searchDir, direction="down") ->
285
293
  for subdir in getSubDirs(searchDir)
286
294
  dirpath = mkpath(searchDir, subdir)
287
295
  debug "check #{subdir}"
288
- if defined(fpath = pathTo(fname, dirpath))
296
+ if defined(fpath = pathTo(fname, dirpath, hOptions))
297
+ if relative
298
+ fpath = fpath.replace('./', "./#{subdir}/")
289
299
  debug "return from pathTo()", fpath
290
300
  return fpath
291
301
  else if (direction == 'up')
302
+ nLevels = 0
292
303
  while defined(dirPath = getParentDir(searchDir))
304
+ nLevels += 1
293
305
  debug "check #{dirPath}"
294
- filepath = mkpath(dirPath, fname)
295
- debug "check for #{filepath}"
296
- if fs.existsSync(filepath)
297
- debug "return from pathTo()", filepath
298
- return filepath
306
+ fpath = mkpath(dirPath, fname)
307
+ debug "check for #{fpath}"
308
+ if fs.existsSync(fpath)
309
+ if relative
310
+ fpath = "../".repeat(nLevels) + fname
311
+ debug "return from pathTo()", fpath
312
+ return fpath
313
+ else
314
+ debug "return from pathTo()", fpath
315
+ return fpath
299
316
  searchDir = dirPath
300
317
  else
301
318
  error "pathTo(): Invalid direction '#{direction}'"
@@ -309,12 +326,12 @@ export allPathsTo = (fname, searchDir) ->
309
326
 
310
327
  if ! searchDir
311
328
  searchDir = process.cwd()
312
- path = pathTo(fname, searchDir, "up")
329
+ path = pathTo(fname, searchDir, {direction: "up"})
313
330
  if path?
314
331
  lPaths = [path] # --- build an array of paths
315
332
  # --- search upward for files, but return ordered top down
316
333
  while (h = pathlib.parse(path)) \
317
- && (path = pathTo(fname, pathlib.resolve(h.dir, '..'), "up"))
334
+ && (path = pathTo(fname, pathlib.resolve(h.dir, '..'), {direction: "up"}))
318
335
  lPaths.unshift path
319
336
  return lPaths
320
337
  else
package/src/fs_utils.js CHANGED
@@ -21,6 +21,7 @@ import {
21
21
  nonEmpty,
22
22
  isString,
23
23
  isArray,
24
+ isHash,
24
25
  isRegExp,
25
26
  isFunction,
26
27
  croak,
@@ -326,8 +327,15 @@ export var forEachFile = function(dir, cb, filt = undef, level = 0) {
326
327
  };
327
328
 
328
329
  // ---------------------------------------------------------------------------
329
- export var pathTo = function(fname, searchDir, direction = "down") {
330
- var dirPath, dirpath, filepath, fpath, i, len, ref, subdir;
330
+ export var pathTo = function(fname, searchDir, hOptions = {}) {
331
+ var dirPath, direction, dirpath, filepath, fpath, i, len, nLevels, ref, relative, subdir;
332
+ ({direction, relative} = hOptions);
333
+ if (isEmpty(direction)) {
334
+ direction = 'down';
335
+ }
336
+ if (isEmpty(relative)) {
337
+ relative = false;
338
+ }
331
339
  debug("enter pathTo()", fname, searchDir, direction);
332
340
  if (!searchDir) {
333
341
  searchDir = process.cwd();
@@ -335,6 +343,9 @@ export var pathTo = function(fname, searchDir, direction = "down") {
335
343
  assert(fs.existsSync(searchDir), `Dir ${searchDir} does not exist`);
336
344
  filepath = mkpath(searchDir, fname);
337
345
  if (fs.existsSync(filepath)) {
346
+ if (relative) {
347
+ filepath = `./${fname}`;
348
+ }
338
349
  debug("return from pathTo() - file exists", filepath);
339
350
  return filepath;
340
351
  }
@@ -346,19 +357,30 @@ export var pathTo = function(fname, searchDir, direction = "down") {
346
357
  subdir = ref[i];
347
358
  dirpath = mkpath(searchDir, subdir);
348
359
  debug(`check ${subdir}`);
349
- if (defined(fpath = pathTo(fname, dirpath))) {
360
+ if (defined(fpath = pathTo(fname, dirpath, hOptions))) {
361
+ if (relative) {
362
+ fpath = fpath.replace('./', `./${subdir}/`);
363
+ }
350
364
  debug("return from pathTo()", fpath);
351
365
  return fpath;
352
366
  }
353
367
  }
354
368
  } else if (direction === 'up') {
369
+ nLevels = 0;
355
370
  while (defined(dirPath = getParentDir(searchDir))) {
371
+ nLevels += 1;
356
372
  debug(`check ${dirPath}`);
357
- filepath = mkpath(dirPath, fname);
358
- debug(`check for ${filepath}`);
359
- if (fs.existsSync(filepath)) {
360
- debug("return from pathTo()", filepath);
361
- return filepath;
373
+ fpath = mkpath(dirPath, fname);
374
+ debug(`check for ${fpath}`);
375
+ if (fs.existsSync(fpath)) {
376
+ if (relative) {
377
+ fpath = "../".repeat(nLevels) + fname;
378
+ debug("return from pathTo()", fpath);
379
+ return fpath;
380
+ } else {
381
+ debug("return from pathTo()", fpath);
382
+ return fpath;
383
+ }
362
384
  }
363
385
  searchDir = dirPath;
364
386
  }
@@ -375,11 +397,15 @@ export var allPathsTo = function(fname, searchDir) {
375
397
  if (!searchDir) {
376
398
  searchDir = process.cwd();
377
399
  }
378
- path = pathTo(fname, searchDir, "up");
400
+ path = pathTo(fname, searchDir, {
401
+ direction: "up"
402
+ });
379
403
  if (path != null) {
380
404
  lPaths = [path]; // --- build an array of paths
381
405
  // --- search upward for files, but return ordered top down
382
- while ((h = pathlib.parse(path)) && (path = pathTo(fname, pathlib.resolve(h.dir, '..'), "up"))) {
406
+ while ((h = pathlib.parse(path)) && (path = pathTo(fname, pathlib.resolve(h.dir, '..'), {
407
+ direction: "up"
408
+ }))) {
383
409
  lPaths.unshift(path);
384
410
  }
385
411
  return lPaths;