json-log-line 1.0.4 → 1.0.5

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/README.md CHANGED
@@ -107,14 +107,16 @@ A special key that contains the rest of the log object fields which were both in
107
107
 
108
108
  #### Multi keys
109
109
 
110
- You can map a single formatter to multiple keys by separating them with a pipe (`|`). Each key is resolved independently (dot notation is supported) and rendered in order. Every matched value is passed through the same formatter, and each consumed key is removed from the remaining `extraFields` payload.
110
+ You can map a single formatter to multiple keys using either commas (`,`) or pipes (`|`). Dot notation works for both. Do **not** mix `,` and `|` in the same formatter key.
111
+
112
+ - `|` (take one): evaluate keys in order and format the first one that exists, then stop.
113
+ - `,` (take all): evaluate every listed key and format each one in order.
111
114
 
112
115
  ```javascript
116
+ // Take one: stop after the first matching value for each formatter key
113
117
  const lineFormatter = logLineFactory({
114
118
  format: {
115
- // Applies to both top-level keys
116
119
  'foo|baz': (value) => `!${value}`,
117
- // Works with nested keys, too
118
120
  'nested.a|other.a': (value) => `:${value}:`,
119
121
  },
120
122
  });
@@ -129,6 +131,27 @@ console.log(
129
131
  }),
130
132
  ),
131
133
  );
134
+ // => !bar :x:
135
+ // {"baz":"buz","nested":{"b":"y"},"other":{}}
136
+
137
+ // Take all: format every listed key
138
+ const takeAllFormatter = logLineFactory({
139
+ format: {
140
+ 'foo,baz': (value) => `!${value}`,
141
+ 'nested.a,other.a': (value) => `:${value}:`,
142
+ },
143
+ });
144
+
145
+ console.log(
146
+ takeAllFormatter(
147
+ JSON.stringify({
148
+ foo: 'bar',
149
+ baz: 'buz',
150
+ nested: {a: 'x', b: 'y'},
151
+ other: {a: 'z'},
152
+ }),
153
+ ),
154
+ );
132
155
  // => !bar !buz :x: :z:
133
156
  // {"nested":{"b":"y"},"other":{}}
134
157
  ```
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,OAAO,GAAG;IACpB,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC5B,MAAM,CAAC,EAAE,MAAM,CACb,MAAM,EACN,CAAC,KAAK,EAAE,GAAG,EAAE,eAAe,CAAC,EAAE,GAAG,EAAE,GAAG,UAAU,EAAE,GAAG,EAAE,KAAK,MAAM,CACpE,CAAC;CACH,CAAC;AAMF,wBAAgB,cAAc,CAAC;AAC7B;;GAEG;AACH,OAAY;AACZ;;GAEG;AACH,OAAY;AACZ;;GAEG;AACH,MAAW,GACZ,GAAE,OAAY,IASI,WAAW,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,MAAM,CA0FtE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,OAAO,GAAG;IACpB,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC5B,MAAM,CAAC,EAAE,MAAM,CACb,MAAM,EACN,CAAC,KAAK,EAAE,GAAG,EAAE,eAAe,CAAC,EAAE,GAAG,EAAE,GAAG,UAAU,EAAE,GAAG,EAAE,KAAK,MAAM,CACpE,CAAC;CACH,CAAC;AAMF,wBAAgB,cAAc,CAAC;AAC7B;;GAEG;AACH,OAAY;AACZ;;GAEG;AACH,OAAY;AACZ;;GAEG;AACH,MAAW,GACZ,GAAE,OAAY,IASI,WAAW,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,MAAM,CAgGtE"}
package/dist/index.js CHANGED
@@ -20,7 +20,7 @@ include = [],
20
20
  */
21
21
  format = {}, } = {}) {
22
22
  const logLineKeys = Object.keys(format);
23
- const splitLogLineKeys = logLineKeys.flatMap((key) => key.split('|'));
23
+ const splitLogLineKeys = logLineKeys.flatMap((key) => key.split(/\||,/));
24
24
  format.extraFields ||= (object) => JSON.stringify(object) + nl;
25
25
  /**
26
26
  * @param inputData - The input data to be formatted can be a JSON stringified object or a plain object
@@ -57,7 +57,8 @@ format = {}, } = {}) {
57
57
  object = deepMerge()(object, whiteListObject);
58
58
  const output = [];
59
59
  for (const _key of logLineKeys) {
60
- const keys = _key.split('|');
60
+ const isTakeOne = _key.includes('|');
61
+ const keys = _key.split(/\||,/);
61
62
  for (const key of keys) {
62
63
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
63
64
  const value = get(object, key);
@@ -68,6 +69,9 @@ format = {}, } = {}) {
68
69
  if (formatter) {
69
70
  output.push(formatter(value, object));
70
71
  }
72
+ if (isTakeOne) {
73
+ break;
74
+ }
71
75
  }
72
76
  }
73
77
  // remove the properties that were used to create the log-line
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-log-line",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "A utility for building awesome log lines from JSON",
5
5
  "homepage": "https://github.com/spence-s/json-log-line",
6
6
  "bugs": {