@jdeighan/coffee-utils 11.0.1 → 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.1",
4
+ "version": "11.0.2",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -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
  // ---------------------------------------------------------------------------
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, escapeStr,
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,24 +63,28 @@ export taml = (text) ->
63
63
  export fromTAML = taml
64
64
 
65
65
  # ---------------------------------------------------------------------------
66
+ # --- a replacer is (key, value) -> newvalue
66
67
 
67
- escReplacer = (name, value) ->
68
+ myReplacer = (name, value) ->
68
69
 
69
- if ! isString(value)
70
+ if isString(value)
71
+ return escapeStr(value)
72
+ else
70
73
  return value
71
- return escapeStr(value)
72
74
 
73
75
  # ---------------------------------------------------------------------------
74
76
 
75
77
  export toTAML = (obj, hOptions={}) ->
76
78
 
77
- {useTabs, sortKeys, escape} = hOptions
79
+ {useTabs, sortKeys, escape, replacer} = hOptions
80
+ if notdefined(replacer)
81
+ replacer = myReplacer
78
82
  str = yaml.dump(obj, {
79
83
  skipInvalid: true
80
84
  indent: 3
81
85
  sortKeys: !!sortKeys
82
86
  lineWidth: -1
83
- replacer: if !!escape then escReplacer else (name,value) -> value
87
+ replacer
84
88
  })
85
89
  if useTabs
86
90
  str = str.replace(/ /g, "\t")
package/src/taml.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.7.0
2
2
  // taml.coffee
3
- var escReplacer, squote;
3
+ var myReplacer, squote;
4
4
 
5
5
  import yaml from 'js-yaml';
6
6
 
@@ -12,6 +12,8 @@ import {
12
12
 
13
13
  import {
14
14
  undef,
15
+ defined,
16
+ notdefined,
15
17
  oneline,
16
18
  isString,
17
19
  chomp,
@@ -90,25 +92,28 @@ export var taml = function(text) {
90
92
  export var fromTAML = taml;
91
93
 
92
94
  // ---------------------------------------------------------------------------
93
- escReplacer = function(name, value) {
94
- if (!isString(value)) {
95
+ // --- a replacer is (key, value) -> newvalue
96
+ myReplacer = function(name, value) {
97
+ if (isString(value)) {
98
+ return escapeStr(value);
99
+ } else {
95
100
  return value;
96
101
  }
97
- return escapeStr(value);
98
102
  };
99
103
 
100
104
  // ---------------------------------------------------------------------------
101
105
  export var toTAML = function(obj, hOptions = {}) {
102
- var escape, sortKeys, str, useTabs;
103
- ({useTabs, sortKeys, escape} = hOptions);
106
+ var escape, replacer, sortKeys, str, useTabs;
107
+ ({useTabs, sortKeys, escape, replacer} = hOptions);
108
+ if (notdefined(replacer)) {
109
+ replacer = myReplacer;
110
+ }
104
111
  str = yaml.dump(obj, {
105
112
  skipInvalid: true,
106
113
  indent: 3,
107
114
  sortKeys: !!sortKeys,
108
115
  lineWidth: -1,
109
- replacer: !!escape ? escReplacer : function(name, value) {
110
- return value;
111
- }
116
+ replacer
112
117
  });
113
118
  if (useTabs) {
114
119
  str = str.replace(/ /g, "\t");