-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest.js
More file actions
82 lines (64 loc) · 1.74 KB
/
test.js
File metadata and controls
82 lines (64 loc) · 1.74 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
const toString = type => Object.prototype.toString.call(type)
const objectToString = obj => {
return Object.entries(obj).reduce((str, current) => {
const [key, value] = current
switch (toString(value)) {
case '[object Array]':
break
case '[object Object]':
break
default:
str.push([
key,
value
].join(': '))
break
}
return str
}, [])
}
console.log(toString({}))
const arrayToString = arr => {
if (arr.length === 0) return '[]'
return arr.reduce((total, current) => {
if (toString(current) === '[object Object]') {
objectToString(current)
}
return current
}, [])
}
// arrayToString([
// {
// a: 1
// }
// ])
const result = objectToString({
a: 1,
b: '3',
c: [],
d: [
{
a: 1
}
]
})
console.log(`{ \n${result.join('\n')}\n}`)
function getDates(format, date) {
const str = format.replace(/(\w)\1+|(\w)/g, ({ length }, w, s) => {
return `(?<${w || s}>${s ? '\\d+' : '\\d'.repeat(length)})`
})
const regex = new RegExp(str)
const match = date.match(regex);
// console.log(result)
const dateList = Object.entries(match.groups)
const result = dateList.reduce((str, current, index) => {
const [format, time] = current
str += format === 'D' ? `${time} ` : `${time}${index !== dateList.length - 1 ? ':' : ''}`
return str
}, '')
console.log(result)
}
getDates('DD hh:mm:ss', '23 01:23:11')
getDates('hh:mm:ss', '23 01:23:11')
getDates('mm:ss', '23 01:23:11')
getDates('ss', '23 01:23:11')