@jdeighan/coffee-utils 14.0.9 → 14.0.11

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": "14.0.9",
4
+ "version": "14.0.11",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -3,7 +3,9 @@
3
3
  import pathlib from 'path'
4
4
  import {writable, readable, get} from 'svelte/store'
5
5
 
6
- import {undef, pass, range} from '@jdeighan/base-utils'
6
+ import {
7
+ undef, defined, notdefined, pass, range, getOptions,
8
+ } from '@jdeighan/base-utils'
7
9
  import {assert, croak} from '@jdeighan/base-utils/exceptions'
8
10
  import {fromTAML} from '@jdeighan/base-utils/taml'
9
11
  import {localStore} from '@jdeighan/coffee-utils/browser'
@@ -41,9 +43,11 @@ export class WritableDataStore
41
43
 
42
44
  set: (value) ->
43
45
  @store.set(value)
46
+ return
44
47
 
45
48
  update: (func) ->
46
49
  @store.update(func)
50
+ return
47
51
 
48
52
  # ---------------------------------------------------------------------------
49
53
 
@@ -55,21 +59,22 @@ export class LocalStorageDataStore extends WritableDataStore
55
59
  # so we can't get the localStorage value first
56
60
  super defValue
57
61
  value = localStore(@masterKey)
58
- if value?
62
+ if defined(value)
59
63
  @set value
60
64
 
61
65
  # --- I'm assuming that when update() is called,
62
66
  # set() will also be called
63
67
 
64
68
  set: (value) ->
65
- if ! value?
66
- croak "LocalStorageStore.set(): cannont set to undef"
69
+ assert defined(value), "set(): cannot set to undef"
67
70
  super value
68
71
  localStore @masterKey, value
72
+ return
69
73
 
70
74
  update: (func) ->
71
75
  super func
72
76
  localStore @masterKey, get(@store)
77
+ return
73
78
 
74
79
  # ---------------------------------------------------------------------------
75
80
 
@@ -79,11 +84,12 @@ export class PropsDataStore extends LocalStorageDataStore
79
84
  super masterKey, {}
80
85
 
81
86
  setProp: (name, value) ->
82
- if ! name?
83
- croak "PropStore.setProp(): empty key"
87
+
88
+ assert defined(name), "PropStore.setProp(): empty key"
84
89
  @update (hPrefs) ->
85
90
  hPrefs[name] = value
86
91
  return hPrefs
92
+ return
87
93
 
88
94
  # ---------------------------------------------------------------------------
89
95
 
@@ -99,10 +105,10 @@ export class ReadableDataStore
99
105
  return @store.subscribe(callback)
100
106
 
101
107
  start: () ->
102
- pass
108
+ return
103
109
 
104
110
  stop: () ->
105
- pass
111
+ return
106
112
 
107
113
  # ---------------------------------------------------------------------------
108
114
 
@@ -113,9 +119,11 @@ export class DateTimeDataStore extends ReadableDataStore
113
119
  @interval = setInterval(() ->
114
120
  @setter new Date()
115
121
  , 1000)
122
+ return
116
123
 
117
124
  stop: () ->
118
125
  clearInterval @interval
126
+ return
119
127
 
120
128
  # ---------------------------------------------------------------------------
121
129
 
@@ -129,9 +137,11 @@ export class MousePosDataStore extends ReadableDataStore
129
137
  y: e.clientY,
130
138
  }
131
139
  document.body.addEventListener('mousemove', @mouseMoveHandler)
140
+ return
132
141
 
133
142
  stop: () ->
134
143
  document.body.removeEventListener('mousemove', @mouseMoveHandler)
144
+ return
135
145
 
136
146
  # ---------------------------------------------------------------------------
137
147
 
@@ -199,8 +209,7 @@ export class ToDoDataStore extends BaseDataStore
199
209
  return
200
210
 
201
211
  add: (name) ->
