@jdeighan/coffee-utils 4.1.27 → 4.1.31

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": "4.1.27",
4
+ "version": "4.1.31",
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,157 @@
1
+ # DataStores.coffee
2
+
3
+ import pathlib from 'path'
4
+ import {writable, readable, get} from 'svelte/store'
5
+
6
+ import {
7
+ assert, undef, pass, error, localStore, isEmpty,
8
+ } from '@jdeighan/coffee-utils'
9
+ import {log} from '@jdeighan/coffee-utils/log'
10
+ import {
11
+ withExt, slurp, barf, newerDestFileExists,
12
+ } from '@jdeighan/coffee-utils/fs'
13
+ import {isTAML, taml} from '@jdeighan/string-input/taml'
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 brewTamlStr = (code, stub) ->
132
+
133
+ return """
134
+ import {TAMLDataStore} from '@jdeighan/starbucks/stores';
135
+
136
+ export let #{stub} = new TAMLDataStore(`#{code}`);
137
+ """
138
+
139
+ # ---------------------------------------------------------------------------
140
+
141
+ export brewTamlFile = (srcPath, destPath=undef, hOptions={}) ->
142
+ # --- taml => js
143
+ # Valid Options:
144
+ # force
145
+
146
+ if ! destPath?
147
+ destPath = withExt(srcPath, '.js', {removeLeadingUnderScore:true})
148
+ if hOptions.force || ! newerDestFileExists(srcPath, destPath)
149
+ hInfo = pathlib.parse(destPath)
150
+ stub = hInfo.name
151
+
152
+ tamlCode = slurp(srcPath)
153
+ jsCode = brewTamlStr(tamlCode, stub)
154
+ barf destPath, jsCode
155
+ return
156
+
157
+ # ---------------------------------------------------------------------------
@@ -0,0 +1,197 @@
1
+ // Generated by CoffeeScript 2.6.1
2
+ // DataStores.coffee
3
+ import pathlib from 'path';
4
+
5
+ import {
6
+ writable,
7
+ readable,
8
+ get
9
+ } from 'svelte/store';
10
+
11
+ import {
12
+ assert,
13
+ undef,
14
+ pass,
15
+ error,
16
+ localStore,
17
+ isEmpty
18
+ } from '@jdeighan/coffee-utils';
19
+
20
+ import {
21
+ log
22
+ } from '@jdeighan/coffee-utils/log';
23
+
24
+ import {
25
+ withExt,
26
+ slurp,
27
+ barf,
28
+ newerDestFileExists
29
+ } from '@jdeighan/coffee-utils/fs';
30
+
31
+ import {
32
+ isTAML,
33
+ taml
34
+ } from '@jdeighan/string-input/taml';
35
+
36
+ // ---------------------------------------------------------------------------
37
+ export var WritableDataStore = class WritableDataStore {
38
+ constructor(value = undef) {
39
+ this.store = writable(value);
40
+ }
41
+
42
+ subscribe(callback) {
43
+ return this.store.subscribe(callback);
44
+ }
45
+
46
+ set(value) {
47
+ return this.store.set(value);
48
+ }
49
+
50
+ update(func) {
51
+ return this.store.update(func);
52
+ }
53
+
54
+ };
55
+
56
+ // ---------------------------------------------------------------------------
57
+ export var LocalStorageDataStore = class LocalStorageDataStore extends WritableDataStore {
58
+ constructor(masterKey1, defValue = undef) {
59
+ var value;
60
+ super(defValue);
61
+ this.masterKey = masterKey1;
62
+ value = localStore(this.masterKey);
63
+ if (value != null) {
64
+ this.set(value);
65
+ }
66
+ }
67
+
68
+ // --- I'm assuming that when update() is called,
69
+ // set() will also be called
70
+ set(value) {
71
+ if (value == null) {
72
+ error("LocalStorageStore.set(): cannont set to undef");
73
+ }
74
+ super.set(value);
75
+ return localStore(this.masterKey, value);
76
+ }
77
+
78
+ update(func) {
79
+ super.update(func);
80
+ return localStore(this.masterKey, get(this.store));
81
+ }
82
+
83
+ };
84
+
85
+ // ---------------------------------------------------------------------------
86
+ export var PropsDataStore = class PropsDataStore extends LocalStorageDataStore {
87
+ constructor(masterKey) {
88
+ super(masterKey, {});
89
+ }
90
+
91
+ setProp(name, value) {
92
+ if (name == null) {
93
+ error("PropStore.setProp(): empty key");
94
+ }
95
+ return this.update(function(hPrefs) {
96
+ hPrefs[name] = value;
97
+ return hPrefs;
98
+ });
99
+ }
100
+
101
+ };
102
+
103
+ // ---------------------------------------------------------------------------
104
+ export var ReadableDataStore = class ReadableDataStore {
105
+ constructor() {
106
+ this.store = readable(null, function(set) {
107
+ this.setter = set; // store the setter function
108
+ this.start(); // call your start() method
109
+ return () => {
110
+ return this.stop(); // return function capable of stopping
111
+ };
112
+ });
113
+ }
114
+
115
+ subscribe(callback) {
116
+ return this.store.subscribe(callback);
117
+ }
118
+
119
+ start() {
120
+ return pass;
121
+ }
122
+
123
+ stop() {
124
+ return pass;
125
+ }
126
+
127
+ };
128
+
129
+ // ---------------------------------------------------------------------------
130
+ export var DateTimeDataStore = class DateTimeDataStore extends ReadableDataStore {
131
+ start() {
132
+ // --- We need to store this interval for use in stop() later
133
+ return this.interval = setInterval(function() {
134
+ return this.setter(new Date(), 1000);
135
+ });
136
+ }
137
+
138
+ stop() {
139
+ return clearInterval(this.interval);
140
+ }
141
+
142
+ };
143
+
144
+ // ---------------------------------------------------------------------------
145
+ export var MousePosDataStore = class MousePosDataStore extends ReadableDataStore {
146
+ start() {
147
+ // --- We need to store this handler for use in stop() later
148
+ this.mouseMoveHandler = function(e) {
149
+ return this.setter({
150
+ x: e.clientX,
151
+ y: e.clientY
152
+ });
153
+ };
154
+ return document.body.addEventListener('mousemove', this.mouseMoveHandler);
155
+ }
156
+
157
+ stop() {
158
+ return document.body.removeEventListener('mousemove', this.mouseMoveHandler);
159
+ }
160
+
161
+ };
162
+
163
+ // ---------------------------------------------------------------------------
164
+ export var TAMLDataStore = class TAMLDataStore extends WritableDataStore {
165
+ constructor(str) {
166
+ super(taml(str));
167
+ }
168
+
169
+ };
170
+
171
+ // ---------------------------------------------------------------------------
172
+ // UTILITIES
173
+ // ---------------------------------------------------------------------------
174
+ export var brewTamlStr = function(code, stub) {
175
+ return `import {TAMLDataStore} from '@jdeighan/starbucks/stores';
176
+
177
+ export let ${stub} = new TAMLDataStore(\`${code}\`);`;
178
+ };
179
+
180
+ // ---------------------------------------------------------------------------
181
+ export var brewTamlFile = function(srcPath, destPath = undef, hOptions = {}) {
182
+ var hInfo, jsCode, stub, tamlCode;
183
+ if (destPath == null) {
184
+ destPath = withExt(srcPath, '.js', {
185
+ removeLeadingUnderScore: true
186
+ });
187
+ }
188
+ if (hOptions.force || !newerDestFileExists(srcPath, destPath)) {
189
+ hInfo = pathlib.parse(destPath);
190
+ stub = hInfo.name;
191
+ tamlCode = slurp(srcPath);
192
+ jsCode = brewTamlStr(tamlCode, stub);
193
+ barf(destPath, jsCode);
194
+ }
195
+ };
196
+
197
+ // ---------------------------------------------------------------------------
@@ -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
@@ -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
+ };
@@ -1,6 +1,7 @@
1
1
  # svelte_utils.coffee
