-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
356 lines (326 loc) · 9.88 KB
/
util.go
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package util
import (
"encoding/hex"
"github.com/api-go/plugin"
"github.com/ssgo/u"
"gopkg.in/yaml.v3"
)
func init() {
plugin.Register(plugin.Plugin{
Id: "util",
Name: "基础工具",
JsCode: `
util.formatDate = function (date, fmt, timezone) {
let ret
if (!date) return ''
if (!fmt) fmt = 'YYYY-mm-dd HH:MM:SS'
const opt = {
'YYYY': date.getFullYear().toString(),
'YY': date.getFullYear().toString().substring(2),
'm+': (date.getMonth() + 1).toString(),
'd+': date.getDate().toString(),
'H+': date.getHours().toString(),
'h+': (date.getHours() % 12).toString(),
'APM': (date.getHours() > 12 ? 'PM' : 'AM'),
'M+': date.getMinutes().toString(),
'S+': date.getSeconds().toString(),
'YW': 'W' + Math.ceil(((date.getTime() - new Date(date.getFullYear(), 0, 0))) / (24 * 60 * 60 * 1000) / 7),
}
for (let k in opt) {
ret = new RegExp('(' + k + ')').exec(fmt)
if (ret) {
fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')))
}
}
return fmt
}
util.parseDate = function (str) {
if (typeof str === 'number') str = str + ''
if (/^\d+[\\.]?\d*$/.test(str)) {
if (str.length === 10) str += '000'
return new Date(util.int(str))
}
if (str.length === 19) {
return new Date(parseInt(str.substr(0, 4)), parseInt(str.substr(5, 2)) - 1, parseInt(str.substr(8, 2)), parseInt(str.substr(11, 2)), parseInt(str.substr(14, 2)), parseInt(str.substr(17, 2)))
} else if (str.length === 10) {
return new Date(parseInt(str.substr(0, 4)), parseInt(str.substr(5, 2)) - 1, parseInt(str.substr(8, 2)))
} else if (str.length === 7) {
return new Date(parseInt(str.substr(0, 4)), parseInt(str.substr(5, 2)) - 1, 1)
} else if (str.length === 8) {
let date = new Date()
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), parseInt(str.substr(0, 2)), parseInt(str.substr(3, 2)), parseInt(str.substr(6, 2)))
} else if (str.length === 5) {
let date = new Date()
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), parseInt(str.substr(0, 2)), parseInt(str.substr(3, 2)), 0)
}
return new Date(str)
}
util.int = function (v) {
if (!v) return 0
if (typeof v === 'number' || v instanceof Number) return v
if (typeof v === 'object') v = v.toString ? v.toString() : ''
if (typeof v !== 'string') return 0
try {
return Math.round(parseFloat(v.replace(/,/g, '').trim())) || 0
} catch (e) {
return 0
}
}
util.float = function (v) {
if (!v) return 0.0
if (typeof v === 'number' || v instanceof Number) return v
if (typeof v === 'object') v = v.toString ? v.toString() : ''
if (typeof v !== 'string') return 0.0
try {
return parseFloat(v.replace(/,/g, '').trim()) || 0.0
} catch (e) {
return 0.0
}
}
util.str = function (v) {
if (!v) return ''
if (typeof v === 'string') return v
if (v.toString) return v.toString()
return ''
}
util.keysBy = function (obj, ...fieldAndValues) {
let keys = []
for (let k in obj) {
let match = true
if (fieldAndValues.length === 1) {
// 查找一位数组
if (obj[k] != fieldAndValues[0]) {
match = false
}
} else {
// 查找二维数组
for (let i = 0; i < fieldAndValues.length; i += 2) {
if (obj[k][fieldAndValues[i]] != fieldAndValues[i + 1]) {
match = false
break
}
}
}
if (match) {
keys.push(k)
}
}
return keys
}
util.listBy = function (obj, ...fieldAndValues) {
let list = obj instanceof Array || obj instanceof NodeList ? [] : {}
let keys = util.keysBy(obj, ...fieldAndValues)
for (let k of keys) {
if (obj instanceof Array || obj instanceof NodeList) {
list.push(obj[k])
} else {
list[k] = obj[k]
}
}
return list
}
util.hasBy = function (obj, ...fieldAndValues) {
let keys = util.keysBy(obj, ...fieldAndValues)
return keys.length > 0
}
util.getBy = function (obj, ...fieldAndValues) {
let keys = util.keysBy(obj, ...fieldAndValues)
if (keys.length > 0) return obj[keys[0]]
return null
}
util.setBy = function (obj, value, ...fieldAndValues) {
let keys = util.keysBy(obj, ...fieldAndValues)
if (keys.length > 0) obj[keys[0]] = value
}
util.indexBy = function (obj, ...fieldAndValues) {
let keys = util.keysBy(obj, ...fieldAndValues)
if (keys.length > 0) {
return obj instanceof Array || obj instanceof NodeList ? util.int(keys[0]) : keys[0]
}
return -1
}
util.removeBy = function (obj, ...fieldAndValues) {
let keys = util.keysBy(obj, ...fieldAndValues)
let n = 0
for (let i = keys.length - 1; i >= 0; i--) {
let k = keys[i]
if (obj instanceof Array || obj instanceof NodeList) {
obj.splice(k, 1)
} else {
delete obj[k]
}
n++
}
return n
}
util.removeArrayItem = function (list, item) {
let pos = list.indexOf(item)
if (pos !== -1) list.splice(pos, 1)
}
util.last = function (arr) {
if (arr && arr.length) {
return arr[arr.length - 1]
}
return null
}
util.len = function (obj) {
if (obj instanceof Array || obj instanceof NodeList) {
return obj.length
} else {
let n = 0
for (let k in obj) n++
return n
}
}
util.mergeBy = function (olds, news, ...fields) {
if (!olds) return news
for (let newItem of news) {
let fieldAndValues = []
for (let field of fields) {
fieldAndValues.push(field, newItem[field])
}
let oldIndex = util.indexBy(olds, ...fieldAndValues)
if (oldIndex === -1) {
olds.push(newItem)
} else {
olds[oldIndex] = newItem
}
}
return olds
}
util.sortBy = function (obj, field, isReverse = false, sortType) {
let list = obj instanceof Array || obj instanceof NodeList ? [] : {}
let sortedKeys = {}
let sortArr = []
for (let k in obj) {
let v = ''
if (field instanceof Array) {
for (let f of field) v += obj[k][f]
} else {
v = obj[k][field]
}
if (!sortedKeys[v]) {
sortedKeys[v] = true
sortArr.push(v)
}
}
sortArr.sort((a, b) => {
if(sortType === 'int'){
a = util.int(a)
b = util.int(b)
} else if(sortType === 'float'){
a = util.float(a)
b = util.float(b)
}
if (a == b) return 0
if (typeof a === 'number' && typeof b === 'number') {
return isReverse ? b - a : a - b
} else {
return (isReverse ? a < b : a > b) ? 1 : -1
}
})
for (let sortKey of sortArr) {
for (let k in obj) {
let v = ''
if (field instanceof Array) {
for (let f of field) v += obj[k][f]
} else {
v = obj[k][field]
}
if (obj instanceof Array || obj instanceof NodeList) {
if (v == sortKey) list.push(obj[k])
} else {
if (v == sortKey) list[k] = obj[k]
}
}
}
return list
}
util.in = function (v1, v2) {
if (!(v1 instanceof Array)) v1 = util.split(v1, /,\s*/)
return v1.indexOf(String(v2)) !== -1
}
util.uniquePush = function (arr, ...values) {
for (let v of values) {
if (arr.indexOf(v) === -1) arr.push(v)
}
}
util.clearEmpty = function (arr) {
let a = []
for (let v of arr) if (v) a.push(v)
return a
}
util.split = function (v1, v2) {
return util.clearEmpty(util.str(v1).split(v2))
}
util.join = function (arr, separator) {
return util.clearEmpty(arr).join(separator)
}
util.copy = function (obj, isDeepCopy) {
let newObj
if (obj instanceof Array || obj instanceof NodeList) {
newObj = []
for (let o of obj) {
if (isDeepCopy && typeof o === 'object' && o) o = util.copy(o)
newObj.push(o)
}
} else {
newObj = {}
for (let k in obj) {
let v = obj[k]
if (isDeepCopy && typeof v === 'object' && v) v = util.copy(v)
newObj[k] = v
}
}
return newObj
}
`,
Objects: map[string]interface{}{
// makeToken 生成指定长度的随机二进制数组
// makeToken size token长度
// makeToken return Hex编码的字符串
"makeToken": func(size int) string {
return hex.EncodeToString(u.MakeToken(size))
},
// makeTokenBytes 生成指定长度的随机二进制数组
// makeTokenBytes size token长度
// makeTokenBytes return 二进制数据
"makeTokenBytes": u.MakeToken,
// makeId 生成指定长度的随机ID
// makeId size ID长度(6~20)
// makeId return 二进制数据
"makeId": func(size int) string {
if size > 20 {
return u.UniqueId()
} else if size > 14 {
return u.UniqueId()[0:size]
} else if size > 12 {
return u.ShortUniqueId()[0:size]
} else if size > 10 {
return u.Id12()[0:size]
} else if size > 8 {
return u.Id10()[0:size]
} else if size >= 6 {
return u.Id8()[0:size]
} else {
return u.Id6()
}
},
// encodeYaml 将对象转换为YAML格式
// encodeYaml content 对象
// encodeYaml return YAML字符串
"encodeYaml": func(content interface{}) (string, error) {
buf, err := yaml.Marshal(content)
return string(buf), err
},
// decodeYaml 将YAML转化为对象
// decodeYaml content YAML字符串
// decodeYaml return 对象
"decodeYaml": func(content string) (interface{}, error) {
var data interface{}
err := yaml.Unmarshal([]byte(content), &data)
return data, err
},
},
})
}