@jdeighan/coffee-utils 4.1.28 → 4.1.32
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +2 -1
- package/src/DataStores.coffee +165 -0
- package/src/DataStores.js +204 -0
- package/src/coffee_utils.coffee +21 -0
- package/src/coffee_utils.js +25 -0
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@jdeighan/coffee-utils",
|
3
3
|
"type": "module",
|
4
|
-
"version": "4.1.
|
4
|
+
"version": "4.1.32",
|
5
5
|
"description": "A set of utility functions for CoffeeScript",
|
6
6
|
"main": "coffee_utils.js",
|
7
7
|
"exports": {
|
@@ -16,6 +16,7 @@
|
|
16
16
|
"./debug": "./src/debug_utils.js",
|
17
17
|
"./svelte": "./src/svelte_utils.js",
|
18
18
|
"./test": "./src/UnitTester.js",
|
19
|
+
"./store": "./src/DataStores.js",
|
19
20
|
"./package.json": "./package.json"
|
20
21
|
},
|
21
22
|
"engines": {
|
@@ -0,0 +1,165 @@
|
|
1
|
+
# DataStores.coffee
|
2
|
+
|
3
|
+
import pathlib from 'path'
|
4
|
+
import yaml from 'js-yaml'
|
5
|
+
import {writable, readable, get} from 'svelte/store'
|
6
|
+
|
7
|
+
import {
|
8
|
+
assert, undef, pass, error, localStore, isEmpty,
|
9
|
+
} from '@jdeighan/coffee-utils'
|
10
|
+
import {log} from '@jdeighan/coffee-utils/log'
|
11
|
+
import {
|
12
|
+
withExt, slurp, barf, newerDestFileExists,
|
13
|
+
} from '@jdeighan/coffee-utils/fs'
|
14
|
+
|
15
|
+
# ---------------------------------------------------------------------------
|
16
|
+
|
17
|
+
export class WritableDataStore
|
18
|
+
|
19
|
+
constructor: (value=undef) ->
|
20
|
+
@store = writable value
|
21
|
+
|
22
|
+
subscribe: (callback) ->
|
23
|
+
return @store.subscribe(callback)
|
24
|
+
|
25
|
+
set: (value) ->
|
26
|
+
@store.set(value)
|
27
|
+
|
28
|
+
update: (func) ->
|
29
|
+
@store.update(func)
|
30
|
+
|
31
|
+
# ---------------------------------------------------------------------------
|
32
|
+
|
33
|
+
export class LocalStorageDataStore extends WritableDataStore
|
34
|
+
|
35
|
+
constructor: (@masterKey, defValue=undef) ->
|
36
|
+
|
37
|
+
# --- CoffeeScript forces us to call super first
|
38
|
+
# so we can't get the localStorage value first
|
39
|
+
super defValue
|
40
|
+
value = localStore(@masterKey)
|
41
|
+
if value?
|
42
|
+
@set value
|
43
|
+
|
44
|
+
# --- I'm assuming that when update() is called,
|
45
|
+
# set() will also be called
|
46
|
+
|
47
|
+
set: (value) ->
|
48
|
+
if ! value?
|
49
|
+
error "LocalStorageStore.set(): cannont set to undef"
|
50
|
+
super value
|
51
|
+
localStore @masterKey, value
|
52
|
+
|
53
|
+
update: (func) ->
|
54
|
+
super func
|
55
|
+
localStore @masterKey, get(@store)
|
56
|
+
|
57
|
+
# ---------------------------------------------------------------------------
|
58
|
+
|
59
|
+
export class PropsDataStore extends LocalStorageDataStore
|
60
|
+
|
61
|
+
constructor: (masterKey) ->
|
62
|
+
super masterKey, {}
|
63
|
+
|
64
|
+
setProp: (name, value) ->
|
65
|
+
if ! name?
|
66
|
+
error "PropStore.setProp(): empty key"
|
67
|
+
@update (hPrefs) ->
|
68
|
+
hPrefs[name] = value
|
69
|
+
return hPrefs
|
70
|
+
|
71
|
+
# ---------------------------------------------------------------------------
|
72
|
+
|
73
|
+
export class ReadableDataStore
|
74
|
+
|
75
|
+
constructor: () ->
|
76
|
+
@store = readable null, (set) ->
|
77
|
+
@setter = set # store the setter function
|
78
|
+
@start() # call your start() method
|
79
|
+
return () => @stop() # return function capable of stopping
|
80
|
+
|
81
|
+
subscribe: (callback) ->
|
82
|
+
return @store.subscribe(callback)
|
83
|
+
|
84
|
+
start: () ->
|
85
|
+
pass
|
86
|
+
|
87
|
+
stop: () ->
|
88
|
+
pass
|
89
|
+
|
90
|
+
# ---------------------------------------------------------------------------
|
91
|
+
|
92
|
+
export class DateTimeDataStore extends ReadableDataStore
|
93
|
+
|
94
|
+
start: () ->
|
95
|
+
# --- We need to store this interval for use in stop() later
|
96
|
+
@interval = setInterval(() ->
|
97
|
+
@setter new Date()
|
98
|
+
, 1000)
|
99
|
+
|
100
|
+
stop: () ->
|
101
|
+
clearInterval @interval
|
102
|
+
|
103
|
+
# ---------------------------------------------------------------------------
|
104
|
+
|
105
|
+
export class MousePosDataStore extends ReadableDataStore
|
106
|
+
|
107
|
+
start: () ->
|
108
|
+
# --- We need to store this handler for use in stop() later
|
109
|
+
@mouseMoveHandler = (e) ->
|
110
|
+
@setter {
|
111
|
+
x: e.clientX,
|
112
|
+
y: e.clientY,
|
113
|
+
}
|
114
|
+
document.body.addEventListener('mousemove', @mouseMoveHandler)
|
115
|
+
|
116
|
+
stop: () ->
|
117
|
+
document.body.removeEventListener('mousemove', @mouseMoveHandler)
|
118
|
+
|
119
|
+
# ---------------------------------------------------------------------------
|
120
|
+
|
121
|
+
export class TAMLDataStore extends WritableDataStore
|
122
|
+
|
123
|
+
constructor: (str) ->
|
124
|
+
|
125
|
+
super taml(str)
|
126
|
+
|
127
|
+
# ---------------------------------------------------------------------------
|
128
|
+
# UTILITIES
|
129
|
+
# ---------------------------------------------------------------------------
|
130
|
+
|
131
|
+
export taml = (text) ->
|
132
|
+
|
133
|
+
if ! text?
|
134
|
+
return undef
|
135
|
+
return yaml.load(untabify(text, 1), {skipInvalid: true})
|
136
|
+
|
137
|
+
# ---------------------------------------------------------------------------
|
138
|
+
|
139
|
+
export brewTamlStr = (code, stub) ->
|
140
|
+
|
141
|
+
return """
|
142
|
+
import {TAMLDataStore} from '@jdeighan/starbucks/stores';
|
143
|
+
|
144
|
+
export let #{stub} = new TAMLDataStore(`#{code}`);
|
145
|
+
"""
|
146
|
+
|
147
|
+
# ---------------------------------------------------------------------------
|
148
|
+
|
149
|
+
export brewTamlFile = (srcPath, destPath=undef, hOptions={}) ->
|
150
|
+
# --- taml => js
|
151
|
+
# Valid Options:
|
152
|
+
# force
|
153
|
+
|
154
|
+
if ! destPath?
|
155
|
+
destPath = withExt(srcPath, '.js', {removeLeadingUnderScore:true})
|
156
|
+
if hOptions.force || ! newerDestFileExists(srcPath, destPath)
|
157
|
+
hInfo = pathlib.parse(destPath)
|
158
|
+
stub = hInfo.name
|
159
|
+
|
160
|
+
tamlCode = slurp(srcPath)
|
161
|
+
jsCode = brewTamlStr(tamlCode, stub)
|
162
|
+
barf destPath, jsCode
|
163
|
+
return
|
164
|
+
|
165
|
+
# ---------------------------------------------------------------------------
|
@@ -0,0 +1,204 @@
|
|
1
|
+
// Generated by CoffeeScript 2.6.1
|
2
|
+
// DataStores.coffee
|
3
|
+
import pathlib from 'path';
|
4
|
+
|
5
|
+
import yaml from 'js-yaml';
|
6
|
+
|
7
|
+
import {
|
8
|
+
writable,
|
9
|
+
readable,
|
10
|
+
get
|
11
|
+
} from 'svelte/store';
|
12
|
+
|
13
|
+
import {
|
14
|
+
assert,
|
15
|
+
undef,
|
16
|
+
pass,
|
17
|
+
error,
|
18
|
+
localStore,
|
19
|
+
isEmpty
|
20
|
+
} from '@jdeighan/coffee-utils';
|
21
|
+
|
22
|
+
import {
|
23
|
+
log
|
24
|
+
} from '@jdeighan/coffee-utils/log';
|
25
|
+
|
26
|
+
import {
|
27
|
+
withExt,
|
28
|
+
slurp,
|
29
|
+
barf,
|
30
|
+
newerDestFileExists
|
31
|
+
} from '@jdeighan/coffee-utils/fs';
|
32
|
+
|
33
|
+
// ---------------------------------------------------------------------------
|
34
|
+
export var WritableDataStore = class WritableDataStore {
|
35
|
+
constructor(value = undef) {
|
36
|
+
this.store = writable(value);
|
37
|
+
}
|
38
|
+
|
39
|
+
subscribe(callback) {
|
40
|
+
return this.store.subscribe(callback);
|
41
|
+
}
|
42
|
+
|
43
|
+
set(value) {
|
44
|
+
return this.store.set(value);
|
45
|
+
}
|
46
|
+
|
47
|
+
update(func) {
|
48
|
+
return this.store.update(func);
|
49
|
+
}
|
50
|
+
|
51
|
+
};
|
52
|
+
|
53
|
+
// ---------------------------------------------------------------------------
|
54
|
+
export var LocalStorageDataStore = class LocalStorageDataStore extends WritableDataStore {
|
55
|
+
constructor(masterKey1, defValue = undef) {
|
56
|
+
var value;
|
57
|
+
super(defValue);
|
58
|
+
this.masterKey = masterKey1;
|
59
|
+
value = localStore(this.masterKey);
|
60
|
+
if (value != null) {
|
61
|
+
this.set(value);
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
// --- I'm assuming that when update() is called,
|
66
|
+
// set() will also be called
|
67
|
+
set(value) {
|
68
|
+
if (value == null) {
|
69
|
+
error("LocalStorageStore.set(): cannont set to undef");
|
70
|
+
}
|
71
|
+
super.set(value);
|
72
|
+
return localStore(this.masterKey, value);
|
73
|
+
}
|
74
|
+
|
75
|
+
update(func) {
|
76
|
+
super.update(func);
|
77
|
+
return localStore(this.masterKey, get(this.store));
|
78
|
+
}
|
79
|
+
|
80
|
+
};
|
81
|
+
|
82
|
+
// ---------------------------------------------------------------------------
|
83
|
+
export var PropsDataStore = class PropsDataStore extends LocalStorageDataStore {
|
84
|
+
constructor(masterKey) {
|
85
|
+
super(masterKey, {});
|
86
|
+
}
|
87
|
+
|
88
|
+
setProp(name, value) {
|
89
|
+
if (name == null) {
|
90
|
+
error("PropStore.setProp(): empty key");
|
91
|
+
}
|
92
|
+
return this.update(function(hPrefs) {
|
93
|
+
hPrefs[name] = value;
|
94
|
+
return hPrefs;
|
95
|
+
});
|
96
|
+
}
|
97
|
+
|
98
|
+
};
|
99
|
+
|
100
|
+
// ---------------------------------------------------------------------------
|
101
|
+
export var ReadableDataStore = class ReadableDataStore {
|
102
|
+
constructor() {
|
103
|
+
this.store = readable(null, function(set) {
|
104
|
+
this.setter = set; // store the setter function
|
105
|
+
this.start(); // call your start() method
|
106
|
+
return () => {
|
107
|
+
return this.stop(); // return function capable of stopping
|
108
|
+
};
|
109
|
+
});
|
110
|
+
}
|
111
|
+
|
112
|
+
subscribe(callback) {
|
113
|
+
return this.store.subscribe(callback);
|
114
|
+
}
|
115
|
+
|
116
|
+
start() {
|
117
|
+
return pass;
|
118
|
+
}
|
119
|
+
|
120
|
+
stop() {
|
121
|
+
return pass;
|
122
|
+
}
|
123
|
+
|
124
|
+
};
|
125
|
+
|
126
|
+
// ---------------------------------------------------------------------------
|
127
|
+
export var DateTimeDataStore = class DateTimeDataStore extends ReadableDataStore {
|
128
|
+
start() {
|
129
|
+
// --- We need to store this interval for use in stop() later
|
130
|
+
return this.interval = setInterval(function() {
|
131
|
+
return this.setter(new Date(), 1000);
|
132
|
+
});
|
133
|
+
}
|
134
|
+
|
135
|
+
stop() {
|
136
|
+
return clearInterval(this.interval);
|
137
|
+
}
|
138
|
+
|
139
|
+
};
|
140
|
+
|
141
|
+
// ---------------------------------------------------------------------------
|
142
|
+
export var MousePosDataStore = class MousePosDataStore extends ReadableDataStore {
|
143
|
+
start() {
|
144
|
+
// --- We need to store this handler for use in stop() later
|
145
|
+
this.mouseMoveHandler = function(e) {
|
146
|
+
return this.setter({
|
147
|
+
x: e.clientX,
|
148
|
+
y: e.clientY
|
149
|
+
});
|
150
|
+
};
|
151
|
+
return document.body.addEventListener('mousemove', this.mouseMoveHandler);
|
152
|
+
}
|
153
|
+
|
154
|
+
stop() {
|
155
|
+
return document.body.removeEventListener('mousemove', this.mouseMoveHandler);
|
156
|
+
}
|
157
|
+
|
158
|
+
};
|
159
|
+
|
160
|
+
// ---------------------------------------------------------------------------
|
161
|
+
export var TAMLDataStore = class TAMLDataStore extends WritableDataStore {
|
162
|
+
constructor(str) {
|
163
|
+
super(taml(str));
|
164
|
+
}
|
165
|
+
|
166
|
+
};
|
167
|
+
|
168
|
+
// ---------------------------------------------------------------------------
|
169
|
+
// UTILITIES
|
170
|
+
// ---------------------------------------------------------------------------
|
171
|
+
export var taml = function(text) {
|
172
|
+
if (text == null) {
|
173
|
+
return undef;
|
174
|
+
}
|
175
|
+
return yaml.load(untabify(text, 1), {
|
176
|
+
skipInvalid: true
|
177
|
+
});
|
178
|
+
};
|
179
|
+
|
180
|
+
// ---------------------------------------------------------------------------
|
181
|
+
export var brewTamlStr = function(code, stub) {
|
182
|
+
return `import {TAMLDataStore} from '@jdeighan/starbucks/stores';
|
183
|
+
|
184
|
+
export let ${stub} = new TAMLDataStore(\`${code}\`);`;
|
185
|
+
};
|
186
|
+
|
187
|
+
// ---------------------------------------------------------------------------
|
188
|
+
export var brewTamlFile = function(srcPath, destPath = undef, hOptions = {}) {
|
189
|
+
var hInfo, jsCode, stub, tamlCode;
|
190
|
+
if (destPath == null) {
|
191
|
+
destPath = withExt(srcPath, '.js', {
|
192
|
+
removeLeadingUnderScore: true
|
193
|
+
});
|
194
|
+
}
|
195
|
+
if (hOptions.force || !newerDestFileExists(srcPath, destPath)) {
|
196
|
+
hInfo = pathlib.parse(destPath);
|
197
|
+
stub = hInfo.name;
|
198
|
+
tamlCode = slurp(srcPath);
|
199
|
+
jsCode = brewTamlStr(tamlCode, stub);
|
200
|
+
barf(destPath, jsCode);
|
201
|
+
}
|
202
|
+
};
|
203
|
+
|
204
|
+
// ---------------------------------------------------------------------------
|
package/src/coffee_utils.coffee
CHANGED
@@ -381,3 +381,24 @@ export envVarsWithPrefix = (prefix, hOptions={}) ->
|
|
381
381
|
|
382
382
|
# ---------------------------------------------------------------------------
|
383
383
|
|
384
|
+
export getTimeStr = (date=undef) ->
|
385
|
+
|
386
|
+
if date == undef
|
387
|
+
date = new Date()
|
388
|
+
return date.toLocaleTimeString('en-US')
|
389
|
+
|
390
|
+
# ---------------------------------------------------------------------------
|
391
|
+
|
392
|
+
export getDateStr = (date=undef) ->
|
393
|
+
|
394
|
+
if date == undef
|
395
|
+
date = new Date()
|
396
|
+
return date.toLocaleDateString('en-US')
|
397
|
+
|
398
|
+
# ---------------------------------------------------------------------------
|
399
|
+
|
400
|
+
export strcat = (lItems...) ->
|
401
|
+
str = ''
|
402
|
+
for item in lItems
|
403
|
+
str += item.toString()
|
404
|
+
return str
|
package/src/coffee_utils.js
CHANGED
@@ -417,3 +417,28 @@ export var envVarsWithPrefix = function(prefix, hOptions = {}) {
|
|
417
417
|
};
|
418
418
|
|
419
419
|
// ---------------------------------------------------------------------------
|
420
|
+
export var getTimeStr = function(date = undef) {
|
421
|
+
if (date === undef) {
|
422
|
+
date = new Date();
|
423
|
+
}
|
424
|
+
return date.toLocaleTimeString('en-US');
|
425
|
+
};
|
426
|
+
|
427
|
+
// ---------------------------------------------------------------------------
|
428
|
+
export var getDateStr = function(date = undef) {
|
429
|
+
if (date === undef) {
|
430
|
+
date = new Date();
|
431
|
+
}
|
432
|
+
return date.toLocaleDateString('en-US');
|
433
|
+
};
|
434
|
+
|
435
|
+
// ---------------------------------------------------------------------------
|
436
|
+
export var strcat = function(...lItems) {
|
437
|
+
var i, item, len, str;
|
438
|
+
str = '';
|
439
|
+
for (i = 0, len = lItems.length; i < len; i++) {
|
440
|
+
item = lItems[i];
|
441
|
+
str += item.toString();
|
442
|
+
}
|
443
|
+
return str;
|
444
|
+
};
|