@jdeighan/coffee-utils 10.0.17 → 11.0.0

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": "10.0.17",
4
+ "version": "11.0.0",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -69,7 +69,7 @@ export isUndented = (line) ->
69
69
 
70
70
  # ---------------------------------------------------------------------------
71
71
  # indented - add indentation to each string in a block or array
72
- # - always returns a string
72
+ # - returns the same type as input, i.e. array or string
73
73
 
74
74
  export indented = (input, level=1, oneIndent="\t") ->
75
75
 
@@ -78,14 +78,20 @@ export indented = (input, level=1, oneIndent="\t") ->
78
78
  return input
79
79
 
80
80
  toAdd = indentation(level, oneIndent)
81
- lInputLines = toArray(input)
82
81
 
83
- lLines = for line in lInputLines
82
+ # --- NOTE: toArray(input) just returns input if it's an array
83
+ # else it splits the string into an array of lines
84
+ lLines = for line in toArray(input)
84
85
  if isEmpty(line)
85
86
  ""
86
87
  else
87
88
  "#{toAdd}#{line}"
88
- return arrayToBlock(lLines)
89
+ if isArray(input)
90
+ return lLines
91
+ else if isString(input)
92
+ return toBlock(lLines)
93
+ else
94
+ croak "Invalid input; #{OL(input)}"
89
95
 
90
96
  # ---------------------------------------------------------------------------
91
97
  # undented - string with 1st line indentation removed for each line
@@ -82,20 +82,22 @@ export var isUndented = function(line) {
82
82
 
83
83
  // ---------------------------------------------------------------------------
84
84
  // indented - add indentation to each string in a block or array
85
- // - always returns a string
85
+ // - returns the same type as input, i.e. array or string
86
86
  export var indented = function(input, level = 1, oneIndent = "\t") {
87
- var lInputLines, lLines, line, toAdd;
87
+ var lLines, line, toAdd;
88
88
  assert(level >= 0, "indented(): negative level");
89
89
  if (level === 0) {
90
90
  return input;
91
91
  }
92
92
  toAdd = indentation(level, oneIndent);
93
- lInputLines = toArray(input);
93
+ // --- NOTE: toArray(input) just returns input if it's an array
94
+ // else it splits the string into an array of lines
94
95
  lLines = (function() {
95
- var i, len1, results;
96
+ var i, len1, ref, results;
97
+ ref = toArray(input);
96
98
  results = [];
97
- for (i = 0, len1 = lInputLines.length; i < len1; i++) {
98
- line = lInputLines[i];
99
+ for (i = 0, len1 = ref.length; i < len1; i++) {
100
+ line = ref[i];
99
101
  if (isEmpty(line)) {
100
102
  results.push("");
101
103
  } else {
@@ -104,7 +106,13 @@ export var indented = function(input, level = 1, oneIndent = "\t") {
104
106
  }
105
107
  return results;
106
108
  })();
107
- return arrayToBlock(lLines);
109
+ if (isArray(input)) {
110
+ return lLines;
111
+ } else if (isString(input)) {
112
+ return toBlock(lLines);
113
+ } else {
114
+ return croak(`Invalid input; ${OL(input)}`);
115
+ }
108
116
  };
109
117
 
110
118
  // ---------------------------------------------------------------------------