@jdeighan/coffee-utils 13.0.9 → 13.0.10

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": "13.0.9",
4
+ "version": "13.0.10",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
package/src/indent.coffee CHANGED
@@ -38,22 +38,30 @@ export indentation = (level, oneIndent="\t") ->
38
38
  # indentLevel - determine indent level of a string
39
39
  # it's OK if the string is ONLY indentation
40
40
 
41
- export indentLevel = (line, oneIndent="\t") ->
41
+ export indentLevel = (line, oneIndent=undef) ->
42
42
 
43
- len = oneIndent.length
43
+ assert isString(line), "not a string"
44
44
 
45
- # --- This will always match
45
+ # --- This will always match, and it's greedy
46
46
  if lMatches = line.match(/^(\s*)/)
47
47
  prefix = lMatches[1]
48
48
  prefixLen = prefix.length
49
49
 
50
- remain = prefixLen % len
51
- if (remain != 0)
52
- throw new Error("prefix #{OL(prefix)} not a mult of #{OL(oneIndent)}")
50
+ if (prefixLen == 0)
51
+ return 0
52
+
53
+ if defined(oneIndent)
54
+ len = oneIndent.length
55
+ else
56
+ oneIndent = "\t"
57
+ len = 1
58
+
59
+ if (prefixLen % len != 0)
60
+ croak "prefix #{OL(prefix)} not a mult of #{OL(oneIndent)}"
53
61
 
54
62
  level = prefixLen / len
55
63
  if (prefix != oneIndent.repeat(level))
56
- throw new Error("prefix #{OL(prefix)} not a mult of #{OL(oneIndent)}")
64
+ croak "prefix #{OL(prefix)} not a mult of #{OL(oneIndent)}"
57
65
 
58
66
  return level
59
67
 
package/src/indent.js CHANGED
@@ -51,21 +51,29 @@ export var indentation = function(level, oneIndent = "\t") {
51
51
  // ---------------------------------------------------------------------------
52
52
  // indentLevel - determine indent level of a string
53
53
  // it's OK if the string is ONLY indentation
54
- export var indentLevel = function(line, oneIndent = "\t") {
55
- var lMatches, len, level, prefix, prefixLen, remain;
56
- len = oneIndent.length;
57
- // --- This will always match
54
+ export var indentLevel = function(line, oneIndent = undef) {
55
+ var lMatches, len, level, prefix, prefixLen;
56
+ assert(isString(line), "not a string");
57
+ // --- This will always match, and it's greedy
58
58
  if (lMatches = line.match(/^(\s*)/)) {
59
59
  prefix = lMatches[1];
60
60
  prefixLen = prefix.length;
61
61
  }
62
- remain = prefixLen % len;
63
- if (remain !== 0) {
64
- throw new Error(`prefix ${OL(prefix)} not a mult of ${OL(oneIndent)}`);
62
+ if (prefixLen === 0) {
63
+ return 0;
64
+ }
65
+ if (defined(oneIndent)) {
66
+ len = oneIndent.length;
67
+ } else {
68
+ oneIndent = "\t";
69
+ len = 1;
70
+ }
71
+ if (prefixLen % len !== 0) {
72
+ croak(`prefix ${OL(prefix)} not a mult of ${OL(oneIndent)}`);
65
73
  }
66
74
  level = prefixLen / len;
67
75
  if (prefix !== oneIndent.repeat(level)) {
68
- throw new Error(`prefix ${OL(prefix)} not a mult of ${OL(oneIndent)}`);
76
+ croak(`prefix ${OL(prefix)} not a mult of ${OL(oneIndent)}`);
69
77
  }
70
78
  return level;
71
79
  };