@yuneta/gobj-js 7.26.15 → 7.26.17

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.
@@ -3207,11 +3207,7 @@ var LOG_BACKLOG_MAX = 600;
3207
3207
  var __log_backlog__ = [];
3208
3208
  function keep_in_backlog(level, msg, hora) {
3209
3209
  let m = msg;
3210
- if (level === "json") try {
3211
- m = JSON.stringify(msg);
3212
- } catch (e) {
3213
- m = String(msg);
3214
- }
3210
+ if (level === "json") m = log_stringify(msg);
3215
3211
  __log_backlog__.push([
3216
3212
  level,
3217
3213
  m,
@@ -3296,38 +3292,67 @@ function log_stringify(v) {
3296
3292
  }
3297
3293
  }
3298
3294
  }
3295
+ function log_string(v) {
3296
+ try {
3297
+ return String(v);
3298
+ } catch (e) {
3299
+ return log_stringify(v);
3300
+ }
3301
+ }
3302
+ function log_number(parse, v) {
3303
+ try {
3304
+ return parse(v);
3305
+ } catch (e) {
3306
+ return log_string(v);
3307
+ }
3308
+ }
3299
3309
  function format_log(format) {
3300
3310
  const args = Array.prototype.slice.call(arguments);
3301
3311
  let output = "";
3302
3312
  if (typeof format === "string") {
3303
3313
  if (args.length === 1) return format;
3304
3314
  let i = 1;
3305
- output = format.replace(/%[sdifoO]/g, (match) => {
3315
+ output = format.replace(/%[sdifoOj%]/g, (match) => {
3316
+ if (match === "%%") return "%";
3306
3317
  const val = args[i++];
3307
3318
  switch (match) {
3308
- case "%s": return String(val);
3319
+ case "%s": return log_string(val);
3309
3320
  case "%d":
3310
- case "%i": return parseInt(val);
3311
- case "%f": return parseFloat(val);
3321
+ case "%i":
3322
+ if (typeof val === "bigint") return val.toString();
3323
+ return log_number(parseInt, val);
3324
+ case "%f":
3325
+ if (typeof val === "bigint") return val.toString();
3326
+ return log_number(parseFloat, val);
3327
+ case "%j": return log_stringify(val);
3312
3328
  case "%o":
3313
- case "%O": return typeof val === "object" ? log_stringify(val) : String(val);
3329
+ case "%O": return typeof val === "object" ? log_stringify(val) : log_string(val);
3314
3330
  default: return match;
3315
3331
  }
3316
3332
  });
3317
- const rest = args.slice(i).map((arg) => typeof arg === "object" ? log_stringify(arg) : String(arg));
3333
+ const rest = args.slice(i).map((arg) => typeof arg === "object" ? log_stringify(arg) : log_string(arg));
3318
3334
  if (rest.length > 0) output += " " + rest.join(" ");
3319
- } else output = args.map((arg) => typeof arg === "object" ? log_stringify(arg) : String(arg)).join(" ");
3335
+ } else output = args.map((arg) => typeof arg === "object" ? log_stringify(arg) : log_string(arg)).join(" ");
3320
3336
  return output;
3321
3337
  }
3338
+ function call_log_function(fn, ...args) {
3339
+ try {
3340
+ fn(...args);
3341
+ } catch (e) {
3342
+ try {
3343
+ _console.error("a log function threw:", e);
3344
+ } catch (e2) {}
3345
+ }
3346
+ }
3322
3347
  function log_error(format, ...args) {
3323
3348
  let msg = format_log(format, ...args);
3324
3349
  let hora = current_timestamp();
3325
3350
  if (f_error) {
3326
3351
  if (f_error === _console.error) {
3327
- if (console_wants("error", msg)) f_error("%c" + hora + " ERROR: " + String(msg), "color:red");
3352
+ if (console_wants("error", msg)) call_log_function(f_error, "%c" + hora + " ERROR: " + String(msg), "color:red");
3328
3353
  } else {
3329
3354
  if (console_wants("error", msg)) _console.error("%c" + hora + " ERROR: " + String(msg), "color:red");
3330
- f_error(`${hora} ERROR: ${String(msg)}`);
3355
+ call_log_function(f_error, `${hora} ERROR: ${String(msg)}`);
3331
3356
  }
3332
3357
  }
3333
3358
  emit_log_callback("error", msg, hora);
@@ -3337,10 +3362,10 @@ function log_warning(format, ...args) {
3337
3362
  let hora = current_timestamp();
3338
3363
  if (f_warning) {
3339
3364
  if (f_warning === _console.warn) {
3340
- if (console_wants("warning", msg)) f_warning("%c" + hora + " WARNING: " + String(msg), "color:yellow");
3365
+ if (console_wants("warning", msg)) call_log_function(f_warning, "%c" + hora + " WARNING: " + String(msg), "color:yellow");
3341
3366
  } else {
3342
3367
  if (console_wants("warning", msg)) _console.warn("%c" + hora + " WARNING: " + String(msg), "color:yellow");
3343
- f_warning(`${hora} WARNING: ${String(msg)}`);
3368
+ call_log_function(f_warning, `${hora} WARNING: ${String(msg)}`);
3344
3369
  }
3345
3370
  }
3346
3371
  emit_log_callback("warning", msg, hora);
@@ -3349,7 +3374,7 @@ function log_info(format, ...args) {
3349
3374
  let msg = format_log(format, ...args);
3350
3375
  let hora = current_timestamp();
3351
3376
  if (f_info) {
3352
- if (console_wants("info", msg)) f_info("%c" + hora + " INFO: " + String(msg), "color:cyan");
3377
+ if (console_wants("info", msg)) call_log_function(f_info, "%c" + hora + " INFO: " + String(msg), "color:cyan");
3353
3378
  }
3354
3379
  emit_log_callback("info", msg, hora);
3355
3380
  }
@@ -3357,7 +3382,7 @@ function log_debug(format, ...args) {
3357
3382
  let msg = format_log(format, ...args);
3358
3383
  let hora = current_timestamp();
3359
3384
  if (f_debug) {
3360
- if (console_wants("debug", msg)) f_debug("%c" + hora + " DEBUG: " + String(msg), "color:silver");
3385
+ if (console_wants("debug", msg)) call_log_function(f_debug, "%c" + hora + " DEBUG: " + String(msg), "color:silver");
3361
3386
  }
3362
3387
  emit_log_callback("debug", msg, hora);
3363
3388
  }
@@ -3365,14 +3390,14 @@ function trace_msg(format, ...args) {
3365
3390
  let msg = format_log(format, ...args);
3366
3391
  let hora = current_timestamp();
3367
3392
  if (f_debug) {
3368
- if (console_wants("msg", msg)) f_debug("%c" + hora + " MSG: " + String(msg), "color:cyan");
3393
+ if (console_wants("msg", msg)) call_log_function(f_debug, "%c" + hora + " MSG: " + String(msg), "color:cyan");
3369
3394
  }
3370
3395
  emit_log_callback("msg", msg, hora);
3371
3396
  }
3372
3397
  function trace_json(jn, msg) {
3373
3398
  if (console_wants("json", msg)) {
3374
- if (msg) _console.warn("=====> " + msg);
3375
- _console.dir(jn);
3399
+ if (msg) call_log_function(_console.warn, "=====> " + log_string(msg));
3400
+ call_log_function(_console.dir, jn);
3376
3401
  }
3377
3402
  emit_log_callback("json", jn, current_timestamp());
3378
3403
  }
@@ -4371,6 +4396,13 @@ function kw_get_dict(gobj, kw, path, default_value, flag) {
4371
4396
  kw_set_dict_value(gobj, kw, path, default_value);
4372
4397
  return default_value;
4373
4398
  }
4399
+ if (create && v === null) {
4400
+ if (required) {
4401
+ log_error(`path not found: '${path}'`);
4402
+ trace_json_masked(kw);
4403
+ }
4404
+ return default_value;
4405
+ }
4374
4406
  if (required || create) {
4375
4407
  log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json dict: '${path}'` + (create ? ", cannot create" : ""));
4376
4408
  trace_json_masked(kw);
@@ -4435,6 +4467,13 @@ function kw_get_list(gobj, kw, path, default_value, flag) {
4435
4467
  kw_set_dict_value(gobj, kw, path, default_value);
4436
4468
  return default_value;
4437
4469
  }
4470
+ if (create && v === null) {
4471
+ if (required) {
4472
+ log_error(`path not found: '${path}'`);
4473
+ trace_json_masked(kw);
4474
+ }
4475
+ return default_value;
4476
+ }
4438
4477
  if (required || create) {
4439
4478
  log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json list: '${path}'` + (create ? ", cannot create" : ""));
4440
4479
  trace_json_masked(kw);
@@ -4648,8 +4687,8 @@ Keys:
4648
4687
  ["$key1", "$key2", ...]
4649
4688
  {"$key1":*, "$key2":*, ...}
4650
4689
 
4651
- It's not a deep copy, new keys are the paths.
4652
- If paths are empty return kw
4690
+ It's not a deep copy. A key is a KEY, not a path (as C, SDK 7.26.9).
4691
+ If keys are empty return kw
4653
4692
  *************************************************************/
4654
4693
  function kw_clone_by_keys(gobj, kw, keys, verbose) {
4655
4694
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
@@ -4676,8 +4715,8 @@ Keys:
4676
4715
  ["$key1", "$key2", ...]
4677
4716
  {"$key1":*, "$key2":*, ...}
4678
4717
 
4679
- It's not a deep copy, new keys are the paths.
4680
- If paths are empty return empty
4718
+ It's not a deep copy. A key is a KEY, not a path (as C, SDK 7.26.9).
4719
+ If keys are empty return empty
4681
4720
  ***************************************************************************/
4682
4721
  function kw_clone_by_not_keys(gobj, kw, keys, verbose) {
4683
4722
  let kw_clone = {};