-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautocast.js
34 lines (30 loc) · 1007 Bytes
/
autocast.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
These codes is borrowed from
https://github.com/mholt/PapaParse/blob/master/papaparse.js
*/
const MAX_FLOAT = Math.pow(2, 53);
const MIN_FLOAT = -MAX_FLOAT;
const FLOAT = /^\s*-?(\d+\.?|\.\d+|\d+\.\d+)(e[-+]?\d+)?\s*$/;
const ISO_DATE = /(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/;
function autoCast(value) {
if (value === 'true' || value === 'TRUE')
return true;
else if (value === 'false' || value === 'FALSE')
return false;
else if (testFloat(value))
return parseFloat(value);
else if (ISO_DATE.test(value))
return new Date(value);
else
return (value === '' ? undefined : value);
}
function testFloat(s) {
if (FLOAT.test(s)) {
const floatValue = parseFloat(s);
if (floatValue > MIN_FLOAT && floatValue < MAX_FLOAT) {
return true;
}
}
return false;
}
module.exports = autoCast;