@jdeighan/coffee-utils 13.0.11 → 13.0.13

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": "13.0.11",
4
+ "version": "13.0.13",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -52,8 +52,6 @@
52
52
  "dependencies": {
53
53
  "@jdeighan/base-utils": "^2.0.8",
54
54
  "cross-env": "^7.0.3",
55
- "immer": "^9.0.16",
56
- "js-yaml": "^4.1.0",
57
55
  "n-readlines": "^1.0.1",
58
56
  "readline-sync": "^1.4.10",
59
57
  "svelte": "^3.55.0"
@@ -4,114 +4,46 @@ import pathlib from 'path'
4
4
  import {writable, readable, get} from 'svelte/store'
5
5
 
6
6
  import {assert, croak} from '@jdeighan/base-utils'
7
- import {undef, pass} from '@jdeighan/coffee-utils'
7
+ import {fromTAML} from '@jdeighan/base-utils/taml'
8
+ import {undef, pass, range} from '@jdeighan/coffee-utils'
8
9
  import {localStore} from '@jdeighan/coffee-utils/browser'
9
10
  import {
10
11
  withExt, slurp, barf, newerDestFileExists,
11
12
  } from '@jdeighan/coffee-utils/fs'
12
- import {fromTAML} from '@jdeighan/coffee-utils/taml'
13
- import {createDraft, finishDraft, produce} from 'immer'
14
13
 
15
14
  # ---------------------------------------------------------------------------
16
15
 
17
- export class WritableDataStore
18
-
19
- constructor: (value=undef) ->
20
- @store = writable value
21
-
22
- subscribe: (func) ->
23
- return @store.subscribe(func)
24
-
25
- set: (value) ->
26
- @store.set(value)
27
-
28
- update: (func) ->
29
- @store.update(func)
16
+ export class StaticDataStore
30
17
 
31
- # ---------------------------------------------------------------------------
32
-
33
- export class BaseDataStore
18
+ constructor: (value) ->
19
+ @value = value
34
20
 
35
- constructor: (@value=undef) ->
36
- @lSubscribers = []
37
-
38
- subscribe: (func) ->
39
- func @value
40
- @lSubscribers.push func
21
+ subscribe: (cbFunc) ->
22
+ cbFunc @value
41
23
  return () ->
42
- pos = @lSubscribers.indexOf func
43
- @lSubscribers.splice pos, 1
24
+ pass()
44
25
 
45
26
  set: (val) ->
46
- @value = val
47
- @alertSubscribers()
48
- return
27
+ croak "Can't set() a StaticDataStore"
49
28
 
50
29
  update: (func) ->
51
- @value = func(@value)
52
- @alertSubscribers()
53
- return
54
-
55
- alertSubscribers: () ->
56
- for func in @lSubscribers
57
- func @value
58
- return
59
-
60
- # ---------------------------------------------------------------------------
61
-
62
- export class ImmerDataStore extends BaseDataStore
63
-
64
- constructor: () ->
65
- super [] # initialize with an empty array
66
-
67
- getNewState: () ->
68
-
69
- return produce state, draft =>
70
- @addGift draft, description, image
71
- return
72
-
73
- addGift: (draft, description, image) ->
74
- draft.push {
75
- id: 1
76
- description
77
- image
78
- }
30
+ croak "Can't update() a StaticDataStore"
79
31
 
80
32
  # ---------------------------------------------------------------------------
81
33
 
82
- export class ToDoDataStore
83
- # --- implemented with immer
34
+ export class WritableDataStore
84
35
 
85
- constructor: () ->
86
- @lToDos = []
87
- @lSubscribers = []
36
+ constructor: (value=undef) ->
37
+ @store = writable value
88
38
 
89
39
  subscribe: (func) ->
90
- func(@lToDos)
91
- @lSubscribers.push func
92
- return () ->
93
- index = @lSubscribers.indexOf func
94
- @lSubscribers.splice index, 1
95
-
96
- alertSubscribers: () ->
97
- for func in @lSubscribers
98
- func(@lToDos)
99
- return
40
+ return @store.subscribe(func)
100
41
 
101
42
  set: (value) ->
102
- # --- Set new value
103
- @alertSubscribers()
43
+ @store.set(value)
104
44
 
105
45
  update: (func) ->
106
- # --- Update value
107
- @alertSubscribers()
108
-
109
- add: (name) ->
110
- @lToDos.push {
111
- text: name
112
- done: false
113
- }
114
- return
46
+ @store.update(func)
115
47
 
116
48
  # ---------------------------------------------------------------------------
117
49
 
@@ -209,6 +141,79 @@ export class TAMLDataStore extends WritableDataStore
209
141
 
210
142
  super fromTAML(str)
211
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
+
212
217
  # ---------------------------------------------------------------------------
213
218
  # UTILITIES
214
219
  # ---------------------------------------------------------------------------
package/src/DataStores.js CHANGED
@@ -13,9 +13,14 @@ import {
13
13
  croak
14
14
  } from '@jdeighan/base-utils';
15
15
 
16
+ import {
17
+ fromTAML
18
+ } from '@jdeighan/base-utils/taml';
19
+
16
20
  import {
17
21
  undef,
18
- pass
22
+ pass,
23
+ range
19
24
  } from '@jdeighan/coffee-utils';
20
25
 
21
26
  import {
@@ -29,138 +34,45 @@ import {
29
34
  newerDestFileExists
30
35
  } from '@jdeighan/coffee-utils/fs';
31
36
 
32
- import {
33
- fromTAML
34
- } from '@jdeighan/coffee-utils/taml';
35
-
36
- import {
37
- createDraft,
38
- finishDraft,
39
- produce
40
- } from 'immer';
41
-
42
37
  // ---------------------------------------------------------------------------
43
- export var WritableDataStore = class WritableDataStore {
44
- constructor(value = undef) {
45
- this.store = writable(value);
38
+ export var StaticDataStore = class StaticDataStore {
39
+ constructor(value) {
40
+ this.value = value;
46
41
  }
47
42
 
48
- subscribe(func) {
49
- return this.store.subscribe(func);
50
- }
51
-
52
- set(value) {
53
- return this.store.set(value);
54
- }
55
-
56
- update(func) {
57
- return this.store.update(func);
58
- }
59
-
60
- };
61
-
62
- // ---------------------------------------------------------------------------
63
- export var BaseDataStore = class BaseDataStore {
64
- constructor(value1 = undef) {
65
- this.value = value1;
66
- this.lSubscribers = [];
67
- }
68
-
69
- subscribe(func) {
70
- func(this.value);
71
- this.lSubscribers.push(func);
43
+ subscribe(cbFunc) {
44
+ cbFunc(this.value);
72
45
  return function() {
73
- var pos;
74
- pos = this.lSubscribers.indexOf(func);
75
- return this.lSubscribers.splice(pos, 1);
46
+ return pass();
76
47
  };
77
48
  }
78
49
 
79
50
  set(val) {
80
- this.value = val;
81
- this.alertSubscribers();
51
+ return croak("Can't set() a StaticDataStore");
82
52
  }
83
53
 
84
54
  update(func) {
85
- this.value = func(this.value);
86
- this.alertSubscribers();
87
- }
88
-
89
- alertSubscribers() {
90
- var func, i, len, ref;
91
- ref = this.lSubscribers;
92
- for (i = 0, len = ref.length; i < len; i++) {
93
- func = ref[i];
94
- func(this.value);
95
- }
55
+ return croak("Can't update() a StaticDataStore");
96
56
  }
97
57
 
98
58
  };
99
59
 
100
60
  // ---------------------------------------------------------------------------
101
- export var ImmerDataStore = class ImmerDataStore extends BaseDataStore {
102
- constructor() {
103
- super([]); // initialize with an empty array
104
- }
105
-
106
- getNewState() {
107
- return produce(state, draft(() => {
108
- return this.addGift(draft, description, image);
109
- }));
110
- }
111
-
112
- addGift(draft, description, image) {
113
- return draft.push({
114
- id: 1,
115
- description,
116
- image
117
- });
118
- }
119
-
120
- };
121
-
122
- // ---------------------------------------------------------------------------
123
- export var ToDoDataStore = class ToDoDataStore {
124
- // --- implemented with immer
125
- constructor() {
126
- this.lToDos = [];
127
- this.lSubscribers = [];
61
+ export var WritableDataStore = class WritableDataStore {
62
+ constructor(value = undef) {
63
+ this.store = writable(value);
128
64
  }
129
65
 
130
66
  subscribe(func) {
131
- func(this.lToDos);
132
- this.lSubscribers.push(func);
133
- return function() {
134
- var index;
135
- index = this.lSubscribers.indexOf(func);
136
- return this.lSubscribers.splice(index, 1);
137
- };
138
- }
139
-
140
- alertSubscribers() {
141
- var func, i, len, ref;
142
- ref = this.lSubscribers;
143
- for (i = 0, len = ref.length; i < len; i++) {
144
- func = ref[i];
145
- func(this.lToDos);
146
- }
67
+ return this.store.subscribe(func);
147
68
  }
148
69
 
149
70
  set(value) {
150
- // --- Set new value
151
- return this.alertSubscribers();
71
+ return this.store.set(value);
152
72
  }
153
73
 
154
74
  update(func) {
155
- // --- Update value
156
- return this.alertSubscribers();
157
- }
158
-
159
- add(name) {
160
- this.lToDos.push({
161
- text: name,
162
- done: false
163
- });
75
+ return this.store.update(func);
164
76
  }
165
77
 
166
78
  };
@@ -280,6 +192,100 @@ export var TAMLDataStore = class TAMLDataStore extends WritableDataStore {
280
192
 
281
193
  };
282
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
+
283
289
  // ---------------------------------------------------------------------------
284
290
  // UTILITIES
285
291
  // ---------------------------------------------------------------------------
@@ -1,61 +0,0 @@
1
- # GiftSet.coffee
2
-
3
- import {produce, enableMapSet} from 'immer'
4
- import {assert, croak, LOG, LOGVALUE} from '@jdeighan/base-utils'
5
- import {
6
- dbgEnter, dbgReturn, dbg,
7
- } from '@jdeighan/base-utils/debug'
8
- import {
9
- undef, defined, notdefined, OL,
10
- isString, isNonEmptyString, isArray, isHash, isArrayOfStrings,
11
- isEmpty, nonEmpty,
12
- } from '@jdeighan/coffee-utils'
13
-
14
- enableMapSet()
15
-
16
- # ---------------------------------------------------------------------------
17
-
18
- export LOGMAP = (label, map) ->
19
-
20
- lLines = ["MAP #{label}:"]
21
- for [key, value] from map.entries()
22
- lLines.push " #{OL(key)}: #{OL(value)}"
23
- LOG lLines.join("\n")
24
- LOG()
25
- return
26
-
27
- # ---------------------------------------------------------------------------
28
-
29
- export addGift = produce (draft, giftName, hData={}) ->
30
-
31
- assert (draft instanceof Map), "draft is not a Map"
32
- if draft.get giftName
33
- throw new Error("Gift #{giftName} already exists")
34
- hValue = {hData...}
35
- hValue.name = giftName
36
- draft.set giftName, hValue
37
- return
38
-
39
- # ---------------------------------------------------------------------------
40
-
41
- export reserveGift = produce (draft, giftName, user) ->
42
-
43
- assert (draft instanceof Map), "draft is not a Map"
44
- gift = draft.get giftName
45
- assert gift?, "No such gift: #{giftName}"
46
- gift.reservedBy = user
47
- return
48
-
49
- # ---------------------------------------------------------------------------
50
-
51
- export cancelReservation = produce (draft, giftName) ->
52
-
53
- assert (draft instanceof Map), "draft is not a Map"
54
- gift = draft.get giftName
55
- assert defined(gift), "No such gift: #{giftName}"
56
- delete gift.reservedBy
57
- return
58
-
59
- # ---------------------------------------------------------------------------
60
-
61
-
package/src/GiftSet.js DELETED
@@ -1,80 +0,0 @@
1
- // Generated by CoffeeScript 2.7.0
2
- // GiftSet.coffee
3
- import {
4
- produce,
5
- enableMapSet
6
- } from 'immer';
7
-
8
- import {
9
- assert,
10
- croak,
11
- LOG,
12
- LOGVALUE
13
- } from '@jdeighan/base-utils';
14
-
15
- import {
16
- dbgEnter,
17
- dbgReturn,
18
- dbg
19
- } from '@jdeighan/base-utils/debug';
20
-
21
- import {
22
- undef,
23
- defined,
24
- notdefined,
25
- OL,
26
- isString,
27
- isNonEmptyString,
28
- isArray,
29
- isHash,
30
- isArrayOfStrings,
31
- isEmpty,
32
- nonEmpty
33
- } from '@jdeighan/coffee-utils';
34
-
35
- enableMapSet();
36
-
37
- // ---------------------------------------------------------------------------
38
- export var LOGMAP = function(label, map) {
39
- var key, lLines, ref, value, x;
40
- lLines = [`MAP ${label}:`];
41
- ref = map.entries();
42
- for (x of ref) {
43
- [key, value] = x;
44
- lLines.push(` ${OL(key)}: ${OL(value)}`);
45
- }
46
- LOG(lLines.join("\n"));
47
- LOG();
48
- };
49
-
50
- // ---------------------------------------------------------------------------
51
- export var addGift = produce(function(draft, giftName, hData = {}) {
52
- var hValue;
53
- assert(draft instanceof Map, "draft is not a Map");
54
- if (draft.get(giftName)) {
55
- throw new Error(`Gift ${giftName} already exists`);
56
- }
57
- hValue = {...hData};
58
- hValue.name = giftName;
59
- draft.set(giftName, hValue);
60
- });
61
-
62
- // ---------------------------------------------------------------------------
63
- export var reserveGift = produce(function(draft, giftName, user) {
64
- var gift;
65
- assert(draft instanceof Map, "draft is not a Map");
66
- gift = draft.get(giftName);
67
- assert(gift != null, `No such gift: ${giftName}`);
68
- gift.reservedBy = user;
69
- });
70
-
71
- // ---------------------------------------------------------------------------
72
- export var cancelReservation = produce(function(draft, giftName) {
73
- var gift;
74
- assert(draft instanceof Map, "draft is not a Map");
75
- gift = draft.get(giftName);
76
- assert(defined(gift), `No such gift: ${giftName}`);
77
- delete gift.reservedBy;
78
- });
79
-
80
- // ---------------------------------------------------------------------------
package/src/taml.coffee DELETED
@@ -1,16 +0,0 @@
1
- # taml.coffee
2
-
3
- import yaml from 'js-yaml'
4
-
5
- import {assert, croak, isTAML, fromTAML, toTAML} from '@jdeighan/base-utils'
6
- import {
7
- undef, defined, notdefined, OL, chomp, escapeStr,
8
- isString, isObject, isEmpty,
9
- } from '@jdeighan/coffee-utils'
10
- import {splitLine} from '@jdeighan/coffee-utils/indent'
11
- import {
12
- firstLine, toArray, toBlock,
13
- } from '@jdeighan/coffee-utils/block'
14
- import {slurpTAML} from '@jdeighan/coffee-utils/fs'
15
-
16
- export {isTAML, fromTAML, toTAML, slurpTAML}
package/src/taml.js DELETED
@@ -1,44 +0,0 @@
1
- // Generated by CoffeeScript 2.7.0
2
- // taml.coffee
3
- import yaml from 'js-yaml';
4
-
5
- import {
6
- assert,
7
- croak,
8
- isTAML,
9
- fromTAML,
10
- toTAML
11
- } from '@jdeighan/base-utils';
12
-
13
- import {
14
- undef,
15
- defined,
16
- notdefined,
17
- OL,
18
- chomp,
19
- escapeStr,
20
- isString,
21
- isObject,
22
- isEmpty
23
- } from '@jdeighan/coffee-utils';
24
-
25
- import {
26
- splitLine
27
- } from '@jdeighan/coffee-utils/indent';
28
-
29
- import {
30
- firstLine,
31
- toArray,
32
- toBlock
33
- } from '@jdeighan/coffee-utils/block';
34
-
35
- import {
36
- slurpTAML
37
- } from '@jdeighan/coffee-utils/fs';
38
-
39
- export {
40
- isTAML,
41
- fromTAML,
42
- toTAML,
43
- slurpTAML
44
- };