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