@jdeighan/coffee-utils 11.0.0 → 11.0.2

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": "11.0.0",
4
+ "version": "11.0.2",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -5,7 +5,6 @@ import {
5
5
  undef, defined, OL, escapeStr, deepCopy,
6
6
  isArray, isBoolean,
7
7
  } from '@jdeighan/coffee-utils'
8
- import {LOG} from '@jdeighan/coffee-utils/log'
9
8
 
10
9
  doDebugStack = false
11
10
 
@@ -29,7 +28,7 @@ export class CallStack
29
28
  reset: () ->
30
29
 
31
30
  if doDebugStack
32
- LOG "RESET STACK"
31
+ console.log "RESET STACK"
33
32
  @lStack = []
34
33
  return
35
34
 
@@ -48,7 +47,7 @@ export class CallStack
48
47
  assert isBoolean(isLogged), "missing isLogged"
49
48
 
50
49
  if doDebugStack
51
- LOG @indent() + "[--> ENTER #{funcName}]"
50
+ console.log @indent() + "[--> ENTER #{funcName}]"
52
51
 
53
52
  lMatches = funcName.match(///^
54
53
  ([A-Za-z_][A-Za-z0-9_]*)
@@ -101,13 +100,13 @@ export class CallStack
101
100
  returnFrom: (fName) ->
102
101
 
103
102
  if @lStack.length == 0
104
- LOG "ERROR: returnFrom('#{fName}') but stack is empty"
103
+ console.log "ERROR: returnFrom('#{fName}') but stack is empty"
105
104
  return
106
105
  {fullName, isLogged} = @lStack.pop()
107
106
  if doDebugStack
108
- LOG @indent() + "[<-- BACK #{fName}]"
107
+ console.log @indent() + "[<-- BACK #{fName}]"
109
108
  if (fullName != fName)
110
- LOG "ERROR: returnFrom('#{fName}') but TOS is #{fullName}"
109
+ console.log "ERROR: returnFrom('#{fName}') but TOS is #{fullName}"
111
110
  return
112
111
 
113
112
  return
package/src/call_stack.js CHANGED
@@ -17,10 +17,6 @@ import {
17
17
  isBoolean
18
18
  } from '@jdeighan/coffee-utils';
19
19
 
20
- import {
21
- LOG
22
- } from '@jdeighan/coffee-utils/log';
23
-
24
20
  doDebugStack = false;
25
21
 
26
22
  // ---------------------------------------------------------------------------
@@ -37,7 +33,7 @@ export var CallStack = class CallStack {
37
33
  // ........................................................................
38
34
  reset() {
39
35
  if (doDebugStack) {
40
- LOG("RESET STACK");
36
+ console.log("RESET STACK");
41
37
  }
42
38
  this.lStack = [];
43
39
  }
@@ -54,7 +50,7 @@ export var CallStack = class CallStack {
54
50
  assert(isArray(lArgs), "missing lArgs");
55
51
  assert(isBoolean(isLogged), "missing isLogged");
56
52
  if (doDebugStack) {
57
- LOG(this.indent() + `[--> ENTER ${funcName}]`);
53
+ console.log(this.indent() + `[--> ENTER ${funcName}]`);
58
54
  }
59
55
  lMatches = funcName.match(/^([A-Za-z_][A-Za-z0-9_]*)(?:\.([A-Za-z_][A-Za-z0-9_]*))?$/);
60
56
  assert(defined(lMatches), `Bad funcName: ${OL(funcName)}`);
@@ -106,15 +102,15 @@ export var CallStack = class CallStack {
106
102
  returnFrom(fName) {
107
103
  var fullName, isLogged;
108
104
  if (this.lStack.length === 0) {
109
- LOG(`ERROR: returnFrom('${fName}') but stack is empty`);
105
+ console.log(`ERROR: returnFrom('${fName}') but stack is empty`);
110
106
  return;
111
107
  }
112
108
  ({fullName, isLogged} = this.lStack.pop());
113
109
  if (doDebugStack) {
114
- LOG(this.indent() + `[<-- BACK ${fName}]`);
110
+ console.log(this.indent() + `[<-- BACK ${fName}]`);
115
111
  }
116
112
  if (fullName !== fName) {
117
- LOG(`ERROR: returnFrom('${fName}') but TOS is ${fullName}`);
113
+ console.log(`ERROR: returnFrom('${fName}') but TOS is ${fullName}`);
118
114
  return;
119
115
  }
120
116
  }
@@ -162,13 +162,22 @@ export isBoolean = (x) ->
162
162
 
163
163
  # ---------------------------------------------------------------------------
164
164
 
165
- export isObject = (x) ->
165
+ export isObject = (x, lReqKeys=undef) ->
166
166
 
167
- return (typeof x == 'object') \
167
+ result = (typeof x == 'object') \
168
168
  && ! isString(x) \
169
169
  && ! isArray(x) \
170
170
  && ! isHash(x) \
171
171
  && ! isNumber(x)
172
+ if result
173
+ if defined(lReqKeys)
174
+ assert isArray(lReqKeys), "lReqKeys is not an array"
175
+ for key in lReqKeys
176
+ if ! x.hasOwnProperty(key)
177
+ return false
178
+ return true
179
+ else
180
+ return false
172
181
 
173
182
  # ---------------------------------------------------------------------------
174
183
 
@@ -177,8 +177,23 @@ export var isBoolean = function(x) {
177
177
  };
178
178
 
179
179
  // ---------------------------------------------------------------------------
180
- export var isObject = function(x) {
181
- return (typeof x === 'object') && !isString(x) && !isArray(x) && !isHash(x) && !isNumber(x);
180
+ export var isObject = function(x, lReqKeys = undef) {
181
+ var i, key, len1, result;
182
+ result = (typeof x === 'object') && !isString(x) && !isArray(x) && !isHash(x) && !isNumber(x);
183
+ if (result) {
184
+ if (defined(lReqKeys)) {
185
+ assert(isArray(lReqKeys), "lReqKeys is not an array");
186
+ for (i = 0, len1 = lReqKeys.length; i < len1; i++) {
187
+ key = lReqKeys[i];
188
+ if (!x.hasOwnProperty(key)) {
189
+ return false;
190
+ }
191
+ }
192
+ }
193
+ return true;
194
+ } else {
195
+ return false;
196
+ }
182
197
  };
183
198
 
184
199
  // ---------------------------------------------------------------------------
@@ -1,7 +1,5 @@
1
1
  # log_utils.coffee
2
2
 
3
- import yaml from 'js-yaml'
4
-
5
3
  import {assert, error, croak} from '@jdeighan/unit-tester/utils'
6
4
  import {
7
5
  undef, isNumber, isInteger, isString, isHash, isFunction,
@@ -11,6 +9,7 @@ import {blockToArray} from '@jdeighan/coffee-utils/block'
11
9
  import {
12
10
  tabify, untabify, indentation, indented,
13
11
  } from '@jdeighan/coffee-utils/indent'
12
+ import {toTAML} from '@jdeighan/coffee-utils/taml'
14
13
 
15
14
  # --- This logger only ever gets passed a single string argument
16
15
  putstr = undef
@@ -96,39 +95,24 @@ export resetLogger = () ->
96
95
 
97
96
  # ---------------------------------------------------------------------------
98
97
 
99
- escReplacer = (name, value) ->
100
-
101
- if ! isString(value)
102
- return value
103
- return escapeStr(value)
104
-
105
- # ---------------------------------------------------------------------------
106
-
107
98
  export tamlStringify = (obj, escape=false) ->
108
99
 
109
- str = yaml.dump(obj, {
110
- skipInvalid: true
111
- indent: 1
100
+ return toTAML(obj, {
101
+ useTabs: false
112
102
  sortKeys: false
113
- lineWidth: -1
114
- replacer: if escape then escReplacer else (name,value) -> value
103
+ escape
115
104
  })
116
- return "---\n" + str
117
105
 
118
106
  # ---------------------------------------------------------------------------
119
107
 
120
108
  export orderedStringify = (obj, escape=false) ->
121
109
 
122
- str = yaml.dump(obj, {
123
- skipInvalid: true
124
- indent: 1
110
+ return toTAML(obj, {
111
+ useTabs: false
125
112
  sortKeys: true
126
- lineWidth: 40
127
- replacer: if escape then escReplacer else (name,value) -> value
113
+ escape
128
114
  })
129
115
 
130
- return "---\n" + str
131
-
132
116
  # ---------------------------------------------------------------------------
133
117
 
134
118
  maxOneLine = 32
package/src/log_utils.js CHANGED
@@ -1,8 +1,6 @@
1
1
  // Generated by CoffeeScript 2.7.0
2
2
  // log_utils.coffee
3
- var doDebugLog, escReplacer, fixForTerminal, fourSpaces, loaded, maxOneLine, putBlock, putstr;
4
-
5
- import yaml from 'js-yaml';
3
+ var doDebugLog, fixForTerminal, fourSpaces, loaded, maxOneLine, putBlock, putstr;
6
4
 
7
5
  import {
8
6
  assert,
@@ -35,6 +33,10 @@ import {
35
33
  indented
36
34
  } from '@jdeighan/coffee-utils/indent';
37
35
 
36
+ import {
37
+ toTAML
38
+ } from '@jdeighan/coffee-utils/taml';
39
+
38
40
  // --- This logger only ever gets passed a single string argument
39
41
  putstr = undef;
40
42
 
@@ -122,42 +124,22 @@ export var resetLogger = function() {
122
124
  return setLogger(console.log);
123
125
  };
124
126
 
125
- // ---------------------------------------------------------------------------
126
- escReplacer = function(name, value) {
127
- if (!isString(value)) {
128
- return value;
129
- }
130
- return escapeStr(value);
131
- };
132
-
133
127
  // ---------------------------------------------------------------------------
134
128
  export var tamlStringify = function(obj, escape = false) {
135
- var str;
136
- str = yaml.dump(obj, {
137
- skipInvalid: true,
138
- indent: 1,
129
+ return toTAML(obj, {
130
+ useTabs: false,
139
131
  sortKeys: false,
140
- lineWidth: -1,
141
- replacer: escape ? escReplacer : function(name, value) {
142
- return value;
143
- }
132
+ escape
144
133
  });
145
- return "---\n" + str;
146
134
  };
147
135
 
148
136
  // ---------------------------------------------------------------------------
149
137
  export var orderedStringify = function(obj, escape = false) {
150
- var str;
151
- str = yaml.dump(obj, {
152
- skipInvalid: true,
153
- indent: 1,
138
+ return toTAML(obj, {
139
+ useTabs: false,
154
140
  sortKeys: true,
155
- lineWidth: 40,
156
- replacer: escape ? escReplacer : function(name, value) {
157
- return value;
158
- }
141
+ escape
159
142
  });
160
- return "---\n" + str;
161
143
  };
162
144
 
163
145
  // ---------------------------------------------------------------------------
package/src/taml.coffee CHANGED
@@ -4,7 +4,7 @@ import yaml from 'js-yaml'
4
4
 
5
5
  import {assert, error, croak} from '@jdeighan/unit-tester/utils'
6
6
  import {
7
- undef, oneline, isString, chomp,
7
+ undef, defined, notdefined, oneline, isString, chomp, escapeStr,
8
8
  } from '@jdeighan/coffee-utils'
9
9
  import {untabify, tabify, splitLine} from '@jdeighan/coffee-utils/indent'
10
10
  import {slurp} from '@jdeighan/coffee-utils/fs'
@@ -63,10 +63,29 @@ export taml = (text) ->
63
63
  export fromTAML = taml
64
64
 
65
65
  # ---------------------------------------------------------------------------
66
+ # --- a replacer is (key, value) -> newvalue
66
67
 
67
- export toTAML = (x, useTabs=false) ->
68
+ myReplacer = (name, value) ->
68
69
 
69
- str = yaml.dump(x, {indent:3})
70
+ if isString(value)
71
+ return escapeStr(value)
72
+ else
73
+ return value
74
+
75
+ # ---------------------------------------------------------------------------
76
+
77
+ export toTAML = (obj, hOptions={}) ->
78
+
79
+ {useTabs, sortKeys, escape, replacer} = hOptions
80
+ if notdefined(replacer)
81
+ replacer = myReplacer
82
+ str = yaml.dump(obj, {
83
+ skipInvalid: true
84
+ indent: 3
85
+ sortKeys: !!sortKeys
86
+ lineWidth: -1
87
+ replacer
88
+ })
70
89
  if useTabs
71
90
  str = str.replace(/ /g, "\t")
72
91
  return "---\n" + chomp(str)
package/src/taml.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.7.0
2
2
  // taml.coffee
3
- var squote;
3
+ var myReplacer, squote;
4
4
 
5
5
  import yaml from 'js-yaml';
6
6
 
@@ -12,9 +12,12 @@ import {
12
12
 
13
13
  import {
14
14
  undef,
15
+ defined,
16
+ notdefined,
15
17
  oneline,
16
18
  isString,
17
- chomp
19
+ chomp,
20
+ escapeStr
18
21
  } from '@jdeighan/coffee-utils';
19
22
 
20
23
  import {
@@ -89,10 +92,28 @@ export var taml = function(text) {
89
92
  export var fromTAML = taml;
90
93
 
91
94
  // ---------------------------------------------------------------------------
92
- export var toTAML = function(x, useTabs = false) {
93
- var str;
94
- str = yaml.dump(x, {
95
- indent: 3
95
+ // --- a replacer is (key, value) -> newvalue
96
+ myReplacer = function(name, value) {
97
+ if (isString(value)) {
98
+ return escapeStr(value);
99
+ } else {
100
+ return value;
101
+ }
102
+ };
103
+
104
+ // ---------------------------------------------------------------------------
105
+ export var toTAML = function(obj, hOptions = {}) {
106
+ var escape, replacer, sortKeys, str, useTabs;
107
+ ({useTabs, sortKeys, escape, replacer} = hOptions);
108
+ if (notdefined(replacer)) {
109
+ replacer = myReplacer;
110
+ }
111
+ str = yaml.dump(obj, {
112
+ skipInvalid: true,
113
+ indent: 3,
114
+ sortKeys: !!sortKeys,
115
+ lineWidth: -1,
116
+ replacer
96
117
  });
97
118
  if (useTabs) {
98
119
  str = str.replace(/ /g, "\t");