-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrndneeds.js
65 lines (51 loc) · 2.13 KB
/
rndneeds.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
// returns a sequence of integers, misses some if density < 100
const integers = (start, length, density=100) => {
for (var arr = [], num = start; arr.length < length; num++) {
if (rnd(density,'%')) arr.push(num) }
return arr
}
// without next function rnd function will fail to make (some) arrays of values
// makes an array by using the given function,
const makeArr = (length, func, distinct, persist) => {
if (distinct) {
const max = persist? Infinity : 100000
for (var set = new Set(), i = 0; set.size < length && i < max; i++) {
const next = func()
if (next != undefined) i = (set.size < set.add(next).size)? 0 : i
}
return Array.from(set)
}
return Array(length).fill(0).map( () => {
let item = func()
while (item == undefined) item = func()
return item
} )
}
// without next three functions rnd function will fail to work on dates
// adds the corresponding letters to the number (useful for dates etc.)
const nth = (num) => {
num = +num
if (num > 3 && num < 21) return num + 'th'
switch (num % 10) {
case 1 : return num + 'st'
case 2 : return num + 'nd'
case 3 : return num + 'rd'
default: return num + 'th'
}
}
// takes a Date object, returns a string of format "2019-05-13 18:37:59"
const standartDatetime = (date) =>
new Date( date.getTime() - date.getTimezoneOffset()*60000 )
.toISOString().replace('T', ' ').slice(0, -5)
// formats datetime string or Date object by preset like "YYYY, DD Month"
const formatDatetime = (datetime, format) => {
if (datetime instanceof Date) datetime = standartDatetime(datetime)
const [YYYY, MM, DD, HH, mm, ss] = datetime.split(/[- :]/)
return format.replace('YYYY', YYYY).replace('YY', YYYY.slice(2))
.replace('Month', ["January","February","March","April","May","June","July",
"August","September","October","November","December"][MM-1])
.replace('month', ["Jan","Feb","Mar","Apr","May","Jun","Jul", "Aug","Sep",
"Oct","Nov","Dec"][MM-1])
.replace('MM', MM).replace('DDth', nth(DD)).replace('DD', DD)
.replace('HH', HH).replace('mm', mm).replace('ss', ss)
}