@yuneta/gobj-js 7.25.3 → 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}'`);
@@ -3655,7 +3658,34 @@
3655
3658
  return value;
3656
3659
  }
3657
3660
  /************************************************************
3658
- *
3661
+ * The integer at the start of a string, as C's strtoll(s, 0, 0)
3662
+ * reads it: leading spaces, a sign, then "0x" for hex, "0" for
3663
+ * octal, else decimal, up to the first character that is not a
3664
+ * digit of that base. 0 when there is no digit.
3665
+ ************************************************************/
3666
+ function strtoll_base0(s) {
3667
+ const m = /^\s*([+-]?)(0[xX][0-9a-fA-F]+|0[0-7]*|[1-9][0-9]*)/.exec(s);
3668
+ if (!m) return 0;
3669
+ let digits = m[2];
3670
+ let value;
3671
+ if (digits.length > 1 && (digits[1] === "x" || digits[1] === "X")) value = parseInt(digits.substring(2), 16);
3672
+ else if (digits[0] === "0") value = parseInt(digits, 8);
3673
+ else value = parseInt(digits, 10);
3674
+ return m[1] === "-" ? -value : value;
3675
+ }
3676
+ /************************************************************
3677
+ * As the C kw_get_int(): a number found is the answer,
3678
+ * truncated toward zero as C casts a real, and anything else
3679
+ * gives the default back (as an integer), LOGGED ("path MUST BE
3680
+ * a json integer", required or not) -- unless KW_WILD_NUMBER
3681
+ * asks to read a boolean (1/0), a string (as strtoll() with base
3682
+ * 0: "0x1F" is 31, "017" is 15, "12abc" is 12, "abc" is 0) or
3683
+ * null (0); a list or a dict is then 0, logged too. KW_EXTRACT
3684
+ * takes out only a value the reader answers with.
3685
+ * Before gobj-js 7.25.4 KW_EXTRACT deleted the value before its
3686
+ * type was looked at, a value of another type was logged only
3687
+ * with KW_REQUIRED, KW_WILD_NUMBER was ignored, and a number went
3688
+ * through parseInt(), which reads it as a string: 1e-7 gave 1.
3659
3689
  ************************************************************/
3660
3690
  function kw_get_int(gobj, kw, path, default_value, flag) {
3661
3691
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
@@ -3663,6 +3693,7 @@
3663
3693
  const required = flag && flag & kw_flag_t.KW_REQUIRED;
3664
3694
  const create = flag && flag & kw_flag_t.KW_CREATE;
3665
3695
  const extract = flag && flag & kw_flag_t.KW_EXTRACT;
3696
+ const wild = flag && flag & kw_flag_t.KW_WILD_NUMBER;
3666
3697
  if (v === void 0) {
3667
3698
  if (create) {
3668
3699
  let v = parseInt(default_value);
@@ -3674,18 +3705,34 @@
3674
3705
  }
3675
3706
  return parseInt(default_value);
3676
3707
  }
3677
- if (extract) kw_delete(gobj, kw, path);
3678
- if (!is_number(v)) {
3679
- if (required) {
3680
- log_error(`path value MUST BE a number: ${path}`);
3681
- trace_msg(kw);
3682
- }
3708
+ let value;
3709
+ if (is_number(v)) value = Math.trunc(v);
3710
+ else if (!wild) {
3711
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json integer: '${path}'`);
3712
+ trace_json(kw);
3683
3713
  return parseInt(default_value);
3714
+ } else if (is_boolean(v)) value = v ? 1 : 0;
3715
+ else if (is_string(v)) value = strtoll_base0(v);
3716
+ else if (v === null) value = 0;
3717
+ else {
3718
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a simple json element: '${path}'`);
3719
+ trace_json(kw);
3720
+ return 0;
3684
3721
  }
3685
- return parseInt(v);
3722
+ if (extract) kw_delete(gobj, kw, path);
3723
+ return value;
3686
3724
  }
3687
3725
  /************************************************************
3688
- *
3726
+ * As the C kw_get_real(): a number found is the answer, and
3727
+ * anything else gives the default back (as a number), LOGGED
3728
+ * ("path MUST BE a json real", required or not) -- unless
3729
+ * KW_WILD_NUMBER asks to read a boolean (1/0), a string (as
3730
+ * parseFloat() reads it, 0 when it does not start with a number)
3731
+ * or null (0); a list or a dict is then 0, logged too.
3732
+ * KW_EXTRACT takes out only a value the reader answers with.
3733
+ * Before gobj-js 7.25.4 KW_EXTRACT deleted the value before its
3734
+ * type was looked at, a value of another type was logged only
3735
+ * with KW_REQUIRED, and KW_WILD_NUMBER was ignored.
3689
3736
  ************************************************************/
