cafe-utility 10.10.1 → 10.11.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.
Files changed (3) hide show
  1. package/index.d.ts +1 -1
  2. package/index.js +26 -7
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -64,7 +64,7 @@ declare function asId(value: any): number;
64
64
  declare function asTime(value: any): string;
65
65
  declare function asArray(value: any): unknown[];
66
66
  declare function asObject(value: any): Record<string, unknown>;
67
- declare function represent(value: any): string;
67
+ declare function represent(value: any, strategy?: 'json' | 'key-value', depth?: number): string;
68
68
  declare function expandError(error: any, stackTrace?: boolean): string;
69
69
  declare function deepMergeInPlace<X extends object, Y extends object>(target: X, source: Y): X & Y;
70
70
  declare function deepMerge2<X extends object, Y extends object>(target: X, source: Y): X & Y;
package/index.js CHANGED
@@ -501,17 +501,36 @@ function asObject(value) {
501
501
  return value
502
502
  }
503
503
 
504
- function represent(value) {
504
+ function represent(value, strategy = 'json', depth = 0) {
505
505
  if (isObject(value)) {
506
- return JSON.stringify(value)
507
- }
508
- if (value === null) {
509
- return 'null'
506
+ if (depth > 1) {
507
+ return '[object Object]'
508
+ }
509
+ if (strategy === 'json') {
510
+ if (Array.isArray(value)) {
511
+ const transformed = value.map(element => represent(element, 'json', depth + 1))
512
+ return depth === 0 ? JSON.stringify(transformed) : transformed
513
+ }
514
+ const object = {}
515
+ if (value.message) {
516
+ object.message = represent(value.message, 'json', depth + 1)
517
+ }
518
+ for (const [key, val] of Object.entries(value)) {
519
+ object[key] = represent(val, 'json', depth + 1)
520
+ }
521
+ return depth === 0 ? JSON.stringify(object) : object
522
+ } else if (strategy === 'key-value') {
523
+ const keys = Object.keys(value)
524
+ if (value.message && !keys.includes('message')) {
525
+ keys.unshift('message')
526
+ }
527
+ return keys.map(key => `${key}=${JSON.stringify(represent(value[key], 'json', depth + 1))}`).join(' ')
528
+ }
510
529
  }
511
530
  if (isUndefined(value)) {
512
- return 'undefined'
531
+ value = 'undefined'
513
532
  }
514
- return value
533
+ return depth === 0 ? JSON.stringify(value) : value
515
534
  }
516
535
 
517
536
  function expandError(error, stackTrace) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "10.10.1",
3
+ "version": "10.11.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {