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