@jdeighan/coffee-utils 13.0.12 → 13.0.14
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 +3 -3
- package/src/DataStores.coffee +90 -77
- package/src/DataStores.js +111 -97
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@jdeighan/coffee-utils",
|
3
3
|
"type": "module",
|
4
|
-
"version": "13.0.
|
4
|
+
"version": "13.0.14",
|
5
5
|
"description": "A set of utility functions for CoffeeScript",
|
6
6
|
"main": "coffee_utils.js",
|
7
7
|
"exports": {
|
@@ -50,13 +50,13 @@
|
|
50
50
|
},
|
51
51
|
"homepage": "https://github.com/johndeighan/coffee-utils#readme",
|
52
52
|
"dependencies": {
|
53
|
-
"@jdeighan/base-utils": "^2.0.
|
53
|
+
"@jdeighan/base-utils": "^2.0.9",
|
54
54
|
"cross-env": "^7.0.3",
|
55
55
|
"n-readlines": "^1.0.1",
|
56
56
|
"readline-sync": "^1.4.10",
|
57
57
|
"svelte": "^3.55.0"
|
58
58
|
},
|
59
59
|
"devDependencies": {
|
60
|
-
"@jdeighan/unit-tester": "^3.0.
|
60
|
+
"@jdeighan/unit-tester": "^3.0.9"
|
61
61
|
}
|
62
62
|
}
|
package/src/DataStores.coffee
CHANGED
@@ -13,97 +13,37 @@ import {
|
|
13
13
|
|
14
14
|
# ---------------------------------------------------------------------------
|
15
15
|
|
16
|
-
export class
|
17
|
-
|
18
|
-
constructor: (value=undef) ->
|
19
|
-
@store = writable value
|
20
|
-
|
21
|
-
subscribe: (func) ->
|
22
|
-
return @store.subscribe(func)
|
23
|
-
|
24
|
-
set: (value) ->
|
25
|
-
@store.set(value)
|
26
|
-
|
27
|
-
update: (func) ->
|
28
|
-
@store.update(func)
|
29
|
-
|
30
|
-
# ---------------------------------------------------------------------------
|
31
|
-
|
32
|
-
export class BaseDataStore
|
16
|
+
export class StaticDataStore
|
33
17
|
|
34
|
-
constructor: (
|
35
|
-
@
|
18
|
+
constructor: (value) ->
|
19
|
+
@value = value
|
36
20
|
|
37
|
-
subscribe: (
|
38
|
-
|
39
|
-
@lSubscribers.push func
|
21
|
+
subscribe: (cbFunc) ->
|
22
|
+
cbFunc @value
|
40
23
|
return () ->
|
41
|
-
|
42
|
-
@lSubscribers.splice pos, 1
|
24
|
+
pass()
|
43
25
|
|
44
26
|
set: (val) ->
|
45
|
-
|
46
|
-
@alertSubscribers()
|
47
|
-
return
|
27
|
+
croak "Can't set() a StaticDataStore"
|
48
28
|
|
49
29
|
update: (func) ->
|
50
|
-
|
51
|
-
@alertSubscribers()
|
52
|
-
return
|
53
|
-
|
54
|
-
alertSubscribers: () ->
|
55
|
-
for func in @lSubscribers
|
56
|
-
func @value
|
57
|
-
return
|
30
|
+
croak "Can't update() a StaticDataStore"
|
58
31
|
|
59
32
|
# ---------------------------------------------------------------------------
|
60
33
|
|
61
|
-
export class
|
62
|
-
# --- implement without using svelte's readable or writable
|
63
|
-
|
64
|
-
constructor: () ->
|
65
|
-
@lToDos = []
|
66
|
-
@lSubscribers = [] # --- array of callback functions
|
67
|
-
|
68
|
-
subscribe: (cbFunc) ->
|
69
|
-
cbFunc(@lToDos)
|
70
|
-
@lSubscribers.push cbFunc
|
71
|
-
return () ->
|
72
|
-
index = @lSubscribers.indexOf cbFunc
|
73
|
-
@lSubscribers.splice index, 1
|
74
|
-
|
75
|
-
alertSubscribers: () ->
|
76
|
-
for cbFunc in @lSubscribers
|
77
|
-
cbFunc(@lToDos)
|
78
|
-
return
|
34
|
+
export class WritableDataStore
|
79
35
|
|
80
|
-
|
81
|
-
|
82
|
-
for index in range(@lToDos.length)
|
83
|
-
if (@lToDos[index].text == name)
|
84
|
-
return index
|
85
|
-
return undef
|
36
|
+
constructor: (value=undef) ->
|
37
|
+
@store = writable value
|
86
38
|
|
87
|
-
|
88
|
-
|
89
|
-
@lToDos = []
|
90
|
-
@alertSubscribers()
|
91
|
-
return
|
39
|
+
subscribe: (func) ->
|
40
|
+
return @store.subscribe(func)
|
92
41
|
|
93
|
-
|
94
|
-
|
95
|
-
croak "Attempt to add duplicate #{name} todo"
|
96
|
-
@lToDos.push {
|
97
|
-
text: name
|
98
|
-
done: false
|
99
|
-
}
|
100
|
-
@alertSubscribers()
|
101
|
-
return
|
42
|
+
set: (value) ->
|
43
|
+
@store.set(value)
|
102
44
|
|
103
|
-
|
104
|
-
|
105
|
-
@lToDos.splice index, 1
|
106
|
-
return
|
45
|
+
update: (func) ->
|
46
|
+
@store.update(func)
|
107
47
|
|
108
48
|
# ---------------------------------------------------------------------------
|
109
49
|
|
@@ -201,6 +141,79 @@ export class TAMLDataStore extends WritableDataStore
|
|
201
141
|
|
202
142
|
super fromTAML(str)
|
203
143
|
|
144
|
+
# ---------------------------------------------------------------------------
|
145
|
+
# --- Mainly for better understanding, I've implemented data stores
|
146
|
+
# without using svelte's readable or writable data stores
|
147
|
+
|
148
|
+
export class BaseDataStore
|
149
|
+
|
150
|
+
constructor: (@value=undef) ->
|
151
|
+
@lSubscribers = []
|
152
|
+
|
153
|
+
subscribe: (cbFunc) ->
|
154
|
+
cbFunc @value
|
155
|
+
@lSubscribers.push cbFunc
|
156
|
+
return () ->
|
157
|
+
index = @lSubscribers.indexOf cbFunc
|
158
|
+
@lSubscribers.splice index, 1
|
159
|
+
|
160
|
+
set: (val) ->
|
161
|
+
@value = val
|
162
|
+
return
|
163
|
+
|
164
|
+
update: (func) ->
|
165
|
+
@value = func(@value)
|
166
|
+
@alertSubscribers()
|
167
|
+
return
|
168
|
+
|
169
|
+
alertSubscribers: () ->
|
170
|
+
for cbFunc in @lSubscribers
|
171
|
+
cbFunc @value
|
172
|
+
return
|
173
|
+
|
174
|
+
# ---------------------------------------------------------------------------
|
175
|
+
|
176
|
+
export class ToDoDataStore extends BaseDataStore
|
177
|
+
|
178
|
+
constructor: () ->
|
179
|
+
lToDos = [] # save local reference to make code easier to grok
|
180
|
+
super lToDos
|
181
|
+
@lToDos = lToDos # can't do this before calling super
|
182
|
+
|
183
|
+
set: (val) ->
|
184
|
+
croak "Don't use set()"
|
185
|
+
|
186
|
+
update: (func) ->
|
187
|
+
croak "Don't use update()"
|
188
|
+
|
189
|
+
find: (name) ->
|
190
|
+
# --- returns index
|
191
|
+
for index in range(@lToDos.length)
|
192
|
+
if (@lToDos[index].text == name)
|
193
|
+
return index
|
194
|
+
return undef
|
195
|
+
|
196
|
+
clear: () ->
|
197
|
+
# --- Don't set a new array. That would break our reference
|
198
|
+
@lToDos.splice 0, @lToDos.length
|
199
|
+
return
|
200
|
+
|
201
|
+
add: (name) ->
|
202
|
+
if defined(@find(name))
|
203
|
+
croak "Attempt to add duplicate #{name} todo"
|
204
|
+
@lToDos.push {
|
205
|
+
text: name
|
206
|
+
done: false
|
207
|
+
}
|
208
|
+
@alertSubscribers()
|
209
|
+
return
|
210
|
+
|
211
|
+
remove: (name) ->
|
212
|
+
index = @find(name)
|
213
|
+
@lToDos.splice index, 1
|
214
|
+
@alertSubscribers()
|
215
|
+
return
|
216
|
+
|
204
217
|
# ---------------------------------------------------------------------------
|
205
218
|
# UTILITIES
|
206
219
|
# ---------------------------------------------------------------------------
|
package/src/DataStores.js
CHANGED
@@ -35,124 +35,44 @@ import {
|
|
35
35
|
} from '@jdeighan/coffee-utils/fs';
|
36
36
|
|
37
37
|
// ---------------------------------------------------------------------------
|
38
|
-
export var
|
39
|
-
constructor(value
|
40
|
-
this.
|
41
|
-
}
|
42
|
-
|
43
|
-
subscribe(func) {
|
44
|
-
return this.store.subscribe(func);
|
45
|
-
}
|
46
|
-
|
47
|
-
set(value) {
|
48
|
-
return this.store.set(value);
|
38
|
+
export var StaticDataStore = class StaticDataStore {
|
39
|
+
constructor(value) {
|
40
|
+
this.value = value;
|
49
41
|
}
|
50
42
|
|
51
|
-
|
52
|
-
|
53
|
-
}
|
54
|
-
|
55
|
-
};
|
56
|
-
|
57
|
-
// ---------------------------------------------------------------------------
|
58
|
-
export var BaseDataStore = class BaseDataStore {
|
59
|
-
constructor(value1 = undef) {
|
60
|
-
this.value = value1;
|
61
|
-
this.lSubscribers = [];
|
62
|
-
}
|
63
|
-
|
64
|
-
subscribe(func) {
|
65
|
-
func(this.value);
|
66
|
-
this.lSubscribers.push(func);
|
43
|
+
subscribe(cbFunc) {
|
44
|
+
cbFunc(this.value);
|
67
45
|
return function() {
|
68
|
-
|
69
|
-
pos = this.lSubscribers.indexOf(func);
|
70
|
-
return this.lSubscribers.splice(pos, 1);
|
46
|
+
return pass();
|
71
47
|
};
|
72
48
|
}
|
73
49
|
|
74
50
|
set(val) {
|
75
|
-
|
76
|
-
this.alertSubscribers();
|
51
|
+
return croak("Can't set() a StaticDataStore");
|
77
52
|
}
|
78
53
|
|
79
54
|
update(func) {
|
80
|
-
|
81
|
-
this.alertSubscribers();
|
82
|
-
}
|
83
|
-
|
84
|
-
alertSubscribers() {
|
85
|
-
var func, i, len, ref;
|
86
|
-
ref = this.lSubscribers;
|
87
|
-
for (i = 0, len = ref.length; i < len; i++) {
|
88
|
-
func = ref[i];
|
89
|
-
func(this.value);
|
90
|
-
}
|
55
|
+
return croak("Can't update() a StaticDataStore");
|
91
56
|
}
|
92
57
|
|
93
58
|
};
|
94
59
|
|
95
60
|
// ---------------------------------------------------------------------------
|
96
|
-
export var
|
97
|
-
|
98
|
-
|
99
|
-
this.lToDos = [];
|
100
|
-
this.lSubscribers = []; // --- array of callback functions
|
101
|
-
}
|
102
|
-
|
103
|
-
subscribe(cbFunc) {
|
104
|
-
cbFunc(this.lToDos);
|
105
|
-
this.lSubscribers.push(cbFunc);
|
106
|
-
return function() {
|
107
|
-
var index;
|
108
|
-
index = this.lSubscribers.indexOf(cbFunc);
|
109
|
-
return this.lSubscribers.splice(index, 1);
|
110
|
-
};
|
111
|
-
}
|
112
|
-
|
113
|
-
alertSubscribers() {
|
114
|
-
var cbFunc, i, len, ref;
|
115
|
-
ref = this.lSubscribers;
|
116
|
-
for (i = 0, len = ref.length; i < len; i++) {
|
117
|
-
cbFunc = ref[i];
|
118
|
-
cbFunc(this.lToDos);
|
119
|
-
}
|
120
|
-
}
|
121
|
-
|
122
|
-
find(name) {
|
123
|
-
var i, index, len, ref;
|
124
|
-
ref = range(this.lToDos.length);
|
125
|
-
// --- returns index
|
126
|
-
for (i = 0, len = ref.length; i < len; i++) {
|
127
|
-
index = ref[i];
|
128
|
-
if (this.lToDos[index].text === name) {
|
129
|
-
return index;
|
130
|
-
}
|
131
|
-
}
|
132
|
-
return undef;
|
61
|
+
export var WritableDataStore = class WritableDataStore {
|
62
|
+
constructor(value = undef) {
|
63
|
+
this.store = writable(value);
|
133
64
|
}
|
134
65
|
|
135
|
-
|
136
|
-
|
137
|
-
this.lToDos = [];
|
138
|
-
this.alertSubscribers();
|
66
|
+
subscribe(func) {
|
67
|
+
return this.store.subscribe(func);
|
139
68
|
}
|
140
69
|
|
141
|
-
|
142
|
-
|
143
|
-
croak(`Attempt to add duplicate ${name} todo`);
|
144
|
-
}
|
145
|
-
this.lToDos.push({
|
146
|
-
text: name,
|
147
|
-
done: false
|
148
|
-
});
|
149
|
-
this.alertSubscribers();
|
70
|
+
set(value) {
|
71
|
+
return this.store.set(value);
|
150
72
|
}
|
151
73
|
|
152
|
-
|
153
|
-
|
154
|
-
index = this.find(name);
|
155
|
-
this.lToDos.splice(index, 1);
|
74
|
+
update(func) {
|
75
|
+
return this.store.update(func);
|
156
76
|
}
|
157
77
|
|
158
78
|
};
|
@@ -272,6 +192,100 @@ export var TAMLDataStore = class TAMLDataStore extends WritableDataStore {
|
|
272
192
|
|
273
193
|
};
|
274
194
|
|
195
|
+
// ---------------------------------------------------------------------------
|
196
|
+
// --- Mainly for better understanding, I've implemented data stores
|
197
|
+
// without using svelte's readable or writable data stores
|
198
|
+
export var BaseDataStore = class BaseDataStore {
|
199
|
+
constructor(value1 = undef) {
|
200
|
+
this.value = value1;
|
201
|
+
this.lSubscribers = [];
|
202
|
+
}
|
203
|
+
|
204
|
+
subscribe(cbFunc) {
|
205
|
+
cbFunc(this.value);
|
206
|
+
this.lSubscribers.push(cbFunc);
|
207
|
+
return function() {
|
208
|
+
var index;
|
209
|
+
index = this.lSubscribers.indexOf(cbFunc);
|
210
|
+
return this.lSubscribers.splice(index, 1);
|
211
|
+
};
|
212
|
+
}
|
213
|
+
|
214
|
+
set(val) {
|
215
|
+
this.value = val;
|
216
|
+
}
|
217
|
+
|
218
|
+
update(func) {
|
219
|
+
this.value = func(this.value);
|
220
|
+
this.alertSubscribers();
|
221
|
+
}
|
222
|
+
|
223
|
+
alertSubscribers() {
|
224
|
+
var cbFunc, i, len, ref;
|
225
|
+
ref = this.lSubscribers;
|
226
|
+
for (i = 0, len = ref.length; i < len; i++) {
|
227
|
+
cbFunc = ref[i];
|
228
|
+
cbFunc(this.value);
|
229
|
+
}
|
230
|
+
}
|
231
|
+
|
232
|
+
};
|
233
|
+
|
234
|
+
// ---------------------------------------------------------------------------
|
235
|
+
export var ToDoDataStore = class ToDoDataStore extends BaseDataStore {
|
236
|
+
constructor() {
|
237
|
+
var lToDos;
|
238
|
+
lToDos = []; // save local reference to make code easier to grok
|
239
|
+
super(lToDos);
|
240
|
+
this.lToDos = lToDos; // can't do this before calling super
|
241
|
+
}
|
242
|
+
|
243
|
+
set(val) {
|
244
|
+
return croak("Don't use set()");
|
245
|
+
}
|
246
|
+
|
247
|
+
update(func) {
|
248
|
+
return croak("Don't use update()");
|
249
|
+
}
|
250
|
+
|
251
|
+
find(name) {
|
252
|
+
var i, index, len, ref;
|
253
|
+
ref = range(this.lToDos.length);
|
254
|
+
// --- returns index
|
255
|
+
for (i = 0, len = ref.length; i < len; i++) {
|
256
|
+
index = ref[i];
|
257
|
+
if (this.lToDos[index].text === name) {
|
258
|
+
return index;
|
259
|
+
}
|
260
|
+
}
|
261
|
+
return undef;
|
262
|
+
}
|
263
|
+
|
264
|
+
clear() {
|
265
|
+
// --- Don't set a new array. That would break our reference
|
266
|
+
this.lToDos.splice(0, this.lToDos.length);
|
267
|
+
}
|
268
|
+
|
269
|
+
add(name) {
|
270
|
+
if (defined(this.find(name))) {
|
271
|
+
croak(`Attempt to add duplicate ${name} todo`);
|
272
|
+
}
|
273
|
+
this.lToDos.push({
|
274
|
+
text: name,
|
275
|
+
done: false
|
276
|
+
});
|
277
|
+
this.alertSubscribers();
|
278
|
+
}
|
279
|
+
|
280
|
+
remove(name) {
|
281
|
+
var index;
|
282
|
+
index = this.find(name);
|
283
|
+
this.lToDos.splice(index, 1);
|
284
|
+
this.alertSubscribers();
|
285
|
+
}
|
286
|
+
|
287
|
+
};
|
288
|
+
|
275
289
|
// ---------------------------------------------------------------------------
|
276
290
|
// UTILITIES
|
277
291
|
// ---------------------------------------------------------------------------
|