@naturalcycles/js-lib 15.88.0 → 15.89.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.
package/dist/is.util.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { AnyObject, FalsyValue, NullishValue, Primitive } from './types.js';
1
+ import type { AnyObject, FalsyValue, NullishValue, PlainObject, Primitive } from './types.js';
2
2
  type Nullish<T> = T extends NullishValue ? T : never;
3
3
  type Truthy<T> = T extends FalsyValue ? never : T;
4
4
  type Falsy<T> = T extends FalsyValue ? T : never;
@@ -25,6 +25,10 @@ export declare const _isFalsy: <T>(v: T) => v is Falsy<T>;
25
25
  * Currently treats RegEx as Object too, e.g _isObject(/some/) === true
26
26
  */
27
27
  export declare function _isObject(obj: any): obj is AnyObject;
28
+ /**
29
+ * Returns true if item is an object literal: not an Array, Error, RegExp or class instance.
30
+ */
31
+ export declare function _isPlainObject(v: any): v is PlainObject;
28
32
  export declare function _isPrimitive(v: any): v is Primitive;
29
33
  export declare function _isEmptyObject(obj: AnyObject): boolean;
30
34
  export declare function _isNotEmptyObject(obj: AnyObject): boolean;
package/dist/is.util.js CHANGED
@@ -23,6 +23,15 @@ export const _isFalsy = (v) => !v;
23
23
  export function _isObject(obj) {
24
24
  return (typeof obj === 'object' && obj !== null && !Array.isArray(obj)) || false;
25
25
  }
