@jdeighan/coffee-utils 4.1.29 → 4.1.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "4.1.29",
4
+ "version": "4.1.33",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -16,6 +16,8 @@
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",
20
+ "./taml": "./src/taml.js",
19
21
  "./package.json": "./package.json"
20
22
  },
21
23
  "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
+ // ---------------------------------------------------------------------------
@@ -394,3 +394,11 @@ export getDateStr = (date=undef) ->
394
394
  if date == undef
395
395
  date = new Date()
396
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
@@ -431,3 +431,14 @@ export var getDateStr = function(date = undef) {
431
431
  }
432
432
  return date.toLocaleDateString('en-US');
433
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
+ };
@@ -0,0 +1,40 @@
1
+ # taml.coffee
2
+
3
+ import yaml from 'js-yaml'
4
+
5
+ import {
6
+ assert, undef, oneline, isString,
7
+ } from '@jdeighan/coffee-utils'
8
+ import {untabify, tabify} from '@jdeighan/coffee-utils/indent'
9
+ import {log, tamlStringify} from '@jdeighan/coffee-utils/log'
10
+ import {slurp} from '@jdeighan/coffee-utils/fs'
11
+ import {debug} from '@jdeighan/coffee-utils/debug'
12
+ import {firstLine} from '@jdeighan/coffee-utils/block'
13
+
14
+ # ---------------------------------------------------------------------------
15
+ # isTAML - is the string valid TAML?
16
+
17
+ export isTAML = (text) ->
18
+
19
+ return isString(text) && (firstLine(text).indexOf('---') == 0)
20
+
21
+ # ---------------------------------------------------------------------------
22
+ # taml - convert valid TAML string to a JavaScript value
23
+
24
+ export taml = (text) ->
25
+
26
+ debug "enter taml(#{oneline(text)})"
27
+ if ! text?
28
+ debug "return undef from taml() - text is not defined"
29
+ return undef
30
+ assert isTAML(text), "taml(): string #{oneline(text)} isn't TAML"
31
+ debug "return from taml()"
32
+ return yaml.load(untabify(text, 1), {skipInvalid: true})
33
+
34
+ # ---------------------------------------------------------------------------
35
+ # slurpTAML - read TAML from a file
36
+
37
+ export slurpTAML = (filepath) ->
38
+
39
+ contents = slurp(filepath)
40
+ return taml(contents)
package/src/taml.js ADDED
@@ -0,0 +1,61 @@
1
+ // Generated by CoffeeScript 2.6.1
2
+ // taml.coffee
3
+ import yaml from 'js-yaml';
4
+
5
+ import {
6
+ assert,
7
+ undef,
8
+ oneline,
9
+ isString
10
+ } from '@jdeighan/coffee-utils';
11
+
12
+ import {
13
+ untabify,
14
+ tabify
15
+ } from '@jdeighan/coffee-utils/indent';
16
+
17
+ import {
18
+ log,
19
+ tamlStringify
20
+ } from '@jdeighan/coffee-utils/log';
21
+
22
+ import {
23
+ slurp
24
+ } from '@jdeighan/coffee-utils/fs';
25
+
26
+ import {
27
+ debug
28
+ } from '@jdeighan/coffee-utils/debug';
29
+
30
+ import {
31
+ firstLine
32
+ } from '@jdeighan/coffee-utils/block';
33
+
34
+ // ---------------------------------------------------------------------------
35
+ // isTAML - is the string valid TAML?
36
+ export var isTAML = function(text) {
37
+ return isString(text) && (firstLine(text).indexOf('---') === 0);
38
+ };
39
+
40
+ // ---------------------------------------------------------------------------
41
+ // taml - convert valid TAML string to a JavaScript value
42
+ export var taml = function(text) {
43
+ debug(`enter taml(${oneline(text)})`);
44
+ if (text == null) {
45
+ debug("return undef from taml() - text is not defined");
46
+ return undef;
47
+ }
48
+ assert(isTAML(text), `taml(): string ${oneline(text)} isn't TAML`);
49
+ debug("return from taml()");
50
+ return yaml.load(untabify(text, 1), {
51
+ skipInvalid: true
52
+ });
53
+ };
54
+
55
+ // ---------------------------------------------------------------------------
56
+ // slurpTAML - read TAML from a file
57
+ export var slurpTAML = function(filepath) {
58
+ var contents;
59
+ contents = slurp(filepath);
60
+ return taml(contents);
61
+ };