@lumjs/core 1.15.0 → 1.18.0

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.
@@ -2,7 +2,7 @@ const def = require('./def');
2
2
  const {F} = require('./js');
3
3
 
4
4
  /**
5
- * A super simple wrapper around the Javascript console.
5
+ * A super simple singleton wrapper around the Javascript console.
6
6
  *
7
7
  * By default includes `info`, `log`, `warn`, and `error` methods.
8
8
  * It may be expanded further using the `addMethod` method.
@@ -14,25 +14,68 @@ const LC =
14
14
  /**
15
15
  * A custom handler.
16
16
  *
17
- * If a `function`, will be passed the console method name as the
18
- * first parameter, and method arguments as subsequent parameters.
17
+ * If a `function`, it will be called to handle all method calls.
19
18
  *
20
19
  * If an `Array`, it will have all log entries added as objects with
21
- * `time`, `method`, and `arguments` properties.
20
+ * at least `time`, `method`, and `opts` properties.
21
+ * By default and `arguments` property will also be included.
22
+ * See `msgArguments` and `msgArgs` for further details.
22
23
  *
23
24
  * If the boolean value `false`, all output will be thrown into the void.
24
25
  *
25
- * Any other value (including the default `null`), passes the method call
26
- * through to the real console object.
26
+ * If it is an `object`, then each property name represents a method name,
27
+ * and the values will be treated as a handler for that method only.
28
+ * All handler types are supported (i.e. `function`, `Array`, or `false`.)
29
+ * A special method name of `DEFAULT` may be specified as a default handler
30
+ * for method names not explicitly specified in the object.
31
+ * If no `DEFAULT` is specified and there is no handler value for a method
32
+ * name, calls to it will be passed through to the real console object.
33
+ *
34
+ * If no handler was set, all methods are passed to the real console object.
35
+ *
36
+ * Default value: `null`
27
37
  */
28
- handler: null,
38
+ handler: null,
39
+
29
40
  /**
30
41
  * Return the message object?
31
42
  *
32
43
  * Only applicable if `handler` is an `Array`.
33
- * If true, return the message object. If false, returns void/undefined.
44
+ *
45
+ * If `true`, return the message `object`.
46
+ * If `false`, returns `undefined`.
47
+ *
48
+ * Default value: `false`
49
+ */
50
+ returnMsg: false,
51
+
52
+ /**
53
+ * The name to include the `arguments` magic object in the log.
54
+ *
55
+ * Only applicable if `handler` is an `Array`.
56
+ *
57
+ * This used to be hard-coded as `"arguments"` but the `shortArgs`
58
+ * option has replaced it as the array is simpler for most purposes.
59
+ *
60
+ * Default value: `null`
61
+ */
62
+ msgArguments: null,
63
+
64
+ /**
65
+ * The name to include the `args` array in the log.
66
+ *
67
+ * Only applicable if `handler` is an `Array`.
68
+ *
69
+ * Default value: `"arguments"`
34
70
  */
35
- returnMsg: false
71
+ msgArgs: 'arguments',
72
+
73
+ /**
74
+ * Use the method name as the first argument in function calls.
75
+ *
76
+ * Default value: `true`
77
+ */
78
+ methodArg: true,
36
79
  }
37
80
 
38
81
  /**
@@ -45,6 +88,12 @@ def(LC, 'real', RC);
45
88
  // Only including a few common ones.
46
89
  const DEFAULT_METHODS = ['debug','info','log','warn','error'];
47
90
 
91
+ // Options that can be changed via handler settings.
92
+ const HANDLER_OPTIONS =
93
+ [
94
+ 'returnMsg', 'methodArg', 'msgArguments', 'msgArgs'
95
+ ];
96
+
48
97
  /**
49
98
  * Add a wrapper method to the console wrapper object.
50
99
  * @param {string} method - The console method to wrap.
@@ -52,25 +101,75 @@ const DEFAULT_METHODS = ['debug','info','log','warn','error'];
52
101
  */
53
102
  function addMethod(method)
