-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutils.mjs
More file actions
731 lines (650 loc) · 23.7 KB
/
utils.mjs
File metadata and controls
731 lines (650 loc) · 23.7 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
import moment from 'moment-timezone'
import stopNameModifier from './additional-data/stop-name-modifier.mjs'
import TimedCache from './TimedCache.mjs'
import EventEmitter from 'events'
import crypto from 'crypto'
import fs from 'fs'
import fsp from 'fs/promises'
import path from 'path'
import { spawn } from 'child_process'
import util from 'util'
import fetch from 'node-fetch'
moment.tz.setDefault('Australia/Melbourne')
const daysOfWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
const locks = {}, caches = {}
let forceLongRequest = true
let interval = setInterval(() => {
if (utils.uptime() >= 40000) {
forceLongRequest = false
clearInterval(interval)
// if (global.loggers) global.loggers.fetch.log('Stopping long request timeout')
// else console.log('Stopping long request timeout')
}
}, 3000)
interval.unref()
String.prototype.format = (function (i, safe, arg) {
function format () {
var str = this; var len = arguments.length + 1
for (i = 0; i < len; arg = arguments[i++]) {
safe = typeof arg === 'object' ? JSON.stringify(arg) : arg
str = str.replace(RegExp('\\{' + (i - 1) + '\\}', 'g'), safe)
}
return str
}
format.native = String.prototype.format
return format
}())
const utils = {
encodeName: name => name.toLowerCase().replace(/[^\w\d ]/g, '-').replace(/ */g, '-').replace(/--+/g, '-').replace(/-$/, '').replace(/^-/, ''),
adjustRouteName: routeName => {
return utils.titleCase(routeName.replace(/via .+/i, '')
.replace(/\(?(?:anti)? ?-? ?(?:clockwise)(?: loop)?\)?$/i, '')
.replace(/ \(SMARTBUS.+/gi, '')
.replace(/ to /i, ' - ')
.replace(/ +/g, ' ')
.replace(/(\w) *- *(\w)/g, '$1 - $2')
.replace(/(?:Railway)? Station/gi, '')
.replace(/ \((From|Until) .+\)$/i, '')
.trim()
)
},
adjustRawStopName: name => {
let directionParts
if (directionParts = name.match(/\/\((.*?)\) (.*?) \((.+)/)) {
name = name.replace(/\/.+/, '/')
let roadName = directionParts[2],
direction = directionParts[1],
remaining = directionParts[3]
direction = direction[0].toUpperCase() + direction.slice(1).toLowerCase()
name += `${roadName} - ${direction} (${remaining}`
} else if (directionParts = name.match(/^\((.*?)\) ([^/]+)\/(.+)/)) {
name = name.replace(/\/.+/, '/')
let roadName1 = directionParts[2],
remaining = directionParts[3],
direction = directionParts[1]
direction = direction[0].toUpperCase() + direction.slice(1).toLowerCase()
name = `${roadName1} - ${direction}/${remaining}`
} else if (directionParts = name.match(/\/(.*?) \((\w+)\) (.*?) \((.+)/)) {
// canberra st/lorwhatver (north) st (docklands)
name = name.replace(/\/.+/, '/')
let roadName1 = directionParts[1].trim(),
roadName2 = directionParts[3].trim(),
direction = directionParts[2].trim(),
remaining = directionParts[4].trim()
direction = direction[0].toUpperCase() + direction.slice(1).toLowerCase()
name += `${roadName1} ${roadName2} - ${direction} (${remaining}`
} else if (directionParts = name.match(/(.*?) \((\w+)(?: Side)?\) (.*?)\//)) {
//Albert (east) St/Stephensons Rd (Mount Waverley)
name = name.replace(/.*?\//, '/')
let roadName1 = directionParts[1].trim(),
roadName2 = directionParts[3].trim(),
direction = directionParts[2].trim()
direction = direction[0].toUpperCase() + direction.slice(1).toLowerCase()
name = `${roadName1} ${roadName2} - ${direction}` + name
} else if (directionParts = name.match(/\((\w+)\)\//)) {
// some st (south)/whatever rd (x)
direction = directionParts[1]
direction = direction[0].toUpperCase() + direction.slice(1).toLowerCase()
name = name.replace(/\(\w+\)\//, ` - ${direction}/`)
} else if (directionParts = name.match(/\((\w+)\) \(/)) {
// some st/whatever rd (south) (x)
direction = directionParts[1]
direction = direction[0].toUpperCase() + direction.slice(1).toLowerCase()
name = name.replace(/\((\w+)\) \(/, ` - ${direction} (`)
}
if (name.match(/\(([\w ]+) \((\d{4})\)\)$/)) {
// Suburbs with duplicate names: newton, hillside
name = name.replace(/\(([\w ]+) \((\d{4})\)\)$/, '($1: $2)')
}
if (name.match(/\(([\w ]+) \(\w+ - ([A-Z]{2,4})\)\)$/)) {
// Suburbs in other states (Glenroy (Albury - NSW))
name = name.replace(/\(([\w ]+) \(\w+ - ([A-Z]{2,4})\)\)$/, '($1, $2)')
}
if (name.match(/\(([\w ]+) \(([A-Z]{2,4})\)\)$/)) {
// Suburbs in other states (Albury (NSW))
name = name.replace(/\(([\w ]+) \(([A-Z]{2,4})\)\)$/, '($1, $2)')
}
return name.replace(/ +/g, ' ')
},
adjustStopName: name => {
if (name.includes('Jolimont-MCG')) {
name = name.replace('Jolimont-MCG', 'Jolimont')
}
if (name.includes('Jolimont Station-MCG')) {
name = name.replace('Jolimont Station-MCG', 'Jolimont Station')
}
if (name.includes(' Railway Station')) {
name = name.replace(' Railway Station', ' Station')
}
let expandStation = !(
name.includes('Police Station') || name.includes('Service Station')
|| name.includes('Fire Station') || name.includes('Petrol Station')
|| name.includes('Caltex Station') || name.match(/Station (St|Rd)/)
|| name.match(/CFA (Fire )?Station/) || name.match(/[\d]+\w? Station/)
)
let isBusStation = name.includes('Bus Station')
if (name.includes(' Station') && !isBusStation && (expandStation || name.match(/Station\/Station/))) {
name = name.replace(' Station', ' Railway Station')
}
name = utils.expandStopName(name)
return name
},
expandStopName: name => {
name = name.replace(/St(\/|$)/g, 'Street$1')
.replace(/St S(\b)/, 'Street South$1')
.replace(/St N(\b)/, 'Street North$1')
.replace(/(\w+) St(\b)/, '$1 Street$2')
.replace(/St -/g, 'Street -')
.replace(/Rd(\b)/g, 'Road$1')
.replace(/Mt\.?(\b)/g, 'Mount$1')
.replace(/Pde(\b)/g, 'Parade$1')
.replace(/Cl(\b)/g, 'Close$1')
.replace(/Dr(\b)/g, 'Drive$1')
.replace(/ Ave?(\b)/g, ' Avenue$1') // Cannot be at the start of a stop name, eg Ave Maria College
.replace(/Gr(\b)/g, 'Grove$1')
.replace(/Ct(\b)/g, 'Court$1')
.replace(/Cr(\b)/g, 'Crescent$1')
.replace(/Hwy(\b)/g, 'Highway$1')
.replace(/Fwy(\b)/g, 'Freeway$1')
.replace(/Tce(\b)/g, 'Terrace$1')
.replace(/Crst(\b)/g, 'Crescent$1')
.replace(/Pl(\b)/g, 'Place$1')
.replace(/Bl?vd(\b)/g, 'Boulevard$1')
.replace(/Cres(\b)/g, 'Crescent$1')
.replace(/Crse(\b)/g, 'Crescent$1')
.replace(/Ctr(\b)/g, 'Centre$1')
.replace(/Lt(\b)/g, 'Little$1')
.replace(/Lwr(\b)/g, 'Lower$1')
.replace(/Prom(\b)/g, 'Promenade$1')
.replace(/PS(\b)/g, 'Primary School$1')
.replace(/Esp(\b)/g, 'Esplanade$1')
.replace(/Cct(\b)/g, 'Circuit$1')
.replace(/Mount\./g, 'Mount')
.replace(/Sq(\b)/g, 'Square$1')
.replace(/Sth(\b)/g, 'South$1')
.replace(/Nth(\b)/g, 'North$1')
.replace(/Gdn(s?)(\b)/g, 'Garden$1$2')
.replace(/Cir(\b)/g, 'Circle$1')
.replace(/Con(\b)/g, 'Concourse$1')
.replace(/Ch(\b)/g, 'Chase$1')
.replace(/Gra(\b)/g, 'Grange$1')
.replace(/Grn(\b)/g, 'Green$1')
.replace(/Gtwy(\b)/g, 'Gateway$1')
.replace(/Uni(\b)/g, 'University$1')
.replace(/Plza(\b)/g, 'Plaza$1')
.replace(/Psge(\b)/g, 'Passage$1')
.replace(/Rdge(\b)/g, 'Ridge$1')
.replace(/Strp(\b)/g, 'Strip$1')
.replace(/Tafe(\b)/g, 'TAFE$1')
.replace(/Trk(\b)/g, 'Track$1')
.replace(/Vsta(\b)/g, 'Vista$1')
.replace(/Pkwy(\b)/g, 'Parkway$1')
.replace(/Devn(\b)/g, 'Deviation$1')
.replace(/Sec Col(\b)/g, 'Secondary College$1')
.replace(/Sec College(\b)/g, 'Secondary College$1')
.replace(/Rec Res(\b)/g, 'Rec Reserve$1')
.replace(/SC Senior Campus(\b)/g, 'Secondary College Senior Campus$1')
.replace(/([\w ]*?) ?- ?([\w ]*?) Road/g, '$1-$2 Road')
.replace(/St(\b)/, 'St.$1')
.replace('St..', 'St. ')
.replace('Ret Village', 'Retirement Village')
.replace(/ SC(\b)/, ' Shopping Centre$1')
.replace(/ RS(\b)/, ' Railway Station$1')
.replace(/Cresent/g, 'Crescent')
return name.replace(/ +/g, ' ')
},
shorternStopName: name => {
name = name.replace('Railway Station', 'RS')
.replace('Shopping Centre', 'SC')
.replace('University', 'Uni')
.replace('Road', 'Rd')
.replace('Street', 'St')
.replace('Ferntree', 'FT')
.replace('South', 'Sth')
.replace('North', 'Nth')
.replace('Gardens', 'Gdns')
.replace(/\/.+/, '')
if (name === 'Monash Uni Bus Loop')
name = 'Monash Uni'
return name
},
pad: (data, length, filler='0') => Array(length).fill(filler).concat([...data.toString()]).slice(-length).join(''),
allDaysBetweenDates: (startDate, endDate) => {
startDate = startDate.clone().startOf('day').add(-1, 'days')
endDate = endDate.startOf('day')
let dates = []
while(startDate.add(1, 'days').diff(endDate) <= 0) {
dates.push(startDate.clone())
}
return dates
},
adjustPTHHMM: time => {
const parts = time.slice(0, 5).split(':')
const [ hours, minutes ] = parts
if (hours < 3) return `${parseInt(hours) + 24}:${minutes}`
return time
},
getMomentFromMinutesPastMidnight: (minutes, day) => {
return day.clone().startOf('day').set('hours', Math.floor(minutes / 60)).set('minutes', minutes % 60)
},
getMinutesPastMidnightFromHHMM: time => {
if (!time) return null
const parts = time.slice(0, 5).split(':')
return parts[0] * 60 + parts[1] * 1
},
getHHMMFromMinutesPastMidnight: time => {
let hours = Math.floor(time / 60)
let minutes = time % 60
let mainTime = ''
hours %= 24
if (hours < 10) mainTime += '0'
mainTime += hours
mainTime += ':'
if (minutes < 10) mainTime += '0'
mainTime += minutes
return mainTime
},
getPTHHMMFromMinutesPastMidnight: time => {
let hours = Math.floor(time / 60)
let minutes = time % 60
let mainTime = ''
if (hours < 10) mainTime += '0'
mainTime += hours
mainTime += ':'
if (minutes < 10) mainTime += '0'
mainTime += minutes
return mainTime
},
getMinutesPastMidnightNow: () => {
return utils.getMinutesPastMidnight(utils.now())
},
getPTMinutesPastMidnight: time => {
let minutesPastMidnight = utils.getMinutesPastMidnight(time)
let offset = 0
if (minutesPastMidnight < 180) offset = 1440
return minutesPastMidnight + offset
},
getMinutesPastMidnight: time => {
return time.get('hours') * 60 + time.get('minutes')
},
getPTDayName: time => {
let minutesPastMidnight = utils.getMinutesPastMidnight(time)
let offset = 0
if (minutesPastMidnight < 180) offset = 1440
return daysOfWeek[time.clone().subtract(offset, 'minutes').day()]
},
getDayOfWeek: time => {
return daysOfWeek[time.day()]
},
isWeekday: dayOfWeek => {
return ['Mon', 'Tues', 'Wed', 'Thur', 'Fri'].includes(dayOfWeek)
},
isNightNetworkDay: dayOfWeek => {
return ['Fri', 'Sat'].includes(dayOfWeek)
},
formatHHMM: time => { // TODO: Rename getHHMM
return time.format('HH:mm')
},
formatPTHHMM: time => { // TODO: Rename getHHMM
let hours = time.get('hours')
let hour = time.format('HH')
if (hours < 3) {
let startOfPTDay = time.clone().startOf('day').add(-1, 'day')
hour = time.diff(startOfPTDay, 'hours')
return `${hour}:${time.format('mm')}`
}
return time.format('HH:mm')
},
formatPTHHMMForOpDay: (time, operationDay) => {
if (time.clone().startOf('day') - operationDay.clone().startOf('day') === 0) return time.format('HH:mm')
const hour = time.diff(operationDay, 'hours')
return `${hour}:${time.format('mm')}`
},
getPTYYYYMMDD: time => {
let cloned = time.clone()
if (cloned.get('hours') < 3) // 3am PT day :((((
cloned.add(-1, 'days')
return cloned.format('YYYYMMDD')
},
getYYYYMMDD: time => {
return time.format('YYYYMMDD')
},
getYYYYMMDDNow: () => utils.getYYYYMMDD(utils.now()),
getHumanDateShort: time => {
return time.format('DD/MM')
},
getHumanDate: time => {
return time.format('DD/MM/YYYY')
},
now: () => moment.tz('Australia/Melbourne'),
parseTime: (time, format) => {
if (format)
return moment.tz(time, format, 'Australia/Melbourne')
else
return moment.tz(time, 'Australia/Melbourne')
},
request: async (url, options={}) => {
let start = +new Date()
let body
let error
let maxRetries = (options ? options.maxRetries : null) || 3
let fullOptions = {
timeout: 2000,
compress: true,
highWaterMark: 1024 * 1024,
...options
}
if (forceLongRequest) fullOptions.timeout = Math.max(fullOptions.timeout, 6000)
for (let i = 0; i < maxRetries; i++) {
try {
body = await fetch(url, fullOptions)
break
} catch (e) {
error = e
}
}
let diff = (+new Date()) - start
let logMessage = `${diff}ms ${url}`
if (!body && error) {
if (error.message && error.message.toLowerCase().includes('network timeout')) {
let totalTime = fullOptions.timeout * maxRetries
logMessage = `${totalTime}ms ${url}`
error.timeoutDuration = totalTime
}
if (global.loggers) global.loggers.fetch.log(logMessage)
else console.log(logMessage)
throw error
}
if (body && body.status.toString()[0] !== '2') {
let err = new Error('Bad Request Status')
err.status = body.status
err.response = await (options.raw ? body.buffer() : body.text())
if (global.loggers) global.loggers.fetch.log(logMessage)
else console.log(logMessage)
throw err
}
let size = body.headers.get('content-length')
if (options.stream) {
if (global.loggers) global.loggers.fetch.log(logMessage)
else console.log(logMessage)
return body.body
}
let returnData = await (options.raw ? body.buffer() : body.text())
if (!size) size = returnData.length
logMessage = `${diff}ms ${url} ${size}R`
if (global.loggers) global.loggers.fetch.log(logMessage)
else console.log(logMessage)
return returnData
},
isStreet: shortName => {
return (shortName.endsWith('Street') || shortName.endsWith('Road')
|| shortName.endsWith('Parade') || shortName.endsWith('Close')
|| shortName.endsWith('Drive') || shortName.endsWith('Avenue')
|| shortName.endsWith('Grove') || shortName.endsWith('Court')
|| shortName.endsWith('Highway') || shortName.endsWith('Terrace')
|| shortName.endsWith('Way') || shortName.endsWith('Crescent')
|| shortName.endsWith('Place') || shortName.endsWith('Boulevard')
|| shortName.endsWith('Crescent') || shortName.endsWith('Freeway'))
|| shortName.endsWith('Lane')
},
isCheckpointStop: stopName => stopName.includes('University')
|| stopName.includes('Railway Station')
|| stopName.includes('Bus Station')
|| stopName.includes('SC') || stopName.includes('Shopping Centre'),
getDistanceFromLatLon: (lat1, lon1, lat2, lon2) => {
var R = 6371 // Radius of the earth in km
var dLat = utils.deg2rad(lat2-lat1)
var dLon = utils.deg2rad(lon2-lon1)
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(utils.deg2rad(lat1)) * Math.cos(utils.deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
var d = R * c // Distance in km
return Math.floor(d * 1000) // distance in m
},
deg2rad: deg => {
return deg * (Math.PI/180)
},
titleCase: (str, anyLength=false) => str.replace(/\w\S*/g, txt => {
if (txt.length > 2 || anyLength)
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
return txt
}).replace(/\b\w/g, txt => {
let punctuation = (txt.match(/(\b)/)||[,''])[1]
let text = txt.slice(punctuation.length)
if (text.length > 2 || anyLength)
return punctuation + text.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
return txt
}),
uptime: () => process.uptime() * 1000,
getStopName: stopName => {
let parts = stopName.split('/')
if (parts.length > 1)
return parts.slice(0, -1).join('/')
return stopName
},
parseDate: date => {
if (date instanceof Date) return utils.parseTime(date)
if (date.match(/^\d{1,2}\/\d{1,2}\/\d{1,4}$/)) return utils.parseTime(date, 'DD/MM/YYYY')
else return utils.parseTime(date, 'YYYYMMDD')
},
prettyTime: (time, showHours, blankOld) => {
time = utils.parseTime(time)
let timeDifference = moment.utc(time.diff(utils.now()))
if (blankOld && +timeDifference <= -60000 * 2) return ''
if (+timeDifference <= 60000) return 'Now'
if (+timeDifference > 1440 * 60 * 1000) return utils.getHumanDateShort(time)
let hours = timeDifference.get('hours')
let minutes = timeDifference.get('minutes')
let prettyTime = ''
let minutesToDeparture = hours * 60 + minutes
if (!showHours && minutesToDeparture > 180) {
prettyTime += hours + ' h'
} else if (showHours) {
if (hours) prettyTime += hours + ' h '
if (minutes) prettyTime += minutes + ' min'
} else {
prettyTime = minutesToDeparture + ' m'
}
return prettyTime.trim()
},
findHeadwayDeviance: (scheduledDepartureTime, estimatedDepartureTime, thresholds, isCancelled = false) => {
if (!estimatedDepartureTime || isCancelled) return 'unknown'
let headwayDeviance = scheduledDepartureTime.diff(estimatedDepartureTime, 'seconds') / 60
if (headwayDeviance > thresholds.early) {
return 'early'
} else if (headwayDeviance <= -thresholds.late) {
return 'late'
} else {
return 'on-time'
}
},
getProperStopName: ptvStopName => {
return utils.adjustRawStopName(stopNameModifier(utils.adjustStopName(ptvStopName.trim().replace(/ #.+$/, '').replace(/^(D?[\d]+[A-Za-z]?)-/, ''))))
},
getShortName: stopName => stopName.replace('Shopping Centre', 'SC').replace('Railway Station', 'Station'),
getDestinationName: stopName => {
let shortName = utils.getShortName(stopName)
let primaryStopName = utils.getStopName(shortName)
if (utils.isStreet(primaryStopName)) return shortName
else return primaryStopName
},
getRunID: ptvRunID => {
let parts
if (parts = ptvRunID.toString().match(/^9(\d\d)(\d\d\d)$/)) {
return `${String.fromCharCode(parseInt(parts[1]))}${parts[2]}`
}
},
getPTVRunID: runID => {
if (utils.isValidRunID(runID)) {
return `9${runID.charCodeAt(0)}${runID.slice(1)}`
} else return null
},
isValidRunID: runID => {
return !!runID.match(/^\w\d{3}$/)
},
sleep: time => {
return new Promise(resolve => {
setTimeout(resolve, time)
})
},
findSubstrings: (str, size=0) => {
if (str.length < size) return [str]
let result = []
for (let i = 0; i < str.length; i++) {
for (let j = str.length; j - i >= size; j--) {
result.push(str.slice(i, j))
}
}
return result
},
tokeniseAndSubstring: text => {
let words = text.split(/[^\w]/)
return words.map(w => utils.findSubstrings(w, 4)).reduce((a, e) => a.concat(e), [])
},
shuffle: a => {
let j, x, i
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1))
x = a[i]
a[i] = a[j]
a[j] = x
}
return a
},
getData: async (lock, key, noMatch, ttl=1000 * 60) => {
if (!locks[lock]) locks[lock] = {}
if (!caches[lock]) caches[lock] = new TimedCache(ttl)
if (locks[lock][key]) {
return await new Promise((resolve, reject) => {
locks[lock][key].on('loaded', resolve)
locks[lock][key].on('fail', e => {
reject(e)
})
})
}
if (caches[lock].get(key)) {
return caches[lock].get(key)
}
locks[lock][key] = new EventEmitter()
locks[lock][key].setMaxListeners(1000)
let data
try {
data = await noMatch()
} catch (e) {
if (global.loggers) global.loggers.general.err('Getting data for', lock, 'for key', key, 'failed', e)
else console.error('Getting data for', lock, 'for key', key, 'failed', e)
locks[lock][key].emit('fail', e)
delete locks[lock][key]
throw e
}
caches[lock].put(key, data)
locks[lock][key].emit('loaded', data)
delete locks[lock][key]
if (ttl !== caches[lock].getTTL()) caches[lock].setTTL(ttl)
return data
},
hash: data => {
return crypto.createHash('md5').update(data).digest('hex')
},
tripsEqual: (tripA, tripB) => {
return tripA.routeGTFSID === tripB.routeGTFSID
&& tripA.origin === tripB.origin
&& tripA.destination === tripB.destination
&& tripA.departureTime === tripB.departureTime
&& tripA.destinationArrivalTime === tripB.destinationArrivalTime
},
spawnProcess: async (path, args, finish) => {
return await new Promise(resolve => {
let childProcess = spawn(path, args)
childProcess.stdout.on('data', data => {
process.stdout.write(data.toString())
})
childProcess.stderr.on('data', data => {
process.stderr.write(data.toString())
})
childProcess.on('close', code => {
resolve()
})
})
},
walkDir: dir => {
return new Promise((resolve, reject) => {
let results = []
let dirs = []
fs.readdir(dir, async function(err, list) {
if (err) return reject(err)
let i = 0
function next() {
let file = list[i++]
if (!file) {
results = results.concat(dirs)
return resolve(results)
}
file = path.resolve(dir, file)
fs.stat(file, async function(err, stat) {
if (stat && stat.isDirectory()) {
dirs.push({file: false, path: file})
results = results.concat(await utils.walkDir(file))
next()
} else {
results.push({file: true, path: file})
next()
}
})
}
next()
})
})
},
rmDir: async dir => {
let allFiles = await utils.walkDir(dir)
for (let file of allFiles) {
try {
if (file.file) await new Promise(resolve => fs.unlink(file.path, resolve))
else await new Promise(resolve => fs.rmdir(file.path, resolve))
} catch (err) {}
}
await new Promise(resolve => fs.rmdir(dir, resolve))
},
findLastIndex: (arr, func) => {
return arr.reduce((prev, curr, index) => func(curr) ? index : prev, -1);
},
inspect: e => console.log(util.inspect(e, { depth: null, colors: true, maxArrayLength: null })),
chunkText: (text, maxChunkSize) => {
if (text.length <= maxChunkSize) return [[ text ]]
let chunks = []
let lines = text.split('\n')
let currentChunk = []
let currentLength = 0
for (let i = 0; i < lines.length; i ++) {
let currentLine = lines[i]
if (currentLength + currentLine.length <= maxChunkSize) {
currentChunk.push(currentLine)
currentLength += currentLine.length + 1
} else {
chunks.push(currentChunk)
currentChunk = [currentLine]
currentLength = currentLine.length
}
}
if (currentChunk) chunks.push(currentChunk)
return chunks
},
setEnv: async () => {
try {
const env = (await fsp.readFile(path.join(import.meta.dirname, '.env'))).toString()
const nodeEnv = env.match(/NODE_ENV=(.+)/)
if (nodeEnv) process.env.NODE_ENV = nodeEnv[1].trim()
} catch (e) {
}
},
getPrettyStopName: (stopName, destinations, { routeNumber, routeGTFSID } = {}) => {
const keyStopName = utils.getDestinationName(stopName)
const primaryStopName = utils.getShortName(utils.getStopName(stopName))
const serviceData = destinations.service[routeGTFSID] || destinations.service[routeNumber] || {}
return (serviceData[keyStopName] || destinations.generic[keyStopName] || primaryStopName)
}
}
export default utils