mirror of
https://github.com/flynx/serialize.js.git
synced 2026-07-10 19:20:56 +00:00
better number support...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
6bc6d7cd53
commit
0feee69ee9
27
serialize.js
27
serialize.js
@ -408,13 +408,32 @@ module.eJSON = {
|
|||||||
&& str.slice(i, i+'-Infinity'.length) == '-Infinity'){
|
&& str.slice(i, i+'-Infinity'.length) == '-Infinity'){
|
||||||
return [-Infinity, i+'-Infinity'.length, line] }
|
return [-Infinity, i+'-Infinity'.length, line] }
|
||||||
// numbers...
|
// numbers...
|
||||||
var j = i+1
|
// XXX do we need to handle the odd case of 077 vs 088???
|
||||||
|
var j = i
|
||||||
|
var mode = 'dec'
|
||||||
|
if(str[j] == '0'
|
||||||
|
&& 'xXbBoO'.includes(str[j+1])){
|
||||||
|
mode = str[j+1]
|
||||||
|
j++ }
|
||||||
|
j++
|
||||||
while(j < str.length
|
while(j < str.length
|
||||||
&& (str[j] == '.'
|
&& (str[j] == '.'
|
||||||
|| str[j] == 'e'
|
// dec/oct/bin...
|
||||||
|
// XXX do we need to be pedantic and check the rest
|
||||||
|
// of the modes explicitly???
|
||||||
|| (str[j] >= '0'
|
|| (str[j] >= '0'
|
||||||
&& str[j] <= '9'))){
|
&& str[j] <= '9')
|
||||||
if(str.slice(j, j+2) == 'e+'){
|
// hex...
|
||||||
|
|| (mode == 'x'
|
||||||
|
&& ((str[j] >= 'a'
|
||||||
|
&& str[j] <= 'f')
|
||||||
|
|| (str[j] >= 'A'
|
||||||
|
&& str[j] <= 'F')))
|
||||||
|
// exponent...
|
||||||
|
|| str[j] == 'e'
|
||||||
|
|| str[j] == 'E')){
|
||||||
|
if('eE'.includes(str[j])
|
||||||
|
&& '+-'.includes(str[j+1])){
|
||||||
j++ }
|
j++ }
|
||||||
j++ }
|
j++ }
|
||||||
// BigInt...
|
// BigInt...
|
||||||
|
|||||||
3
test.js
3
test.js
@ -72,6 +72,8 @@ var setups = test.Setups({
|
|||||||
// XXX also test diffrerent quotations...
|
// XXX also test diffrerent quotations...
|
||||||
string: function(assert){
|
string: function(assert){
|
||||||
return ['"string"', json] },
|
return ['"string"', json] },
|
||||||
|
'string-empty': function(assert){
|
||||||
|
return ['""', json] },
|
||||||
'string-multiline': function(assert){
|
'string-multiline': function(assert){
|
||||||
return ['"string\\nstring\\nstring"', json] },
|
return ['"string\\nstring\\nstring"', json] },
|
||||||
|
|
||||||
@ -202,6 +204,7 @@ test.Cases({
|
|||||||
['123.', '123'],
|
['123.', '123'],
|
||||||
['+123', '123'],
|
['+123', '123'],
|
||||||
['123e100', '123e+100'],
|
['123e100', '123e+100'],
|
||||||
|
['0xff', '255'],
|
||||||
|
|
||||||
// string quotes...
|
// string quotes...
|
||||||
["'abc'", '"abc"'],
|
["'abc'", '"abc"'],
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user