mobility-toolbox-js 2.0.0 → 2.0.1-beta.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/api/RoutingAPI.js +15 -0
  2. package/api/RoutingAPI.test.js +25 -0
  3. package/api/StopsAPI.js +12 -0
  4. package/api/StopsAPI.test.js +22 -0
  5. package/api/TralisAPI.js +359 -0
  6. package/api/TralisAPI.test.js +67 -0
  7. package/api/{tralis/TralisAPIUtils.js → TralisAPIUtils.js} +2 -32
  8. package/api/index.js +3 -3
  9. package/{ol/README.md → api/typedefs.js} +0 -0
  10. package/common/Tracker.js +14 -118
  11. package/common/api/HttpAPI.js +30 -0
  12. package/common/api/HttpAPI.test.js +50 -0
  13. package/common/api/WebSocketAPI.js +175 -0
  14. package/{api/tralis/WebSocketConnector.test.js → common/api/WebSocketAPI.test.js} +100 -145
  15. package/common/controls/Control.js +26 -91
  16. package/common/controls/Control.test.js +32 -43
  17. package/common/index.js +4 -0
  18. package/common/layers/Layer.js +53 -244
  19. package/common/layers/Layer.test.js +185 -244
  20. package/common/mixins/CopyrightMixin.js +20 -44
  21. package/common/mixins/SearchMixin.js +100 -166
  22. package/common/mixins/TralisLayerMixin.js +443 -894
  23. package/common/styles/index.js +4 -4
  24. package/common/styles/trackerDefaultStyle.js +39 -175
  25. package/common/styles/trackerDelayStyle.js +2 -11
  26. package/common/styles/trackerSimpleStyle.js +4 -8
  27. package/common/trackerConfig.js +61 -99
  28. package/common/trackerConfig.test.js +15 -17
  29. package/common/typedefs.js +0 -23
  30. package/common/utils/createTrackerFilters.js +10 -41
  31. package/common/utils/createTrackerFilters.test.js +40 -56
  32. package/common/utils/getMapboxMapCopyrights.js +3 -16
  33. package/common/utils/getMapboxMapCopyrights.test.js +32 -39
  34. package/common/utils/getMapboxStyleUrl.js +3 -13
  35. package/common/utils/getVehiclePosition.js +3 -33
  36. package/common/utils/index.js +5 -6
  37. package/common/utils/removeDuplicate.js +3 -17
  38. package/common/utils/removeDuplicate.test.js +17 -20
  39. package/common/utils/sortByDelay.js +2 -7
  40. package/common/utils/timeUtils.js +8 -32
  41. package/common/utils/timeUtils.test.js +7 -13
  42. package/index.js +8 -2
  43. package/mapbox/controls/CopyrightControl.js +9 -38
  44. package/mapbox/controls/index.js +1 -0
  45. package/mapbox/index.js +4 -3
  46. package/mapbox/layers/Layer.js +15 -76
  47. package/mapbox/layers/Layer.test.js +81 -101
  48. package/mapbox/layers/TralisLayer.js +46 -193
  49. package/mapbox/layers/TralisLayer.test.js +12 -14
  50. package/mapbox/layers/index.js +2 -0
  51. package/mapbox/utils.js +7 -21
  52. package/mbt.js +50444 -0
  53. package/mbt.js.map +7 -0
  54. package/mbt.min.js +1005 -0
  55. package/mbt.min.js.map +7 -0
  56. package/ol/controls/CopyrightControl.js +8 -46
  57. package/ol/controls/CopyrightControl.test.js +75 -121
  58. package/ol/controls/RoutingControl.js +167 -532
  59. package/ol/controls/RoutingControl.test.js +99 -164
  60. package/ol/controls/StopFinderControl.js +3 -31
  61. package/ol/controls/StopFinderControl.test.js +18 -29
  62. package/ol/controls/index.js +3 -0
  63. package/ol/index.js +5 -13
  64. package/ol/layers/Layer.js +23 -128
  65. package/ol/layers/Layer.test.js +79 -102
  66. package/ol/layers/MapboxLayer.js +62 -237
  67. package/ol/layers/MapboxLayer.test.js +58 -84
  68. package/ol/layers/MapboxStyleLayer.js +38 -268
  69. package/ol/layers/MapboxStyleLayer.test.js +97 -128
  70. package/ol/layers/MaplibreLayer.js +46 -187
  71. package/ol/layers/RoutingLayer.js +21 -51
  72. package/ol/layers/RoutingLayer.test.js +15 -24
  73. package/ol/layers/TralisLayer.js +102 -276
  74. package/ol/layers/TralisLayer.test.js +32 -50
  75. package/ol/layers/VectorLayer.js +3 -24
  76. package/ol/layers/VectorLayer.test.js +34 -45
  77. package/ol/layers/WMSLayer.js +15 -57
  78. package/ol/layers/WMSLayer.test.js +35 -43
  79. package/ol/layers/index.js +8 -0
  80. package/ol/styles/fullTrajectoryDelayStyle.js +11 -15
  81. package/ol/styles/fullTrajectoryStyle.js +17 -25
  82. package/ol/styles/index.js +2 -2
  83. package/package.json +35 -62
  84. package/api/routing/RoutingAPI.js +0 -44
  85. package/api/routing/RoutingAPI.test.js +0 -41
  86. package/api/stops/StopsAPI.js +0 -41
  87. package/api/stops/StopsAPI.test.js +0 -34
  88. package/api/tralis/TralisAPI.js +0 -731
  89. package/api/tralis/TralisAPI.test.js +0 -75
  90. package/api/tralis/WebSocketConnector.js +0 -338
  91. package/api/tralis/typedefs.js +0 -81
  92. package/common/api/api.js +0 -64
  93. package/common/api/api.test.js +0 -68
  94. package/index.js.map +0 -1
  95. package/module.js +0 -23
  96. package/ol/controls/snapshots/RoutingControlRouteGen10.json +0 -58
  97. package/ol/controls/snapshots/RoutingControlRouteGen100.json +0 -292
  98. package/ol/controls/snapshots/RoutingControlRouteGen30.json +0 -69
  99. package/ol/controls/snapshots/RoutingControlRouteGen5.json +0 -58
  100. package/ol/controls/snapshots/RoutingControlRouteOSM.json +0 -759
  101. package/ol/controls/snapshots/RoutingControlStation1.json +0 -60
  102. package/ol/controls/snapshots/RoutingControlStation2.json +0 -49
