@jdeighan/coffee-utils 9.0.4 → 9.0.5

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": "9.0.4",
4
+ "version": "9.0.5",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -25,6 +25,23 @@ export blockToArray = (block) ->
25
25
  len -= 1
26
26
  return lLines
27
27
 
28
+ # ---------------------------------------------------------------------------
29
+ # toArray - split a block or array into lines w/o newlines
30
+
31
+ export toArray = (item) ->
32
+
33
+ if isString(item)
34
+ return item.split(/\r?\n/)
35
+ else if isArray(item)
36
+ # --- We still need to ensure that no strings contain newlines
37
+ lLines = []
38
+ for str in item
39
+ for substr in toArray(str)
40
+ lLines.push substr
41
+ return lLines
42
+ else
43
+ croak "Not a string or array"
44
+
28
45
  # ---------------------------------------------------------------------------
29
46
  # arrayToBlock - block will have no trailing whitespace
30
47
 
@@ -39,6 +56,21 @@ export arrayToBlock = (lLines) ->
39
56
  else
40
57
  return rtrim(lLines.join('\n'))
41
58
 
59
+ # ---------------------------------------------------------------------------
60
+ # toBlock - block may have trailing whitespace
61
+ # but undef items are ignored
62
+
63
+ export toBlock = (lLines) ->
64
+
65
+ if (lLines == undef)
66
+ return undef
67
+ assert isArray(lLines), "lLines is not an array"
68
+ lLines = lLines.filter((line) => defined(line));
69
+ if lLines.length == 0
70
+ return undef
71
+ else
72
+ return lLines.join('\n')
73
+
42
74
  # ---------------------------------------------------------------------------
43
75
 
44
76
  export splitBlock = (block) ->
@@ -37,6 +37,29 @@ export var blockToArray = function(block) {
37
37
  }
38
38
  };
39
39
 
40
+ // ---------------------------------------------------------------------------
41
+ // toArray - split a block or array into lines w/o newlines
42
+ export var toArray = function(item) {
43
+ var i, j, lLines, len1, len2, ref, str, substr;
44
+ if (isString(item)) {
45
+ return item.split(/\r?\n/);
46
+ } else if (isArray(item)) {
47
+ // --- We still need to ensure that no strings contain newlines
48
+ lLines = [];
49
+ for (i = 0, len1 = item.length; i < len1; i++) {
50
+ str = item[i];
51
+ ref = toArray(str);
52
+ for (j = 0, len2 = ref.length; j < len2; j++) {
53
+ substr = ref[j];
54
+ lLines.push(substr);
55
+ }
56
+ }
57
+ return lLines;
58
+ } else {
59
+ return croak("Not a string or array");
60
+ }
61
+ };
62
+
40
63
  // ---------------------------------------------------------------------------
41
64
  // arrayToBlock - block will have no trailing whitespace
42
65
  export var arrayToBlock = function(lLines) {
@@ -54,6 +77,24 @@ export var arrayToBlock = function(lLines) {
54
77
  }
55
78
  };
56
79
 
80
+ // ---------------------------------------------------------------------------
81
+ // toBlock - block may have trailing whitespace
82
+ // but undef items are ignored
83
+ export var toBlock = function(lLines) {
84
+ if (lLines === undef) {
85
+ return undef;
86
+ }
87
+ assert(isArray(lLines), "lLines is not an array");
88
+ lLines = lLines.filter((line) => {
89
+ return defined(line);
90
+ });
91
+ if (lLines.length === 0) {
92
+ return undef;
93
+ } else {
94
+ return lLines.join('\n');
95
+ }
96
+ };
97
+
57
98
  // ---------------------------------------------------------------------------
