@jdeighan/coffee-utils 4.1.4 → 4.1.8

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "4.1.4",
4
+ "version": "4.1.8",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -13,7 +13,6 @@
13
13
  "./debug": "./src/debug_utils.js",
14
14
  "./svelte": "./src/svelte_utils.js",
15
15
  "./test": "./src/UnitTester.js",
16
- "./privenv": "./src/private_env.js",
17
16
  "./package.json": "./package.json"
18
17
  },
19
18
  "engines": {
@@ -202,6 +202,12 @@ export isInteger = (x) ->
202
202
  else
203
203
  return false
204
204
 
205
+ # ---------------------------------------------------------------------------
206
+
207
+ export uniq = (lItems) ->
208
+
209
+ return [...new Set(lItems)]
210
+
205
211
  # ---------------------------------------------------------------------------
206
212
  # warn - issue a warning
207
213
 
@@ -209,13 +215,23 @@ export warn = (message) ->
209
215
 
210
216
  log "WARNING: #{message}"
211
217
 
218
+ # ---------------------------------------------------------------------------
219
+ # hashToStr - stringify a hash
220
+
221
+ export hashToStr = (h) ->
222
+
223
+ return JSON.stringify(h, Object.keys(h).sort(), 3)
224
+
212
225
  # ---------------------------------------------------------------------------
213
226
  # say - print to the console (for now)
214
227
  # later, on a web page, call alert(str)
215
228
 
216
- export say = (str) ->
229
+ export say = (x) ->
217
230
 
218
- console.log str
231
+ if isHash(x)
232
+ console.log hashToStr(x)
233
+ else
234
+ console.log x
219
235
  return
220
236
 
221
237
  # ---------------------------------------------------------------------------
@@ -341,3 +357,21 @@ export extractMatches = (line, regexp, convertFunc=undef) ->
341
357
  return lStrings
342
358
 
343
359
  # ---------------------------------------------------------------------------
360
+
361
+ export envVarsWithPrefix = (prefix, hOptions={}) ->
362
+ # --- valid options:
363
+ # stripPrefix
364
+
365
+ assert prefix, "envVarsWithPrefix: empty prefix!"
366
+ plen = prefix.length
367
+ h = {}
368
+ for key in Object.keys(process.env)
369
+ if key.indexOf(prefix) == 0
370
+ if hOptions.stripPrefix
371
+ h[key.substr(plen)] = process.env[key]
372
+ else
373
+ h[key] = process.env[key]
374
+ return h
375
+
376
+ # ---------------------------------------------------------------------------
377
+
@@ -210,17 +210,32 @@ export var isInteger = function(x) {
210
210
  }
211
211
  };
212
212
 
213
+ // ---------------------------------------------------------------------------
214
+ export var uniq = function(lItems) {
215
+ return [...new Set(lItems)];
216
+ };
217
+
213
218
  // ---------------------------------------------------------------------------
214
219
  // warn - issue a warning
215
220
  export var warn = function(message) {
216
221
  return log(`WARNING: ${message}`);
217
222
  };
218
223
 
224
+ // ---------------------------------------------------------------------------
225
+ // hashToStr - stringify a hash
226
+ export var hashToStr = function(h) {
227
+ return JSON.stringify(h, Object.keys(h).sort(), 3);
228
+ };
229
+
219
230
  // ---------------------------------------------------------------------------
220
231
  // say - print to the console (for now)
221
232
  // later, on a web page, call alert(str)