202
- if defined(@find(name))
203
- croak "Attempt to add duplicate #{name} todo"
212
+ assert notdefined(@find(name)), "Todo #{name} already exists"
204
213
  @lToDos.push {
205
214
  text: name
206
215
  done: false
@@ -233,9 +242,10 @@ export brewTamlFile = (srcPath, destPath=undef, hOptions={}) =>
233
242
  # Valid Options:
234
243
  # force
235
244
 
236
- if ! destPath?
237
- destPath = withExt(srcPath, '.js', {removeLeadingUnderScore:true})
238
- if hOptions.force || ! newerDestFileExists(srcPath, destPath)
245
+ if notdefined(destPath)
246
+ destPath = withExt(srcPath, '.js')
247
+ {force} = getOptions(hOptions)
248
+ if force || ! newerDestFileExists(srcPath, destPath)
239
249
  hInfo = pathlib.parse(destPath)
240
250
  stub = hInfo.name
241
251
 
package/src/DataStores.js CHANGED
@@ -10,8 +10,11 @@ import {
10
10
 
11
11
  import {
12
12
  undef,
13
+ defined,
14
+ notdefined,
13
15
  pass,
14
- range
16
+ range,
17
+ getOptions
15
18
  } from '@jdeighan/base-utils';
16
19
 
17
20
  import {
@@ -68,11 +71,11 @@ export var WritableDataStore = class WritableDataStore {
68
71
  }
69
72
 
70
73
  set(value) {
71
- return this.store.set(value);
74
+ this.store.set(value);
72
75
  }
73
76
 
74
77
  update(func) {
75
- return this.store.update(func);
78
+ this.store.update(func);
76
79
  }
77
80
 
78
81
  };
@@ -84,7 +87,7 @@ export var LocalStorageDataStore = class LocalStorageDataStore extends WritableD
84
87
  super(defValue);
85
88
  this.masterKey = masterKey1;
86
89
  value = localStore(this.masterKey);
87
- if (value != null) {
90
+ if (defined(value)) {
88
91
  this.set(value);
89
92
  }
90
93
  }
@@ -92,16 +95,14 @@ export var LocalStorageDataStore = class LocalStorageDataStore extends WritableD
92
95
  // --- I'm assuming that when update() is called,
93
96
  // set() will also be called
94
97
  set(value) {
95
- if (value == null) {
96
- croak("LocalStorageStore.set(): cannont set to undef");
97
- }
98
+ assert(defined(value), "set(): cannot set to undef");
98
99
  super.set(value);
99
- return localStore(this.masterKey, value);
100
+ localStore(this.masterKey, value);
100
101
  }
101
102
 
102
103
  update(func) {
103
104
  super.update(func);
104
- return localStore(this.masterKey, get(this.store));
105
+ localStore(this.masterKey, get(this.store));
105
106
  }
106
107
 
107
108
  };
@@ -113,10 +114,8 @@ export var PropsDataStore = class PropsDataStore extends LocalStorageDataStore {
113
114
  }
114
115
 
115
116
  setProp(name, value) {
116
- if (name == null) {
117
- croak("PropStore.setProp(): empty key");
118
- }
119
- return this.update(function(hPrefs) {
117
+ assert(defined(name), "PropStore.setProp(): empty key");
118
+ this.update(function(hPrefs) {
120
119
  hPrefs[name] = value;
121
120
  return hPrefs;
122
121
  });
@@ -140,13 +139,9 @@ export var ReadableDataStore = class ReadableDataStore {
140
139
  return this.store.subscribe(callback);
141
140
  }
142
141
 
143
- start() {
144
- return pass;
145
- }
142
+ start() {}
146
143
 
147
- stop() {
148
- return pass;
149
- }
144
+ stop() {}
150
145
 
151
146
  };
152
147
 
@@ -154,13 +149,13 @@ export var ReadableDataStore = class ReadableDataStore {
154
149
  export var DateTimeDataStore = class DateTimeDataStore extends ReadableDataStore {
155
150
  start() {
156
151
  // --- We need to store this interval for use in stop() later
157
- return this.interval = setInterval(function() {
152
+ this.interval = setInterval(function() {
158
153
  return this.setter(new Date(), 1000);
159
154
  });
160
155
  }
161
156
 
162
157
  stop() {
163
- return clearInterval(this.interval);
158
+ clearInterval(this.interval);
164
159
  }
165
160
 
166
161
  };
@@ -175,11 +170,11 @@ export var MousePosDataStore = class MousePosDataStore extends ReadableDataStore
175
170
  y: e.clientY
176
171
  });
177
172
  };
178
- return document.body.addEventListener('mousemove', this.mouseMoveHandler);
173
+ document.body.addEventListener('mousemove', this.mouseMoveHandler);
179
174
  }
180
175
 
181
176
  stop() {
182
- return document.body.removeEventListener('mousemove', this.mouseMoveHandler);
177
+ document.body.removeEventListener('mousemove', this.mouseMoveHandler);
183
178
  }
184
179
 
185
180
  };
@@ -267,9 +262,7 @@ export var ToDoDataStore = class ToDoDataStore extends BaseDataStore {
267
262
  }
268
263
 
269
264
  add(name) {
270
- if (defined(this.find(name))) {
271
- croak(`Attempt to add duplicate ${name} todo`);
272
- }
265
+ assert(notdefined(this.find(name)), `Todo ${name} already exists`);
273
266
  this.lToDos.push({
274
267
  text: name,
275
268
  done: false
@@ -297,13 +290,15 @@ export let ${stub} = new TAMLDataStore(\`${code}\`);`;
297
290
 
298
291
  // ---------------------------------------------------------------------------
299
292
  export var brewTamlFile = (srcPath, destPath = undef, hOptions = {}) => {
300
- var hInfo, jsCode, stub, tamlCode;
301
- if (destPath == null) {
302
- destPath = withExt(srcPath, '.js', {
303
- removeLeadingUnderScore: true
304
- });
305
- }
306
- if (hOptions.force || !newerDestFileExists(srcPath, destPath)) {
293
+ var force, hInfo, jsCode, stub, tamlCode;
294
+ // --- taml => js
295
+ // Valid Options:
296
+ // force
297
+ if (notdefined(destPath)) {
298
+ destPath = withExt(srcPath, '.js');
299
+ }
300
+ ({force} = getOptions(hOptions));
301
+ if (force || !newerDestFileExists(srcPath, destPath)) {
307
302
  hInfo = pathlib.parse(destPath);
308
303
  stub = hInfo.name;
309
304
  tamlCode = slurp(srcPath);
@@ -1,6 +1,6 @@
1
1
  # browser.coffee
2
2
 
3
- import {undef} from '@jdeighan/base-utils'
3
+ import {undef, defined, notdefined} from '@jdeighan/base-utils'
4
4
 
5
5
  audio = undef # audio context - create only when needed, then keep
6
6
 
@@ -27,15 +27,11 @@ export beep = (volume=100, freq=520, duration=200) =>
27
27
  export localStore = (key, value=undef) =>
28
28
  # --- if value is undef, returns the current value
29
29
 
30
- if typeof localStorage == 'undefined'
31
- return
32
- if value?
33
- localStorage.setItem key, JSON.stringify(value)
34
- return
35
- else
36
- value = localStorage.getItem(key)
37
- if value?
38
- return JSON.parse(localStorage.getItem(key))
30
+ if (typeof localStorage != 'undefined')
31
+ if defined(value)
32
+ localStorage.setItem key, JSON.stringify(value)
39
33
  else
40
- return undef
41
-
34
+ value = localStorage.getItem(key)
35
+ if defined(value)
36
+ return JSON.parse(localStorage.getItem(key))
37
+ return undef
package/src/browser.js CHANGED
@@ -3,7 +3,9 @@
3
3
  var audio;
4
4
 
5
5
  import {
6
- undef
6
+ undef,
7
+ defined,
8
+ notdefined
7
9
  } from '@jdeighan/base-utils';
8
10
 
9
11
  audio = undef; // audio context - create only when needed, then keep
@@ -30,17 +32,15 @@ export var beep = (volume = 100, freq = 520, duration = 200) => {
30
32
  // ---------------------------------------------------------------------------
31
33
  export var localStore = (key, value = undef) => {
32
34
  // --- if value is undef, returns the current value
33
- if (typeof localStorage === 'undefined') {
34
- return;
35
- }
36
- if (value != null) {
37
- localStorage.setItem(key, JSON.stringify(value));
38
- } else {
39
- value = localStorage.getItem(key);
40
- if (value != null) {
41
- return JSON.parse(localStorage.getItem(key));
35
+ if (typeof localStorage !== 'undefined') {
36
+ if (defined(value)) {
37
+ localStorage.setItem(key, JSON.stringify(value));
42
38
  } else {
43
- return undef;
39
+ value = localStorage.getItem(key);
40
+ if (defined(value)) {
41
+ return JSON.parse(localStorage.getItem(key));
42
+ }
44
43
  }
45
44
  }
45
+ return undef;
46
46
  };
package/src/fs.coffee CHANGED
@@ -7,8 +7,9 @@ import readline from 'readline'
7
7
  import NReadLines from 'n-readlines'
8
8
 
9
9
  import {
10
- undef, pass, defined, rtrim, isEmpty, nonEmpty, getOptions,
11
- isString, isArray, isHash, isRegExp, isFunction, OL, toBlock,
10
+ undef, pass, defined, notdefined, rtrim, isEmpty, nonEmpty,
11
+ isString, isArray, isHash, isRegExp, isFunction,
12
+ OL, toBlock, getOptions,
12
13
  } from '@jdeighan/base-utils'
13
14
  import {assert, croak} from '@jdeighan/base-utils/exceptions'
14
15
  import {LOG, LOGVALUE} from '@jdeighan/base-utils/log'
@@ -190,7 +191,7 @@ export forEachSetOfBlocks = (filepath, func,
190
191
  if (result == true)
191
192
  earlyExit = true
192
193
  return true
193
- else if result?
194
+ else if defined(result)
194
195
  croak "forEachSetOfBlocks() - callback returned '#{result}'"
195
196
  lBlocks = []
196
197
  firstLineNum = lineNum+1
@@ -241,17 +242,13 @@ export barf = (filepath, contents) =>
241
242
  # ---------------------------------------------------------------------------
242
243
  # withExt - change file extention in a file name
243
244
 
244
- export withExt = (path, newExt, hOptions={}) =>
245
- # --- Valid options:
246
- # removeLeadingUnderScore - boolean
245
+ export withExt = (path, newExt) =>
247
246
 
248
247
  assert newExt, "withExt(): No newExt provided"
249
248
  if newExt.indexOf('.') != 0
250
249
  newExt = '.' + newExt
251
250
 
252
251
  {dir, name, ext} = pathlib.parse(path)
253
- if hOptions.removeLeadingUnderScore && (name.indexOf('_')==0)
254
- name = name.substr(1)
255
252
  return mkpath(dir, "#{name}#{newExt}")
256
253
 
257
254
  # ---------------------------------------------------------------------------
@@ -260,12 +257,12 @@ export withExt = (path, newExt, hOptions={}) =>
260
257
  export removeFileWithExt = (path, newExt, hOptions={}) =>
261
258
  # --- Valid options:
262
259
  # doLog
263
- # removeLeadingUnderScore
264
260
 
265
- fullpath = withExt(path, newExt, hOptions)
261
+ {doLog} = getOptions(hOptions)
262
+ fullpath = withExt(path, newExt)
266
263
  try
267
264
  fs.unlinkSync fullpath
268
- if hOptions.doLog
265
+ if doLog
269
266
  LOG " unlink #{filename}"
270
267
  success = true
271
268
  catch err
@@ -319,7 +316,7 @@ export forEachFile = (dir, cb, filt=undef, level=0) =>
319
316
  if ent.isDirectory()
320
317
  lSubDirectories.push ent
321
318
  else if ent.isFile()
322
- if ! filt?
319
+ if notdefined(filt)
323
320
  cb(ent.name, dir, level)
324
321
  else if isRegExp(filt)
325
322
  if ent.name.match(filt)
@@ -394,7 +391,7 @@ export allPathsTo = (fname, searchDir) =>
394
391
  if ! searchDir
395
392
  searchDir = process.cwd()
396
393
  path = pathTo(fname, searchDir, {direction: "up"})
397
- if path?
394
+ if defined(path)
398
395
  lPaths = [path] # --- build an array of paths
399
396
  # --- search upward for files, but return ordered top down
400
397
  while (h = pathlib.parse(path)) \
package/src/fs.js CHANGED
@@ -16,17 +16,18 @@ import {
16
16
  undef,
17
17
  pass,
18
18
  defined,
19
+ notdefined,
19
20
  rtrim,
20
21
  isEmpty,
21
22
  nonEmpty,
22
- getOptions,
23
23
  isString,
24
24
  isArray,
25
25
  isHash,
26
26
  isRegExp,
27
27
  isFunction,
28
28
  OL,
29
- toBlock
29
+ toBlock,
30
+ getOptions
30
31
  } from '@jdeighan/base-utils';
31
32
 
32
33
  import {
@@ -226,7 +227,7 @@ export var forEachSetOfBlocks = (filepath, func, block_regexp = /^-{16,}$/, set_
226
227
  if (result === true) {
227
228
  earlyExit = true;
228
229
  return true;
229
- } else if (result != null) {
230
+ } else if (defined(result)) {
230
231
  croak(`forEachSetOfBlocks() - callback returned '${result}'`);
231
232
  }
232
233
  }
@@ -283,32 +284,27 @@ export var barf = (filepath, contents) => {
283
284
 
284
285
  // ---------------------------------------------------------------------------
285
286
  // withExt - change file extention in a file name
286
- export var withExt = (path, newExt, hOptions = {}) => {
287
+ export var withExt = (path, newExt) => {
287
288
  var dir, ext, name;
288
- // --- Valid options:
289
- // removeLeadingUnderScore - boolean
290
289
  assert(newExt, "withExt(): No newExt provided");
291
290
  if (newExt.indexOf('.') !== 0) {
292
291
  newExt = '.' + newExt;
293
292
  }
294
293
  ({dir, name, ext} = pathlib.parse(path));
295
- if (hOptions.removeLeadingUnderScore && (name.indexOf('_') === 0)) {
296
- name = name.substr(1);
297
- }
298
294
  return mkpath(dir, `${name}${newExt}`);
299
295
  };
300
296
 
301
297
  // ---------------------------------------------------------------------------
302
298
  // removeFileWithExt - remove file with different ext
303
299
  export var removeFileWithExt = (path, newExt, hOptions = {}) => {
304
- var err, fullpath, success;
300
+ var doLog, err, fullpath, success;
305
301
  // --- Valid options:
306
302
  // doLog
307
- // removeLeadingUnderScore
308
- fullpath = withExt(path, newExt, hOptions);
303
+ ({doLog} = getOptions(hOptions));
304
+ fullpath = withExt(path, newExt);
309
305
  try {
310
306
  fs.unlinkSync(fullpath);
311
- if (hOptions.doLog) {
307
+ if (doLog) {
312
308
  LOG(` unlink ${filename}`);
313
309
  }
314
310
  success = true;
@@ -371,7 +367,7 @@ export var forEachFile = (dir, cb, filt = undef, level = 0) => {
371
367
  if (ent.isDirectory()) {
372
368
  lSubDirectories.push(ent);
373
369
  } else if (ent.isFile()) {
374
- if (filt == null) {
370
+ if (notdefined(filt)) {
375
371
  cb(ent.name, dir, level);
376
372
  } else if (isRegExp(filt)) {
377
373
  if (ent.name.match(filt)) {
@@ -466,7 +462,7 @@ export var allPathsTo = (fname, searchDir) => {
466
462
  path = pathTo(fname, searchDir, {
467
463
  direction: "up"
468
464
  });
469
- if (path != null) {
465
+ if (defined(path)) {
470
466
  lPaths = [path]; // --- build an array of paths
471
467
  // --- search upward for files, but return ordered top down
472
468
  while ((h = pathlib.parse(path)) && (path = pathTo(fname, pathlib.resolve(h.dir, '..'), {