@yuneta/gobj-js 7.26.14 → 7.26.16

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.
@@ -3281,13 +3281,34 @@ function set_remote_log_functions(remote_log_fn) {
3281
3281
  }
3282
3282
  /************************************************************
3283
3283
  * JSON.stringify for a log line: a circular object (a gobj,
3284
- * a DOM node) must not make the log call itself throw.
3284
+ * a DOM node), a BigInt or a toJSON() that throws must not make
3285
+ * the log call itself throw -- and neither must the fallback:
3286
+ * String() throws too, on an object with no prototype or with a
3287
+ * toString() that throws.
3285
3288
  ************************************************************/
3286
3289
  function log_stringify(v) {
3287
3290
  try {
3288
3291
  return JSON.stringify(v);
3289
3292
  } catch (e) {
3293
+ try {
3294
+ return String(v);
3295
+ } catch (e2) {
3296
+ return `<${typeof v}: not printable>`;
3297
+ }
3298
+ }
3299
+ }
3300
+ function log_string(v) {
3301
+ try {
3290
3302
  return String(v);
3303
+ } catch (e) {
3304
+ return log_stringify(v);
3305
+ }
3306
+ }
3307
+ function log_number(parse, v) {
3308
+ try {
3309
+ return parse(v);
3310
+ } catch (e) {
3311
+ return log_string(v);
3291
3312
  }
3292
3313
  }
3293
3314
  function format_log(format) {
@@ -3299,18 +3320,18 @@ function format_log(format) {
3299
3320
  output = format.replace(/%[sdifoO]/g, (match) => {
3300
3321
  const val = args[i++];
3301
3322
  switch (match) {
3302
- case "%s": return String(val);
3323
+ case "%s": return log_string(val);
3303
3324
  case "%d":
3304
- case "%i": return parseInt(val);
3305
- case "%f": return parseFloat(val);
3325
+ case "%i": return log_number(parseInt, val);
3326
+ case "%f": return log_number(parseFloat, val);
3306
3327
  case "%o":
3307
- case "%O": return typeof val === "object" ? log_stringify(val) : String(val);
3328
+ case "%O": return typeof val === "object" ? log_stringify(val) : log_string(val);
3308
3329
  default: return match;
3309
3330
  }
3310
3331
  });
3311
- const rest = args.slice(i).map((arg) => typeof arg === "object" ? log_stringify(arg) : String(arg));
3332
+ const rest = args.slice(i).map((arg) => typeof arg === "object" ? log_stringify(arg) : log_string(arg));
3312
3333
  if (rest.length > 0) output += " " + rest.join(" ");
3313
- } else output = args.map((arg) => typeof arg === "object" ? log_stringify(arg) : String(arg)).join(" ");
3334
+ } else output = args.map((arg) => typeof arg === "object" ? log_stringify(arg) : log_string(arg)).join(" ");
3314
3335
  return output;
3315
3336
  }
3316
3337
  function log_error(format, ...args) {
@@ -4365,6 +4386,13 @@ function kw_get_dict(gobj, kw, path, default_value, flag) {
4365
4386
  kw_set_dict_value(gobj, kw, path, default_value);
4366
4387
  return default_value;
4367
4388
  }
4389
+ if (create && v === null) {
4390
+ if (required) {
4391
+ log_error(`path not found: '${path}'`);
4392
+ trace_json_masked(kw);
4393
+ }
4394
+ return default_value;
4395
+ }
4368
4396
  if (required || create) {
4369
4397
  log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json dict: '${path}'` + (create ? ", cannot create" : ""));
4370
4398
  trace_json_masked(kw);
@@ -4429,6 +4457,13 @@ function kw_get_list(gobj, kw, path, default_value, flag) {
4429
4457
  kw_set_dict_value(gobj, kw, path, default_value);
4430
4458
  return default_value;
4431
4459
  }
4460
+ if (create && v === null) {
4461
+ if (required) {
4462
+ log_error(`path not found: '${path}'`);
4463
+ trace_json_masked(kw);
4464
+ }
4465
+ return default_value;
4466
+ }
4432
4467
  if (required || create) {
4433
4468
  log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json list: '${path}'` + (create ? ", cannot create" : ""));
4434
4469
  trace_json_masked(kw);
@@ -4642,8 +4677,8 @@ Keys:
4642
4677
  ["$key1", "$key2", ...]
4643
4678
  {"$key1":*, "$key2":*, ...}
4644
4679
 
4645
- It's not a deep copy, new keys are the paths.
4646
- If paths are empty return kw
4680
+ It's not a deep copy. A key is a KEY, not a path (as C, SDK 7.26.9).
4681
+ If keys are empty return kw
4647
4682
  *************************************************************/
4648
4683
  function kw_clone_by_keys(gobj, kw, keys, verbose) {
4649
4684
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
@@ -4670,8 +4705,8 @@ Keys:
4670
4705
  ["$key1", "$key2", ...]
4671
4706
  {"$key1":*, "$key2":*, ...}
4672
4707
 
4673
- It's not a deep copy, new keys are the paths.
4674
- If paths are empty return empty
4708
+ It's not a deep copy. A key is a KEY, not a path (as C, SDK 7.26.9).
4709
+ If keys are empty return empty
4675
4710
  ***************************************************************************/
4676
4711
  function kw_clone_by_not_keys(gobj, kw, keys, verbose) {
4677
4712
  let kw_clone = {};
@@ -5254,17 +5289,6 @@ function jdb_init(jdb, prefix, duplicate) {
5254
5289
  return jdb;
5255
5290
  }
5256
5291
  /********************************************
5257
- * A value as json text for a log line: the error path must not throw
5258
- * (a circular record, a BigInt).
5259
- ********************************************/
5260
- function safe_json_text(v) {
5261
- try {
5262
- return JSON.stringify(v);
5263
- } catch (e) {
5264
- return `<${typeof v}: not json (${e.message})>`;
5265
- }
5266
- }
5267
- /********************************************
5268
5292
  *
5269
5293
  ********************************************/
5270
5294
  function jdb_update(jdb, topic_name, path, kw) {
@@ -5274,7 +5298,7 @@ function jdb_update(jdb, topic_name, path, kw) {
5274
5298
  return null;
5275
5299
  }
5276
5300
  if (!is_object(kw) || empty_string(kw["id"])) {
5277
- log_error(`jdb_update: record without id: ${safe_json_text(kw)}`);
5301
+ log_error(`jdb_update: record without id: ${log_stringify(kw)}`);
5278
5302
  return null;
5279
5303
  }
5280
5304
  if (!is_string(path)) {
@@ -5327,7 +5351,7 @@ function jdb_delete(jdb, topic_name, path, kw) {
5327
5351
  return null;
5328
5352
  }
5329
5353
  if (!is_object(kw) || empty_string(kw["id"])) {
5330
- log_error(`jdb_delete: record without id: ${safe_json_text(kw)}`);
5354
+ log_error(`jdb_delete: record without id: ${log_stringify(kw)}`);
5331
5355
  return null;
5332
5356
  }
5333
5357
  if (!is_string(path)) {