@kizmann/pico-js 0.5.1 → 0.5.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kizmann/pico-js",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "private": false,
@@ -101,14 +101,56 @@ export class Any
101
101
  return this.isEmpty(val) ? empty : this.string(val);
102
102
  }
103
103
 
104
+ static number(val, fallback = NaN)
105
+ {
106
+ let res = typeof val.toString === 'undefined' ?
107
+ String(val) : val.toString();
108
+
109
+ if ( ! Any.isString(res) ) {
110
+ return fallback;
111
+ }
112
+
113
+ if ( res.match(/^[0-9]+$/) ) {
114
+ return this.integer(val);
115
+ }
116
+
117
+ if ( res.match(/^[0-9.,]+$/) ) {
118
+ return this.float(val);
119
+ }
120
+
121
+ if ( ! this.isNumber(res) || Number.isNaN(res) ) {
122
+ return fallback;
123
+ }
124
+
125
+ return res;
126
+ }
127
+
104
128
  static integer(val)
105
129
  {
130
+ let res = typeof val.toString === 'undefined' ?
131
+ String(val) : val.toString();
132
+
133
+ if ( ! Any.isString(res) ) {
134
+ return NaN;
135
+ }
136
+
106
137
  return parseInt(val);
107
138
  }
108
139
 
109
140
  static float(val)
110
141
  {
111
- return parseFloat(val);
142
+ let res = typeof val.toString === 'undefined' ?
143
+ String(val) : val.toString();
144
+
145
+ if ( ! Any.isString(res) ) {
146
+ return NaN;
147
+ }
148
+
149
+ if ( res.match(/^[0-9,]+$/) ) {
150
+ res = res.replace(/,/g, '.');
151
+ }
152
+
153
+ return parseFloat(res);
112
154
  }
113
155
 
114
156
  static bool(val)