3690
3737
  function kw_get_real(gobj, kw, path, default_value, flag) {
3691
3738
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
@@ -3693,6 +3740,7 @@
3693
3740
  const required = flag && flag & kw_flag_t.KW_REQUIRED;
3694
3741
  const create = flag && flag & kw_flag_t.KW_CREATE;
3695
3742
  const extract = flag && flag & kw_flag_t.KW_EXTRACT;
3743
+ const wild = flag && flag & kw_flag_t.KW_WILD_NUMBER;
3696
3744
  if (v === void 0) {
3697
3745
  if (create) {
3698
3746
  let v = Number(default_value);
@@ -3704,15 +3752,39 @@
3704
3752
  }
3705
3753
  return Number(default_value);
3706
3754
  }
3707
- if (extract) kw_delete(gobj, kw, path);
3708
- if (!is_number(v)) {
3709
- if (required) {
3710
- log_error(`path value MUST BE a number: ${path}`);
3711
- trace_msg(kw);
3712
- }
3755
+ let value;
3756
+ if (is_number(v)) value = v;
3757
+ else if (!wild) {
3758
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json real: '${path}'`);
3759
+ trace_json(kw);
3713
3760
  return Number(default_value);
3761
+ } else if (is_boolean(v)) value = v ? 1 : 0;
3762
+ else if (is_string(v)) {
3763
+ value = parseFloat(v);
3764
+ if (Number.isNaN(value)) value = 0;
3765
+ } else if (v === null) value = 0;
3766
+ else {
3767
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a simple json element: '${path}'`);
3768
+ trace_json(kw);
3769
+ return 0;
3770
+ }
3771
+ if (extract) kw_delete(gobj, kw, path);
3772
+ return value;
3773
+ }
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]];
3714
3785
  }
3715
- return Number(v);
3786
+ if (!(is_object(parent) || is_array(parent)) || ss.length === 0) return false;
3787
+ return Object.prototype.hasOwnProperty.call(parent, ss[ss.length - 1]);
3716
3788
  }
3717
3789
  /************************************************************
3718
3790
  * The default comes back AS GIVEN, as in the C kw_get_str()
@@ -3721,6 +3793,17 @@
3721
3793
  * TRUE: an `if(value)` on the answer took an absent key for a
3722
3794
  * present one. KW_CREATE stores a string default, and null for
3723
3795
  * any other, as C stores json_null() for a NULL default.
3796
+ * KW_EXTRACT takes out only a string (before gobj-js 7.25.4 it
3797
+ * deleted any value, and a value that was not a string was lost).
3798
+ * C refuses to extract a string at all ("Cannot extract a
3799
+ * string": its answer would point into the freed json); a JS
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.
3724
3807
  ************************************************************/
3725
3808
  function kw_get_str(gobj, kw, path, default_value, flag) {
3726
3809
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
@@ -3732,20 +3815,20 @@
3732
3815
  if (create) {
3733
3816
  kw_set_dict_value(gobj, kw, path, is_string(default_value) ? default_value : null);
3734
3817
  return default_value;
3735
- } else if (required) {
3818
+ } else if (required && !kw_path_holds_key(kw, path)) {
3736
3819
  log_error(`path not found: '${path}'`);
3737
3820
  trace_json(kw);
3738
3821
  }
3739
3822
  return default_value;
3740
3823
  }
3741
- if (extract) kw_delete(gobj, kw, path);
3742
3824
  if (!is_string(v)) {
3743
- if (required) {
3744
- log_error(`${gobj_short_name(gobj)}: path value MUST BE a string: ${path}`);
3745
- 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);
3746
3828
  }
3747
3829
  return default_value;
3748
3830
  }
3831
+ if (extract) kw_delete(gobj, kw, path);
3749
3832
  return v;
3750
3833
  }
3751
3834
  /************************************************************
@@ -3797,8 +3880,8 @@
3797
3880
  }
3798
3881
  if (!is_object(v)) {
3799
3882
  if (required) {
3800
- log_error(`${gobj_short_name(gobj)}: path value MUST BE a dict: ${path}`);
3801
- trace_msg(kw);
3883
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json dict: '${path}'`);
3884
+ trace_json(kw);
3802
3885
  }
3803
3886
  return default_value;
3804
3887
  }
@@ -3855,8 +3938,8 @@
3855
3938
  }
3856
3939
  if (!is_array(v)) {
3857
3940
  if (required) {
3858
- log_error(`${gobj_short_name(gobj)}: path value MUST BE an array: ${path}`);
3859
- trace_msg(kw);
3941
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json list: '${path}'`);
3942
+ trace_json(kw);
3860
3943
  }
3861
3944
  return default_value;
3862
3945
  }