@yuneta/gobj-js 7.25.2 → 7.25.4

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.
@@ -3604,10 +3604,13 @@ function kw_delete(gobj, kw, path) {
3604
3604
  }
3605
3605
  /************************************************************
3606
3606
  * As the C kw_get_bool(): a boolean found is the answer, and
3607
- * anything else gives the default back -- unless KW_WILD_NUMBER
3608
- * asks to read a number (0 is false), a string ("true"/"false"
3609
- * in any case, else its integer) or null (false). Before gobj-js
3610
- * 7.25.2 the value went through Boolean(), so "false" was TRUE.
3607
+ * anything else gives the default back, LOGGED ("path MUST BE a
3608
+ * json boolean", required or not) -- unless KW_WILD_NUMBER asks
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.
3611
3614
  ************************************************************/
3612
3615
  function kw_get_bool(gobj, kw, path, default_value, flag) {
3613
3616
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
@@ -3635,18 +3638,47 @@ function kw_get_bool(gobj, kw, path, default_value, flag) {
3635
3638
  else if (b.toLowerCase() === "false") value = false;
3636
3639
  else value = (parseInt(b) || 0) !== 0;
3637
3640
  } else if (wild && b === null) value = false;
3638
- else {
3639
- if (required) {
3640
- log_error(`${gobj_short_name(gobj)}: path value MUST BE a boolean: ${path}`);
3641
- trace_msg(kw);
3642
- }
3641
+ else if (wild) {
3642
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a simple json element: '${path}'`);
3643
+ trace_json(kw);
3644
+ return false;
3645
+ } else {
3646
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json boolean: '${path}'`);
3647
+ trace_json(kw);
3643
3648
  return Boolean(default_value);
3644
3649
  }
3645
3650
  if (extract) kw_delete(gobj, kw, path);
3646
3651
  return value;
3647
3652
  }
3648
3653
  /************************************************************
3649
- *
3654
+ * The integer at the start of a string, as C's strtoll(s, 0, 0)
3655
+ * reads it: leading spaces, a sign, then "0x" for hex, "0" for
3656
+ * octal, else decimal, up to the first character that is not a
3657
+ * digit of that base. 0 when there is no digit.
3658
+ ************************************************************/
3659
+ function strtoll_base0(s) {
3660
+ const m = /^\s*([+-]?)(0[xX][0-9a-fA-F]+|0[0-7]*|[1-9][0-9]*)/.exec(s);
3661
+ if (!m) return 0;
3662
+ let digits = m[2];
3663
+ let value;
3664
+ if (digits.length > 1 && (digits[1] === "x" || digits[1] === "X")) value = parseInt(digits.substring(2), 16);
3665
+ else if (digits[0] === "0") value = parseInt(digits, 8);
3666
+ else value = parseInt(digits, 10);
3667
+ return m[1] === "-" ? -value : value;
3668
+ }
3669
+ /************************************************************
3670
+ * As the C kw_get_int(): a number found is the answer,
3671
+ * truncated toward zero as C casts a real, and anything else
3672
+ * gives the default back (as an integer), LOGGED ("path MUST BE
3673
+ * a json integer", required or not) -- unless KW_WILD_NUMBER
3674
+ * asks to read a boolean (1/0), a string (as strtoll() with base
3675
+ * 0: "0x1F" is 31, "017" is 15, "12abc" is 12, "abc" is 0) or
3676
+ * null (0); a list or a dict is then 0, logged too. KW_EXTRACT
3677
+ * takes out only a value the reader answers with.
3678
+ * Before gobj-js 7.25.4 KW_EXTRACT deleted the value before its
3679
+ * type was looked at, a value of another type was logged only
3680
+ * with KW_REQUIRED, KW_WILD_NUMBER was ignored, and a number went
3681
+ * through parseInt(), which reads it as a string: 1e-7 gave 1.
3650
3682
  ************************************************************/
3651
3683
  function kw_get_int(gobj, kw, path, default_value, flag) {
3652
3684
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
@@ -3654,6 +3686,7 @@ function kw_get_int(gobj, kw, path, default_value, flag) {
3654
3686
  const required = flag && flag & kw_flag_t.KW_REQUIRED;
3655
3687
  const create = flag && flag & kw_flag_t.KW_CREATE;
3656
3688
  const extract = flag && flag & kw_flag_t.KW_EXTRACT;
3689
+ const wild = flag && flag & kw_flag_t.KW_WILD_NUMBER;
3657
3690
  if (v === void 0) {
3658
3691
  if (create) {
3659
3692
  let v = parseInt(default_value);
@@ -3665,18 +3698,34 @@ function kw_get_int(gobj, kw, path, default_value, flag) {
3665
3698
  }
3666
3699
  return parseInt(default_value);
3667
3700
  }
3668
- if (extract) kw_delete(gobj, kw, path);
3669
- if (!is_number(v)) {
3670
- if (required) {
3671
- log_error(`path value MUST BE a number: ${path}`);
3672
- trace_msg(kw);
3673
- }
3701
+ let value;
3702
+ if (is_number(v)) value = Math.trunc(v);
3703
+ else if (!wild) {
3704
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json integer: '${path}'`);
3705
+ trace_json(kw);
3674
3706
  return parseInt(default_value);
3707
+ } else if (is_boolean(v)) value = v ? 1 : 0;
3708
+ else if (is_string(v)) value = strtoll_base0(v);
3709
+ else if (v === null) value = 0;
3710
+ else {
3711
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a simple json element: '${path}'`);
3712
+ trace_json(kw);
3713
+ return 0;
3675
3714
  }
3676
- return parseInt(v);
3715
+ if (extract) kw_delete(gobj, kw, path);
3716
+ return value;
3677
3717
  }
3678
3718
  /************************************************************
3679
- *
3719
+ * As the C kw_get_real(): a number found is the answer, and
3720
+ * anything else gives the default back (as a number), LOGGED
3721
+ * ("path MUST BE a json real", required or not) -- unless
3722
+ * KW_WILD_NUMBER asks to read a boolean (1/0), a string (as
3723
+ * parseFloat() reads it, 0 when it does not start with a number)
3724
+ * or null (0); a list or a dict is then 0, logged too.
3725
+ * KW_EXTRACT takes out only a value the reader answers with.
3726
+ * Before gobj-js 7.25.4 KW_EXTRACT deleted the value before its
3727
+ * type was looked at, a value of another type was logged only
3728
+ * with KW_REQUIRED, and KW_WILD_NUMBER was ignored.
3680
3729
  ************************************************************/
3681
3730
  function kw_get_real(gobj, kw, path, default_value, flag) {
3682
3731
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
@@ -3684,6 +3733,7 @@ function kw_get_real(gobj, kw, path, default_value, flag) {
3684
3733
  const required = flag && flag & kw_flag_t.KW_REQUIRED;
3685
3734
  const create = flag && flag & kw_flag_t.KW_CREATE;
3686
3735
  const extract = flag && flag & kw_flag_t.KW_EXTRACT;
3736
+ const wild = flag && flag & kw_flag_t.KW_WILD_NUMBER;
3687
3737
  if (v === void 0) {
3688
3738
  if (create) {
3689
3739
  let v = Number(default_value);
@@ -3695,15 +3745,24 @@ function kw_get_real(gobj, kw, path, default_value, flag) {
3695
3745
  }
3696
3746
  return Number(default_value);
3697
3747
  }
3698
- if (extract) kw_delete(gobj, kw, path);
3699
- if (!is_number(v)) {
3700
- if (required) {
3701
- log_error(`path value MUST BE a number: ${path}`);
3702
- trace_msg(kw);
3703
- }
3748
+ let value;
3749
+ if (is_number(v)) value = v;
3750
+ else if (!wild) {
3751
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json real: '${path}'`);
3752
+ trace_json(kw);
3704
3753
  return Number(default_value);
3754
+ } else if (is_boolean(v)) value = v ? 1 : 0;
3755
+ else if (is_string(v)) {
3756
+ value = parseFloat(v);
3757
+ if (Number.isNaN(value)) value = 0;
3758
+ } else if (v === null) value = 0;
3759
+ else {
3760
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a simple json element: '${path}'`);
3761
+ trace_json(kw);
3762
+ return 0;
3705
3763
  }
3706
- return Number(v);
3764
+ if (extract) kw_delete(gobj, kw, path);
3765
+ return value;
3707
3766
  }
3708
3767
  /************************************************************
3709
3768
  * The default comes back AS GIVEN, as in the C kw_get_str()
@@ -3712,6 +3771,11 @@ function kw_get_real(gobj, kw, path, default_value, flag) {
3712
3771
  * TRUE: an `if(value)` on the answer took an absent key for a
3713
3772
  * present one. KW_CREATE stores a string default, and null for
3714
3773
  * any other, as C stores json_null() for a NULL default.
3774
+ * KW_EXTRACT takes out only a string (before gobj-js 7.25.4 it
3775
+ * deleted any value, and a value that was not a string was lost).
3776
+ * C refuses to extract a string at all ("Cannot extract a
3777
+ * string": its answer would point into the freed json); a JS
3778
+ * string outlives the kw, so here it is taken out.
3715
3779
  ************************************************************/
3716
3780
  function kw_get_str(gobj, kw, path, default_value, flag) {
3717
3781
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
@@ -3729,7 +3793,6 @@ function kw_get_str(gobj, kw, path, default_value, flag) {
3729
3793
  }
3730
3794
  return default_value;
3731
3795
  }
3732
- if (extract) kw_delete(gobj, kw, path);
3733
3796
  if (!is_string(v)) {
3734
3797
  if (required) {
3735
3798
  log_error(`${gobj_short_name(gobj)}: path value MUST BE a string: ${path}`);
@@ -3737,6 +3800,7 @@ function kw_get_str(gobj, kw, path, default_value, flag) {
3737
3800
  }
3738
3801
  return default_value;
3739
3802
  }
3803
+ if (extract) kw_delete(gobj, kw, path);
3740
3804
  return v;
3741
3805
  }
3742
3806
  /************************************************************