222
- export var say = function(str) {
223
- console.log(str);
233
+ export var say = function(x) {
234
+ if (isHash(x)) {
235
+ console.log(hashToStr(x));
236
+ } else {
237
+ console.log(x);
238
+ }
224
239
  };
225
240
 
226
241
  // ---------------------------------------------------------------------------
@@ -377,3 +392,25 @@ export var extractMatches = function(line, regexp, convertFunc = undef) {
377
392
  };
378
393
 
379
394
  // ---------------------------------------------------------------------------
395
+ export var envVarsWithPrefix = function(prefix, hOptions = {}) {
396
+ var h, i, key, len, plen, ref;
397
+ // --- valid options:
398
+ // stripPrefix
399
+ assert(prefix, "envVarsWithPrefix: empty prefix!");
400
+ plen = prefix.length;
401
+ h = {};
402
+ ref = Object.keys(process.env);
403
+ for (i = 0, len = ref.length; i < len; i++) {
404
+ key = ref[i];
405
+ if (key.indexOf(prefix) === 0) {
406
+ if (hOptions.stripPrefix) {
407
+ h[key.substr(plen)] = process.env[key];
408
+ } else {
409
+ h[key] = process.env[key];
410
+ }
411
+ }
412
+ }
413
+ return h;
414
+ };
415
+
416
+ // ---------------------------------------------------------------------------
@@ -45,8 +45,14 @@ export fileExt = (path) ->
45
45
 
46
46
  export mydir = (url) ->
47
47
 
48
- dir = pathlib.dirname(urllib.fileURLToPath(url))
49
- return mkpath(dir)
48
+ debug "url = #{url}"
49
+ path = urllib.fileURLToPath(url)
50
+ debug "path = #{path}"
51
+ dir = pathlib.dirname(path)
52
+ debug "dir = #{dir}"
53
+ final = mkpath(dir)
54
+ debug "final = #{final}"
55
+ return final
50
56
 
51
57
  # ---------------------------------------------------------------------------
52
58
 
package/src/fs_utils.js CHANGED
@@ -57,9 +57,15 @@ export var fileExt = function(path) {
57
57
  // mydir() - pass argument `import.meta.url` and it will return
58
58
  // the directory your file is in
59
59
  export var mydir = function(url) {
60
- var dir;
61
- dir = pathlib.dirname(urllib.fileURLToPath(url));
62
- return mkpath(dir);
60
+ var dir, final, path;
61
+ debug(`url = ${url}`);
62
+ path = urllib.fileURLToPath(url);
63
+ debug(`path = ${path}`);
64
+ dir = pathlib.dirname(path);
65
+ debug(`dir = ${dir}`);
66
+ final = mkpath(dir);
67
+ debug(`final = ${final}`);
68
+ return final;
63
69
  };
64
70
 
65
71
  // ---------------------------------------------------------------------------
package/temp.js ADDED
@@ -0,0 +1,26 @@
1
+ // Generated by CoffeeScript 2.6.1
2
+ // temp.coffee
3
+ var dir;
4
+
5
+ import {
6
+ say
7
+ } from '@jdeighan/coffee-utils';
8
+
9
+ import {
10
+ log
11
+ } from '@jdeighan/coffee-utils/log';
12
+
13
+ import {
14
+ setDebugging
15
+ } from '@jdeighan/coffee-utils/debug';
16
+
17
+ import {
18
+ mydir,
19
+ mkpath
20
+ } from '@jdeighan/coffee-utils/fs';
21
+
22
+ setDebugging(true);
23
+
24
+ dir = mydir(import.meta.url);
25
+
26
+ say(`dir = ${dir}`);
@@ -1,185 +0,0 @@
1
- # block.test.coffee
2
-
3
- import assert from 'assert'
4
-
5
- import {UnitTester} from '@jdeighan/coffee-utils/test'
6
- import {
7
- blockToArray, arrayToBlock, firstLine, remainingLines,
8
- normalizeBlock, truncateBlock,
9
- joinBlocks, forEachLine, forEachBlock, forEachSetOfBlocks,
10
- } from '@jdeighan/coffee-utils/block'
11
-
12
- simple = new UnitTester()
13
-
14
- # ---------------------------------------------------------------------------
15
-
16
- simple.equal 108, blockToArray("abc\nxyz\n"), [
17
- 'abc'
18
- 'xyz'
19
- ]
20
-
21
- simple.equal 113, blockToArray("abc\nxyz\n\n\n\n"), [
22
- 'abc'
23
- 'xyz'
24
- ]
25
-
26
- simple.equal 118, blockToArray("abc\n\nxyz\n"), [
27
- 'abc'
28
- ''
29
- 'xyz'
30
- ]
31
-
32
- # ---------------------------------------------------------------------------
33
-
34
- simple.equal 126, arrayToBlock(['a','b','c']), "a\nb\nc\n"
35
-
36
- # ---------------------------------------------------------------------------
37
-
38
- simple.equal 225, firstLine("""
39
- #starbucks
40
- do this
41
- do that
42
- """), '#starbucks'
43
-
44
- # ---------------------------------------------------------------------------
45
-
46
- simple.equal 225, remainingLines("""
47
- #starbucks
48
- do this
49
- do that
50
- """), """
51
- do this
52
- do that
53
- """
54
-
55
- # ---------------------------------------------------------------------------
56
-
57
- (() ->
58
- str = joinBlocks('import me', '', 'do this\ndo that')
59
- simple.equal 17, str, """
60
- import me
61
- do this
62
- do that
63
- """
64
- )()
65
-
66
- # ---------------------------------------------------------------------------
67
-
68
- simple.equal 49, normalizeBlock("""
69
- line 1
70
- line 2
71
- """), """
72
- line 1
73
- line 2
74
- """ + '\n'
75
-
76
- simple.equal 57, normalizeBlock("""
77
- line 1
78
-
79
- line 2
80
- """), """
81
- line 1
82
- line 2
83
- """ + '\n'
84
-
85
- simple.equal 66, normalizeBlock("""
86
-
87
- line 1
88
-
89
- line 2
90
-
91
-
92
- """), """
93
- line 1
94
- line 2
95
- """ + '\n'
96
-
97
- # ---------------------------------------------------------------------------
98
-
99
- simple.equal 96, truncateBlock("""
100
- line 1
101
- line 2
102
- line 3
103
- line 4
104
- """, 2), """
105
- line 1
106
- line 2
107
- """ + '\n'
108
-
109
- # ---------------------------------------------------------------------------
110
-
111
- (() ->
112
- lBlocks = [
113
- "import {say} from '@jdeighan/coffee-utils'",
114
- "",
115
- "<script>\n\tx = 42\n</script>",
116
- "",
117
- ]
118
- str = joinBlocks(lBlocks...)
119
- simple.equal 34, str, """
120
- import {say} from '@jdeighan/coffee-utils'
121
- <script>
122
- x = 42
123
- </script>
124
- """
125
- )()
126
-
127
- # ---------------------------------------------------------------------------
128
-
129
- (() ->
130
- lImports = [
131
- "import {say} from '@jdeighan/coffee-utils'",
132
- ]
133
- code = """
134
- if (x==42)
135
- log "line 2 in unit test"
136
- """
137
- str = joinBlocks(lImports..., code)
138
- simple.equal 34, str, """
139
-
140
- import {say} from '@jdeighan/coffee-utils'
141
- if (x==42)
142
- log "line 2 in unit test"
143
- """
144
- )()
145
-
146
- # ---------------------------------------------------------------------------
147
- # test forEachLine()
148
-
149
- (() ->
150
- lLines = []
151
-
152
- callback = (line) ->
153
- lLines.push line
154
- return
155
-
156
- filepath = "c:/Users/johnd/coffee-utils/test/data/file2.txt"
157
- await forEachLine filepath, callback
158
-
159
- simple.equal 55, lLines, [
160
- "abc",
161
- "def",
162
- "ghi",
163
- "jkl",
164
- ]
165
- )()
166
-
167
- # ---------------------------------------------------------------------------
168
- # test forEachBlock()
169
-
170
- (() ->
171
- lBlocks = []
172
-
173
- callback = (block) ->
174
- lBlocks.push block
175
- return
176
-
177
- filepath = "c:/Users/johnd/coffee-utils/test/data/file3.txt"
178
- await forEachBlock filepath, callback
179
-
180
- simple.equal 76, lBlocks, [
181
- "abc\ndef",
182
- "abc\ndef\nghi",
183
- "abc\ndef\nghi\njkl",
184
- ]
185
- )()
@@ -1,129 +0,0 @@
1
- // Generated by CoffeeScript 2.6.1
2
- // block.test.coffee
3
- var simple;
4
-
5
- import assert from 'assert';
6
-
7
- import {
8
- UnitTester
9
- } from '@jdeighan/coffee-utils/test';
10
-
11
- import {
12
- blockToArray,
13
- arrayToBlock,
14
- firstLine,
15
- remainingLines,
16
- normalizeBlock,
17
- truncateBlock,
18
- joinBlocks,
19
- forEachLine,
20
- forEachBlock,
21
- forEachSetOfBlocks
22
- } from '@jdeighan/coffee-utils/block';
23
-
24
- simple = new UnitTester();
25
-
26
- // ---------------------------------------------------------------------------
27
- simple.equal(108, blockToArray("abc\nxyz\n"), ['abc', 'xyz']);
28
-
29
- simple.equal(113, blockToArray("abc\nxyz\n\n\n\n"), ['abc', 'xyz']);
30
-
31
- simple.equal(118, blockToArray("abc\n\nxyz\n"), ['abc', '', 'xyz']);
32
-
33
- // ---------------------------------------------------------------------------
34
- simple.equal(126, arrayToBlock(['a', 'b', 'c']), "a\nb\nc\n");
35
-
36
- // ---------------------------------------------------------------------------
37
- simple.equal(225, firstLine(`#starbucks
38
- do this
39
- do that`), '#starbucks');
40
-
41
- // ---------------------------------------------------------------------------
42
- simple.equal(225, remainingLines(`#starbucks
43
- do this
44
- do that`), `do this
45
- do that`);
46
-
47
- // ---------------------------------------------------------------------------
48
- (function() {
49
- var str;
50
- str = joinBlocks('import me', '', 'do this\ndo that');
51
- return simple.equal(17, str, `import me
52
- do this
53
- do that`);
54
- })();
55
-
56
- // ---------------------------------------------------------------------------
57
- simple.equal(49, normalizeBlock(`line 1
58
- line 2`), `line 1
59
- line 2` + '\n');
60
-
61
- simple.equal(57, normalizeBlock(`line 1
62
-
63
- line 2`), `line 1
64
- line 2` + '\n');
65
-
66
- simple.equal(66, normalizeBlock(`
67
- line 1
68
-
69
- line 2
70
-
71
- `), `line 1
72
- line 2` + '\n');
73
-
74
- // ---------------------------------------------------------------------------
75
- simple.equal(96, truncateBlock(`line 1
76
- line 2
77
- line 3
78
- line 4`, 2), `line 1
79
- line 2` + '\n');
80
-
81
- // ---------------------------------------------------------------------------
82
- (function() {
83
- var lBlocks, str;
84
- lBlocks = ["import {say} from '@jdeighan/coffee-utils'", "", "<script>\n\tx = 42\n</script>", ""];
85
- str = joinBlocks(...lBlocks);
86
- return simple.equal(34, str, `import {say} from '@jdeighan/coffee-utils'
87
- <script>
88
- x = 42
89
- </script>`);
90
- })();
91
-
92
- // ---------------------------------------------------------------------------
93
- (function() {
94
- var code, lImports, str;
95
- lImports = ["import {say} from '@jdeighan/coffee-utils'"];
96
- code = `if (x==42)
97
- log "line 2 in unit test"`;
98
- str = joinBlocks(...lImports, code);
99
- return simple.equal(34, str, `
100
- import {say} from '@jdeighan/coffee-utils'
101
- if (x==42)
102
- log "line 2 in unit test"`);
103
- })();
104
-
105
- // ---------------------------------------------------------------------------
106
- // test forEachLine()
107
- (async function() {
108
- var callback, filepath, lLines;
109
- lLines = [];
110
- callback = function(line) {
111
- lLines.push(line);
112
- };
113
- filepath = "c:/Users/johnd/coffee-utils/test/data/file2.txt";
114
- await forEachLine(filepath, callback);
115
- return simple.equal(55, lLines, ["abc", "def", "ghi", "jkl"]);
116
- })();
117
-
118
- // ---------------------------------------------------------------------------
119
- // test forEachBlock()
120
- (async function() {
121
- var callback, filepath, lBlocks;
122
- lBlocks = [];
123
- callback = function(block) {
124
- lBlocks.push(block);
125
- };
126
- filepath = "c:/Users/johnd/coffee-utils/test/data/file3.txt";
127
- await forEachBlock(filepath, callback);
128
- return simple.equal(76, lBlocks, ["abc\ndef", "abc\ndef\nghi", "abc\ndef\nghi\njkl"]);
129
- })();
@@ -1,47 +0,0 @@
1
- # block2.test.coffee
2
-
3
- import assert from 'assert'
4
- import test from 'ava'
5
-
6
- import {
7
- undef, say, isString, isHash, isEmpty, nonEmpty,
8
- } from '@jdeighan/coffee-utils'
9
- import {mydir, mkpath} from '@jdeighan/coffee-utils/fs'
10
- import {UnitTester} from '@jdeighan/coffee-utils/test'
11
- import {
12
- forEachLine, forEachBlock, forEachSetOfBlocks,
13
- } from '@jdeighan/coffee-utils/block'
14
-
15
- testDir = mydir(`import.meta.url`)
16
- simple = new UnitTester()
17
-
18
- filepath = mkpath(testDir, 'code2.test.txt')
19
-
20
- # ----------------------------------------------------------------------------
21
-
22
- (() ->
23
- lFirstBlocks = undef
24
- callback = (lBlocks) ->
25
- lFirstBlocks = lBlocks
26
- return true # we're only interested in the first set
27
-
28
- test "line 29", (t) ->
29
- await forEachSetOfBlocks filepath, callback
30
- t.deepEqual lFirstBlocks, ["f()", "f"]
31
- )()
32
-
33
- # ----------------------------------------------------------------------------
34
-
35
- (() ->
36
- lAllBlockSets = []
37
- callback = (lBlocks) ->
38
- lAllBlockSets.push(lBlocks)
39
- return
40
-
41
- test "line 44", (t) ->
42
- await forEachSetOfBlocks filepath, callback
43
- t.deepEqual lAllBlockSets, [
44
- ["f()", "f"],
45
- ["f = (key=undef) ->\n\tswitch key\n\t\twhen 'ok'\n\t\t\tsay 'all is OK'", "f,say,mkpath"],
46
- ]
47
- )()
@@ -1,64 +0,0 @@
1
- // Generated by CoffeeScript 2.6.1
2
- // block2.test.coffee
3
- var filepath, simple, testDir;
4
-
5
- import assert from 'assert';
6
-
7
- import test from 'ava';
8
-
9
- import {
10
- undef,
11
- say,
12
- isString,
13
- isHash,
14
- isEmpty,
15
- nonEmpty
16
- } from '@jdeighan/coffee-utils';
17
-
18
- import {
19
- mydir,
20
- mkpath
21
- } from '@jdeighan/coffee-utils/fs';
22
-
23
- import {
24
- UnitTester
25
- } from '@jdeighan/coffee-utils/test';
26
-
27
- import {
28
- forEachLine,
29
- forEachBlock,
30
- forEachSetOfBlocks
31
- } from '@jdeighan/coffee-utils/block';
32
-
33
- testDir = mydir(import.meta.url);
34
-
35
- simple = new UnitTester();
36
-
37
- filepath = mkpath(testDir, 'code2.test.txt');
38
-
39
- // ----------------------------------------------------------------------------
40
- (function() {
41
- var callback, lFirstBlocks;
42
- lFirstBlocks = undef;
43
- callback = function(lBlocks) {
44
- lFirstBlocks = lBlocks;
45
- return true; // we're only interested in the first set
46
- };
47
- return test("line 29", async function(t) {
48
- await forEachSetOfBlocks(filepath, callback);
49
- return t.deepEqual(lFirstBlocks, ["f()", "f"]);
50
- });
51
- })();
52
-
53
- // ----------------------------------------------------------------------------
54
- (function() {
55
- var callback, lAllBlockSets;
56
- lAllBlockSets = [];
57
- callback = function(lBlocks) {
58
- lAllBlockSets.push(lBlocks);
59
- };
60
- return test("line 44", async function(t) {
61
- await forEachSetOfBlocks(filepath, callback);
62
- return t.deepEqual(lAllBlockSets, [["f()", "f"], ["f = (key=undef) ->\n\tswitch key\n\t\twhen 'ok'\n\t\t\tsay 'all is OK'", "f,say,mkpath"]]);
63
- });
64
- })();