-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumberParser.js
More file actions
109 lines (102 loc) · 2.39 KB
/
numberParser.js
File metadata and controls
109 lines (102 loc) · 2.39 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Number Parser Test Cases
const arr = [
'012',
'-0',
'0',
'-01',
'-',
'--',
'00',
'-00',
'+21',
' ',
'1221',
'-1---1',
'1001',
'-1001',
'-231',
'123ABC',
'123,456',
'-213abc,',
'-0ABD',
'-ABC123',
'ABC12',
'1.33E+4r66r+++r',
'-1.3333e+9uu',
'--1.3333e+9uu',
'+1.3333e+9uu',
'0',
'0.0',
'-0',
'012',
'-012',
'00',
'0e45',
'0e45',
'0ab',
'.err',
'-0.1e10',
'.12',
'0.12',
'e12',
'.e12',
'e+12',
'.e+12',
'1.e12',
'0 ',
'1e12',
'0.e',
'1.3abc'
]
// Number Parser
function numberParser (str) {
// if (/^[-]?0[\d]+/.test(str)) {
// return null
// }
// // if (/^[-]?0[\w]+/.test(str)) {
// // const matchLength = str.match(/^[-]?0[\w]/)[0].length
// // return [str.slice(0, matchLength), str.slice(matchLength)]
// // }
// // if (/^[-]?0$|^[-]?[1-9][0-9]*/.test(str)) {
// // const matchLength = str.match(/^[-]?0$|^[-]?[1-9][0-9]*/)[0].length
// // return [str.slice(0, matchLength), str.slice(matchLength)]
// // }
// if (/^[-]?0[\d]+/.test(str)) {
// return null
// }
// // if (/^[-]?0[\w]+/.test(str)) {
// // const matchLength = str.match(/^[-]?0[\w]/)[0].length
// // return [str.slice(0, matchLength), str.slice(matchLength)]
// // }
// // if (/^[-]?0$|^[-]?[1-9][0-9]*/.test(str)) {
// // const matchLength = str.match(/^[-]?0$|^[-]?[1-9][0-9]*/)[0].length
// // return [str.slice(0, matchLength), str.slice(matchLength)]
// // }
// if (/^[-]?0\d+/.test(str)) {
// return null
// }
// if (
// /^[-]?((0(\.\d+)?(e[+-]?\d+)?)|([1-9]\d*(\.\d+)?)(e[+-]?\d+)?)/i.test(str)
// ) {
// const matchLength = str.match(
// /^[-]?((0(\.\d+)?(e[+-]?\d+)?)|([1-9]\d*(\.\d+)?)(e[+-]?\d+)?)/i
// )[0].length
// return [str.slice(0, matchLength), str.slice(matchLength)]
// }
// return null
// if (/^[-]?(0|[1-9]\d*)(\.\d+)?(e[+-]?\d+)?/i.test(str)) {
// const matchLength = str.match(/^[-]?(0|[1-9]\d*)(\.\d+)?(e[+-]?\d+)?/i)[0]
// .length
// return [str.slice(0, matchLength), str.slice(matchLength)]
// }
if (/^[-]?0\d+/.test(str)) {
return null
}
const matchLength = str.match(/^[-]?(0|[1-9]\d*)(\.\d+)?(e[+-]?\d+)?/i)
if (!matchLength) return null
return [matchLength[0], str.slice(matchLength[0].length)]
}
// for (const ele of arr) {
// console.log(ele + ' -->', numberParser(ele))
// }
module.exports = numberParser