@@ -1,25 +1,23 @@
1
- import { getTypeIndex } from './trackerConfig';
2
-
3
- describe('trackerConfig', () => {
4
- describe('#getTypeIndex()', () => {
1
+ import { getTypeIndex } from "./trackerConfig";
2
+ describe("trackerConfig", () => {
3
+ describe("#getTypeIndex()", () => {
5
4
  test("retrurn the type is it's not a string", () => {
6
- const obj = { foo: 'foo' };
5
+ const obj = { foo: "foo" };
7
6
  expect(getTypeIndex(obj)).toBe(obj);
8
7
  expect(getTypeIndex(0)).toBe(0);
9
8
  expect(getTypeIndex(null)).toBe(null);
10
- expect(getTypeIndex(undefined)).toBe(undefined);
9
+ expect(getTypeIndex(void 0)).toBe(void 0);
11
10
  });
12
-
13
- test('find good index for new tracker values', () => {
14
- expect(getTypeIndex('tram')).toBe(0);
15
- expect(getTypeIndex('subway')).toBe(1);
16
- expect(getTypeIndex('bus')).toBe(3);
17
- expect(getTypeIndex('ferry')).toBe(4);
18
- expect(getTypeIndex('cablecar')).toBe(5);
19
- expect(getTypeIndex('gondola')).toBe(6);
20
- expect(getTypeIndex('funicular')).toBe(7);
21
- expect(getTypeIndex('coach')).toBe(8);
22
- expect(getTypeIndex('rail')).toBe(9);
11
+ test("find good index for new tracker values", () => {
12
+ expect(getTypeIndex("tram")).toBe(0);
13
+ expect(getTypeIndex("subway")).toBe(1);
14
+ expect(getTypeIndex("bus")).toBe(3);
15
+ expect(getTypeIndex("ferry")).toBe(4);
16
+ expect(getTypeIndex("cablecar")).toBe(5);
17
+ expect(getTypeIndex("gondola")).toBe(6);
18
+ expect(getTypeIndex("funicular")).toBe(7);
19
+ expect(getTypeIndex("coach")).toBe(8);
20
+ expect(getTypeIndex("rail")).toBe(9);
23
21
  });
24
22
  });
25
23
  });
@@ -1,23 +0,0 @@
1
- /**
2
- * @typedef {function} FilterFunction
3
- * @param {Object} vehicle Vehicle to filter.
4
- */
5
-
6
- /**
7
- * @typedef {Object} ViewState
8
- * @property {number} time A time in ms.
9
- * @property {number[2]} center A center in mercator coordinate.
10
- * @property {number[4]} extent An Extent in mercator coordinates.
11
- * @property {number[2]} size A size ([width, height]).
12
- * @property {number} rotation A rotation in radians.
13
- * @property {number} resolution A resolution.
14
- * @property {number} zoom A zoom level.
15
- * @property {number} pixelRatio A pixel ratio.
16
- */
17
-
18
- /**
19
- * @typedef {Object} FeatureInfo
20
- * @property {Layer} layer A layer.
21
- * @property {ol/Feature~Feature[]} features An array of features.
22
- * @property {number[2]} coordinate The coordinate where to find the featue.
23
- */
@@ -1,45 +1,25 @@
1
- /**
2
- * Return a filter functions based on some parameters of a vehicle.
3
- *
4
- * @param {string|Array<string>} line - A list of vehicle's name to filter. Names can be separated by a comma. Ex: 'S1,S2,S3'
5
- * @param {string|Array<string} route - A list of vehicle's route (contained in routeIdentifier property) to filter. Indentifiers can be separated by a comma. Ex: 'id1,id2,id3'
6
- * @param {string|Array<string} operator A list of vehicle's operator to filter. Operators can be separated by a comma. Ex: 'SBB,DB'
7
- * @param {Regexp} regexLine - A regex aplly of vehcile's name.
8
- * @private
9
- */
10
1
  const createFilters = (line, route, operator, regexLine) => {
11
2
  const filterList = [];
12
-
13
3
  if (!line && !route && !operator && !regexLine) {
14
4
  return null;
15
5
  }
16
-
17
6
  if (regexLine) {
18
- const regexLineList =
19
- typeof regexLine === 'string' ? [regexLine] : regexLine;
7
+ const regexLineList = typeof regexLine === "string" ? [regexLine] : regexLine;
20
8
  const lineFilter = (item) => {
21
- const name =
22
- item.properties.name ||
23
- (item.properties.line && item.properties.line.name) ||
24
- '';
9
+ const name = item.properties.name || item.properties.line && item.properties.line.name || "";
25
10
  if (!name) {
26
11
  return false;
27
12
  }
28
- return regexLineList.some((regexStr) =>
29
- new RegExp(regexStr, 'i').test(name),
30
- );
13
+ return regexLineList.some((regexStr) => new RegExp(regexStr, "i").test(name));
31
14
  };
32
15
  filterList.push(lineFilter);
33
16
  }
34
-
35
17
  if (line) {
36
- const lineFiltersList = typeof line === 'string' ? line.split(',') : line;
37
- const lineList = lineFiltersList.map((l) =>
38
- l.replace(/\s+/g, '').toUpperCase(),
39
- );
18
+ const lineFiltersList = typeof line === "string" ? line.split(",") : line;
19
+ const lineList = lineFiltersList.map((l) => l.replace(/\s+/g, "").toUpperCase());
40
20
  const lineFilter = (item) => {
41
21
  const { line: linee, name } = item.properties;
42
- const lineName = (name || (linee && linee.name) || '').toUpperCase();
22
+ const lineName = (name || linee && linee.name || "").toUpperCase();
43
23
  if (!lineName) {
44
24
  return false;
45
25
  }
@@ -47,33 +27,23 @@ const createFilters = (line, route, operator, regexLine) => {
47
27
  };
48
28
  filterList.push(lineFilter);
49
29
  }
50
-
51
30
  if (route) {
52
- const routes = typeof route === 'string' ? route.split(',') : route;
31
+ const routes = typeof route === "string" ? route.split(",") : route;
53
32
  const routeList = routes.map((item) => parseInt(item, 10));
54
33
  const routeFilter = (item) => {
55
- const routeId = parseInt(
56
- item.properties.routeIdentifier.split('.')[0],
57
- 10,
58
- );
34
+ const routeId = parseInt(item.properties.routeIdentifier.split(".")[0], 10);
59
35
  return routeList.includes(routeId);
60
36
  };
61
37
  filterList.push(routeFilter);
62
38
  }
63
-
64
39
  if (operator) {
65
- const operatorList = typeof operator === 'string' ? [operator] : operator;
66
- const operatorFilter = (item) =>
67
- operatorList.some((op) =>
68
- new RegExp(op, 'i').test(item.properties.operator),
69
- );
40
+ const operatorList = typeof operator === "string" ? [operator] : operator;
41
+ const operatorFilter = (item) => operatorList.some((op) => new RegExp(op, "i").test(item.properties.operator));
70
42
  filterList.push(operatorFilter);
71
43
  }
72
-
73
44
  if (!filterList.length) {
74
45
  return null;
75
46
  }
76
-
77
47
  return (t) => {
78
48
  for (let i = 0; i < filterList.length; i += 1) {
79
49
  if (!filterList[i](t)) {
@@ -83,5 +53,4 @@ const createFilters = (line, route, operator, regexLine) => {
83
53
  return true;
84
54
  };
85
55
  };
86
-
87
56
  export default createFilters;
@@ -1,93 +1,77 @@
1
- import createTrackerFilters from './createTrackerFilters';
2
-
1
+ import createTrackerFilters from "./createTrackerFilters";
3
2
  const u1 = {
4
3
  properties: {
5
- routeIdentifier: '001.000827.004:7',
6
- operator: 'FoO',
4
+ routeIdentifier: "001.000827.004:7",
5
+ operator: "FoO",
7
6
  line: {
8
- name: 'U1',
9
- },
10
- },
7
+ name: "U1"
8
+ }
9
+ }
11
10
  };
12
11
  const ireta = {
13
12
  properties: {
14
- routeIdentifier: '0022.000827.004:7',
15
- operator: 'BAR',
13
+ routeIdentifier: "0022.000827.004:7",
14
+ operator: "BAR",
16
15
  line: {
17
- name: 'IRETA',
18
- },
19
- },
16
+ name: "IRETA"
17
+ }
18
+ }
20
19
  };
21
20
  const arb = {
22
21
  properties: {
23
- routeIdentifier: '00333.000827.004:7',
24
- operator: 'qux',
22
+ routeIdentifier: "00333.000827.004:7",
23
+ operator: "qux",
25
24
  line: {
26
- name: 'ARB',
27
- },
28
- },
25
+ name: "ARB"
26
+ }
27
+ }
29
28
  };
30
-
31
29
  const trajectories = [u1, ireta, arb];
32
-
33
- describe('#createTrackerFilter()', () => {
34
- test('returns null', () => {
30
+ describe("#createTrackerFilter()", () => {
31
+ test("returns null", () => {
35
32
  const filterFunc = createTrackerFilters();
36
33
  expect(filterFunc).toBe(null);
37
34
  });
38
-
39
- describe('using line', () => {
40
- test('as string', () => {
41
- const filterFunc = createTrackerFilters('u1,foo');
35
+ describe("using line", () => {
36
+ test("as string", () => {
37
+ const filterFunc = createTrackerFilters("u1,foo");
42
38
  expect(trajectories.filter(filterFunc)).toEqual([u1]);
43
39
  });
44
-
45
- test('as array of string', () => {
46
- const filterFunc = createTrackerFilters(['u1', 'foo', 'IRETA']);
40
+ test("as array of string", () => {
41
+ const filterFunc = createTrackerFilters(["u1", "foo", "IRETA"]);
47
42
  expect(trajectories.filter(filterFunc)).toEqual([u1, ireta]);
48
43
  });
49
44
  });
50
-
51
- describe('using route identifier', () => {
52
- test('as string', () => {
53
- const filterFunc = createTrackerFilters(null, '1,foo');
45
+ describe("using route identifier", () => {
46
+ test("as string", () => {
47
+ const filterFunc = createTrackerFilters(null, "1,foo");
54
48
  expect(trajectories.filter(filterFunc)).toEqual([u1]);
55
49
  });
56
-
57
- test('as array of string', () => {
58
- const filterFunc = createTrackerFilters(null, ['22', 'foo', '1']);
50
+ test("as array of string", () => {
51
+ const filterFunc = createTrackerFilters(null, ["22", "foo", "1"]);
59
52
  expect(trajectories.filter(filterFunc)).toEqual([u1, ireta]);
60
53
  });
61
54
  });
62
-
63
- describe('using operator', () => {
64
- test('as string', () => {
65
- const filterFunc = createTrackerFilters(null, null, 'foo');
55
+ describe("using operator", () => {
56
+ test("as string", () => {
57
+ const filterFunc = createTrackerFilters(null, null, "foo");
66
58
  expect(trajectories.filter(filterFunc)).toEqual([u1]);
67
59
  });
68
-
69
- test('as array of string', () => {
70
- const filterFunc = createTrackerFilters(null, null, ['bar', 'foo', '1']);
60
+ test("as array of string", () => {
61
+ const filterFunc = createTrackerFilters(null, null, ["bar", "foo", "1"]);
71
62
  expect(trajectories.filter(filterFunc)).toEqual([u1, ireta]);
72
63
  });
73
64
  });
74
-
75
- describe('using regexLine', () => {
76
- test('as string', () => {
77
- const filterFunc = createTrackerFilters(
78
- null,
79
- null,
80
- null,
81
- '^(S|R$|RE|PE|D|IRE|RB|TER)',
82
- );
65
+ describe("using regexLine", () => {
66
+ test("as string", () => {
67
+ const filterFunc = createTrackerFilters(null, null, null, "^(S|R$|RE|PE|D|IRE|RB|TER)");
83
68
  expect(trajectories.filter(filterFunc)).toEqual([ireta]);
84
69
  });
85
-
86
- test('as array of string', () => {
70
+ test("as array of string", () => {
87
71
  const filterFunc = createTrackerFilters(null, null, null, [
88
- '^IR',
89
- '^ARB$',
90
- 'foo',
72
+ "^IR",
73
+ "^ARB$",
74
+ "foo"
91
75
  ]);
92
76
  expect(trajectories.filter(filterFunc)).toEqual([ireta, arb]);
93
77
  });
@@ -1,32 +1,19 @@
1
- import removeDuplicate from './removeDuplicate';
2
-
3
- /**
4
- * Return the copyright a Mapbox map.
5
- * @param {mapboxgl.Map} map A Mapbox map
6
- * @ignore
7
- */
1
+ import removeDuplicate from "./removeDuplicate";
8
2
  const getMapboxMapCopyrights = (map) => {
9
3
  if (!map || !map.style) {
10
4
  return [];
11
5
  }
12
6
  const { sourceCaches } = map.style;
13
7
  let copyrights = [];
14
-
15
8
  Object.values(sourceCaches).forEach((sourceCache) => {
16
9
  if (sourceCache.used) {
17
10
  const source = sourceCache.getSource();
18
- const attribution =
19
- source.attribution || (source.options && source.options.attribution);
20
-
11
+ const attribution = source.attribution || source.options && source.options.attribution;
21
12
  if (attribution) {
22
- copyrights = copyrights.concat(
23
- attribution.replace(/&copy;/g, '©').split(/(<a.*?<\/a>)/),
24
- );
13
+ copyrights = copyrights.concat(attribution.replace(/&copy;/g, "\xA9").split(/(<a.*?<\/a>)/));
25
14
  }
26
15
  }
27
16
  });
28
-
29
17
  return removeDuplicate(copyrights);
30
18
  };
31
-
32
19
  export default getMapboxMapCopyrights;
@@ -1,47 +1,40 @@
1
- import getMapboxMapCopyrights from './getMapboxMapCopyrights';
2
-
3
- describe('getMapboxMapCopyrights()', () => {
4
- test('returns an empty array if map is not defined', () => {
1
+ import getMapboxMapCopyrights from "./getMapboxMapCopyrights";
2
+ describe("getMapboxMapCopyrights()", () => {
3
+ test("returns an empty array if map is not defined", () => {
5
4
  expect(getMapboxMapCopyrights().length).toBe(0);
6
5
  });
7
-
8
- test('returns an empty array if map.style is not defined', () => {
6
+ test("returns an empty array if map.style is not defined", () => {
9
7
  expect(getMapboxMapCopyrights({}).length).toBe(0);
10
8
  });
11
-
12
- test('returns non depluicated copyrights in an array', () => {
13
- expect(
14
- getMapboxMapCopyrights({
15
- style: {
16
- sourceCaches: {
17
- used: {
18
- used: true,
19
- getSource() {
20
- return {
21
- attribution:
22
- '<a href="https://www.openmaptiles.org/" target="_blank">© OpenMapTiles</a> <a href="https://www.openstreetmap.org/about/" target="_blank">&copy; OpenStreetMap contributors</a>',
23
- };
24
- },
25
- },
26
- 'not used': {
27
- used: false,
28
- },
29
- 'also used': {
30
- used: true,
31
- getSource() {
32
- return {
33
- attribution:
34
- '<a href="https://www.openmaptiles.org/" target="_blank">&copy; OpenMapTiles</a> <a href="https://www.imagico.de/" target="_blank">&copy; imagico</a>',
35
- };
36
- },
37
- },
9
+ test("returns non depluicated copyrights in an array", () => {
10
+ expect(getMapboxMapCopyrights({
11
+ style: {
12
+ sourceCaches: {
13
+ used: {
14
+ used: true,
15
+ getSource() {
16
+ return {
17
+ attribution: '<a href="https://www.openmaptiles.org/" target="_blank">\xA9 OpenMapTiles</a> <a href="https://www.openstreetmap.org/about/" target="_blank">&copy; OpenStreetMap contributors</a>'
18
+ };
19
+ }
38
20
  },
39
- },
40
- }),
41
- ).toEqual([
42
- '<a href="https://www.openmaptiles.org/" target="_blank">© OpenMapTiles</a>',
43
- '<a href="https://www.openstreetmap.org/about/" target="_blank">© OpenStreetMap contributors</a>',
44
- '<a href="https://www.imagico.de/" target="_blank">© imagico</a>',
21
+ "not used": {
22
+ used: false
23
+ },
24
+ "also used": {
25
+ used: true,
26
+ getSource() {
27
+ return {
28
+ attribution: '<a href="https://www.openmaptiles.org/" target="_blank">&copy; OpenMapTiles</a> <a href="https://www.imagico.de/" target="_blank">&copy; imagico</a>'
29
+ };
30
+ }
31
+ }
32
+ }
33
+ }
34
+ })).toEqual([
35
+ '<a href="https://www.openmaptiles.org/" target="_blank">\xA9 OpenMapTiles</a>',
36
+ '<a href="https://www.openstreetmap.org/about/" target="_blank">\xA9 OpenStreetMap contributors</a>',
37
+ '<a href="https://www.imagico.de/" target="_blank">\xA9 imagico</a>'
45
38
  ]);
46
39
  });
47
40
  });
@@ -1,12 +1,4 @@
1
- import qs from 'query-string';
2
-
3
- /**
4
- * Return the styleUrl with apiKey parameters set.
5
- * @param {string} apiKey apiKey value for the mapbox request.
6
- * @param {string} apiKeyName parameter name for apiKey in the mapbox request.
7
- * @param {string} styleUrl mapbox styleUrl value.
8
- * @ignore
9
- */
1
+ import qs from "query-string";
10
2
  const getMapboxStyleUrl = (apiKey, apiKeyName, styleUrl) => {
11
3
  if (apiKey === false) {
12
4
  return styleUrl;
@@ -16,7 +8,6 @@ const getMapboxStyleUrl = (apiKey, apiKeyName, styleUrl) => {
16
8
  return styleUrl;
17
9
  }
18
10
  if (!apiKey) {
19
- // eslint-disable-next-line no-console
20
11
  console.warn(`No apiKey is defined for request to ${styleUrl}`);
21
12
  return null;
22
13
  }
@@ -24,9 +15,8 @@ const getMapboxStyleUrl = (apiKey, apiKeyName, styleUrl) => {
24
15
  ...parsedStyle,
25
16
  query: {
26
17
  ...parsedStyle.query,
27
- [apiKeyName]: apiKey,
28
- },
18
+ [apiKeyName]: apiKey
19
+ }
29
20
  });
30
21
  };
31
-
32
22
  export default getMapboxStyleUrl;
@@ -1,23 +1,12 @@
1
- import GeomType from 'ol/geom/GeometryType';
2
-
3
- /**
4
- * Interpolate or not the vehicle position from a trajectory at a specific date.
5
- *
6
- * @param {number} now Current date to interpolate a position with. In ms.
7
- * @param {TralisTrajectory} trajectory The trajectory to interpolate.
8
- * @param {boolean} noInterpolate If true, the vehicle position is not interpolated on each render but only once.
9
- * @returns
10
- */
1
+ import GeomType from "ol/geom/GeometryType";
11
2
  const getVehiclePosition = (now, trajectory, noInterpolate) => {
12
3
  const {
13
4
  time_intervals: timeIntervals,
14
5
  olGeometry: geometry,
15
- coordinate,
6
+ coordinate
16
7
  } = trajectory.properties;
17
-
18
8
  let coord;
19
9
  let rotation;
20
-
21
10
  if (noInterpolate && coordinate) {
22
11
  coord = coordinate;
23
12
  } else if (geometry.getType() === GeomType.POINT) {
@@ -26,30 +15,17 @@ const getVehiclePosition = (now, trajectory, noInterpolate) => {
26
15
  const intervals = timeIntervals || [[]];
27
16
  const firstInterval = intervals[0];
28
17
  const lastInterval = intervals[intervals.length - 1];
29
-
30
- // Between the last time interval of a trajectory event and the beginning
31
- // of the new trajectory event, there is few seconds, can be 6 to 30
32
- // seconds (that's why the vehicle jumps sometimes).
33
- // So we make the choice here to display the last (or the first) position
34
- // of an trajectory event instead of removing them, if the current date is
35
- // outside the time intervals we display the vehicle at the last (or first) position known.
36
18
  if (now < firstInterval[0]) {
37
- // Display first position known.
38
19
  [, , rotation] = firstInterval;
39
20
  coord = geometry.getFirstCoordinate();
40
21
  } else if (now > lastInterval[0]) {
41
- // Display last position known.
42
22
  [, , rotation] = lastInterval;
43
23
  coord = geometry.getLastCoordinate();
44
24
  } else {
45
- // Interpolate position using time intervals.
46
25
  for (let j = 0; j < intervals.length - 1; j += 1) {
47
- // Rotation only available in tralis layer.
48
26
  const [start, startFrac] = intervals[j];
49
27
  const [end, endFrac] = intervals[j + 1];
50
-
51
28
  if (start <= now && now <= end) {
52
- // interpolate position inside the time interval.
53
29
  const timeFrac = Math.min((now - start) / (end - start), 1);
54
30
  const geomFrac = timeFrac * (endFrac - startFrac) + startFrac;
55
31
  coord = geometry.getCoordinateAt(geomFrac);
@@ -59,14 +35,8 @@ const getVehiclePosition = (now, trajectory, noInterpolate) => {
59
35
  }
60
36
  }
61
37
  } else {
62
- // eslint-disable-next-line no-console
63
- console.error(
64
- 'This geometry type is not supported. Only Point or LineString are. Current geometry: ',
65
- geometry,
66
- );
38
+ console.error("This geometry type is not supported. Only Point or LineString are. Current geometry: ", geometry);
67
39
  }
68
-
69
40
  return { coord, rotation };
70
41
  };
71
-
72
42
  export default getVehiclePosition;
@@ -1,6 +1,5 @@
1
- export { default as getMapboxStyleUrl } from './getMapboxStyleUrl';
2
- export { default as getMapboxMapCopyrights } from './getMapboxMapCopyrights';
3
- export { default as removeDuplicate } from './removeDuplicate';
4
- export * from './timeUtils';
5
- export * from '../styles';
6
- export { default as sortByDelay } from './sortByDelay';
1
+ export { default as getMapboxStyleUrl } from "./getMapboxStyleUrl";
2
+ export { default as getMapboxMapCopyrights } from "./getMapboxMapCopyrights";
3
+ export { default as removeDuplicate } from "./removeDuplicate";
4
+ export * from "./timeUtils";
5
+ export { default as sortByDelay } from "./sortByDelay";
@@ -1,22 +1,8 @@
1
- /**
2
- * This function remove duplicates lower case string value of an array.
3
- * It removes also null, undefined or non string values.
4
- *
5
- * @param {array} array Array of values.
6
- * @ignore
7
- */
8
1
  const removeDuplicate = (array) => {
9
- const arrWithoutEmptyValues = array.filter(
10
- (val) => val !== undefined && val !== null && val.trim && val.trim(),
11
- );
12
- const lowerCasesValues = arrWithoutEmptyValues.map((str) =>
13
- str.toLowerCase(),
14
- );
2
+ const arrWithoutEmptyValues = array.filter((val) => val !== void 0 && val !== null && val.trim && val.trim());
3
+ const lowerCasesValues = arrWithoutEmptyValues.map((str) => str.toLowerCase());
15
4
  const uniqueLowerCaseValues = [...new Set(lowerCasesValues)];
16
- const uniqueValues = uniqueLowerCaseValues.map((uniqueStr) =>
17
- arrWithoutEmptyValues.find((str) => str.toLowerCase() === uniqueStr),
18
- );
5
+ const uniqueValues = uniqueLowerCaseValues.map((uniqueStr) => arrWithoutEmptyValues.find((str) => str.toLowerCase() === uniqueStr));
19
6
  return uniqueValues;
20
7
  };
21
-
22
8
  export default removeDuplicate;
@@ -1,22 +1,19 @@
1
- import removeDuplicate from './removeDuplicate';
2
-
3
- describe('removeDuplicate()', () => {
4
- test('removes duplicates', () => {
5
- expect(
6
- removeDuplicate([
7
- 'a',
8
- ' ',
9
- ' ',
10
- 'b',
11
- 'a',
12
- undefined,
13
- null,
14
- 'A',
15
- 0,
16
- 'c',
17
- 'b',
18
- 'B',
19
- ]),
20
- ).toEqual(['a', 'b', 'c']);
1
+ import removeDuplicate from "./removeDuplicate";
2
+ describe("removeDuplicate()", () => {
3
+ test("removes duplicates", () => {
4
+ expect(removeDuplicate([
5
+ "a",
6
+ " ",
7
+ " ",
8
+ "b",
9
+ "a",
10
+ void 0,
11
+ null,
12
+ "A",
13
+ 0,
14
+ "c",
15
+ "b",
16
+ "B"
17
+ ])).toEqual(["a", "b", "c"]);
21
18
  });
22
19
  });
@@ -1,23 +1,18 @@
1
1
  const sortByDelay = (traj1, traj2) => {
2
2
  const props1 = traj1.properties;
3
3
  const props2 = traj2.properties;
4
-
5
4
  if (props1.delay === null && props2.delay !== null) {
6
5
  return 1;
7
6
  }
8
7
  if (props2.delay === null && props1.delay !== null) {
9
8
  return -1;
10
9
  }
11
-
12
- // We put cancelled train inbetween green and yellow trains
13
- // >=180000ms corresponds to yellow train
14
10
  if (props1.cancelled && !props2.cancelled) {
15
- return props2.delay < 180000 ? -1 : 1;
11
+ return props2.delay < 18e4 ? -1 : 1;
16
12
  }
17
13
  if (props2.cancelled && !props1.cancelled) {
18
- return props1.delay < 180000 ? 1 : -1;
14
+ return props1.delay < 18e4 ? 1 : -1;
19
15
  }
20
16
  return props2.delay - props1.delay;
21
17
  };
22
-
23
18
  export default sortByDelay;