@jdeighan/coffee-utils 14.0.9 → 14.0.10
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +1 -1
- package/src/DataStores.coffee +23 -13
- package/src/DataStores.js +28 -33
- package/src/browser.coffee +8 -12
- package/src/browser.js +11 -11
- package/src/fs.coffee +6 -5
- package/src/fs.js +6 -5
package/package.json
CHANGED
package/src/DataStores.coffee
CHANGED
@@ -3,7 +3,9 @@
|
|
3
3
|
import pathlib from 'path'
|
4
4
|
import {writable, readable, get} from 'svelte/store'
|
5
5
|
|
6
|
-
import {
|
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
|
-
|
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
|
-
|
83
|
-
|
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
|
-
|
108
|
+
return
|
103
109
|
|
104
110
|
stop: () ->
|
105
|
-
|
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
|
-
|
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
|
237
|
-
destPath = withExt(srcPath, '.js'
|
238
|
-
|
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
|
-
|
74
|
+
this.store.set(value);
|
72
75
|
}
|
73
76
|
|
74
77
|
update(func) {
|
75
|
-
|
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
|
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
|
-
|
96
|
-
croak("LocalStorageStore.set(): cannont set to undef");
|
97
|
-
}
|
98
|
+
assert(defined(value), "set(): cannot set to undef");
|
98
99
|
super.set(value);
|
99
|
-
|
100
|
+
localStore(this.masterKey, value);
|
100
101
|
}
|
101
102
|
|
102
103
|
update(func) {
|
103
104
|
super.update(func);
|
104
|
-
|
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
|
-
|
117
|
-
|
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
|
-
|
152
|
+
this.interval = setInterval(function() {
|
158
153
|
return this.setter(new Date(), 1000);
|
159
154
|
});
|
160
155
|
}
|
161
156
|
|
162
157
|
stop() {
|
163
|
-
|
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
|
-
|
173
|
+
document.body.addEventListener('mousemove', this.mouseMoveHandler);
|
179
174
|
}
|
180
175
|
|
181
176
|
stop() {
|
182
|
-
|
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
|
-
|
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
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
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);
|
package/src/browser.coffee
CHANGED
@@ -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
|
31
|
-
|
32
|
-
|
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
|
-
|
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
|
34
|
-
|
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
|
-
|
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,
|
11
|
-
isString, isArray, isHash, isRegExp, isFunction,
|
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
|
@@ -319,7 +320,7 @@ export forEachFile = (dir, cb, filt=undef, level=0) =>
|
|
319
320
|
if ent.isDirectory()
|
320
321
|
lSubDirectories.push ent
|
321
322
|
else if ent.isFile()
|
322
|
-
if
|
323
|
+
if notdefined(filt)
|
323
324
|
cb(ent.name, dir, level)
|
324
325
|
else if isRegExp(filt)
|
325
326
|
if ent.name.match(filt)
|
@@ -394,7 +395,7 @@ export allPathsTo = (fname, searchDir) =>
|
|
394
395
|
if ! searchDir
|
395
396
|
searchDir = process.cwd()
|
396
397
|
path = pathTo(fname, searchDir, {direction: "up"})
|
397
|
-
if path
|
398
|
+
if defined(path)
|
398
399
|
lPaths = [path] # --- build an array of paths
|
399
400
|
# --- search upward for files, but return ordered top down
|
400
401
|
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
|
230
|
+
} else if (defined(result)) {
|
230
231
|
croak(`forEachSetOfBlocks() - callback returned '${result}'`);
|
231
232
|
}
|
232
233
|
}
|
@@ -371,7 +372,7 @@ export var forEachFile = (dir, cb, filt = undef, level = 0) => {
|
|
371
372
|
if (ent.isDirectory()) {
|
372
373
|
lSubDirectories.push(ent);
|
373
374
|
} else if (ent.isFile()) {
|
374
|
-
if (filt
|
375
|
+
if (notdefined(filt)) {
|
375
376
|
cb(ent.name, dir, level);
|
376
377
|
} else if (isRegExp(filt)) {
|
377
378
|
if (ent.name.match(filt)) {
|
@@ -466,7 +467,7 @@ export var allPathsTo = (fname, searchDir) => {
|
|
466
467
|
path = pathTo(fname, searchDir, {
|
467
468
|
direction: "up"
|
468
469
|
});
|
469
|
-
if (path
|
470
|
+
if (defined(path)) {
|
470
471
|
lPaths = [path]; // --- build an array of paths
|
471
472
|
// --- search upward for files, but return ordered top down
|
472
473
|
while ((h = pathlib.parse(path)) && (path = pathTo(fname, pathlib.resolve(h.dir, '..'), {
|