54
103
  {
55
- def(LC, method, function()
104
+ def(LC, method, function(...args)
56
105
  {
57
- if (typeof LC.handler === F)
58
- { // A custom handler.
59
- return LC.handler(method, ...arguments);
106
+ if (typeof LC.trigger === F)
107
+ { // Support for core.observable(core.types.console);
108
+ LC.trigger(method, ...args);
109
+ }
110
+
111
+ let handler = LC.handler;
112
+ const opts = {}
113
+
114
+ function checkHandlerOptions(src)
115
+ {
116
+ for (const opt of HANDLER_OPTIONS)
117
+ {
118
+ if (src[opt] !== undefined)
119
+ {
120
+ opts[opt] = src[opt];
121
+ }
122
+ }
123
+ }
124
+
125
+ // Get the default options.
126
+ checkHandlerOptions(LC);
127
+
128
+ // Check for complex handler definitions as a plain object.
129
+ if (isObj(handler) && !Array.isArray(handler))
130
+ { // Look for options in the handler defs.
131
+ checkHandlerOptions(handler);
132
+
133
+ if (handler[method] !== undefined)
134
+ { // A handler for that method was found.
135
+ handler = handler[method];
136
+ }
137
+ else if (handler.DEFAULT !== undefined)
138
+ { // A default handler.
139
+ handler = handler.DEFAULT;
140
+ }
60
141
  }
61
- else if (Array.isArray(LC.handler))
62
- { // Add a message item.
142
+
143
+ if (typeof handler === F)
144
+ { // A custom handler function.
145
+ checkHandlerOptions(handler);
146
+ if (opts.methodArg)
147
+ args.unshift(method)
148
+ return handler(...args);
149
+ }
150
+ else if (Array.isArray(handler))
151
+ { // Add a message item to a log array.
152
+ checkHandlerOptions(handler);
153
+
63
154
  const time = new Date().toJSON();
64
- const msg = {time, method, arguments};
65
- LC.handler.push(msg);
66
- if (LC.returnMsg) return msg;
155
+ const msg = {time, method, opts};
156
+
157
+ if (typeof opts.msgArguments === S)
158
+ msg[opts.msgArguments] = arguments;
159
+ if (typeof opts.msgArgs === S)
160
+ msg[opts.msgArgs] = args;
161
+
162
+ handler.push(msg);
163
+
164
+ if (opts.returnMsg) return msg;
67
165
  }
68
- else if (LC.handler !== false)
166
+ else if (handler !== false)
69
167
  { // Pass through to the real console.
70
- return RC[method](...arguments);
168
+ return RC[method](...args);
71
169
  }
72
170
  });
73
171
  }
172
+
74
173
  def(LC, 'addMethod', addMethod);
75
174
 
76
175
  for (const method of DEFAULT_METHODS)
@@ -35,6 +35,7 @@ const
35
35
  * @property {string} NIL - Either `null` or `undefined`.
36
36
  * @property {string} NOTNIL - Anything other than `null` or `undefined`.
37
37
  * @property {string} MAP - A `Map` object.
38
+ * @property {string} SET - A `Set` object.
38
39
  * @property {object} tests - A map of tests for the above types.
39
40
  */
40
41
  const TYPES = {};
@@ -162,6 +163,7 @@ const
162
163
  NIL: isNil,
163
164
  NOTNIL: notNil,
164
165
  MAP: v => v instanceof Map,
166
+ SET: v => v instanceof Set,
165
167
  });
166
168
 
167
169
  module.exports = TYPES;
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@lumjs/core",
3
- "version": "1.15.0",
3
+ "version": "1.18.0",
4
4
  "main": "lib/index.js",
5
5
  "exports":
6
6
  {
7
7
  ".": "./lib/index.js",
8
8
 
9
- "./arrays": "./lib/arrays.js",
9
+ "./arrays": "./lib/arrays/index.js",
10
+ "./maps": "./lib/maps.js",
10
11
  "./context": "./lib/context.js",
11
12
  "./enum": "./lib/enum.js",
12
13
  "./flags": "./lib/flags.js",