26
+ /**
27
+ * Returns true if item is an object literal: not an Array, Error, RegExp or class instance.
28
+ */
29
+ export function _isPlainObject(v) {
30
+ if (typeof v !== 'object' || v === null)
31
+ return false;
32
+ const proto = Object.getPrototypeOf(v);
33
+ return proto === Object.prototype || proto === null;
34
+ }
26
35
  export function _isPrimitive(v) {
27
36
  return (v === null ||
28
37
  v === undefined ||
@@ -1,4 +1,4 @@
1
- import type { MutateOptions } from '../types.js';
1
+ import type { AnyObject, MutateOptions } from '../types.js';
2
2
  /**
3
3
  * These levels follow console.* naming,
4
4
  * so you can use console[level] safely.
@@ -51,7 +51,15 @@ export declare function commonLoggerPipe(loggers: CommonLogger[]): CommonLogger;
51
51
  * Creates a "child" CommonLogger with prefix (one or multiple).
52
52
  */
53
53
  export declare function commonLoggerPrefix(logger: CommonLogger, ...prefixes: any[]): CommonLogger;
54
+ export type CommonLogSink = CommonLogger | CommonLogWithLevelFunction;
55
+ export interface CommonLoggerWithContext extends CommonLogger {
56
+ child: (context: AnyObject) => CommonLoggerWithContext;
57
+ }
54
58
  /**
55
59
  * Creates a CommonLogger from a single function that takes `level` and `args`.
60
+ *
61
+ * With a context, every call emits one object: plain-object args merged in, first Error as `err`,
62
+ * the rest joined into `msg`.
56
63
  */
57
- export declare function commonLoggerCreate(fn: CommonLogWithLevelFunction): CommonLogger;
64
+ export declare function commonLoggerCreate(sink: CommonLogSink): CommonLogger;
65
+ export declare function commonLoggerCreate(sink: CommonLogSink, context: AnyObject): CommonLoggerWithContext;
@@ -1,3 +1,4 @@
1
+ import { _isPlainObject } from '../is.util.js';
1
2
  // copy-pasted to avoid weird circular dependency
2
3
  const _noop = (..._args) => undefined;
3
4
  export const commonLogLevelNumber = {
@@ -72,14 +73,45 @@ export function commonLoggerPrefix(logger, ...prefixes) {
72
73
  error: (...args) => logger.error(...prefixes, ...args),
73
74
  };
74
75
  }
75
- /**
76
- * Creates a CommonLogger from a single function that takes `level` and `args`.
77
- */
78
- export function commonLoggerCreate(fn) {
79
- return {
80
- debug: (...args) => fn('debug', args),
81
- log: (...args) => fn('log', args),
82
- warn: (...args) => fn('warn', args),
83
- error: (...args) => fn('error', args),
76
+ export function commonLoggerCreate(sink, context) {
77
+ const fn = typeof sink === 'function' ? sink : (level, args) => sink[level](...args);
78
+ if (!context) {
79
+ return {
80
+ debug: (...args) => fn('debug', args),
81
+ log: (...args) => fn('log', args),
82
+ warn: (...args) => fn('warn', args),
83
+ error: (...args) => fn('error', args),
84
+ };
85
+ }
86
+ const ctx = { ...context };
87
+ const logger = {
88
+ debug: (...args) => fn('debug', [toLogEntry(ctx, args)]),
89
+ log: (...args) => fn('log', [toLogEntry(ctx, args)]),
90
+ warn: (...args) => fn('warn', [toLogEntry(ctx, args)]),
91
+ error: (...args) => fn('error', [toLogEntry(ctx, args)]),
92
+ child: c => commonLoggerCreate(fn, { ...ctx, ...c }),
84
93
  };
94
+ return logger;
95
+ }
96
+ function toLogEntry(context, args) {
97
+ const entry = { ...context };
98
+ let err;
99
+ let msg;
100
+ for (const arg of args) {
101
+ if (_isPlainObject(arg)) {
102
+ Object.assign(entry, arg);
103
+ }
104
+ else if (!err && arg instanceof Error) {
105
+ err = arg;
106
+ }
107
+ else {
108
+ const s = String(arg);
109
+ msg = msg === undefined ? s : `${msg} ${s}`;
110
+ }
111
+ }
112
+ if (err)
113
+ entry['err'] = err;
114
+ if (msg !== undefined)
115
+ entry['msg'] = msg;
116
+ return entry;
85
117
  }
package/dist/types.d.ts CHANGED
@@ -25,6 +25,10 @@ export interface StringMap<T = string> {
25
25
  * Because `object` type is not safe/recommended to be used (e.g discouraged by eslint-typescript due to: https://github.com/microsoft/TypeScript/issues/21732)
26
26
  */
27
27
  export type AnyObject = Record<string, any>;
28
+ /**
29
+ * Object literal, with a prototype of `Object.prototype` or `null`. What `_isPlainObject` narrows to.
30
+ */
31
+ export type PlainObject = Branded<AnyObject, 'PlainObject'>;
28
32
  export type AnyEnum = NumberEnum;
29
33
  export type NumberEnum = Record<string, number | string>;
30
34
  export type StringEnum = Record<string, string>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.88.0",
4
+ "version": "15.89.0",
5
5
  "dependencies": {
6
6
  "tslib": "^2"
7
7
  },
package/src/is.util.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { AnyObject, FalsyValue, NullishValue, Primitive } from './types.js'
1
+ import type { AnyObject, FalsyValue, NullishValue, PlainObject, Primitive } from './types.js'
2
2
 
3
3
  type Nullish<T> = T extends NullishValue ? T : never
4
4
  type Truthy<T> = T extends FalsyValue ? never : T
@@ -32,6 +32,15 @@ export function _isObject(obj: any): obj is AnyObject {
32
32
  return (typeof obj === 'object' && obj !== null && !Array.isArray(obj)) || false
33
33
  }
34
34
 
35
+ /**
36
+ * Returns true if item is an object literal: not an Array, Error, RegExp or class instance.
37
+ */
38
+ export function _isPlainObject(v: any): v is PlainObject {
39
+ if (typeof v !== 'object' || v === null) return false
40
+ const proto = Object.getPrototypeOf(v)
41
+ return proto === Object.prototype || proto === null
42
+ }
43
+
35
44
  export function _isPrimitive(v: any): v is Primitive {
36
45
  return (
37
46
  v === null ||
@@ -1,4 +1,5 @@
1
- import type { MutateOptions } from '../types.js'
1
+ import { _isPlainObject } from '../is.util.js'
2
+ import type { AnyObject, MutateOptions } from '../types.js'
2
3
 
3
4
  // copy-pasted to avoid weird circular dependency
4
5
  const _noop = (..._args: any[]): undefined => undefined
@@ -125,14 +126,62 @@ export function commonLoggerPrefix(logger: CommonLogger, ...prefixes: any[]): Co
125
126
  }
126
127
  }
127
128
 
129
+ export type CommonLogSink = CommonLogger | CommonLogWithLevelFunction
130
+
131
+ export interface CommonLoggerWithContext extends CommonLogger {
132
+ child: (context: AnyObject) => CommonLoggerWithContext
133
+ }
134
+
128
135
  /**
129
136
  * Creates a CommonLogger from a single function that takes `level` and `args`.
137
+ *
138
+ * With a context, every call emits one object: plain-object args merged in, first Error as `err`,
139
+ * the rest joined into `msg`.
130
140
  */
131
- export function commonLoggerCreate(fn: CommonLogWithLevelFunction): CommonLogger {
132
- return {
133
- debug: (...args) => fn('debug', args),
134
- log: (...args) => fn('log', args),
135
- warn: (...args) => fn('warn', args),
136
- error: (...args) => fn('error', args),
141
+ export function commonLoggerCreate(sink: CommonLogSink): CommonLogger
142
+ export function commonLoggerCreate(sink: CommonLogSink, context: AnyObject): CommonLoggerWithContext
143
+ export function commonLoggerCreate(sink: CommonLogSink, context?: AnyObject): CommonLogger {
144
+ const fn: CommonLogWithLevelFunction =
145
+ typeof sink === 'function' ? sink : (level, args) => sink[level](...args)
146
+
147
+ if (!context) {
148
+ return {
149
+ debug: (...args) => fn('debug', args),
150
+ log: (...args) => fn('log', args),
151
+ warn: (...args) => fn('warn', args),
152
+ error: (...args) => fn('error', args),
153
+ }
154
+ }
155
+
156
+ const ctx: AnyObject = { ...context }
157
+
158
+ const logger: CommonLoggerWithContext = {
159
+ debug: (...args) => fn('debug', [toLogEntry(ctx, args)]),
160
+ log: (...args) => fn('log', [toLogEntry(ctx, args)]),
161
+ warn: (...args) => fn('warn', [toLogEntry(ctx, args)]),
162
+ error: (...args) => fn('error', [toLogEntry(ctx, args)]),
163
+ child: c => commonLoggerCreate(fn, { ...ctx, ...c }),
164
+ }
165
+ return logger
166
+ }
167
+
168
+ function toLogEntry(context: AnyObject, args: any[]): AnyObject {
169
+ const entry: AnyObject = { ...context }
170
+ let err: Error | undefined
171
+ let msg: string | undefined
172
+
173
+ for (const arg of args) {
174
+ if (_isPlainObject(arg)) {
175
+ Object.assign(entry, arg)
176
+ } else if (!err && arg instanceof Error) {
177
+ err = arg
178
+ } else {
179
+ const s = String(arg)
180
+ msg = msg === undefined ? s : `${msg} ${s}`
181
+ }
137
182
  }
183
+
184
+ if (err) entry['err'] = err
185
+ if (msg !== undefined) entry['msg'] = msg
186
+ return entry
138
187
  }
package/src/types.ts CHANGED
@@ -30,6 +30,11 @@ export interface StringMap<T = string> {
30
30
  */
31
31
  export type AnyObject = Record<string, any>
32
32
 
33
+ /**
34
+ * Object literal, with a prototype of `Object.prototype` or `null`. What `_isPlainObject` narrows to.
35
+ */
36
+ export type PlainObject = Branded<AnyObject, 'PlainObject'>
37
+
33
38
  export type AnyEnum = NumberEnum
34
39
  export type NumberEnum = Record<string, number | string>
35
40
  export type StringEnum = Record<string, string>