@yuneta/gobj-js 7.25.4 → 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.
@@ -3033,7 +3033,7 @@ var gobjJs = (function(exports) {
3033
3033
  *
3034
3034
  * Licence: MIT (http://www.opensource.org/licenses/mit-license)
3035
3035
  * Copyright (c) 2014,2024 Niyamaka.
3036
- * Copyright (c) 2025, ArtGins.
3036
+ * Copyright (c) 2025-2026, ArtGins.
3037
3037
  *********************************************************************************/
3038
3038
  var kw_flag_t = Object.freeze({
3039
3039
  KW_REQUIRED: 1,
@@ -3561,30 +3561,46 @@ var gobjJs = (function(exports) {
3561
3561
  return false;
3562
3562
  }
3563
3563
  /************************************************************
3564
- *
3564
+ * The value at `path` (keys joined by a backtick), as the C
3565
+ * kw_find_path() finds it: undefined when it is not there.
3566
+ * A kw that is not a dict or a list is LOGGED ("kw must be list
3567
+ * or dict") and answers undefined, as C answers NULL; so does a
3568
+ * segment in the middle of the path whose value is a scalar. A
3569
+ * middle segment that is absent or null answers undefined, logged
3570
+ * only when `verbose`. Before gobj-js 7.25.6 a bad kw answered 0,
3571
+ * which the typed readers took for a value found (kw_get_int() and
3572
+ * kw_get_real() answered 0 instead of the default, kw_get_str() and
3573
+ * kw_get_bool() logged a second line), the log named no path and
3574
+ * called gobj_short_name() on a null gobj ("gobj bad type"), and a
3575
+ * null middle segment threw a TypeError.
3565
3576
  ************************************************************/
3566
3577
  function kw_find_path(gobj, kw, path, verbose) {
3567
3578
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
3579
+ const prefix = gobj ? `${gobj_short_name(gobj)}: ` : "";
3568
3580
  if (!(is_object(kw) || is_array(kw))) {
3569
- log_error(`${gobj_short_name(gobj)}: kw must be list or dict`);
3570
- return 0;
3581
+ log_error(`${prefix}kw must be list or dict: '${String(path)}'`);
3582
+ return;
3571
3583
  }
3572
3584
  if (!is_string(path)) {
3573
- log_error(`${gobj_short_name(gobj)}: path must be a string: ${String(path)}`);
3574
- return 0;
3585
+ log_error(`${prefix}path must be a string: ${String(path)}`);
3586
+ return;
3575
3587
  }
3576
3588
  let ss = path.split("`");
3577
- if (ss.length <= 1) return kw[path];
3578
3589
  let len = ss.length;
3579
- for (let i = 0; i < len; i++) {
3580
- let key = ss[i];
3581
- kw = kw[key];
3582
- if (kw === void 0) {
3583
- if (verbose) log_error(`${gobj_short_name(gobj)}: path not found: ${String(path)}`);
3590
+ for (let i = 0; i < len - 1; i++) {
3591
+ kw = kw[ss[i]];
3592
+ if (kw === void 0 || kw === null) {
3593
+ if (verbose) log_error(`${prefix}path not found: '${path}'`);
3594
+ return;
3595
+ }
3596
+ if (!(is_object(kw) || is_array(kw))) {
3597
+ log_error(`${prefix}kw must be list or dict: '${path}'`);
3584
3598
  return;
3585
3599
  }
3586
3600
  }
3587
- return kw;
3601
+ let value = kw[ss[len - 1]];
3602
+ if (value === void 0 && verbose) log_error(`${prefix}path not found: '${path}'`);
3603
+ return value;
3588
3604
  }
3589
3605
  /************************************************************
3590
3606
  *
@@ -3609,10 +3625,13 @@ var gobjJs = (function(exports) {
3609
3625
  * anything else gives the default back, LOGGED ("path MUST BE a
3610
3626
  * json boolean", required or not) -- unless KW_WILD_NUMBER asks
3611
3627
  * to read a number (0 is false), a string ("true"/"false" in any
3612
- * case, else its integer) or null (false); a list or a dict is
3613
- * then false, logged too. Before gobj-js 7.25.2 the value went
3614
- * through Boolean(), so "false" was TRUE; before 7.25.3 a value
3615
- * of the wrong type gave the default back in silence.
3628
+ * case, else its decimal integer as atoi() reads it: "0x1F" is 0,
3629
+ * so false) or null (false); a list or a dict is then false,
3630
+ * logged too. Before gobj-js 7.25.2 the value went through
3631
+ * Boolean(), so "false" was TRUE; before 7.25.3 a value of the
3632
+ * wrong type gave the default back in silence; before 7.25.5 the
3633
+ * string went through parseInt() with no base, which reads hex:
3634
+ * "0x1F" was TRUE.
3616
3635
  ************************************************************/
3617
3636
  function kw_get_bool(gobj, kw, path, default_value, flag) {
3618
3637
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
@@ -3622,7 +3641,7 @@ var gobjJs = (function(exports) {
3622
3641
  const extract = flag && flag & kw_flag_t.KW_EXTRACT;
3623
3642
  const wild = flag && flag & kw_flag_t.KW_WILD_NUMBER;
3624
3643
  if (b === void 0) {
3625
- if (create) {
3644
+ if (create && kw) {
3626
3645
  let v = Boolean(default_value);
3627
3646
  kw_set_dict_value(gobj, kw, path, v);
3628
3647
  return v;
@@ -3638,7 +3657,7 @@ var gobjJs = (function(exports) {
3638
3657
  else if (wild && is_string(b)) {
3639
3658
  if (b.toLowerCase() === "true") value = true;
3640
3659
  else if (b.toLowerCase() === "false") value = false;
3641
- else value = (parseInt(b) || 0) !== 0;
3660
+ else value = (parseInt(b, 10) || 0) !== 0;
3642
3661
  } else if (wild && b === null) value = false;
3643
3662
  else if (wild) {
3644
3663
  log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a simple json element: '${path}'`);
@@ -3690,7 +3709,7 @@ var gobjJs = (function(exports) {
3690
3709
  const extract = flag && flag & kw_flag_t.KW_EXTRACT;
3691
3710
  const wild = flag && flag & kw_flag_t.KW_WILD_NUMBER;
3692
3711
  if (v === void 0) {
3693
- if (create) {
3712
+ if (create && kw) {
3694
3713
  let v = parseInt(default_value);
3695
3714
  kw_set_dict_value(gobj, kw, path, v);
3696
3715
  return v;
@@ -3737,7 +3756,7 @@ var gobjJs = (function(exports) {
3737
3756
  const extract = flag && flag & kw_flag_t.KW_EXTRACT;
3738
3757
  const wild = flag && flag & kw_flag_t.KW_WILD_NUMBER;
3739
3758
  if (v === void 0) {
3740
- if (create) {
3759
+ if (create && kw) {
3741
3760
  let v = Number(default_value);
3742
3761
  kw_set_dict_value(gobj, kw, path, v);
3743
3762
  return v;
@@ -3767,6 +3786,21 @@ var gobjJs = (function(exports) {
3767
3786
  return value;
3768
3787
  }
3769
3788
  /************************************************************
3789
+ * TRUE when the last key of `path` is a key of its dict, whatever
3790
+ * its value: kw_find_path() answers undefined both for a key that
3791
+ * is not there and for one whose value is undefined.
3792
+ ************************************************************/
3793
+ function kw_path_holds_key(kw, path) {
3794
+ let ss = is_string(path) ? path.split("`") : [];
3795
+ let parent = kw;
3796
+ for (let i = 0; i < ss.length - 1; i++) {
3797
+ if (!(is_object(parent) || is_array(parent))) return false;
3798
+ parent = parent[ss[i]];
3799
+ }
3800
+ if (!(is_object(parent) || is_array(parent)) || ss.length === 0) return false;
3801
+ return Object.prototype.hasOwnProperty.call(parent, ss[ss.length - 1]);
3802
+ }
3803
+ /************************************************************
3770
3804
  * The default comes back AS GIVEN, as in the C kw_get_str()
3771
3805
  * (`const char *default_value`, where 0 is NULL). Turned into
3772
3806
  * a string, a default of 0 or null was "0" or "null", which is
@@ -3778,6 +3812,12 @@ var gobjJs = (function(exports) {
3778
3812
  * C refuses to extract a string at all ("Cannot extract a
3779
3813
  * string": its answer would point into the freed json); a JS
3780
3814
  * string outlives the kw, so here it is taken out.
3815
+ * A value that is there and is not a string gives the default
3816
+ * back, LOGGED ("path MUST BE a json str", required or not), as
3817
+ * C does; a null value is the key saying "no string" and is not
3818
+ * logged, and KW_REQUIRED logs only a path that is not there at
3819
+ * all. Before gobj-js 7.25.5 a value of another type was logged
3820
+ * only with KW_REQUIRED, and then a null one too.
3781
3821
  ************************************************************/
3782
3822
  function kw_get_str(gobj, kw, path, default_value, flag) {
3783
3823
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
@@ -3786,19 +3826,19 @@ var gobjJs = (function(exports) {
3786
3826
  const create = flag && flag & kw_flag_t.KW_CREATE;
3787
3827
  const extract = flag && flag & kw_flag_t.KW_EXTRACT;
3788
3828
  if (v === void 0) {
3789
- if (create) {
3829
+ if (create && kw) {
3790
3830
  kw_set_dict_value(gobj, kw, path, is_string(default_value) ? default_value : null);
3791
3831
  return default_value;
3792
- } else if (required) {
3832
+ } else if (required && !kw_path_holds_key(kw, path)) {
3793
3833
  log_error(`path not found: '${path}'`);
3794
3834
  trace_json(kw);
3795
3835
  }
3796
3836
  return default_value;
3797
3837
  }
3798
3838
  if (!is_string(v)) {
3799
- if (required) {
3800
- log_error(`${gobj_short_name(gobj)}: path value MUST BE a string: ${path}`);
3801
- trace_msg(kw);
3839
+ if (v !== null) {
3840
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json str: '${path}'`);
3841
+ trace_json(kw);
3802
3842
  }
3803
3843
  return default_value;
3804
3844
  }
@@ -3854,8 +3894,8 @@ var gobjJs = (function(exports) {
3854
3894
  }
3855
3895
  if (!is_object(v)) {
3856
3896
  if (required) {
3857
- log_error(`${gobj_short_name(gobj)}: path value MUST BE a dict: ${path}`);
3858
- trace_msg(kw);
3897
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json dict: '${path}'`);
3898
+ trace_json(kw);
3859
3899
  }
3860
3900
  return default_value;
3861
3901
  }
@@ -3912,8 +3952,8 @@ var gobjJs = (function(exports) {
3912
3952
  }
3913
3953
  if (!is_array(v)) {
3914
3954
  if (required) {
3915
- log_error(`${gobj_short_name(gobj)}: path value MUST BE an array: ${path}`);
3916
- trace_msg(kw);
3955
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json list: '${path}'`);
3956
+ trace_json(kw);
3917
3957
  }
3918
3958
  return default_value;
3919
3959
  }