@yuneta/gobj-js 7.25.5 → 7.25.6

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.
package/README.md CHANGED
@@ -519,6 +519,13 @@ kw_has_key(kw, key) // → boolean
519
519
  kw_pop(kw1, kw2) // delete from kw1 the keys listed in kw2
520
520
  kw_delete(gobj, kw, path)
521
521
  kw_find_path(gobj, kw, path, verbose) // back-tick path: "a`b`c"
522
+ // kw_find_path() answers undefined for a path that is not there, as C
523
+ // answers NULL. A kw that is not a dict or a list, or a scalar in the
524
+ // middle of the path, is logged ("kw must be list or dict"); an absent
525
+ // or null middle segment is logged only when verbose. So a typed reader
526
+ // handed a bad kw answers its DEFAULT:
527
+ // kw_get_int(gobj, null, "x", 5, 0) → 5 (logged: kw must be list or dict)
528
+ // kw_get_str(gobj, {a: null}, "a`b", "d", 0) → "d" (not logged)
522
529
 
523
530
  kw_get_bool(gobj, kw, path, default_value, flag)
524
531
  kw_get_int (gobj, kw, path, default_value, flag)
@@ -3560,30 +3560,46 @@ function kw_has_key(kw, key) {
3560
3560
  return false;
3561
3561
  }
3562
3562
  /************************************************************
3563
- *
3563
+ * The value at `path` (keys joined by a backtick), as the C
3564
+ * kw_find_path() finds it: undefined when it is not there.
3565
+ * A kw that is not a dict or a list is LOGGED ("kw must be list
3566
+ * or dict") and answers undefined, as C answers NULL; so does a
3567
+ * segment in the middle of the path whose value is a scalar. A
3568
+ * middle segment that is absent or null answers undefined, logged
3569
+ * only when `verbose`. Before gobj-js 7.25.6 a bad kw answered 0,
3570
+ * which the typed readers took for a value found (kw_get_int() and
3571
+ * kw_get_real() answered 0 instead of the default, kw_get_str() and
3572
+ * kw_get_bool() logged a second line), the log named no path and
3573
+ * called gobj_short_name() on a null gobj ("gobj bad type"), and a
3574
+ * null middle segment threw a TypeError.
3564
3575
  ************************************************************/
3565
3576
  function kw_find_path(gobj, kw, path, verbose) {
3566
3577
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
3578
+ const prefix = gobj ? `${gobj_short_name(gobj)}: ` : "";
3567
3579
  if (!(is_object(kw) || is_array(kw))) {
3568
- log_error(`${gobj_short_name(gobj)}: kw must be list or dict`);
3569
- return 0;
3580
+ log_error(`${prefix}kw must be list or dict: '${String(path)}'`);
3581
+ return;
3570
3582
  }
3571
3583
  if (!is_string(path)) {
3572
- log_error(`${gobj_short_name(gobj)}: path must be a string: ${String(path)}`);
3573
- return 0;
3584
+ log_error(`${prefix}path must be a string: ${String(path)}`);
3585
+ return;
3574
3586
  }
3575
3587
  let ss = path.split("`");
3576
- if (ss.length <= 1) return kw[path];
3577
3588
  let len = ss.length;
3578
- for (let i = 0; i < len; i++) {
3579
- let key = ss[i];
3580
- kw = kw[key];
3581
- if (kw === void 0) {
3582
- if (verbose) log_error(`${gobj_short_name(gobj)}: path not found: ${String(path)}`);
3589
+ for (let i = 0; i < len - 1; i++) {
3590
+ kw = kw[ss[i]];
3591
+ if (kw === void 0 || kw === null) {
3592
+ if (verbose) log_error(`${prefix}path not found: '${path}'`);
3593
+ return;
3594
+ }
3595
+ if (!(is_object(kw) || is_array(kw))) {
3596
+ log_error(`${prefix}kw must be list or dict: '${path}'`);
3583
3597
  return;
3584
3598
  }
3585
3599
  }
3586
- return kw;
3600
+ let value = kw[ss[len - 1]];
3601
+ if (value === void 0 && verbose) log_error(`${prefix}path not found: '${path}'`);
3602
+ return value;
3587
3603
  }
3588
3604
  /************************************************************
3589
3605
  *
@@ -3624,7 +3640,7 @@ function kw_get_bool(gobj, kw, path, default_value, flag) {
3624
3640
  const extract = flag && flag & kw_flag_t.KW_EXTRACT;
3625
3641
  const wild = flag && flag & kw_flag_t.KW_WILD_NUMBER;
3626
3642
  if (b === void 0) {
3627
- if (create) {
3643
+ if (create && kw) {
3628
3644
  let v = Boolean(default_value);
3629
3645
  kw_set_dict_value(gobj, kw, path, v);
3630
3646
  return v;
@@ -3692,7 +3708,7 @@ function kw_get_int(gobj, kw, path, default_value, flag) {
3692
3708
  const extract = flag && flag & kw_flag_t.KW_EXTRACT;
3693
3709
  const wild = flag && flag & kw_flag_t.KW_WILD_NUMBER;
3694
3710
  if (v === void 0) {
3695
- if (create) {
3711
+ if (create && kw) {
3696
3712
  let v = parseInt(default_value);
3697
3713
  kw_set_dict_value(gobj, kw, path, v);
3698
3714
  return v;
@@ -3739,7 +3755,7 @@ function kw_get_real(gobj, kw, path, default_value, flag) {
3739
3755
  const extract = flag && flag & kw_flag_t.KW_EXTRACT;
3740
3756
  const wild = flag && flag & kw_flag_t.KW_WILD_NUMBER;
3741
3757
  if (v === void 0) {
3742
- if (create) {
3758
+ if (create && kw) {
3743
3759
  let v = Number(default_value);
3744
3760
  kw_set_dict_value(gobj, kw, path, v);
3745
3761
  return v;
@@ -3809,7 +3825,7 @@ function kw_get_str(gobj, kw, path, default_value, flag) {
3809
3825
  const create = flag && flag & kw_flag_t.KW_CREATE;
3810
3826
  const extract = flag && flag & kw_flag_t.KW_EXTRACT;
3811
3827
  if (v === void 0) {
3812
- if (create) {
3828
+ if (create && kw) {
3813
3829
  kw_set_dict_value(gobj, kw, path, is_string(default_value) ? default_value : null);
3814
3830
  return default_value;
3815
3831
  } else if (required && !kw_path_holds_key(kw, path)) {