Repository: jsonata-js/jsonata (js)

Issue:
Proposal: Allow $number() to handle strings with leading '0' characters
Consider the following input:
`{"serialNumber": "ABC-1234"}`
with the following JSONata expression:
`{"isUnderThreshold": (serialNumber ~> $substringAfter('-') ~> $number()) < 5000}`
Naturally the result is 
`{"isUnderThreshold": true}`

However, consider the input:
`{"serialNumber": "ABC-0123"}`
 with the same expression.  Intuitively, one would expect that the end result would be the same. However, the end result is the error:
`Unable to cast value to a number: "0123"`
This is because the $number function as designed only handles legal JSON numbers which do not allow leading 0s.

Ideally, it would be nice if this rule was relaxed slightly to allow leading '0' characters.  Otherwise, I would have to change the JSONata expression to:
`{"isUnderThreshold": (serialNumber ~> $replace(/^\w*-?0*/, '') ~> $number()) < 5000}`

Pull request description:
allow leading zeros when casting string to number
Allow numeric strings to have leading zeros when casting to numbers in `$number()`
resolves #368 

Patch:
diff --git a/src/functions.js b/src/functions.js
index 8896e25..a2acf28 100644
--- a/src/functions.js
+++ b/src/functions.js
@@ -1194,7 +1194,7 @@ const functions = (() => {
         if (typeof arg === 'number') {
             // already a number
             result = arg;
-        } else if (typeof arg === 'string' && /^-?(0|([1-9][0-9]*))(\.[0-9]+)?([Ee][-+]?[0-9]+)?$/.test(arg) && !isNaN(parseFloat(arg)) && isFinite(arg)) {
+        } else if (typeof arg === 'string' && /^-?[0-9]+(\.[0-9]+)?([Ee][-+]?[0-9]+)?$/.test(arg) && !isNaN(parseFloat(arg)) && isFinite(arg)) {
             result = parseFloat(arg);
         } else if (arg === true) {
             // boolean true casts to 1


Test patch:
diff --git a/test/test-suite/groups/function-number/case027.json b/test/test-suite/groups/function-number/case027.json
new file mode 100644
index 0000000..24308ef
--- /dev/null
+++ b/test/test-suite/groups/function-number/case027.json
@@ -0,0 +1,6 @@
+{
+    "expr": "$number('00')",
+    "dataset": null,
+    "bindings": {},
+    "result": 0
+}
diff --git a/test/test-suite/groups/function-number/case028.json b/test/test-suite/groups/function-number/case028.json
new file mode 100644
index 0000000..1f98236
--- /dev/null
+++ b/test/test-suite/groups/function-number/case028.json
@@ -0,0 +1,6 @@
+{
+    "expr": "$number('0123')",
+    "dataset": null,
+    "bindings": {},
+    "result": 123
+}
diff --git a/test/test-suite/groups/function-number/case029.json b/test/test-suite/groups/function-number/case029.json
new file mode 100644
index 0000000..73c639b
--- /dev/null
+++ b/test/test-suite/groups/function-number/case029.json
@@ -0,0 +1,6 @@
+{
+    "expr": "$number('-007')",
+    "dataset": null,
+    "bindings": {},
+    "result": -7
+}
diff --git a/test/test-suite/groups/function-number/case030.json b/test/test-suite/groups/function-number/case030.json
new file mode 100644
index 0000000..c35d4d0
--- /dev/null
+++ b/test/test-suite/groups/function-number/case030.json
@@ -0,0 +1,6 @@
+{
+    "expr": "$number('000.00123000')",
+    "dataset": null,
+    "bindings": {},
+    "result": 0.00123
+}