@yuneta/gobj-js 7.25.4 → 7.25.5

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.
@@ -3031,7 +3031,7 @@ function gobj_stats(gobj, stats, kw, src) {
3031
3031
  *
3032
3032
  * Licence: MIT (http://www.opensource.org/licenses/mit-license)
3033
3033
  * Copyright (c) 2014,2024 Niyamaka.
3034
- * Copyright (c) 2025, ArtGins.
3034
+ * Copyright (c) 2025-2026, ArtGins.
3035
3035
  *********************************************************************************/
3036
3036
  var kw_flag_t = Object.freeze({
3037
3037
  KW_REQUIRED: 1,
@@ -3607,10 +3607,13 @@ function kw_delete(gobj, kw, path) {
3607
3607
  * anything else gives the default back, LOGGED ("path MUST BE a
3608
3608
  * json boolean", required or not) -- unless KW_WILD_NUMBER asks
3609
3609
  * to read a number (0 is false), a string ("true"/"false" in any
3610
- * case, else its integer) or null (false); a list or a dict is
3611
- * then false, logged too. Before gobj-js 7.25.2 the value went
3612
- * through Boolean(), so "false" was TRUE; before 7.25.3 a value
3613
- * of the wrong type gave the default back in silence.
3610
+ * case, else its decimal integer as atoi() reads it: "0x1F" is 0,
3611
+ * so false) or null (false); a list or a dict is then false,
3612
+ * logged too. Before gobj-js 7.25.2 the value went through
3613
+ * Boolean(), so "false" was TRUE; before 7.25.3 a value of the
3614
+ * wrong type gave the default back in silence; before 7.25.5 the
3615
+ * string went through parseInt() with no base, which reads hex:
3616
+ * "0x1F" was TRUE.
3614
3617
  ************************************************************/
3615
3618
  function kw_get_bool(gobj, kw, path, default_value, flag) {
3616
3619
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
@@ -3636,7 +3639,7 @@ function kw_get_bool(gobj, kw, path, default_value, flag) {
3636
3639
  else if (wild && is_string(b)) {
3637
3640
  if (b.toLowerCase() === "true") value = true;
3638
3641
  else if (b.toLowerCase() === "false") value = false;
3639
- else value = (parseInt(b) || 0) !== 0;
3642
+ else value = (parseInt(b, 10) || 0) !== 0;
3640
3643
  } else if (wild && b === null) value = false;
3641
3644
  else if (wild) {
3642
3645
  log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a simple json element: '${path}'`);
@@ -3765,6 +3768,21 @@ function kw_get_real(gobj, kw, path, default_value, flag) {
3765
3768
  return value;
3766
3769
  }
3767
3770
  /************************************************************
3771
+ * TRUE when the last key of `path` is a key of its dict, whatever
3772
+ * its value: kw_find_path() answers undefined both for a key that
3773
+ * is not there and for one whose value is undefined.
3774
+ ************************************************************/
3775
+ function kw_path_holds_key(kw, path) {
3776
+ let ss = is_string(path) ? path.split("`") : [];
3777
+ let parent = kw;
3778
+ for (let i = 0; i < ss.length - 1; i++) {
3779
+ if (!(is_object(parent) || is_array(parent))) return false;
3780
+ parent = parent[ss[i]];
3781
+ }
3782
+ if (!(is_object(parent) || is_array(parent)) || ss.length === 0) return false;
3783
+ return Object.prototype.hasOwnProperty.call(parent, ss[ss.length - 1]);
3784
+ }
3785
+ /************************************************************
3768
3786
  * The default comes back AS GIVEN, as in the C kw_get_str()
3769
3787
  * (`const char *default_value`, where 0 is NULL). Turned into
3770
3788
  * a string, a default of 0 or null was "0" or "null", which is
@@ -3776,6 +3794,12 @@ function kw_get_real(gobj, kw, path, default_value, flag) {
3776
3794
  * C refuses to extract a string at all ("Cannot extract a
3777
3795
  * string": its answer would point into the freed json); a JS
3778
3796
  * string outlives the kw, so here it is taken out.
3797
+ * A value that is there and is not a string gives the default
3798
+ * back, LOGGED ("path MUST BE a json str", required or not), as
3799
+ * C does; a null value is the key saying "no string" and is not
3800
+ * logged, and KW_REQUIRED logs only a path that is not there at
3801
+ * all. Before gobj-js 7.25.5 a value of another type was logged
3802
+ * only with KW_REQUIRED, and then a null one too.
3779
3803
  ************************************************************/
3780
3804
  function kw_get_str(gobj, kw, path, default_value, flag) {
3781
3805
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
@@ -3787,16 +3811,16 @@ function kw_get_str(gobj, kw, path, default_value, flag) {
3787
3811
  if (create) {
3788
3812
  kw_set_dict_value(gobj, kw, path, is_string(default_value) ? default_value : null);
3789
3813
  return default_value;
3790
- } else if (required) {
3814
+ } else if (required && !kw_path_holds_key(kw, path)) {
3791
3815
  log_error(`path not found: '${path}'`);
3792
3816
  trace_json(kw);
3793
3817
  }
3794
3818
  return default_value;
3795
3819
  }
3796
3820
  if (!is_string(v)) {
3797
- if (required) {
3798
- log_error(`${gobj_short_name(gobj)}: path value MUST BE a string: ${path}`);
3799
- trace_msg(kw);
3821
+ if (v !== null) {
3822
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json str: '${path}'`);
3823
+ trace_json(kw);
3800
3824
  }
3801
3825
  return default_value;
3802
3826
  }
@@ -3852,8 +3876,8 @@ function kw_get_dict(gobj, kw, path, default_value, flag) {
3852
3876
  }
3853
3877
  if (!is_object(v)) {
3854
3878
  if (required) {
3855
- log_error(`${gobj_short_name(gobj)}: path value MUST BE a dict: ${path}`);
3856
- trace_msg(kw);
3879
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json dict: '${path}'`);
3880
+ trace_json(kw);
3857
3881
  }
3858
3882
  return default_value;
3859
3883
  }
@@ -3910,8 +3934,8 @@ function kw_get_list(gobj, kw, path, default_value, flag) {
3910
3934
  }
3911
3935
  if (!is_array(v)) {
3912
3936
  if (required) {
3913
- log_error(`${gobj_short_name(gobj)}: path value MUST BE an array: ${path}`);
3914
- trace_msg(kw);
3937
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json list: '${path}'`);
3938
+ trace_json(kw);
3915
3939
  }
3916
3940
  return default_value;
3917
3941
  }