58
99
  export var splitBlock = function(block) {
59
100
  var pos;
@@ -5,7 +5,9 @@ import {
5
5
  undef, escapeStr, defined,
6
6
  OL, isInteger, isString, isArray, isEmpty, rtrim,
7
7
  } from '@jdeighan/coffee-utils'
8
- import {arrayToBlock, blockToArray} from '@jdeighan/coffee-utils/block'
8
+ import {
9
+ arrayToBlock, blockToArray, toArray, toBlock,
10
+ } from '@jdeighan/coffee-utils/block'
9
11
 
10
12
  # ---------------------------------------------------------------------------
11
13
 
@@ -71,14 +73,11 @@ export isUndented = (line) ->
71
73
  export indented = (input, level=1, oneIndent="\t") ->
72
74
 
73
75
  assert (level >= 0), "indented(): negative level"
74
- if level == 0
76
+ if (level == 0)
75
77
  return input
76
78
 
77
79
  toAdd = indentation(level, oneIndent)
78
- if isArray(input)
79
- lInputLines = input
80
- else
81
- lInputLines = blockToArray(input)
80
+ lInputLines = toArray(input)
82
81
 
83
82
  lLines = for line in lInputLines
84
83
  if isEmpty(line)
@@ -93,27 +92,18 @@ export indented = (input, level=1, oneIndent="\t") ->
93
92
  # indentation is removed
94
93
  # - returns same type as text, i.e. either string or array
95
94
 
96
- export undented = (text, level=undef, oneIndent="\t") ->
95
+ export undented = (input, level=undef, oneIndent="\t") ->
97
96
 
98
97
  if defined(level) && (level==0)
99
- return text
100
-
101
- if isString(text)
102
- lLines = blockToArray(text)
103
- if (lLines.length == 0)
104
- return ''
105
- else if isArray(text)
106
- lLines = text
107
- for line in lLines
108
- assert isString(line), "undented(): array not all strings"
109
- if (lLines.length == 0)
110
- return []
111
- else
112
- error "undented(): Not an array or string: #{OL(text)}"
98
+ return input
99
+
100
+ lLines = toArray(input)
101
+ if (lLines.length == 0)
102
+ return ''
113
103
 
114
104
  # --- determine what to remove from beginning of each line
115
105
  if defined(level)
116
- assert isInteger(level), "undented(): level must be an integer"
106
+ assert isInteger(level), "level must be an integer"
117
107
  toRemove = indentation(level, oneIndent)
118
108
  else
119
109
  lMatches = lLines[0].match(/^\s*/)
@@ -129,8 +119,8 @@ export undented = (text, level=undef, oneIndent="\t") ->
129
119
  throw new Error("remove #{OL(toRemove)} from #{OL(text)}")
130
120
  lNewLines.push(line.substr(nToRemove))
131
121
 
132
- if isString(text)
133
- return arrayToBlock(lNewLines)
122
+ if isString(input)
123
+ return toBlock(lNewLines)
134
124
  else
135
125
  return lNewLines
136
126
 
@@ -19,7 +19,9 @@ import {
19
19
 
20
20
  import {
21
21
  arrayToBlock,
22
- blockToArray
22
+ blockToArray,
23
+ toArray,
24
+ toBlock
23
25
  } from '@jdeighan/coffee-utils/block';
24
26
 
25
27
  // ---------------------------------------------------------------------------
@@ -87,11 +89,7 @@ export var indented = function(input, level = 1, oneIndent = "\t") {
87
89
  return input;
88
90
  }
89
91
  toAdd = indentation(level, oneIndent);
90
- if (isArray(input)) {
91
- lInputLines = input;
92
- } else {
93
- lInputLines = blockToArray(input);
94
- }
92
+ lInputLines = toArray(input);
95
93
  lLines = (function() {
96
94
  var i, len1, results;
97
95
  results = [];
@@ -113,31 +111,18 @@ export var indented = function(input, level = 1, oneIndent = "\t") {
113
111
  // - unless level is set, in which case exactly that
114
112
  // indentation is removed
115
113
  // - returns same type as text, i.e. either string or array
116
- export var undented = function(text, level = undef, oneIndent = "\t") {
117
- var i, j, lLines, lMatches, lNewLines, len1, len2, line, nToRemove, toRemove;
114
+ export var undented = function(input, level = undef, oneIndent = "\t") {
115
+ var i, lLines, lMatches, lNewLines, len1, line, nToRemove, toRemove;
118
116
  if (defined(level) && (level === 0)) {
119
- return text;
117
+ return input;
120
118
  }
121
- if (isString(text)) {
122
- lLines = blockToArray(text);
123
- if (lLines.length === 0) {
124
- return '';
125
- }
126
- } else if (isArray(text)) {
127
- lLines = text;
128
- for (i = 0, len1 = lLines.length; i < len1; i++) {
129
- line = lLines[i];
130
- assert(isString(line), "undented(): array not all strings");
131
- }
132
- if (lLines.length === 0) {
133
- return [];
134
- }
135
- } else {
136
- error(`undented(): Not an array or string: ${OL(text)}`);
119
+ lLines = toArray(input);
120
+ if (lLines.length === 0) {
121
+ return '';
137
122
  }
138
123
  // --- determine what to remove from beginning of each line
139
124
  if (defined(level)) {
140
- assert(isInteger(level), "undented(): level must be an integer");
125
+ assert(isInteger(level), "level must be an integer");
141
126
  toRemove = indentation(level, oneIndent);
142
127
  } else {
143
128
  lMatches = lLines[0].match(/^\s*/);
@@ -145,8 +130,8 @@ export var undented = function(text, level = undef, oneIndent = "\t") {
145
130
  }
146
131
  nToRemove = indentLevel(toRemove);
147
132
  lNewLines = [];
148
- for (j = 0, len2 = lLines.length; j < len2; j++) {
149
- line = lLines[j];
133
+ for (i = 0, len1 = lLines.length; i < len1; i++) {
134
+ line = lLines[i];
150
135
  if (isEmpty(line)) {
151
136
  lNewLines.push('');
152
137
  } else {
@@ -156,8 +141,8 @@ export var undented = function(text, level = undef, oneIndent = "\t") {
156
141
  lNewLines.push(line.substr(nToRemove));
157
142
  }
158
143
  }
159
- if (isString(text)) {
160
- return arrayToBlock(lNewLines);
144
+ if (isString(input)) {
145
+ return toBlock(lNewLines);
161
146
  } else {
162
147
  return lNewLines;
163
148
  }