@jdeighan/coffee-utils 10.0.16 → 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.16",
4
+ "version": "11.0.0",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -5,8 +5,9 @@ import readline from 'readline'
5
5
 
6
6
  import {assert, error, croak} from '@jdeighan/unit-tester/utils'
7
7
  import {
8
- undef, pass, defined, isEmpty, isString, isArray, nonEmpty, rtrim,
9
- escapeStr,
8
+ undef, pass, defined, notdefined,
9
+ isEmpty, isString, isArray, nonEmpty, isArrayOfStrings,
10
+ escapeStr, rtrim, OL,
10
11
  } from '@jdeighan/coffee-utils'
11
12
 
12
13
  # ---------------------------------------------------------------------------
@@ -90,9 +91,9 @@ export arrayToBlock = (lLines) ->
90
91
 
91
92
  export toBlock = (lLines) ->
92
93
 
93
- if (lLines == undef)
94
+ if notdefined(lLines)
94
95
  return undef
95
- assert isArray(lLines), "lLines is not an array"
96
+ assert isArrayOfStrings(lLines), "lLines is not an array: #{OL(lLines)}"
96
97
  lNewLines = []
97
98
  for line in lLines
98
99
  if defined(line)
@@ -14,12 +14,15 @@ import {
14
14
  undef,
15
15
  pass,
16
16
  defined,
17
+ notdefined,
17
18
  isEmpty,
18
19
  isString,
19
20
  isArray,
20
21
  nonEmpty,
22
+ isArrayOfStrings,
23
+ escapeStr,
21
24
  rtrim,
22
- escapeStr
25
+ OL
23
26
  } from '@jdeighan/coffee-utils';
24
27
 
25
28
  // ---------------------------------------------------------------------------
@@ -113,10 +116,10 @@ export var arrayToBlock = function(lLines) {
113
116
  // but undef items are ignored
114
117
  export var toBlock = function(lLines) {
115
118
  var i, lNewLines, len1, line;
116
- if (lLines === undef) {
119
+ if (notdefined(lLines)) {
117
120
  return undef;
118
121
  }
119
- assert(isArray(lLines), "lLines is not an array");
122
+ assert(isArrayOfStrings(lLines), `lLines is not an array: ${OL(lLines)}`);
120
123
  lNewLines = [];
121
124
  for (i = 0, len1 = lLines.length; i < len1; i++) {
122
125
  line = lLines[i];
@@ -647,3 +647,9 @@ export getOptions = (hOptions, hDefault={}) ->
647
647
  return hOptions
648
648
  else
649
649
  return hDefault
650
+
651
+ # ---------------------------------------------------------------------------
652
+
653
+ export timestamp = () ->
654
+
655
+ return new Date().toLocaleTimeString("en-US")
@@ -731,3 +731,8 @@ export var getOptions = function(hOptions, hDefault = {}) {
731
731
  return hDefault;
732
732
  }
733
733
  };
734
+
735
+ // ---------------------------------------------------------------------------
736
+ export var timestamp = function() {
737
+ return new Date().toLocaleTimeString("en-US");
738
+ };
@@ -68,7 +68,8 @@ export isUndented = (line) ->
68
68
  return (lMatches[0].length == 0)
69
69
 
70
70
  # ---------------------------------------------------------------------------
71
- # indented - add indentation to each string in a block
71
+ # indented - add indentation to each string in a block or array
72
+ # - returns the same type as input, i.e. array or string
72
73
 
73
74
  export indented = (input, level=1, oneIndent="\t") ->
74
75
 
@@ -77,14 +78,20 @@ export indented = (input, level=1, oneIndent="\t") ->
77
78
  return input
78
79
 
79
80
  toAdd = indentation(level, oneIndent)
80
- lInputLines = toArray(input)
81
81
 
82
- 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)
83
85
  if isEmpty(line)
84
86
  ""
85
87
  else
86
88
  "#{toAdd}#{line}"
87
- 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)}"
88
95
 
89
96
  # ---------------------------------------------------------------------------
90
97
  # undented - string with 1st line indentation removed for each line
@@ -81,20 +81,23 @@ export var isUndented = function(line) {
81
81
  };
82
82
 
83
83
  // ---------------------------------------------------------------------------
84
- // indented - add indentation to each string in a block
84
+ // indented - add indentation to each string in a block or array
85
+ // - returns the same type as input, i.e. array or string
85
86
  export var indented = function(input, level = 1, oneIndent = "\t") {
86
- var lInputLines, lLines, line, toAdd;
87
+ var lLines, line, toAdd;
87
88
  assert(level >= 0, "indented(): negative level");
88
89
  if (level === 0) {
89
90
  return input;
90
91
  }
91
92
  toAdd = indentation(level, oneIndent);
92
- 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
93
95
  lLines = (function() {
94
- var i, len1, results;
96
+ var i, len1, ref, results;
97
+ ref = toArray(input);
95
98
  results = [];
96
- for (i = 0, len1 = lInputLines.length; i < len1; i++) {
97
- line = lInputLines[i];
99
+ for (i = 0, len1 = ref.length; i < len1; i++) {
100
+ line = ref[i];
98
101
  if (isEmpty(line)) {
99
102
  results.push("");
100
103
  } else {
@@ -103,7 +106,13 @@ export var indented = function(input, level = 1, oneIndent = "\t") {
103
106
  }
104
107
  return results;
105
108
  })();
106
- 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
+ }
107
116
  };
108
117
 
109
118
  // ---------------------------------------------------------------------------