@jdeighan/coffee-utils 4.1.6 → 4.1.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,214 +0,0 @@
1
- # utils.test.coffee
2
-
3
- import {UnitTester} from '@jdeighan/coffee-utils/test'
4
- import {
5
- say, undef, error, warn, isString, isObject, isArray, isHash,
6
- isEmpty, nonEmpty, isComment, getClassName, isNumber,
7
- isFunction, isInteger, rtrim,
8
- ltrunc, rtrunc, extractMatches,
9
- words, escapeStr, titleLine,
10
- removeCR, CWS, isArrayOfHashes,
11
- oneline, croak, isRegExp,
12
- } from '@jdeighan/coffee-utils'
13
- import {setLogger} from '@jdeighan/coffee-utils/log'
14
- import {setDebugging} from '@jdeighan/coffee-utils/debug'
15
- import {arrayToBlock} from '@jdeighan/coffee-utils/block'
16
-
17
- simple = new UnitTester()
18
-
19
- # ---------------------------------------------------------------------------
20
-
21
- simple.truthy 19, isEmpty('')
22
- simple.truthy 20, isEmpty(' \t\t')
23
- simple.truthy 21, isEmpty([])
24
- simple.truthy 22, isEmpty({})
25
-
26
- simple.truthy 24, nonEmpty('a')
27
- simple.truthy 25, nonEmpty('.')
28
- simple.truthy 26, nonEmpty([2])
29
- simple.truthy 27, nonEmpty({width: 2})
30
-
31
- simple.truthy 29, isComment("# a comment")
32
- simple.truthy 30, isComment("### a comment")
33
- simple.truthy 31, isComment("#\ta comment")
34
- simple.truthy 32, isComment("###\ta comment")
35
- simple.truthy 33, isComment(" # a comment")
36
- simple.truthy 34, isComment(" ### a comment")
37
- simple.falsy 35, isComment("not much")
38
- simple.falsy 36, isComment("#foreach x in lItems")
39
- simple.truthy 37, isComment('#')
40
- simple.truthy 38, isComment(' #')
41
- simple.truthy 39, isComment(' ###')
42
- simple.falsy 40, isComment('#for')
43
- simple.falsy 41, isComment(' #for')
44
- simple.falsy 42, isComment('#for line in lLines')
45
-
46
- simple.equal 44, titleLine('a thing').length, 42
47
- simple.equal 45, titleLine('a thing','-',5,90).length, 90
48
-
49
- # ---------------------------------------------------------------------------
50
-
51
- simple.equal 80, rtrim("abc"), "abc"
52
- simple.equal 81, rtrim(" abc"), " abc"
53
- simple.equal 82, rtrim("abc "), "abc"
54
- simple.equal 83, rtrim(" abc "), " abc"
55
-
56
- # ---------------------------------------------------------------------------
57
-
58
- simple.equal 87, words('a b c'), ['a', 'b', 'c']
59
- simple.equal 88, words(' a b c '), ['a', 'b', 'c']
60
-
61
- # ---------------------------------------------------------------------------
62
-
63
- simple.equal 92, escapeStr("\t\tXXX\n"), "\\t\\tXXX\\n"
64
- hEsc = {
65
- "\n": "\\n"
66
- "\t": "\\t"
67
- "\"": "\\\""
68
- }
69
- simple.equal 64, escapeStr("\thas quote: \"\nnext line", hEsc), \
70
- "\\thas quote: \\\"\\nnext line"
71
-
72
- # ---------------------------------------------------------------------------
73
-
74
- simple.equal 130, rtrunc('/user/lib/.env', 5), '/user/lib'
75
- simple.equal 131, ltrunc('abcdefg', 3), 'defg'
76
-
77
- simple.equal 133, removeCR("abc\r\ndef\r\n"), "abc\ndef\n"
78
- simple.equal 139, CWS("""
79
- abc
80
- def
81
- ghi
82
- """), "abc def ghi"
83
-
84
- # ---------------------------------------------------------------------------
85
-
86
- simple.truthy 147, isArrayOfHashes([])
87
- simple.truthy 148, isArrayOfHashes([{}, {}])
88
- simple.truthy 149, isArrayOfHashes([{a: 1, b:2}, {}])
89
- simple.truthy 150, isArrayOfHashes([{a: 1, b:2, c: [1,2,3]}, {}])
90
-
91
- simple.falsy 152, isArrayOfHashes({})
92
- simple.falsy 153, isArrayOfHashes([1,2,3])
93
- simple.falsy 154, isArrayOfHashes([{a: 1, b:2, c: [1,2,3]}, 4])
94
- simple.falsy 155, isArrayOfHashes([{a: 1, b:2, c: [1,2,3]}, {}, [1,2]])
95
-
96
- # ---------------------------------------------------------------------------
97
-
98
- (() ->
99
- class NewClass
100
-
101
- h = {a:1, b:2}
102
- l = [1,2,2]
103
- o = new NewClass()
104
- n = 42
105
- n2 = new Number(42)
106
- s = 'simple'
107
- s2 = new String('abc')
108
-
109
- simple.truthy 170, isHash(h)
110
- simple.falsy 171, isHash(l)
111
- simple.falsy 172, isHash(o)
112
- simple.falsy 173, isHash(n)
113
- simple.falsy 174, isHash(n2)
114
- simple.falsy 175, isHash(s)
115
- simple.falsy 176, isHash(s2)
116
-
117
- simple.falsy 178, isArray(h)
118
- simple.truthy 179, isArray(l)
119
- simple.falsy 180, isArray(o)
120
- simple.falsy 181, isArray(n)
121
- simple.falsy 182, isArray(n2)
122
- simple.falsy 183, isArray(s)
123
- simple.falsy 184, isArray(s2)
124
-
125
- simple.falsy 186, isString(h)
126
- simple.falsy 187, isString(l)
127
- simple.falsy 188, isString(o)
128
- simple.falsy 189, isString(n)
129
- simple.falsy 190, isString(n2)
130
- simple.truthy 191, isString(s)
131
- simple.truthy 192, isString(s2)
132
-
133
- simple.falsy 194, isObject(h)
134
- simple.falsy 195, isObject(l)
135
- simple.truthy 196, isObject(o)
136
- simple.falsy 197, isObject(n)
137
- simple.falsy 198, isObject(n2)
138
- simple.falsy 199, isObject(s)
139
- simple.falsy 200, isObject(s2)
140
-
141
- simple.falsy 202, isNumber(h)
142
- simple.falsy 203, isNumber(l)
143
- simple.falsy 204, isNumber(o)
144
- simple.truthy 205, isNumber(n)
145
- simple.truthy 206, isNumber(n2)
146
- simple.falsy 207, isNumber(s)
147
- simple.falsy 208, isNumber(s2)
148
-
149
- )()
150
-
151
- # ---------------------------------------------------------------------------
152
-
153
- simple.truthy 214, isFunction(() -> pass)
154
- simple.falsy 215, isFunction(23)
155
-
156
- simple.truthy 217, isInteger(42)
157
- simple.truthy 218, isInteger(new Number(42))
158
- simple.falsy 219, isInteger('abc')
159
- simple.falsy 220, isInteger({})
160
- simple.falsy 221, isInteger([])
161
-
162
- # ---------------------------------------------------------------------------
163
-
164
- simple.equal 233, oneline(undef), "undef"
165
- simple.equal 234, oneline("\t\tabc\nxyz"), "'\\t\\tabc\\nxyz'"
166
- simple.equal 235, oneline({a:1, b:'xyz'}), '{"a":1,"b":"xyz"}'
167
-
168
- # ---------------------------------------------------------------------------
169
-
170
- simple.equal 239, CWS("""
171
- a simple
172
- error message
173
- """), "a simple error message"
174
-
175
- # ---------------------------------------------------------------------------
176
- # test croak()
177
-
178
- (() ->
179
- lLines = []
180
- setLogger (line) -> lLines.push(line)
181
-
182
- obj = {a:1, b:2}
183
- try
184
- croak "bad stuff", "An Object", obj
185
- simple.equal 255, arrayToBlock(lLines), """
186
- ERROR: bad stuff
187
- An Object = {"a":1,"b":2}
188
- """
189
- setLogger()
190
- )()
191
-
192
- # ---------------------------------------------------------------------------
193
- # test isRegExp()
194
-
195
- simple.truthy 265, isRegExp(/^abc$/)
196
- simple.truthy 266, isRegExp(///^
197
- \s*
198
- where
199
- \s+
200
- areyou
201
- $///)
202
- simple.falsy 272, isRegExp(42)
203
- simple.falsy 272, isRegExp('abc')
204
- simple.falsy 272, isRegExp([1,'a'])
205
- simple.falsy 272, isRegExp({a:1, b:'ccc'})
206
- simple.falsy 272, isRegExp(undef)
207
-
208
- simple.truthy 278, isRegExp(/\.coffee/)
209
-
210
- # ---------------------------------------------------------------------------
211
-
212
- simple.equal 212, extractMatches("..3 and 4 plus 5", /\d+/g, parseInt),
213
- [3, 4, 5]
214
- simple.equal 214, extractMatches("And This Is A String", /A/g), ['A','A']
@@ -1,319 +0,0 @@
1
- // Generated by CoffeeScript 2.6.1
2
- // utils.test.coffee
3
- var hEsc, simple;
4
-
5
- import {
6
- UnitTester
7
- } from '@jdeighan/coffee-utils/test';
8
-
9
- import {
10
- say,
11
- undef,
12
- error,
13
- warn,
14
- isString,
15
- isObject,
16
- isArray,
17
- isHash,
18
- isEmpty,
19
- nonEmpty,
20
- isComment,
21
- getClassName,
22
- isNumber,
23
- isFunction,
24
- isInteger,
25
- rtrim,
26
- ltrunc,
27
- rtrunc,
28
- extractMatches,
29
- words,
30
- escapeStr,
31
- titleLine,
32
- removeCR,
33
- CWS,
34
- isArrayOfHashes,
35
- oneline,
36
- croak,
37
- isRegExp
38
- } from '@jdeighan/coffee-utils';
39
-
40
- import {
41
- setLogger
42
- } from '@jdeighan/coffee-utils/log';
43
-
44
- import {
45
- setDebugging
46
- } from '@jdeighan/coffee-utils/debug';
47
-
48
- import {
49
- arrayToBlock
50
- } from '@jdeighan/coffee-utils/block';
51
-
52
- simple = new UnitTester();
53
-
54
- // ---------------------------------------------------------------------------
55
- simple.truthy(19, isEmpty(''));
56
-
57
- simple.truthy(20, isEmpty(' \t\t'));
58
-
59
- simple.truthy(21, isEmpty([]));
60
-
61
- simple.truthy(22, isEmpty({}));
62
-
63
- simple.truthy(24, nonEmpty('a'));
64
-
65
- simple.truthy(25, nonEmpty('.'));
66
-
67
- simple.truthy(26, nonEmpty([2]));
68
-
69
- simple.truthy(27, nonEmpty({
70
- width: 2
71
- }));
72
-
73
- simple.truthy(29, isComment("# a comment"));
74
-
75
- simple.truthy(30, isComment("### a comment"));
76
-
77
- simple.truthy(31, isComment("#\ta comment"));
78
-
79
- simple.truthy(32, isComment("###\ta comment"));
80
-
81
- simple.truthy(33, isComment(" # a comment"));
82
-
83
- simple.truthy(34, isComment(" ### a comment"));
84
-
85
- simple.falsy(35, isComment("not much"));
86
-
87
- simple.falsy(36, isComment("#foreach x in lItems"));
88
-
89
- simple.truthy(37, isComment('#'));
90
-
91
- simple.truthy(38, isComment(' #'));
92
-
93
- simple.truthy(39, isComment(' ###'));
94
-
95
- simple.falsy(40, isComment('#for'));
96
-
97
- simple.falsy(41, isComment(' #for'));
98
-
99
- simple.falsy(42, isComment('#for line in lLines'));
100
-
101
- simple.equal(44, titleLine('a thing').length, 42);
102
-
103
- simple.equal(45, titleLine('a thing', '-', 5, 90).length, 90);
104
-
105
- // ---------------------------------------------------------------------------
106
- simple.equal(80, rtrim("abc"), "abc");
107
-
108
- simple.equal(81, rtrim(" abc"), " abc");
109
-
110
- simple.equal(82, rtrim("abc "), "abc");
111
-
112
- simple.equal(83, rtrim(" abc "), " abc");
113
-
114
- // ---------------------------------------------------------------------------
115
- simple.equal(87, words('a b c'), ['a', 'b', 'c']);
116
-
117
- simple.equal(88, words(' a b c '), ['a', 'b', 'c']);
118
-
119
- // ---------------------------------------------------------------------------
120
- simple.equal(92, escapeStr("\t\tXXX\n"), "\\t\\tXXX\\n");
121
-
122
- hEsc = {
123
- "\n": "\\n",
124
- "\t": "\\t",
125
- "\"": "\\\""
126
- };
127
-
128
- simple.equal(64, escapeStr("\thas quote: \"\nnext line", hEsc), "\\thas quote: \\\"\\nnext line");
129
-
130
- // ---------------------------------------------------------------------------
131
- simple.equal(130, rtrunc('/user/lib/.env', 5), '/user/lib');
132
-
133
- simple.equal(131, ltrunc('abcdefg', 3), 'defg');
134
-
135
- simple.equal(133, removeCR("abc\r\ndef\r\n"), "abc\ndef\n");
136
-
137
- simple.equal(139, CWS(`abc
138
- def
139
- ghi`), "abc def ghi");
140
-
141
- // ---------------------------------------------------------------------------
142
- simple.truthy(147, isArrayOfHashes([]));
143
-
144
- simple.truthy(148, isArrayOfHashes([{}, {}]));
145
-
146
- simple.truthy(149, isArrayOfHashes([
147
- {
148
- a: 1,
149
- b: 2
150
- },
151
- {}
152
- ]));
153
-
154
- simple.truthy(150, isArrayOfHashes([
155
- {
156
- a: 1,
157
- b: 2,
158
- c: [1,
159
- 2,
160
- 3]
161
- },
162
- {}
163
- ]));
164
-
165
- simple.falsy(152, isArrayOfHashes({}));
166
-
167
- simple.falsy(153, isArrayOfHashes([1, 2, 3]));
168
-
169
- simple.falsy(154, isArrayOfHashes([
170
- {
171
- a: 1,
172
- b: 2,
173
- c: [1,
174
- 2,
175
- 3]
176
- },
177
- 4
178
- ]));
179
-
180
- simple.falsy(155, isArrayOfHashes([
181
- {
182
- a: 1,
183
- b: 2,
184
- c: [1,
185
- 2,
186
- 3]
187
- },
188
- {},
189
- [1,
190
- 2]
191
- ]));
192
-
193
- // ---------------------------------------------------------------------------
194
- (function() {
195
- var NewClass, h, l, n, n2, o, s, s2;
196
- NewClass = class NewClass {};
197
- h = {
198
- a: 1,
199
- b: 2
200
- };
201
- l = [1, 2, 2];
202
- o = new NewClass();
203
- n = 42;
204
- n2 = new Number(42);
205
- s = 'simple';
206
- s2 = new String('abc');
207
- simple.truthy(170, isHash(h));
208
- simple.falsy(171, isHash(l));
209
- simple.falsy(172, isHash(o));
210
- simple.falsy(173, isHash(n));
211
- simple.falsy(174, isHash(n2));
212
- simple.falsy(175, isHash(s));
213
- simple.falsy(176, isHash(s2));
214
- simple.falsy(178, isArray(h));
215
- simple.truthy(179, isArray(l));
216
- simple.falsy(180, isArray(o));
217
- simple.falsy(181, isArray(n));
218
- simple.falsy(182, isArray(n2));
219
- simple.falsy(183, isArray(s));
220
- simple.falsy(184, isArray(s2));
221
- simple.falsy(186, isString(h));
222
- simple.falsy(187, isString(l));
223
- simple.falsy(188, isString(o));
224
- simple.falsy(189, isString(n));
225
- simple.falsy(190, isString(n2));
226
- simple.truthy(191, isString(s));
227
- simple.truthy(192, isString(s2));
228
- simple.falsy(194, isObject(h));
229
- simple.falsy(195, isObject(l));
230
- simple.truthy(196, isObject(o));
231
- simple.falsy(197, isObject(n));
232
- simple.falsy(198, isObject(n2));
233
- simple.falsy(199, isObject(s));
234
- simple.falsy(200, isObject(s2));
235
- simple.falsy(202, isNumber(h));
236
- simple.falsy(203, isNumber(l));
237
- simple.falsy(204, isNumber(o));
238
- simple.truthy(205, isNumber(n));
239
- simple.truthy(206, isNumber(n2));
240
- simple.falsy(207, isNumber(s));
241
- return simple.falsy(208, isNumber(s2));
242
- })();
243
-
244
- // ---------------------------------------------------------------------------
245
- simple.truthy(214, isFunction(function() {
246
- return pass;
247
- }));
248
-
249
- simple.falsy(215, isFunction(23));
250
-
251
- simple.truthy(217, isInteger(42));
252
-
253
- simple.truthy(218, isInteger(new Number(42)));
254
-
255
- simple.falsy(219, isInteger('abc'));
256
-
257
- simple.falsy(220, isInteger({}));
258
-
259
- simple.falsy(221, isInteger([]));
260
-
261
- // ---------------------------------------------------------------------------
262
- simple.equal(233, oneline(undef), "undef");
263
-
264
- simple.equal(234, oneline("\t\tabc\nxyz"), "'\\t\\tabc\\nxyz'");
265
-
266
- simple.equal(235, oneline({
267
- a: 1,
268
- b: 'xyz'
269
- }), '{"a":1,"b":"xyz"}');
270
-
271
- // ---------------------------------------------------------------------------
272
- simple.equal(239, CWS(`a simple
273
- error message`), "a simple error message");
274
-
275
- // ---------------------------------------------------------------------------
276
- // test croak()
277
- (function() {
278
- var lLines, obj;
279
- lLines = [];
280
- setLogger(function(line) {
281
- return lLines.push(line);
282
- });
283
- obj = {
284
- a: 1,
285
- b: 2
286
- };
287
- try {
288
- croak("bad stuff", "An Object", obj);
289
- } catch (error1) {}
290
- simple.equal(255, arrayToBlock(lLines), `ERROR: bad stuff
291
- An Object = {"a":1,"b":2}`);
292
- return setLogger();
293
- })();
294
-
295
- // ---------------------------------------------------------------------------
296
- // test isRegExp()
297
- simple.truthy(265, isRegExp(/^abc$/));
298
-
299
- simple.truthy(266, isRegExp(/^\s*where\s+areyou$/));
300
-
301
- simple.falsy(272, isRegExp(42));
302
-
303
- simple.falsy(272, isRegExp('abc'));
304
-
305
- simple.falsy(272, isRegExp([1, 'a']));
306
-
307
- simple.falsy(272, isRegExp({
308
- a: 1,
309
- b: 'ccc'
310
- }));
311
-
312
- simple.falsy(272, isRegExp(undef));
313
-
314
- simple.truthy(278, isRegExp(/\.coffee/));
315
-
316
- // ---------------------------------------------------------------------------
317
- simple.equal(212, extractMatches("..3 and 4 plus 5", /\d+/g, parseInt), [3, 4, 5]);
318
-
319
- simple.equal(214, extractMatches("And This Is A String", /A/g), ['A', 'A']);
@@ -1,52 +0,0 @@
1
- # private_env.coffee
2
-
3
- import {assert} from '@jdeighan/coffee-utils'
4
- import {log} from '@jdeighan/coffee-utils/log'
5
-
6
- # --- Use by simply importing and using hEnvLib
7
- # This module does no loading - it merely holds hEnvLib
8
- export hPrivEnv = {}
9
-
10
- # --- None of these callbacks should replace variable hEnvLib
11
-
12
- export hPrivEnvCallbacks = {
13
- getVar: (name) ->
14
- return hPrivEnv[name]
15
- setVar: (name, value) ->
16
- hPrivEnv[name] = value
17
- return
18
- clearVar: (name) ->
19
- delete hPrivEnv[name]
20
- return
21
- clearAll: () ->
22
- for name in Object.keys(hPrivEnv)
23
- delete hPrivEnv[name]
24
- return
25
- names: () ->
26
- return Object.keys(hPrivEnv)
27
- }
28
-
29
- # ---------------------------------------------------------------------------
30
-
31
- export setPrivEnvVar = (name, value) ->
32
-
33
- hPrivEnv[name] = value
34
- return
35
-
36
- # ---------------------------------------------------------------------------
37
-
38
- export resetPrivEnv = () ->
39
-
40
- for name in Object.keys(hPrivEnv)
41
- delete hPrivEnv[name]
42
- return
43
-
44
- # ---------------------------------------------------------------------------
45
-
46
- export logPrivEnv = () ->
47
-
48
- log "PRIVATE ENVIRONMENT:"
49
- for key,value of hPrivEnv
50
- log " #{key} = '#{value}'"
51
- log '-'.repeat(40)
52
- return
@@ -1,63 +0,0 @@
1
- // Generated by CoffeeScript 2.6.1
2
- // private_env.coffee
3
- import {
4
- assert
5
- } from '@jdeighan/coffee-utils';
6
-
7
- import {
8
- log
9
- } from '@jdeighan/coffee-utils/log';
10
-
11
- // --- Use by simply importing and using hEnvLib
12
- // This module does no loading - it merely holds hEnvLib
13
- export var hPrivEnv = {};
14
-
15
- // --- None of these callbacks should replace variable hEnvLib
16
- export var hPrivEnvCallbacks = {
17
- getVar: function(name) {
18
- return hPrivEnv[name];
19
- },
20
- setVar: function(name, value) {
21
- hPrivEnv[name] = value;
22
- },
23
- clearVar: function(name) {
24
- delete hPrivEnv[name];
25
- },
26
- clearAll: function() {
27
- var i, len, name, ref;
28
- ref = Object.keys(hPrivEnv);
29
- for (i = 0, len = ref.length; i < len; i++) {
30
- name = ref[i];
31
- delete hPrivEnv[name];
32
- }
33
- },
34
- names: function() {
35
- return Object.keys(hPrivEnv);
36
- }
37
- };
38
-
39
- // ---------------------------------------------------------------------------
40
- export var setPrivEnvVar = function(name, value) {
41
- hPrivEnv[name] = value;
42
- };
43
-
44
- // ---------------------------------------------------------------------------
45
- export var resetPrivEnv = function() {
46
- var i, len, name, ref;
47
- ref = Object.keys(hPrivEnv);
48
- for (i = 0, len = ref.length; i < len; i++) {
49
- name = ref[i];
50
- delete hPrivEnv[name];
51
- }
52
- };
53
-
54
- // ---------------------------------------------------------------------------
55
- export var logPrivEnv = function() {
56
- var key, value;
57
- log("PRIVATE ENVIRONMENT:");
58
- for (key in hPrivEnv) {
59
- value = hPrivEnv[key];
60
- log(` ${key} = '${value}'`);
61
- }
62
- log('-'.repeat(40));
63
- };