@jdeighan/coffee-utils 13.0.9 → 13.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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.11",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
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="\t") ->
33
+ export splitLine = (line, oneIndent=undef) ->
24
34
 
25
35
  [prefix, str] = splitPrefix(line)
26
36
  return [indentLevel(prefix, oneIndent), str]
@@ -38,22 +48,30 @@ export indentation = (level, oneIndent="\t") ->
38
48
  # indentLevel - determine indent level of a string
39
49
  # it's OK if the string is ONLY indentation
40
50
 
41
- export indentLevel = (line, oneIndent="\t") ->
51
+ export indentLevel = (line, oneIndent=undef) ->
42
52
 
43
- len = oneIndent.length
53
+ assert isString(line), "not a string"
44
54
 
45
- # --- This will always match
55
+ # --- This will always match, and it's greedy
46
56
  if lMatches = line.match(/^(\s*)/)
47
57
  prefix = lMatches[1]
48
58
  prefixLen = prefix.length
49
59
 
50
- remain = prefixLen % len
51
- if (remain != 0)
52
- throw new Error("prefix #{OL(prefix)} not a mult of #{OL(oneIndent)}")
60
+ if (prefixLen == 0)
61
+ return 0
62
+
63
+ if defined(oneIndent)
64
+ len = oneIndent.length
65
+ else
66
+ oneIndent = "\t"
67
+ len = 1
68
+
69
+ if (prefixLen % len != 0)
70
+ croak "prefix #{OL(prefix)} not a mult of #{OL(oneIndent)}"
53
71
 
54
72
  level = prefixLen / len
55
73
  if (prefix != oneIndent.repeat(level))
56
- throw new Error("prefix #{OL(prefix)} not a mult of #{OL(oneIndent)}")
74
+ croak "prefix #{OL(prefix)} not a mult of #{OL(oneIndent)}"
57
75
 
58
76
  return level
59
77
 
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 = "\t") {
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];
@@ -51,21 +62,29 @@ export var indentation = function(level, oneIndent = "\t") {
51
62
  // ---------------------------------------------------------------------------
52
63
  // indentLevel - determine indent level of a string
53
64
  // 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
65
+ export var indentLevel = function(line, oneIndent = undef) {
66
+ var lMatches, len, level, prefix, prefixLen;
67
+ assert(isString(line), "not a string");
68
+ // --- This will always match, and it's greedy
58
69
  if (lMatches = line.match(/^(\s*)/)) {
59
70
  prefix = lMatches[1];
60
71
  prefixLen = prefix.length;
61
72
  }
62
- remain = prefixLen % len;
63
- if (remain !== 0) {
64
- throw new Error(`prefix ${OL(prefix)} not a mult of ${OL(oneIndent)}`);
73
+ if (prefixLen === 0) {
74
+ return 0;
75
+ }
76
+ if (defined(oneIndent)) {
77
+ len = oneIndent.length;
78
+ } else {
79
+ oneIndent = "\t";
80
+ len = 1;
81
+ }
82
+ if (prefixLen % len !== 0) {
83
+ croak(`prefix ${OL(prefix)} not a mult of ${OL(oneIndent)}`);
65
84
  }
66
85
  level = prefixLen / len;
67
86
  if (prefix !== oneIndent.repeat(level)) {
68
- throw new Error(`prefix ${OL(prefix)} not a mult of ${OL(oneIndent)}`);
87
+ croak(`prefix ${OL(prefix)} not a mult of ${OL(oneIndent)}`);
69
88
  }
70
89
  return level;
71
90
  };