-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.js
81 lines (69 loc) · 2.29 KB
/
examples.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
/* Extracts substrings from any structurized string using a mask */
const getSubstringsByMask = require('./get-substrings-by-mask');
// Date: yyyy-MM-dd
console.log(
getSubstringsByMask('{year}-{month}-{day}', '2003-04-20')
);
// { year: '2003', month: '04', day: '20' }
// Date: yyyy-MM-dd
console.log(
getSubstringsByMask('{century|2}{year}-{month}-{day}', '2003-04-20')
);
// { century: '20', year: '03', month: '04', day: '20' }
// Date: dd/MM/yyyy
console.log(
getSubstringsByMask('{day}/{month}/{year}', '20/04/2003')
);
// { day: '20', month: '04', year: '2003' }
// Date: ddMMyyyy
console.log(
getSubstringsByMask('{day|2}{month|2}{year}', '20042003')
);
// { day: '20', month: '04', year: '2003' }
// Date: MM/dd/yyyy
console.log(
getSubstringsByMask('{month}/{day}/{year}', '04/20/2003')
);
// { month: '04', day: '20', year: '2003' }
// Date: ISO format
console.log(
getSubstringsByMask(
'{year}-{month}-{day}T{hour}:{minute}:{second|2}{timezone}',
'2019-05-24T09:36:28+00:00'
)
);
// { year: '2019', month: '05', day: '24', hour: '09', minute: '36', second: '28', timezone: '+00:00' }
// Date: GMT string
console.log(
getSubstringsByMask(
'{weekday}, {day} {month} {year} {hour}:{minute}:{second} {timezone}',
'Fri, 24 May 2019 10:39:51 GMT'
)
);
// { weekday: 'Fri', day: '24', month: 'May', year: '2019', hour: '10', minute: '39', second: '51', timezone: 'GMT' }
// Date: dd-MMM-yyyy hh:mm
console.log(
getSubstringsByMask('{day}-{month}-{year} {hour}:{minute}', '20-JUN-1990 08:03')
);
// { day: '20', month: 'JUN', year: '1990', hour: '08', minute: '03' }
// URL
console.log(
getSubstringsByMask(
'https://{blog}.blogplatform.com/article/{article}/comment/{comment}?{rest}',
'https://aboutfashion.blogplatform.com/article/120/comment/150?mode=edit'
)
);
// { blog: 'aboutfashion', article: '120', comment: '150', rest: 'mode=edit' }
// IP address
console.log(
getSubstringsByMask('{byte1}.{byte2}.{byte3}.{byte4}', '217.20.164.1')
);
// { byte1: '217', byte2: '20', byte3: '164', byte4: '1' }
// latitude & longitude
console.log(
getSubstringsByMask(
`{deg1}°{min1}'{sec1}"{dim1} {deg2}°{min2}'{sec2}"{dim2}`,
`50°26'45.8"N 30°32'44.7"E`
)
);
// { deg1: '50', min1: '26', sec1: '45.8', dim1: 'N', deg2: '30', min2: '32', sec2: '44.7', dim2: 'E' }