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