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