-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformating.js
1463 lines (1250 loc) · 61.5 KB
/
formating.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
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**Import Utility.js files to gain access to helper functions defined in it */
var ut = require('./utility');
var constants = require('./constants');
var __ = require('underscore');
var _ = require('lodash');
var api = require('./api');
var cfg = require('./config');
/** Processes and Extracts Address Data from Google Geocode API Results
* @param {object} dataset - JSON Dataset returned from API call
* @param {string} unknown_text - default text to set for null or undefined Properties
*/
function formatLocationData(dataset, unknown_text){
/** Final object to return */
var outPutDataSet = {};
var resultSet = dataset[0];
var addressComponent = resultSet[constants.API.PROPERTIES.GEOCODE.ADDRESS_COMPONENT];
/** Extract Various Section from API results */
var locationSet = resultSet[constants.API.PROPERTIES.GEOCODE.GEOMETRY][constants.API.PROPERTIES.LOCATION] || null;
var countrySet = ut.queryGeoCodeJson(addressComponent, constants.API.PROPERTIES.GEOCODE.COUNTRY) || null;
var stateSet = ut.queryGeoCodeJson(addressComponent, constants.API.PROPERTIES.GEOCODE.STATE) || null;
var citySet = ut.queryGeoCodeJson(addressComponent, constants.API.PROPERTIES.GEOCODE.CITY) || null;
var subCitySet = ut.queryGeoCodeJson(addressComponent, constants.API.PROPERTIES.GEOCODE.SUB_CITY) || null;
var postalCodeSet = ut.queryGeoCodeJson(addressComponent, constants.API.PROPERTIES.GEOCODE.POSTAL_CODE) || null;
var hasCityOrSubCity = !__.isNull(citySet) || !__.isNull(subCitySet);
/** Create Properties for user location */
var longName = constants.API.PROPERTIES.GEOCODE.LONG_NAME;
outPutDataSet.country = ut.hasKey(countrySet, longName) ? countrySet[longName] : unknown_text;
outPutDataSet.state = ut.hasKey(stateSet, longName) ? stateSet[longName] : unknown_text;
outPutDataSet.location = locationSet;
if(hasCityOrSubCity)
{
var city = ut.hasKey(citySet, longName) ? citySet[longName] : null;
var subcity = ut.hasKey(subCitySet, longName) ? subCitySet[longName] : null;
outPutDataSet.city = city || subcity || unknown_text;
}
else
{
outPutDataSet.city = null;
}
/** Return Geocode Data */
return outPutDataSet;
}
/** Processes and Extracts Zipcode Data from Google Geocode API Results
* @param {object} dataset - JSON Dataset returned from API call
* @param {string} unknown_text - default text to set for null or undefined Properties
*/
function formatZipCodeData(dataset, unknown_text){
/** Final object to return */
var ds = {};
var resultSet = dataset[0];
var addressComponent = resultSet[constants.API.PROPERTIES.GEOCODE.ADDRESS_COMPONENT];
var postalCodeProp = constants.API.PROPERTIES.GEOCODE.POSTAL_CODE;
var longNameProp = constants.API.PROPERTIES.GEOCODE.LONG_NAME;
/** Extract Postal Code */
var postalCodeSet = ut.queryGeoCodeJson(addressComponent, postalCodeProp || null);
ds.zipecode = ut.hasKey(postalCodeSet, longNameProp) ? postalCodeSet[longNameProp] : unknown_text;
return ds;
}
/** Processes and Extracts Time Zone Data from Google Geocode API Results
* @param {object} dataset - JSON Dataset returned from API call
* @param {string} unknown_text - default text to set for null or undefined Properties
*/
function formatTimezoneData(dataset, unknown_text){
var property = constants.API.PROPERTIES.TIMEZONE.TIMEZONE_ID;
return dataset[property] || unknown_text;
}
/** Adds distance property to API results, by calculating
* the geodistance between the uses lat/lng and Stop lat/lng. then
* sorts stops in ascending order (closest stops first)
* @param {object} dataset - API results
* @param {object} userCordinates - user cordinates {lat:xx, lng:xx}
*/
function addDistanceSortTrimet(dataset, userCordinates){
if(__.isUndefined(dataset) || __.isNull(dataset) || __.isEmpty(dataset)) return null;
var distanceAdded = __.each(dataset, function(value, key, list){
var context = list[key];
var stopCords = ut.createGeoCordinates(context.lat, context.lng);
context.distance = _.round(ut.calculateGeoDistance(userCordinates, stopCords),2);
});
return _.sortBy(distanceAdded, ['distance']);
}
/** Adds distance property to API results, by calculating
* the geodistance between the uses lat/lng and Stop lat/lng. then
* sorts stops in ascending order (closest stops first)
* @param {object} dataset - API results
* @param {object} userCordinates - user cordinates {lat:xx, lng:xx}
*/
function addDistanceSortOBA(dataset, userCordinates){
if(__.isUndefined(dataset) || __.isNull(dataset) || __.isEmpty(dataset)) return null;
var distanceAdded = __.each(dataset, function(value, key, list){
var context = list[key];
var stopCords = ut.createGeoCordinates(context.lat, context.lon);
context.distance = _.round(ut.calculateGeoDistance(userCordinates, stopCords),2);
});
return _.sortBy(distanceAdded, ['distance']);
}
function formatTrimetStopLocation(dataset){
var ds = {};
ds.stopId = dataset[constants.API.PROPERTIES.TRIMET.LOCATION_ID];
ds.desc = dataset[constants.API.PROPERTIES.TRIMET.DESCRIPTION];
ds.direction = dataset[constants.API.PROPERTIES.TRIMET.DIRECTION];
ds.routes = formatTrimetStopRoute(dataset[constants.API.PROPERTIES.TRIMET.ROUTE]);
var descSpeech = ut.replaceSpecialCharacters(ds.desc);
var speltStopId = ut.spellDigitOutput(ds.stopId);
var routeSpeech = ut.getTransitArraySentence(ds.routes.speech);
var output = {};
output.speech = `Stop ID ${speltStopId}. ${descSpeech}, ${ds.direction}, serviced by, ${routeSpeech}.`;
output.stopName = descSpeech;
output.stopId = ds.stopId;
output.routes = ds.routes.routes;
output.buses = ds.routes.buses;
return output;
}
function speakBARTStationLocation(dataset){
var name = dataset[constants.API.PROPERTIES.BART.NAME];
var address = dataset[constants.API.PROPERTIES.BART.ADDRESS];
var stopId = dataset[constants.API.PROPERTIES.BART.ABBR];
var nameSpeech = ut.replaceSpecialCharacters(name);
var ds = {};
ds.speech = `${nameSpeech} on ${address}`;
ds.name = nameSpeech;
ds.address = nameSpeech;
ds.stopId = stopId;
return ds;
}
function formatTrimetStopRoute(dataset){
var output = {};
var routeSpeech = [];
var routes = [];
var buses = [];
var ds = {};
var speech = "";
var descSpeech = "";
var pause = constants.BREAKTIME['100'];
var routeDirDescSpeech = "";
var typeProperty = constants.API.PROPERTIES.TRIMET.TYPE;
var routeProperty = constants.API.PROPERTIES.TRIMET.ROUTE;
var descProperty = constants.API.PROPERTIES.TRIMET.DESCRIPTION;
var dirProperty = constants.API.PROPERTIES.TRIMET.DIRECTION;
__.each(dataset, function(value, key, list){
var context = list[key];
ds.type = constants.ENUM.TRIMET.ROUTE_TYPE[context[typeProperty]];
ds.route = context[routeProperty];
ds.description = context[descProperty];
ds.dir_desc = context[dirProperty][0][descProperty];
descSpeech = ut.replaceSpecialCharacters(ds.description);
routeDirDescSpeech = ut.replaceSpecialCharacters(ds.dir_desc);
speech = `${ds.type} ${descSpeech} ${routeDirDescSpeech}, ${pause}`;
routeSpeech.push(speech);
/** Add unique routes */
if(!__.contains(routes, ds.route)){
routes.push(ds.route );
}
/** Add unique routes */
if(!__.contains(buses, ds.route)){
buses.push(ds.route );
}
ds = {};
descSpeech = "";
speech = "";
});
output.speech = routeSpeech;
output.routes = routes;
output.buses = buses;
return output;
}
/** Format Random fact */
function formatRandomFacts(dataset){
var ds = {};
ds[constants.API.PROPERTIES.FACTS.NUMBER] = dataset[constants.API.PROPERTIES.FACTS.NUMBER];
ds[constants.API.PROPERTIES.FACTS.TEXT] = ut.replaceSpecialCharacters(dataset[constants.API.PROPERTIES.FACTS.TEXT]);
return ds;
}
function formatWeather(dataset){
var ds = {};
ds[constants.API.PROPERTIES.WEATHER.WEATHER] = dataset[constants.API.PROPERTIES.WEATHER.WEATHER];
ds[constants.API.PROPERTIES.WEATHER.HUMIDITY] = dataset[constants.API.PROPERTIES.WEATHER.HUMIDITY];
ds[constants.API.PROPERTIES.WEATHER.WIND_MPH] = dataset[constants.API.PROPERTIES.WEATHER.WIND_MPH];
ds[constants.API.PROPERTIES.WEATHER.FAHRENHEIT_TEMP] = dataset[constants.API.PROPERTIES.WEATHER.FAHRENHEIT_TEMP];
return ds;
}
/** Formats and returns User Country */
function formatPlace(dataset){
var desc = undefined;
var country = "";
desc = _.split(dataset[constants.API.PROPERTIES.PLACES.DESCRIPTION], ',');
country = _.takeRight(desc);
return !__.isEmpty(country) ? _.trim(country[0]) : null;
}
function formatFoundTrimetRoutesAndBuses(dataset){
/** Array to stored unique stops and routes */
var routes = [];
var buses = [];
var ds = {};
var routeProp = constants.API.PROPERTIES.TRIMET.ROUTE;
var route = undefined;
__.each(dataset, function(value, key, list){
var context = list[key];
route = context[routeProp];
/** Added to routes array */
if(!__.contains(routes, route)){
routes.push(route);
}
/** Added to buses array */
if(!__.contains(buses, route)){
buses.push(route);
}
});
ds.routes = routes;
ds.buses = buses;
return ds;
}
/** Prepares Stop Details Speech */
function speakStopAndStationDetails(dataset, providerCode, stopOrStation){
var stopSpeech = undefined;
var speechOutput = undefined;
var stopName = undefined;
var direction = undefined;
var routeAgencyName = undefined;
var stopId = undefined;
var stopIdSpellSpeech = undefined;
var routesList = undefined;
var routes = [];
var northRoutes = [];
var southRoutes = [];
var northPlatforms = [];
var southPlatforms = [];
var pause = constants.BREAKTIME['200'];
var midPause = constants.BREAKTIME['300'];
var longPause = constants.BREAKTIME['350'];
var routesSpeech = undefined;
var platformSpeech = undefined;
var stopAddress = undefined;
switch (providerCode) {
case constants.PROVIDERS.BART:
var northRouteProp = constants.API.PROPERTIES.BART.NORTH_ROUTES;
var southRouteProp = constants.API.PROPERTIES.BART.SOUTH_ROUTES;
var northPlatformProp = constants.API.PROPERTIES.BART.NORTH_PLATFORMS;
var southPlatformProp = constants.API.PROPERTIES.BART.SOUTH_PLATFORMS;
var introProp = constants.API.PROPERTIES.BART.INTRO;
var platformIntroProp = constants.API.PROPERTIES.BART.PLATFORM_INFO;
var nameProp = constants.API.PROPERTIES.BART.NAME;
var addressProp = constants.API.PROPERTIES.BART.ADDRESS;
stopAddress = dataset[addressProp];
stopName = ut.replaceSpecialCharacters(dataset[nameProp]);
cleanArray(dataset[northRouteProp], northRoutes)
cleanArray(dataset[southRouteProp], southRoutes)
cleanArray(dataset[northPlatformProp], northPlatforms)
cleanArray(dataset[southPlatformProp], southPlatforms)
var northRoutesSpeech = ut.getTransitArraySentence(northRoutes);
var southRoutesSpeech = ut.getTransitArraySentence(southRoutes);
var northPlatformsSpeech = ut.getTransitArraySentence(northPlatforms);
var southPlatformsSpeech = ut.getTransitArraySentence(southPlatforms);
var nameAndAddressSpeech = `${stopName} on ${stopAddress}`;
var speech = `${nameAndAddressSpeech}`;
var speech_1 = `Northbound on, ${northRoutesSpeech}, ${pause} Southbound on, ${southRoutesSpeech}. ${midPause}`;
var speech_2 = `Boarding on north platforms, ${northPlatformsSpeech}, and south platforms, ${southPlatformsSpeech}.`;
speechOutput = `${speech} ${midPause} ${speech_1} ${midPause} ${speech_2} `;
return speechOutput;
case constants.PROVIDERS.MTA:
case constants.PROVIDERS.OBA:
default:
/** Get all Formated MTA OBA Routes */
routesList = dataset.routes;
/** Prepare Route speech and add to routes array for processing */
__.each(routesList, function(value, key, list){
var rt = list[key];
var agencyName = getAgencyName(rt.agencyName);
var routeDescription = ut.replaceSpecialCharacters(rt.routeDescription);
var speech = `Route ${rt.routeShortName}, ${midPause} ${rt.routeLongName} ${midPause} ${routeDescription}. ${midPause}`;
routes.push(speech);
});
/** Convert routes array to sentence */
stopIdSpellSpeech = ut.spellDigitOutput(dataset.stopId);
routesSpeech = ut.getTransitArraySentence(routes);
direction = dataset.direction;
speechOutput = `${stopOrStation} ID ${stopIdSpellSpeech}, ${getOBADirections(direction)}, serving, ${pause} ${routesSpeech}`
return speechOutput;
}
}
function cleanArray(dataset, resultset){
__.each(dataset, function(value, key, list){
var item = list[key];
if(!__.contains(resultset, item)){
resultset.push(_.trim(item));
}
});
}
function getAgencyName(searchtext){
return searchtext.replace(/MTA/g, 'Metropolitan Transit Authority,')
}
function getOBADirections(directionCode){
switch (directionCode) {
case "W":
return "Westbound";
case "E":
return "Eastbound";
case "N":
return "Northbound";
case "S":
return "Southbound";
default:
return "";
}
}
/** Process Stops and Stations API results based on service provider */
function formatFoundStop(dataset, providerCode){
var ds = {};
var longProp = undefined;
var latProp = undefined;
var descProp = undefined;
var nameProp = undefined;
var introProp = undefined;
var zipcodProp = undefined;
var stopIdProp = undefined;
var cityProp = undefined;
var routeProp = undefined;
var routesProp = undefined;
var platformProp = undefined;
var platformInfoProp = undefined;
var dataProp = undefined;
var codeProp = undefined;
var directionProp = undefined;
var agencyProp = undefined;
var timezoneProp = undefined;
var longNameProp = undefined;
var shortNameProp = undefined;
var idProp = undefined;
var addressProp = undefined;
switch (providerCode) {
case constants.PROVIDERS.TRIMET:
longProp = constants.API.PROPERTIES.LONGIUDE;
latProp = constants.API.PROPERTIES.LATITUDE;
descProp = constants.API.PROPERTIES.TRIMET.DESCRIPTION;
/** Set Return object */
ut.setDirectProperties(dataset, ds, latProp);
ut.setDirectProperties(dataset, ds, longProp);
ut.setProperties(ds, descProp, ut.replaceSpecialCharacters(dataset[descProp]));
break;
case constants.PROVIDERS.MTA:
dataProp = constants.API.PROPERTIES.OBA.DATA;
longProp = constants.API.PROPERTIES.OBA.LONGIUDE;
latProp = constants.API.PROPERTIES.OBA.LATITUDE;
descProp = constants.API.PROPERTIES.OBA.DESCRIPTION;
nameProp = constants.API.PROPERTIES.OBA.NAME;
codeProp = constants.API.PROPERTIES.OBA.CODE;
directionProp = constants.API.PROPERTIES.OBA.DIRECTION;
agencyProp = constants.API.PROPERTIES.OBA.AGENCY;
timezoneProp = constants.API.PROPERTIES.OBA.TIMEZONE;
longNameProp = constants.API.PROPERTIES.OBA.LONG_NAME;
shortNameProp = constants.API.PROPERTIES.OBA.SHORT_NAME;
idProp = constants.API.PROPERTIES.OBA.ID;
routesProp = constants.API.PROPERTIES.OBA.ROUTES;
var resultSet = dataset[dataProp] || dataset;
var routesSet = resultSet[routesProp];
var timezones = [];
var routeIds = [];
ds.busIds = [];
ds.routes = [];
ds.stopId = resultSet[codeProp];
ds.direction = resultSet[directionProp];
ds.latitude = resultSet[latProp];
ds.longitude = resultSet[longProp];
ds.stopName = ut.replaceSpecialCharacters(resultSet[nameProp]);
/** Cycle through routes */
__.each(routesSet, function(value, key, list){
var rs = {};
var route = list[key];
var agency = route[agencyProp];
var tmz = agency[timezoneProp];
var routeId = route[idProp];
rs.agencyName = agency[nameProp];
rs.agencyId = agency[idProp];
rs.routeDescription = route[descProp];
rs.routeLongName = route[longNameProp];
rs.routeShortName = route[shortNameProp];
if(!__.contains(timezones, tmz)){
timezones.push(tmz);
}
if(!__.contains(routeIds, routeId)){
routeIds.push(routeId);
}
ds.routes.push(rs);
});
ds.timezone = timezones[0];
ds.routeId = routeIds;
break;
case constants.PROVIDERS.BART:
longProp = constants.API.PROPERTIES.BART.LONGIUDE;
latProp = constants.API.PROPERTIES.BART.LATITUDE;
nameProp = constants.API.PROPERTIES.BART.NAME;
zipcodProp = constants.API.PROPERTIES.BART.ZIPCODE;
stopIdProp = constants.API.PROPERTIES.BART.ABBR;
cityProp = constants.API.PROPERTIES.BART.CITY;
introProp = constants.API.PROPERTIES.BART.INTRO;
routesProp = constants.API.PROPERTIES.BART.ROUTES;
routeProp = constants.API.PROPERTIES.BART.ROUTE;
platformProp = constants.API.PROPERTIES.BART.PLATFORM;
platformInfoProp = constants.API.PROPERTIES.BART.PLATFORM_INFO;
addressProp = constants.API.PROPERTIES.BART.ADDRESS;
var north_routes_prop = constants.API.PROPERTIES.BART.NORTH_ROUTES;
var south_routes_prop = constants.API.PROPERTIES.BART.SOUTH_ROUTES;
var north_platform_prop = constants.API.PROPERTIES.BART.NORTH_PLATFORMS;
var south_Platform_prop = constants.API.PROPERTIES.BART.SOUTH_PLATFORMS;
var routes = [];
var platforms = [];
var northRouteSet = dataset[north_routes_prop][routeProp];
var southRouteSet = dataset[south_routes_prop][routeProp];
var northPlatformSet = dataset[north_platform_prop][platformProp];
var southPlatformSet = dataset[south_Platform_prop][platformProp];
ut.combind(northRouteSet, routes);
ut.combind(southRouteSet, routes);
ut.combind(northPlatformSet, platforms);
ut.combind(southPlatformSet, platforms);
/** Set Return object */
ut.setDirectProperties(dataset, ds, longProp);
ut.setDirectProperties(dataset, ds, latProp);
ut.setDirectProperties(dataset, ds, nameProp);
ut.setDirectProperties(dataset, ds, zipcodProp);
ut.setDirectProperties(dataset, ds, stopIdProp);
ut.setDirectProperties(dataset, ds, cityProp);
ut.setDirectProperties(dataset, ds, introProp);
ut.setDirectProperties(dataset, ds, platformInfoProp);
ut.setDirectProperties(dataset, ds, addressProp);
ut.setProperties(ds, north_routes_prop, northRouteSet);
ut.setProperties(ds, south_routes_prop, southRouteSet);
ut.setProperties(ds, north_platform_prop, northPlatformSet);
ut.setProperties(ds, south_Platform_prop, southPlatformSet);
ut.setProperties(ds, 'routes', routes);
ut.setProperties(ds, 'platforms', platforms);
break;
default:
ds = null;
break;
}
/** Return results */
return ds;
}
function formatTrimetDetourResponse(dataset, userTimezone, apiParams){
/** Speech Pauses */
var sentence = constants.BREAKTIME['SENTENCE'];
var paragraph = constants.BREAKTIME['PARAGRAPH'];
var pause = constants.BREAKTIME['100'];
var midPause = constants.BREAKTIME['200'];
var longPause = constants.BREAKTIME['350'];
/** Detour properties */
var routeProp = constants.API.PROPERTIES.TRIMET.ROUTE;
var detourProp = constants.API.PROPERTIES.TRIMET.DETOUR;
var typeProp = constants.API.PROPERTIES.TRIMET.TYPE;
var descProp = constants.API.PROPERTIES.TRIMET.DESCRIPTION;
var endProp = constants.API.PROPERTIES.TRIMET.END;
var beginProp = constants.API.PROPERTIES.TRIMET.BEGIN;
var phoneticProp = constants.API.PROPERTIES.TRIMET.PHONETIC;
/** conditional variables */
var hasDetours = false;
/** dataset counts */
var detourCount = 0;
var myDetourCount = 0;
var locationCount = 0;
/** API Parameters */
var routeIdParam = constants.API.PARAMETERS.ROUTE_ID;
var stopIdParam = constants.API.PARAMETERS.STOP_ID;
var routeId = _.split(apiParams[routeIdParam], ',');
var stopId = apiParams[stopIdParam];
var returnedDetoursSpeech = undefined;
var returnedRoutesSpeech = undefined;
var detourCountSpeech = undefined;
var detoursIntroSpeech = undefined;
var detourSpeech = undefined;
var stopNameSpeech = undefined;
var responseSpeech = undefined;
var pluralSpeech = undefined;
var isAreSpeech = ut.getPlural('is', detourCount);
var detoursPluralSpeech = ut.getPlural('detour', detourCount);
var detours = [];
var myDetours = [];
var stopName = undefined;
var transitType = undefined;
var busName = undefined;
var startDate = undefined;
var endDate = undefined;
var alertMessage = undefined;
var phoneticSpeech = undefined;
/** Extract sections from results */
var detourData = dataset[detourProp];
detourCount = ut.hasValidResponse(detourData) ? detourData.length : 0;
/** Check if detours and arrivals are present */
hasDetours = detourCount > 0;
/** Cycle through detours and generate alert speech */
if(hasDetours){
/** Prepare Breif Intro */
var affectedRoutes = [];
var affectedRoutesCount = 0;
var speech = undefined;
/** Check if my route is affected by the detour */
__.each(detourData, function(detour, key, list){
/** get All Afected Routes */
var routes = detour[routeProp];
/** Cycle through Affected routes and Buses, Only get affected Buses for that route */
__.each(routes, function(route, key, list){
var type = route[typeProp];
var desc = route[descProp];
var rt = route[routeProp];
transitType = constants.ENUM.TRIMET.ROUTE_TYPE[type];
busName = ut.replaceSpecialCharacters(desc);
speech = `${transitType} ${busName}`;
if(__.contains(routeId, rt.toString())){
affectedRoutes.push(speech);
}
});
var beginDateRaw = detour[beginProp];
var endDateRaw = detour[endProp];
var bdate = ut.convertTransitDateTime(beginDateRaw, userTimezone);
var edate = ut.convertTransitDateTime(endDateRaw, userTimezone);
var shortDateFormat = constants.DEFAULTS.SHORT_DATE_FORMAT;
startDate = ut.expandDatetime(beginDateRaw, shortDateFormat);
endDate = ut.expandDatetime(endDateRaw, shortDateFormat);
alertMessage = ut.replaceSpecialCharacters(detour[descProp]);
phoneticSpeech = ut.replaceSpecialCharacters(detour[phoneticProp]);
returnedRoutesSpeech = ut.getTransitArraySentence(affectedRoutes);
speech = `${returnedRoutesSpeech}, from ${startDate}, to, ${endDate}, ${phoneticSpeech}`;
affectedRoutesCount = affectedRoutes.length;
if(affectedRoutesCount > 0)
{
detours.push(speech);
affectedRoutes.length = 0;
}
});
/** Prepare Breif Intro */
myDetourCount = detours.length;
isAreSpeech = ut.getPlural('is', myDetourCount);
detoursPluralSpeech = ut.getPlural('detour', myDetourCount);
if(myDetourCount > 0)
{
detoursIntroSpeech = `There ${isAreSpeech} ${myDetourCount} ${detoursPluralSpeech}.`;
}
else
{
detoursIntroSpeech = `There ${isAreSpeech}, no ${detoursPluralSpeech}, at the moment.`;
}
}
else
{
/** Prepare Breif Intro */
isAreSpeech = ut.getPlural('is', detourCount);
detoursPluralSpeech = ut.getPlural('detour', detourCount);
detoursIntroSpeech = `There ${isAreSpeech}, no ${detoursPluralSpeech}, at the moment.`;
}
returnedDetoursSpeech= ut.getTransitArraySentence(detours);
responseSpeech = `${detoursIntroSpeech} ${paragraph} ${returnedDetoursSpeech}.`;
return responseSpeech;
}
function formatTrimetResponse(dataset, userTimezone, responseType, apiParams){
/** Speech Pauses */
var sentence = constants.BREAKTIME['SENTENCE'];
var paragraph = constants.BREAKTIME['PARAGRAPH'];
var pause = constants.BREAKTIME['100'];
var midPause = constants.BREAKTIME['200'];
var longPause = constants.BREAKTIME['350'];
/** API Parameters */
var routeIdParam = constants.API.PARAMETERS.ROUTE_ID;
var stopIdParam = constants.API.PARAMETERS.STOP_ID;
var routeId = _.split(apiParams[routeIdParam], ',');
var stopId = apiParams[stopIdParam];
/** Detour properties */
var routeProp = constants.API.PROPERTIES.TRIMET.ROUTE;
var detourProp = constants.API.PROPERTIES.TRIMET.DETOUR;
var typeProp = constants.API.PROPERTIES.TRIMET.TYPE;
var descProp = constants.API.PROPERTIES.TRIMET.DESCRIPTION;
var endProp = constants.API.PROPERTIES.TRIMET.END;
var beginProp = constants.API.PROPERTIES.TRIMET.BEGIN;
/** arrivals properties */
var inCongProp = constants.API.PROPERTIES.TRIMET.IN_CONGESTION;
var arrivalProp = constants.API.PROPERTIES.TRIMET.ARRIVAL;
var departedProp = constants.API.PROPERTIES.TRIMET.DEPARTED;
var scheduledProp = constants.API.PROPERTIES.TRIMET.SCHEDULED;
var shortSignProp = constants.API.PROPERTIES.TRIMET.SHORT_SIGN;
var estimatedProp = constants.API.PROPERTIES.TRIMET.ESTIMATED;
var detouredProp = constants.API.PROPERTIES.TRIMET.DETOURED;
var fullSignProp = constants.API.PROPERTIES.TRIMET.FULL_SIGN;
var statusProp = constants.API.PROPERTIES.STATUS;
var locationProp = constants.API.PROPERTIES.LOCATION;
var delayedProp = constants.API.PROPERTIES.TRIMET.IS_DELAYED;
var delayminsProp = constants.API.PROPERTIES.TRIMET.DELAY_MINUTES;
/** conditional variables */
var inCongestion = false;
var hasDeparted = false;
var isDetoured = false;
var hasEstimated = false;
var hasScheduled = false;
var hasDetoures = false;
var hasArrivals = false;
var hasLocation = false;
var isDelayed = false;
/** dataset counts */
var detourCount = 0;
var arrivalCount = 0;
var locationCount = 0;
var estimatedArrivalCount = 0;
var scheduledArrivalCount = 0;
var delayCount = 0;
var delaymins = 0;
var myDetourCount = 0;
var returnedRoutesSpeech = undefined;
var returnedArrivalsSpeech = undefined;
var returnedScheduledArrivalsSpeech = undefined;
var returnedEstimatedArrivalsSpeech = undefined;
var returnedDelaysSpeech = undefined;
var detourCountSpeech = undefined;
var alertIntroSpeech = undefined;
var alertsSpeech = undefined;
var arrivalsIntroSpeech = undefined;
var arrivalsSpeech = undefined;
var stopNameSpeech = undefined;
var responseSpeech = undefined;
var introSpeech = undefined;
var pluralSpeech = undefined;
var delaysIntroSpeech = undefined;
var delaysSpeech = undefined;
var isAreSpeech = ut.getPlural('is', detourCount);
var detoursPluralSpeech = ut.getPlural('alert', detourCount);
var arrivalsPluralSpeech = ut.getPlural('arrival', arrivalCount);
var detours = [];
var arrivals = [];
var estimatedArrivals = [];
var scheduledArrivals = [];
var delays = [];
var myDetours = [];
var stopName = undefined;
var transitType = undefined;
var busName = undefined;
var startDate = undefined;
var endDate = undefined;
var alertMessage = undefined;
/** Extract sections from results */
var detourData = dataset[detourProp];
var arrivalData = dataset[arrivalProp];
var locationData = dataset[locationProp];
detourCount = ut.hasValidResponse(detourData) ? detourData.length : 0;
arrivalCount = ut.hasValidResponse(arrivalData) ? arrivalData.length : 0;
locationCount = ut.hasValidResponse(locationData) ? locationData.length : 0;
/** Check if detours and arrivals are present */
hasDetoures = detourCount > 0;
hasArrivals = arrivalCount > 0;
hasLocation = locationCount > 0;
/** get location Details */
if(hasLocation){
stopName = locationData[0][descProp];
stopNameSpeech = ut.replaceSpecialCharacters(stopName);
}
/** Cycle through detours and generate alert speech */
if(hasDetoures){
var affectedRoutes = [];
var affectedRoutesCount = 0;
var speech = undefined;
/** Check if my route is affected by the detour */
__.each(detourData, function(detour, key, list){
/** get All Afected Routes */
var routes = detour[routeProp];
/** Cycle through Affected routes and Buses, Only get affected Buses for that route */
__.each(routes, function(route, key, list){
var type = route[typeProp];
var desc = route[descProp];
var rt = route[routeProp];
transitType = constants.ENUM.TRIMET.ROUTE_TYPE[type];
busName = ut.replaceSpecialCharacters(desc);
speech = `${transitType}, ${busName}`;
if(__.contains(routeId, rt.toString())){
affectedRoutes.push(speech);
}
});
var beginDateRaw = detour[beginProp];
var endDateRaw = detour[endProp];
var bdate = ut.convertTransitDateTime(beginDateRaw, userTimezone);
var edate = ut.convertTransitDateTime(endDateRaw, userTimezone);
var shortDateFormat = constants.DEFAULTS.SHORT_DATE_FORMAT;
startDate = ut.expandDatetime(beginDateRaw, shortDateFormat);
endDate = ut.expandDatetime(endDateRaw, shortDateFormat);
alertMessage = ut.replaceSpecialCharacters(detour[descProp]);
returnedRoutesSpeech = ut.getTransitArraySentence(affectedRoutes);
speech = `In effect for ${returnedRoutesSpeech}, from ${startDate}, to, ${endDate}, ${alertMessage}`;
affectedRoutesCount = affectedRoutes.length;
if(affectedRoutesCount > 0)
{
detours.push(speech);
affectedRoutes.length = 0;
}
});
/** Prepare Breif Intro */
myDetourCount = detours.length;
isAreSpeech = ut.getPlural('is', myDetourCount);
detoursPluralSpeech = ut.getPlural('alert', myDetourCount);
if(myDetourCount > 0)
{
alertIntroSpeech = `There ${isAreSpeech} ${myDetourCount} ${detoursPluralSpeech}, on ${stopNameSpeech},`;
}
else
{
alertIntroSpeech = `There ${isAreSpeech} no ${detoursPluralSpeech}, on ${stopNameSpeech}.`;
}
}
else
{
/** Prepare Breif Intro */
isAreSpeech = ut.getPlural('is', detourCount);
detoursPluralSpeech = ut.getPlural('alert', detourCount);
alertIntroSpeech = `There ${isAreSpeech} no ${detoursPluralSpeech}, on ${stopNameSpeech}.`;
}
/** Cycle Through Arrivals and Prepare arrivals speech */
if(hasArrivals){
/** Prepare Speech */
isAreSpeech = ut.getPlural('is', arrivalCount);
arrivalsPluralSpeech = ut.getPlural('arrival', arrivalCount);
arrivalsIntroSpeech = `I found, ${arrivalCount}, ${arrivalsPluralSpeech}.`;
__.each(arrivalData, function(value, key, list){
var context = list[key];
inCongestion = context[inCongProp];
hasDeparted = context[departedProp];
hasScheduled = ut.hasKey(context, scheduledProp);
hasEstimated = ut.hasKey(context, estimatedProp);
isDetoured = context[detouredProp];
var speech = undefined;
var estDate = undefined;
var schDate = undefined;
var scheduledAtSpeech = undefined;
var estimatedArrivalSpeech = undefined;
var usertime = undefined;
var busDetails = ut.replaceSpecialCharacters(context[shortSignProp]);
var busStatus = context[statusProp];
var scheduledDateTime = undefined;
var estimatedDateTime = undefined;
var arrivalTense = undefined;
var status = undefined;
switch (busStatus) {
case scheduledProp:
scheduledDateTime = context[scheduledProp];
schDate = ut.convertTransitDateTime(scheduledDateTime, userTimezone);
scheduledAtSpeech = ut.formatTransitTimes(schDate, userTimezone);
speech = `${busDetails}, scheduled ${scheduledAtSpeech}.`;
scheduledArrivals.push(speech);
break;
case estimatedProp:
scheduledDateTime = context[scheduledProp];
estimatedDateTime = context[estimatedProp];
estDate = ut.convertTransitDateTime(estimatedDateTime, userTimezone);
schDate = ut.convertTransitDateTime(scheduledDateTime, userTimezone);
usertime = ut.getUsertime(userTimezone);
status = ut.getArrivalStatus(usertime,schDate, estDate);
arrivalTense = ut.getArrivaltense(usertime, estDate);
scheduledAtSpeech = ut.formatTransitTimes(schDate, userTimezone);
estimatedArrivalSpeech = ut.getTimetoEvent(usertime, estDate);
speech = `${busDetails}, scheduled ${scheduledAtSpeech}, ${arrivalTense} ${status}, ${estimatedArrivalSpeech}.`;
var schedulesSpeech = `${busDetails}, scheduled ${scheduledAtSpeech}.`;
estimatedArrivals.push(speech);
/** Add delayed property to result and add to delays list */
var delayDetails = ut.isDelayed(scheduledDateTime, estimatedDateTime);
isDelayed = delayDetails[delayedProp];
delaymins = delayDetails[delayminsProp];
var delayPlural = ut.getPlural('minute', delaymins);
if(isDelayed){
speech = `${busDetails}, ${delaymins} ${delayPlural} delay.`;
delays.push(speech);
}
break;
default:
break;
}
/** Add to arrivals */
arrivals.push(speech);
});
}
else
{
isAreSpeech = ut.getPlural('is', arrivalCount);
arrivalsPluralSpeech = ut.getPlural('arrival', arrivalCount);
arrivalsIntroSpeech = `There ${isAreSpeech} no ${arrivals} at the moment.`;
}
/** Return briefing Speech */
returnedRoutesSpeech = ut.getTransitArraySentence(detours);
returnedArrivalsSpeech = ut.getTransitArraySentence(arrivals);
returnedEstimatedArrivalsSpeech = ut.getTransitArraySentence(estimatedArrivals);
returnedScheduledArrivalsSpeech = ut.getTransitArraySentence(scheduledArrivals);
returnedDelaysSpeech = ut.getTransitArraySentence(delays);
/** Switch formatingType and return processed speech */
switch (responseType) {
case constants.API.RESPONSE.BRIEFING:
case constants.API.RESPONSE.FLASH:
case constants.API.RESPONSE.SUMMARY:
alertsSpeech = `${alertIntroSpeech} ${paragraph} ${returnedRoutesSpeech}`;
arrivalsSpeech = `${arrivalsIntroSpeech} ${paragraph} ${returnedArrivalsSpeech}`;
responseSpeech = `${alertsSpeech} ${paragraph} ${arrivalsSpeech}`;
break;
case constants.API.RESPONSE.SERVICE_ALERTS:
case constants.API.RESPONSE.ALERTS:
responseSpeech = detourCount > 0 ? `${alertIntroSpeech} ${paragraph} ${returnedRoutesSpeech}` : `There are no alerts for ${stopNameSpeech}.`;
break;
case constants.API.RESPONSE.ARRIVALS:
estimatedArrivalCount = estimatedArrivals.length;
pluralSpeech = ut.getPlural('arrival', estimatedArrivalCount);
introSpeech = estimatedArrivalCount > 0 ? `I found ${estimatedArrivalCount}, ${pluralSpeech}.` : `There are no arrivals for ${stopNameSpeech}.`;
responseSpeech = `${introSpeech} ${paragraph} ${returnedEstimatedArrivalsSpeech}`;
break;
case constants.API.RESPONSE.SCHEDULES:
scheduledArrivalCount = arrivals.length;
pluralSpeech = ut.getPlural('Schedule', scheduledArrivalCount);
introSpeech = scheduledArrivalCount > 0 ? `I found ${scheduledArrivalCount}, ${pluralSpeech}.` : `There are no schedules for ${stopNameSpeech}.`;
responseSpeech = `${introSpeech} ${paragraph} ${returnedArrivalsSpeech}`;
break;
case constants.API.RESPONSE.DELAYS:
delayCount = delays.length;
pluralSpeech = ut.getPlural('Delay', delayCount);
introSpeech = delayCount > 0 ? `I found ${delayCount}, ${pluralSpeech} for ${stopNameSpeech}.` : `There are no delays for ${stopNameSpeech}.`;
responseSpeech = `${introSpeech} ${paragraph} ${returnedDelaysSpeech}`;
break;
default:
break;
}
/** Return Response */
return responseSpeech;
}
/** Extract and return serving agencies in a given State */
function formatAgenciesCovered(dataset){
/** Extract and format Agency List for User Response */
var agencies = [];
var resultSet = dataset[constants.API.PROPERTIES.OBA.DATA];
var agencyList = resultSet[constants.API.PROPERTIES.OBA.LIST];
var ReferenceList = resultSet[constants.API.PROPERTIES.OBA.REFERENCES];
var agenciesRef = ReferenceList[constants.API.PROPERTIES.OBA.AGENCIES];
var agencyCount = agencyList.length;
/** Cycle through agency list and extract reference data */
__.each(agencyList, function(agency, key, list){
var ag = {};
var filter = {};
ut.setDirectProperties(agency, ag, constants.API.PROPERTIES.OBA.AGENCY_ID);
ut.setDirectProperties(agency, ag, constants.API.PROPERTIES.OBA.LATITUDE);
ut.setDirectProperties(agency, ag, constants.API.PROPERTIES.OBA.LONGIUDE);
/** get Agency Details from Reference List */
filter[constants.API.PROPERTIES.OBA.ID] = ag[constants.API.PROPERTIES.OBA.AGENCY_ID];
var refSet = ut.selectFromJsonResult(agenciesRef, filter);
/** Extract Agency name and timezone from agency reference */
ut.setDirectProperties(refSet[0], ag, constants.API.PROPERTIES.OBA.NAME);
ut.setDirectProperties(refSet[0], ag, constants.API.PROPERTIES.OBA.TIMEZONE);
agencies.push(ag);
});
return agencies;
}
function formatOBAStops(dataset){
var ds = undefined;
var enteryProp = constants.API.PROPERTIES.OBA.ENTRY;
var referencesProp = constants.API.PROPERTIES.OBA.REFERENCES;
var routesProp = constants.API.PROPERTIES.OBA.ROUTES;