@jdeighan/coffee-utils 8.0.0 → 8.0.3

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": "8.0.0",
4
+ "version": "8.0.3",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -53,6 +53,6 @@
53
53
  "svelte": "^3.48.0"
54
54
  },
55
55
  "devDependencies": {
56
- "@jdeighan/unit-tester": "^2.0.8"
56
+ "@jdeighan/unit-tester": "^2.0.9"
57
57
  }
58
58
  }
@@ -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)];