/
utils.js
167 lines (137 loc) · 3.74 KB
/
utils.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use babel'
import isObject from 'lodash/isObject'
import isArray from 'lodash/isArray'
import isUndefined from 'lodash/isUndefined'
import minimatch from 'minimatch'
export class ArrayTraverser {
constructor(array = [], index = -1) {
this.array = array
this.index = index
}
current() {
return this.array[this.index]
}
next() {
if (!this.hasNext()) {
throw new Error(`no next element at ${this.index + 1}`)
}
this.index += 1
return this.array[this.index]
}
peekNext(defaultValue) {
return this.hasNext() ? this.array[this.index + 1] : defaultValue
}
peekPrevious(defaultValue) {
return this.hasPrevious() ? this.array[this.index - 1] : defaultValue
}
previous() {
if (!this.hasPrevious()) {
throw new Error(`no previous element at ${this.index}`)
}
this.index -= 1
return this.array[this.index]
}
hasNext() {
return this.index + 1 < this.array.length
}
hasPrevious() {
return this.index - 1 >= 0 && this.array.length !== 0
}
}
export class PositionInfo {
constructor(segments = [],
keyPosition = false,
valuePosition = false,
previousToken = null,
editedToken = null,
nextToken = null
) {
this.segments = segments
this.keyPosition = keyPosition
this.valuePosition = valuePosition
this.previousToken = previousToken
this.editedToken = editedToken
this.nextToken = nextToken
}
setKeyPosition() {
return new PositionInfo(this.segments, true, false, this.previousToken, this.editedToken, this.nextToken)
}
setValuePosition() {
return new PositionInfo(this.segments, false, true, this.previousToken, this.editedToken, this.nextToken)
}
setPreviousToken(token) {
return new PositionInfo(this.segments, this.keyPosition, this.valuePosition, token, this.editedToken, this.nextToken)
}
setEditedToken(token) {
return new PositionInfo(this.segments, this.keyPosition, this.valuePosition, this.previousToken, token, this.nextToken)
}
setNextToken(token) {
return new PositionInfo(this.segments, this.keyPosition, this.valuePosition, this.previousToken, this.editedToken, token)
}
add(segment) {
return this.addAll([segment])
}
addAll(segments) {
return new PositionInfo(
this.segments.concat(segments),
this.keyPosition,
this.valuePosition,
this.previousToken,
this.editedToken,
this.nextToken
)
}
toObject() {
return {
segments: this.segments,
keyPosition: this.keyPosition,
valuePosition: this.valuePosition,
previousToken: this.previousToken,
editedToken: this.editedToken,
nextToken: this.nextToken
}
}
}
export class ValueHolder {
constructor(value) {
this.value = value
}
get() {
if (!this.hasValue()) {
throw new Error('value is not set')
}
return this.value
}
getOrElse(defaultValue) {
return this.hasValue() ? this.get() : defaultValue
}
set(value) {
this.value = value
}
hasValue() {
return !isUndefined(this.value)
}
}
export function resolveObject(segments, object) {
if (!isObject(object)) {
return null
}
if (segments.length === 0) {
return object
}
const [key, ...restOfSegments] = segments
return resolveObject(restOfSegments, object[key])
}
function doMatches(pattern, file) {
const path = pattern.indexOf('/') > -1 ? file.getRealPathSync() : file.getBaseName()
const search = process.platform === 'win32' ? pattern.replace(/\//g, '\\') : pattern
return minimatch(path, search)
}
export function matches(file, patterns) {
return isArray(patterns) ? patterns.some(pattern => doMatches(pattern, file)) : doMatches(patterns, file)
}
export const StorageType = {
FILE: 'FILE',
FOLDER: 'FOLDER',
BOTH: 'BOTH'
}