@jdeighan/coffee-utils 13.0.10 → 13.0.12
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +1 -3
- package/src/DataStores.coffee +28 -36
- package/src/DataStores.js +40 -48
- package/src/indent.coffee +12 -2
- package/src/indent.js +13 -2
- package/src/GiftSet.coffee +0 -61
- package/src/GiftSet.js +0 -80
- package/src/taml.coffee +0 -16
- package/src/taml.js +0 -44
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.12",
|
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"
|
package/src/DataStores.coffee
CHANGED
@@ -4,13 +4,12 @@ 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 {
|
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
|
|
@@ -59,58 +58,51 @@ export class BaseDataStore
|
|
59
58
|
|
60
59
|
# ---------------------------------------------------------------------------
|
61
60
|
|
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
|
-
}
|
79
|
-
|
80
|
-
# ---------------------------------------------------------------------------
|
81
|
-
|
82
61
|
export class ToDoDataStore
|
83
|
-
# ---
|
62
|
+
# --- implement without using svelte's readable or writable
|
84
63
|
|
85
64
|
constructor: () ->
|
86
65
|
@lToDos = []
|
87
|
-
@lSubscribers = []
|
66
|
+
@lSubscribers = [] # --- array of callback functions
|
88
67
|
|
89
|
-
subscribe: (
|
90
|
-
|
91
|
-
@lSubscribers.push
|
68
|
+
subscribe: (cbFunc) ->
|
69
|
+
cbFunc(@lToDos)
|
70
|
+
@lSubscribers.push cbFunc
|
92
71
|
return () ->
|
93
|
-
index = @lSubscribers.indexOf
|
72
|
+
index = @lSubscribers.indexOf cbFunc
|
94
73
|
@lSubscribers.splice index, 1
|
95
74
|
|
96
75
|
alertSubscribers: () ->
|
97
|
-
for
|
98
|
-
|
76
|
+
for cbFunc in @lSubscribers
|
77
|
+
cbFunc(@lToDos)
|
99
78
|
return
|
100
79
|
|
101
|
-
|
102
|
-
# ---
|
103
|
-
@
|
80
|
+
find: (name) ->
|
81
|
+
# --- returns index
|
82
|
+
for index in range(@lToDos.length)
|
83
|
+
if (@lToDos[index].text == name)
|
84
|
+
return index
|
85
|
+
return undef
|
104
86
|
|
105
|
-
|
106
|
-
# ---
|
87
|
+
clear: () ->
|
88
|
+
# --- Set new value
|
89
|
+
@lToDos = []
|
107
90
|
@alertSubscribers()
|
91
|
+
return
|
108
92
|
|
109
93
|
add: (name) ->
|
94
|
+
if defined(@find(name))
|
95
|
+
croak "Attempt to add duplicate #{name} todo"
|
110
96
|
@lToDos.push {
|
111
97
|
text: name
|
112
98
|
done: false
|
113
99
|
}
|
100
|
+
@alertSubscribers()
|
101
|
+
return
|
102
|
+
|
103
|
+
remove: (name) ->
|
104
|
+
index = @find(name)
|
105
|
+
@lToDos.splice index, 1
|
114
106
|
return
|
115
107
|
|
116
108
|
# ---------------------------------------------------------------------------
|
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,16 +34,6 @@ 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
38
|
export var WritableDataStore = class WritableDataStore {
|
44
39
|
constructor(value = undef) {
|
@@ -97,70 +92,67 @@ export var BaseDataStore = class BaseDataStore {
|
|
97
92
|
|
98
93
|
};
|
99
94
|
|
100
|
-
// ---------------------------------------------------------------------------
|
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
95
|
// ---------------------------------------------------------------------------
|
123
96
|
export var ToDoDataStore = class ToDoDataStore {
|
124
|
-
// ---
|
97
|
+
// --- implement without using svelte's readable or writable
|
125
98
|
constructor() {
|
126
99
|
this.lToDos = [];
|
127
|
-
this.lSubscribers = [];
|
100
|
+
this.lSubscribers = []; // --- array of callback functions
|
128
101
|
}
|
129
102
|
|
130
|
-
subscribe(
|
131
|
-
|
132
|
-
this.lSubscribers.push(
|
103
|
+
subscribe(cbFunc) {
|
104
|
+
cbFunc(this.lToDos);
|
105
|
+
this.lSubscribers.push(cbFunc);
|
133
106
|
return function() {
|
134
107
|
var index;
|
135
|
-
index = this.lSubscribers.indexOf(
|
108
|
+
index = this.lSubscribers.indexOf(cbFunc);
|
136
109
|
return this.lSubscribers.splice(index, 1);
|
137
110
|
};
|
138
111
|
}
|
139
112
|
|
140
113
|
alertSubscribers() {
|
141
|
-
var
|
114
|
+
var cbFunc, i, len, ref;
|
142
115
|
ref = this.lSubscribers;
|
143
116
|
for (i = 0, len = ref.length; i < len; i++) {
|
144
|
-
|
145
|
-
|
117
|
+
cbFunc = ref[i];
|
118
|
+
cbFunc(this.lToDos);
|
146
119
|
}
|
147
120
|
}
|
148
121
|
|
149
|
-
|
150
|
-
|
151
|
-
|
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;
|
152
133
|
}
|
153
134
|
|
154
|
-
|
155
|
-
// ---
|
156
|
-
|
135
|
+
clear() {
|
136
|
+
// --- Set new value
|
137
|
+
this.lToDos = [];
|
138
|
+
this.alertSubscribers();
|
157
139
|
}
|
158
140
|
|
159
141
|
add(name) {
|
142
|
+
if (defined(this.find(name))) {
|
143
|
+
croak(`Attempt to add duplicate ${name} todo`);
|
144
|
+
}
|
160
145
|
this.lToDos.push({
|
161
146
|
text: name,
|
162
147
|
done: false
|
163
148
|
});
|
149
|
+
this.alertSubscribers();
|
150
|
+
}
|
151
|
+
|
152
|
+
remove(name) {
|
153
|
+
var index;
|
154
|
+
index = this.find(name);
|
155
|
+
this.lToDos.splice(index, 1);
|
164
156
|
}
|
165
157
|
|
166
158
|
};
|
package/src/indent.coffee
CHANGED
@@ -9,18 +9,28 @@ import {toArray, toBlock} from '@jdeighan/coffee-utils/block'
|
|
9
9
|
|
10
10
|
# ---------------------------------------------------------------------------
|
11
11
|
|
12
|
+
export getOneIndent = (str) ->
|
13
|
+
|
14
|
+
if (lMatches = str.match(/^\t+(?:\S|$)/))
|
15
|
+
return "\t"
|
16
|
+
else if (lMatches = str.match(/^(\x20+)(?:\S|$)/)) # space char
|
17
|
+
return lMatches[1]
|
18
|
+
assert notdefined(str.match(/^\s/)), "Mixed indentation types"
|
19
|
+
return undef
|
20
|
+
|
21
|
+
# ---------------------------------------------------------------------------
|
22
|
+
|
12
23
|
export splitPrefix = (line) ->
|
13
24
|
|
14
25
|
assert isString(line), "non-string #{OL(line)}"
|
15
26
|
line = rtrim(line)
|
16
27
|
lMatches = line.match(/^(\s*)(.*)$/)
|
17
|
-
assert defined(lMatches), "Failed to match: #{OL(line)}"
|
18
28
|
return [lMatches[1], lMatches[2]]
|
19
29
|
|
20
30
|
# ---------------------------------------------------------------------------
|
21
31
|
# splitLine - separate a line into [level, line]
|
22
32
|
|
23
|
-
export splitLine = (line, oneIndent=
|
33
|
+
export splitLine = (line, oneIndent=undef) ->
|
24
34
|
|
25
35
|
[prefix, str] = splitPrefix(line)
|
26
36
|
return [indentLevel(prefix, oneIndent), str]
|
package/src/indent.js
CHANGED
@@ -22,19 +22,30 @@ import {
|
|
22
22
|
toBlock
|
23
23
|
} from '@jdeighan/coffee-utils/block';
|
24
24
|
|
25
|
+
// ---------------------------------------------------------------------------
|
26
|
+
export var getOneIndent = function(str) {
|
27
|
+
var lMatches;
|
28
|
+
if ((lMatches = str.match(/^\t+(?:\S|$)/))) {
|
29
|
+
return "\t";
|
30
|
+
} else if ((lMatches = str.match(/^(\x20+)(?:\S|$)/))) { // space char
|
31
|
+
return lMatches[1];
|
32
|
+
}
|
33
|
+
assert(notdefined(str.match(/^\s/)), "Mixed indentation types");
|
34
|
+
return undef;
|
35
|
+
};
|
36
|
+
|
25
37
|
// ---------------------------------------------------------------------------
|
26
38
|
export var splitPrefix = function(line) {
|
27
39
|
var lMatches;
|
28
40
|
assert(isString(line), `non-string ${OL(line)}`);
|
29
41
|
line = rtrim(line);
|
30
42
|
lMatches = line.match(/^(\s*)(.*)$/);
|
31
|
-
assert(defined(lMatches), `Failed to match: ${OL(line)}`);
|
32
43
|
return [lMatches[1], lMatches[2]];
|
33
44
|
};
|
34
45
|
|
35
46
|
// ---------------------------------------------------------------------------
|
36
47
|
// splitLine - separate a line into [level, line]
|
37
|
-
export var splitLine = function(line, oneIndent =
|
48
|
+
export var splitLine = function(line, oneIndent = undef) {
|
38
49
|
var prefix, str;
|
39
50
|
[prefix, str] = splitPrefix(line);
|
40
51
|
return [indentLevel(prefix, oneIndent), str];
|
package/src/GiftSet.coffee
DELETED
@@ -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
|
-
};
|