2
2
 
3
- import {onDestroy} from 'svelte'
3
+ import {assert, isFunction} from '@jdeighan/coffee-utils'
4
+ import {log} from '@jdeighan/coffee-utils/log'
4
5
 
5
6
  # ---------------------------------------------------------------------------
6
7
  # svelteSourceCodeEsc - to display source code for a *.starbucks page
@@ -26,9 +27,15 @@ export svelteHtmlEsc = (str) ->
26
27
 
27
28
  # ---------------------------------------------------------------------------
28
29
 
29
- export onInterval = (func, secs) ->
30
+ export onInterval = (func, secs, doLog=false) ->
30
31
 
31
- interval = setInterval(func, Math.floor(1000 * secs))
32
+ assert isFunction(func), "onInterval(): 1st arg not a function"
33
+ ms = Math.floor(1000 * secs)
34
+ if doLog
35
+ log "calling func every #{ms} ms."
36
+ interval = setInterval(func, ms)
32
37
 
33
- onDestroy () ->
38
+ return () ->
39
+ if doLog
40
+ log "destroying interval timer"
34
41
  clearInterval interval
@@ -1,8 +1,13 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
2
  // svelte_utils.coffee
3
3
  import {
4
- onDestroy
5
- } from 'svelte';
4
+ assert,
5
+ isFunction
6
+ } from '@jdeighan/coffee-utils';
7
+
8
+ import {
9
+ log
10
+ } from '@jdeighan/coffee-utils/log';
6
11
 
7
12
  // ---------------------------------------------------------------------------
8
13
  // svelteSourceCodeEsc - to display source code for a *.starbucks page
@@ -17,10 +22,18 @@ export var svelteHtmlEsc = function(str) {
17
22
  };
18
23
 
19
24
  // ---------------------------------------------------------------------------
20
- export var onInterval = function(func, secs) {
21
- var interval;
22
- interval = setInterval(func, Math.floor(1000 * secs));
23
- return onDestroy(function() {
25
+ export var onInterval = function(func, secs, doLog = false) {
26
+ var interval, ms;
27
+ assert(isFunction(func), "onInterval(): 1st arg not a function");
28
+ ms = Math.floor(1000 * secs);
29
+ if (doLog) {
30
+ log(`calling func every ${ms} ms.`);
31
+ }
32
+ interval = setInterval(func, ms);
33
+ return function() {
34
+ if (doLog) {
35
+ log("destroying interval timer");
36
+ }
24
37
  return clearInterval(interval);
25
- });
38
+ };
26
39
  };