@jdeighan/coffee-utils 4.1.8 → 4.1.9

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.8",
4
+ "version": "4.1.9",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -86,6 +86,7 @@ export joinBlocks = (lBlocks...) ->
86
86
 
87
87
  lNonEmptyBlocks = []
88
88
  for block in lBlocks
89
+ assert isString(block), "joinBlocks(): block is not a string"
89
90
  if nonEmpty(block)
90
91
  lNonEmptyBlocks.push block
91
92
  return lNonEmptyBlocks.join('\n')
@@ -110,6 +110,7 @@ export var joinBlocks = function(...lBlocks) {
110
110
  lNonEmptyBlocks = [];
111
111
  for (i = 0, len1 = lBlocks.length; i < len1; i++) {
112
112
  block = lBlocks[i];
113
+ assert(isString(block), "joinBlocks(): block is not a string");
113
114
  if (nonEmpty(block)) {
114
115
  lNonEmptyBlocks.push(block);
115
116
  }
@@ -100,6 +100,14 @@ export isHash = (x) ->
100
100
 
101
101
  return (getClassName(x) == 'Object')
102
102
 
103
+ # ---------------------------------------------------------------------------
104
+
105
+ export hashHasKey = (x, key) ->
106
+
107
+ assert isHash(x), "hashHasKey(): not a hash"
108
+ assert isString(key), "hashHasKey(): key not a string"
109
+ return x.hasOwnProperty(key)
110
+
103
111
  # ---------------------------------------------------------------------------
104
112
  # isEmpty
105
113
  # - string is whitespace, array has no elements, hash has no keys
@@ -149,6 +157,7 @@ export setCommentRegexp = (regexp) ->
149
157
 
150
158
  export isComment = (str) ->
151
159
 
160
+ assert isString(str), "isComment(): not a string"
152
161
  return if str.match(commentRegExp) then true else false
153
162
 
154
163
  # ---------------------------------------------------------------------------
@@ -264,6 +273,7 @@ export titleLine = (title, char='=', padding=2, linelen=42) ->
264
273
 
265
274
  export rtrim = (line) ->
266
275
 
276
+ assert isString(line), "rtrim(): line is not a string"
267
277
  lMatches = line.match(/\s+$/)
268
278
  if lMatches?
269
279
  n = lMatches[0].length # num chars to remove
@@ -97,6 +97,13 @@ export var isHash = function(x) {
97
97
  return getClassName(x) === 'Object';
98
98
  };
99
99
 
100
+ // ---------------------------------------------------------------------------
101
+ export var hashHasKey = function(x, key) {
102
+ assert(isHash(x), "hashHasKey(): not a hash");
103
+ assert(isString(key), "hashHasKey(): key not a string");
104
+ return x.hasOwnProperty(key);
105
+ };
106
+
100
107
  // ---------------------------------------------------------------------------
101
108
  // isEmpty
102
109
  // - string is whitespace, array has no elements, hash has no keys
@@ -147,6 +154,7 @@ export var setCommentRegexp = function(regexp) {
147
154
 
148
155
  // ---------------------------------------------------------------------------
149
156
  export var isComment = function(str) {
157
+ assert(isString(str), "isComment(): not a string");
150
158
  if (str.match(commentRegExp)) {
151
159
  return true;
152
160
  } else {
@@ -266,6 +274,7 @@ export var titleLine = function(title, char = '=', padding = 2, linelen = 42) {
266
274
  // rtrim - strip trailing whitespace
267
275
  export var rtrim = function(line) {
268
276
  var lMatches, n;
277
+ assert(isString(line), "rtrim(): line is not a string");
269
278
  lMatches = line.match(/\s+$/);
270
279
  if (lMatches != null) {
271
280
  n = lMatches[0].length; // num chars to remove
@@ -209,6 +209,7 @@ reMethod = ///^
209
209
 
210
210
  export funcMatch = (curFunc) ->
211
211
 
212
+ assert isString(curFunc), "funcMatch(): not a string"
212
213
  if lDebugFuncs.includes(curFunc)
213
214
  return true
214
215
  else if (lMatches = curFunc.match(reMethod)) \
@@ -204,6 +204,7 @@ reMethod = /^([A-Za-z_][A-Za-z0-9_]*)\.([A-Za-z_][A-Za-z0-9_]*)$/;
204
204
  // --- export only to allow unit tests
205
205
  export var funcMatch = function(curFunc) {
206
206
  var _, cls, lMatches, meth;
207
+ assert(isString(curFunc), "funcMatch(): not a string");
207
208
  if (lDebugFuncs.includes(curFunc)) {
208
209
  return true;
209
210
  } else if ((lMatches = curFunc.match(reMethod)) && ([_, cls, meth] = lMatches) && lDebugFuncs.includes(meth)) {
@@ -6,7 +6,7 @@ import fs from 'fs'
6
6
 
7
7
  import {
8
8
  assert, undef, pass, rtrim, error, nonEmpty,
9
- isRegExp, isFunction, croak,
9
+ isString, isRegExp, isFunction, croak,
10
10
  } from '@jdeighan/coffee-utils'
11
11
  import {log} from '@jdeighan/coffee-utils/log'
12
12
  import {debug} from '@jdeighan/coffee-utils/debug'
@@ -34,6 +34,7 @@ export isSimpleFileName = (path) ->
34
34
 
35
35
  export fileExt = (path) ->
36
36
 
37
+ assert isString(path), "fileExt(): path not a string"
37
38
  if lMatches = path.match(/\.[A-Za-z0-9_]+$/)
38
39
  return lMatches[0]
39
40
  else
@@ -135,6 +136,25 @@ export withExt = (path, newExt, hOptions={}) ->
135
136
  name = name.substr(1)
136
137
  return mkpath(dir, "#{name}#{newExt}")
137
138
 
139
+ # ---------------------------------------------------------------------------
140
+ # removeFileWithExt - remove file with different ext
141
+
142
+ export removeFileWithExt = (path, newExt, hOptions={}) ->
143
+ # --- Valid options:
144
+ # doLog
145
+ # removeLeadingUnderScore
146
+
147
+ fullpath = withExt(path, newExt, hOptions)
148
+ try
149
+ fs.unlinkSync fullpath
150
+ if hOptions.doLog
151
+ log " unlink #{filename}"
152
+ success = true
153
+ catch err
154
+ log " UNLINK FAILED: #{err.message}"
155
+ success = false
156
+ return success
157
+
138
158
  # ---------------------------------------------------------------------------
139
159
  # withUnderScore - add '_' to file name
140
160
 
package/src/fs_utils.js CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  rtrim,
14
14
  error,
15
15
  nonEmpty,
16
+ isString,
16
17
  isRegExp,
17
18
  isFunction,
18
19
  croak
@@ -46,6 +47,7 @@ export var isSimpleFileName = function(path) {
46
47
  // ---------------------------------------------------------------------------
47
48
  export var fileExt = function(path) {
48
49
  var lMatches;
50
+ assert(isString(path), "fileExt(): path not a string");
49
51
  if (lMatches = path.match(/\.[A-Za-z0-9_]+$/)) {
50
52
  return lMatches[0];
51
53
  } else {
@@ -154,6 +156,28 @@ export var withExt = function(path, newExt, hOptions = {}) {
154
156
  return mkpath(dir, `${name}${newExt}`);
155
157
  };
156
158
 
159
+ // ---------------------------------------------------------------------------
160
+ // removeFileWithExt - remove file with different ext
161
+ export var removeFileWithExt = function(path, newExt, hOptions = {}) {
162
+ var err, fullpath, success;
163
+ // --- Valid options:
164
+ // doLog
165
+ // removeLeadingUnderScore
166
+ fullpath = withExt(path, newExt, hOptions);
167
+ try {
168
+ fs.unlinkSync(fullpath);
169
+ if (hOptions.doLog) {
170
+ log(` unlink ${filename}`);
171
+ }
172
+ success = true;
173
+ } catch (error1) {
174
+ err = error1;
175
+ log(` UNLINK FAILED: ${err.message}`);
176
+ success = false;
177
+ }
178
+ return success;
179
+ };
180
+
157
181
  // ---------------------------------------------------------------------------
158
182
  // withUnderScore - add '_' to file name
159
183
  export var withUnderScore = function(path) {
@@ -14,7 +14,7 @@ import {arrayToBlock, blockToArray} from '@jdeighan/coffee-utils/block'
14
14
  export splitLine = (line) ->
15
15
 
16
16
  assert line?, "splitLine(): line is undef"
17
- assert (typeof line == 'string'), "splitLine(): line is not a string"
17
+ assert isString(line), "splitLine(): line is not a string"
18
18
  line = rtrim(line)
19
19
  lMatches = line.match(/^(\s*)(.*)$/)
20
20
  return [lMatches[1].length, lMatches[2]]
@@ -34,6 +34,7 @@ export indentation = (level) ->
34
34
 
35
35
  export indentLevel = (str) ->
36
36
 
37
+ assert isString(str), "indentLevel(): not a string"
37
38
  lMatches = str.match(/^\t*/)
38
39
  return lMatches[0].length
39
40
 
@@ -76,6 +77,8 @@ export undented = (text, level=undef) ->
76
77
  return ''
77
78
  else if isArray(text)
78
79
  lLines = text
80
+ for line in lLines
81
+ assert isString(line), "undented(): input array is not all strings"
79
82
  if (lLines.length == 0)
80
83
  return []
81
84
  else
@@ -25,7 +25,7 @@ import {
25
25
  export var splitLine = function(line) {
26
26
  var lMatches;
27
27
  assert(line != null, "splitLine(): line is undef");
28
- assert(typeof line === 'string', "splitLine(): line is not a string");
28
+ assert(isString(line), "splitLine(): line is not a string");
29
29
  line = rtrim(line);
30
30
  lMatches = line.match(/^(\s*)(.*)$/);
31
31
  return [lMatches[1].length, lMatches[2]];
@@ -44,6 +44,7 @@ export var indentation = function(level) {
44
44
  // it's OK if the string is ONLY indentation
45
45
  export var indentLevel = function(str) {
46
46
  var lMatches;
47
+ assert(isString(str), "indentLevel(): not a string");
47
48
  lMatches = str.match(/^\t*/);
48
49
  return lMatches[0].length;
49
50
  };
@@ -84,7 +85,7 @@ export var indented = function(input, level = 0) {
84
85
  // indentation is removed
85
86
  // - returns same type as text, i.e. either string or array
86
87
  export var undented = function(text, level = undef) {
87
- var i, lLines, lMatches, lNewLines, len, line, nToRemove, toRemove;
88
+ var i, j, lLines, lMatches, lNewLines, len, len1, line, nToRemove, toRemove;
88
89
  if ((level != null) && (level === 0)) {
89
90
  return text;
90
91
  }
@@ -95,6 +96,10 @@ export var undented = function(text, level = undef) {
95
96
  }
96
97
  } else if (isArray(text)) {
97
98
  lLines = text;
99
+ for (i = 0, len = lLines.length; i < len; i++) {
100
+ line = lLines[i];
101
+ assert(isString(line), "undented(): input array is not all strings");
102
+ }
98
103
  if (lLines.length === 0) {
99
104
  return [];
100
105
  }
@@ -111,8 +116,8 @@ export var undented = function(text, level = undef) {
111
116
  }
112
117
  nToRemove = toRemove.length;
113
118
  lNewLines = [];
114
- for (i = 0, len = lLines.length; i < len; i++) {
115
- line = lLines[i];
119
+ for (j = 0, len1 = lLines.length; j < len1; j++) {
120
+ line = lLines[j];
116
121
  if (isEmpty(line)) {
117
122
  lNewLines.push('');
118
123
  } else {