1
1
const WHITE_SPACE_CHAR_REGEXP = / ^ \s /
2
2
3
- const throwParsingError = function ( path , index ) {
3
+ interface IParserState {
4
+ index : number ;
5
+ length : number ;
6
+ }
7
+
8
+ const throwParsingError = ( path : string , index : number ) => {
4
9
throw new Error (
5
10
'Parsing data path "' +
6
11
path +
@@ -12,7 +17,7 @@ const throwParsingError = function (path, index) {
12
17
)
13
18
}
14
19
15
- const parseArrIndex = function ( path , state ) {
20
+ const parseArrIndex = ( path : string , state : IParserState ) => {
16
21
const startIndex = state . index
17
22
while ( state . index < state . length ) {
18
23
const ch = path [ state . index ]
@@ -28,7 +33,7 @@ const parseArrIndex = function (path, state) {
28
33
return parseInt ( path . slice ( startIndex , state . index ) , 10 )
29
34
}
30
35
31
- const parseIdent = function ( path , state ) {
36
+ const parseIdent = ( path : string , state : IParserState ) => {
32
37
const startIndex = state . index
33
38
const ch = path [ startIndex ]
34
39
if ( / ^ [ _ a - z A - Z $ ] / . test ( ch ) ) {
@@ -47,7 +52,7 @@ const parseIdent = function (path, state) {
47
52
return path . slice ( startIndex , state . index )
48
53
}
49
54
50
- const parseSinglePath = function ( path , state ) {
55
+ const parseSinglePath = ( path : string , state : IParserState ) => {
51
56
const paths = [ parseIdent ( path , state ) ]
52
57
const options = {
53
58
deepCmp : false ,
@@ -56,7 +61,7 @@ const parseSinglePath = function (path, state) {
56
61
const ch = path [ state . index ]
57
62
if ( ch === '[' ) {
58
63
state . index ++
59
- paths . push ( parseArrIndex ( path , state ) )
64
+ paths . push ( ` ${ parseArrIndex ( path , state ) } ` )
60
65
const nextCh = path [ state . index ]
61
66
if ( nextCh !== ']' ) throwParsingError ( path , state . index )
62
67
state . index ++
@@ -81,7 +86,7 @@ const parseSinglePath = function (path, state) {
81
86
return { path : paths , options }
82
87
}
83
88
84
- const parseMultiPaths = function ( path , state ) {
89
+ const parseMultiPaths = ( path : string , state : IParserState ) => {
85
90
while ( WHITE_SPACE_CHAR_REGEXP . test ( path [ state . index ] ) ) {
86
91
state . index ++
87
92
}
@@ -104,11 +109,11 @@ const parseMultiPaths = function (path, state) {
104
109
return ret
105
110
}
106
111
107
- const parseEOF = function ( path , state ) {
112
+ const parseEOF = ( path : string , state : IParserState ) => {
108
113
if ( state . index < state . length ) throwParsingError ( path , state . index )
109
114
}
110
115
111
- export function parseMultiDataPaths ( path : string ) {
116
+ export const parseMultiDataPaths = ( path : string ) => {
112
117
const state = {
113
118
length : path . length ,
114
119
index : 0 ,
@@ -118,7 +123,7 @@ export function parseMultiDataPaths(path: string) {
118
123
return ret
119
124
}
120
125
121
- export const getDataOnPath = function ( data , path ) {
126
+ export const getDataOnPath = ( data : unknown , path : Array < string > ) => {
122
127
let ret = data
123
128
path . forEach ( ( s ) => {
124
129
if ( typeof ret !== 'object' || ret === null ) ret = undefined
0 commit comments