cozy-client 41.4.1 → 41.6.0

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.
@@ -2703,7 +2703,7 @@ var CozyClient = /*#__PURE__*/function () {
2703
2703
  CozyClient.hooks = CozyClient.hooks || {};
2704
2704
  CozyClient.fetchPolicies = _policies.default; //COZY_CLIENT_VERSION_PACKAGE in replaced by babel. See babel config
2705
2705
 
2706
- CozyClient.version = "41.4.1";
2706
+ CozyClient.version = "41.6.0";
2707
2707
 
2708
2708
  _microee.default.mixin(CozyClient);
2709
2709
 
@@ -3,8 +3,17 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.geodesicDistance = exports.computeSpeed = void 0;
6
+ exports.deltaLatitude = exports.deltaLongitude = exports.computeSphericalCenter = exports.geodesicDistance = exports.computeSpeed = void 0;
7
7
 
8
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
9
+
10
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
11
+
12
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
13
+
14
+ // @ts-check
15
+ var EARTH_RADIUS_M = 6378137;
16
+ var EARTH_CIRCUMFERENCE_M = 40075000;
8
17
  /**
9
18
  * Compute the speed from distance and duration
10
19
  *
@@ -12,6 +21,7 @@ exports.geodesicDistance = exports.computeSpeed = void 0;
12
21
  * @param {number} duration - The duration in seconds
13
22
  * @returns {number} - The speed, in m/s, rounded to 2 decimals
14
23
  */
24
+
15
25
  var computeSpeed = function computeSpeed(distance, duration) {
16
26
  if (!distance || !duration) {
17
27
  return 0;
@@ -19,12 +29,43 @@ var computeSpeed = function computeSpeed(distance, duration) {
19
29
 
20
30
  return Math.round(distance / duration * 100) / 100;
21
31
  };
32
+ /**
33
+ * Convert degrees to radians
34
+ *
35
+ * @param {number} degrees - The degrees to convert
36
+ * @returns {number} The converted radians
37
+ */
38
+
22
39
 
23
40
  exports.computeSpeed = computeSpeed;
24
41
 
25
42
  var degreesToRadians = function degreesToRadians(degrees) {
26
43
  return degrees * Math.PI / 180;
27
44
  };
45
+ /**
46
+ * Convert radians to degrees
47
+ *
48
+ * @param {number} radians - The radians to convert
49
+ * @returns {number} The converted degrees
50
+ */
51
+
52
+
53
+ var radiansToDegrees = function radiansToDegrees(radians) {
54
+ return radians * 180 / Math.PI;
55
+ };
56
+ /**
57
+ * Round the value to the n decimal
58
+ *
59
+ * @param {number} value - The value to round
60
+ * @param {number} n - The number of decimals
61
+ * @returns {number} The rounded value
62
+ */
63
+
64
+
65
+ var roundToNDecimals = function roundToNDecimals(value, n) {
66
+ var multiplier = Math.pow(10, n);
67
+ return Math.round(value * multiplier) / multiplier;
68
+ };
28
69
  /**
29
70
  * Compute the distance between 2 geographic points, in meters.
30
71
  *
@@ -55,10 +96,112 @@ var geodesicDistance = function geodesicDistance(point1, point2) {
55
96
  var aLat = Math.pow(Math.sin(diffLat / 2), 2);
56
97
  var aLon = Math.pow(Math.sin(diffLon / 2), 2);
57
98
  var a = aLat + Math.cos(lat1) * Math.cos(lat2) * aLon;
58
- var c = 2 * Math.asin(Math.sqrt(a)); // Radius of earth is 6371 km
99
+ var c = 2 * Math.asin(Math.sqrt(a));
100
+ var distance = EARTH_RADIUS_M * c;
101
+ return roundToNDecimals(distance, 2);
102
+ };
103
+ /**
104
+ * Compute the geographical center of the given points
105
+ *
106
+ * This consists of finding the centroid of a set of points
107
+ * in a sphere.
108
+ * Note this assumes the Earth is a perfect sphere, which is not,
109
+ * but the approximation should be good enough.
110
+ *
111
+ * @param {Array<import("../types").Coordinates>} coordinates - The geo points
112
+ * @returns {import("../types").Coordinates} The center point
113
+ */
114
+
115
+
116
+ exports.geodesicDistance = geodesicDistance;
117
+
118
+ var computeSphericalCenter = function computeSphericalCenter(coordinates) {
119
+ if (coordinates.length < 1) {
120
+ return null;
121
+ }
122
+
123
+ if (coordinates.length === 1) {
124
+ return coordinates[0];
125
+ }
126
+
127
+ var totalX = 0;
128
+ var totalY = 0;
129
+ var totalZ = 0;
130
+
131
+ var _iterator = _createForOfIteratorHelper(coordinates),
132
+ _step;
133
+
134
+ try {
135
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
136
+ var coord = _step.value;
137
+ var lon = degreesToRadians(coord.lon);
138
+ var lat = degreesToRadians(coord.lat); // Convert spherical coordinates to Cartesian coordinates
139
+
140
+ var x = Math.cos(lat) * Math.cos(lon);
141
+ var y = Math.cos(lat) * Math.sin(lon);
142
+ var z = Math.sin(lat);
143
+ totalX += x;
144
+ totalY += y;
145
+ totalZ += z;
146
+ }
147
+ } catch (err) {
148
+ _iterator.e(err);
149
+ } finally {
150
+ _iterator.f();
151
+ }
152
+
153
+ var avgX = totalX / coordinates.length;
154
+ var avgY = totalY / coordinates.length;
155
+ var avgZ = totalZ / coordinates.length; // Don't forget to convert Cartesian coordinates back to spherical
156
+
157
+ var centralLon = radiansToDegrees(Math.atan2(avgY, avgX));
158
+ var hyp = Math.sqrt(avgX * avgX + avgY * avgY);
159
+ var centralLat = radiansToDegrees(Math.atan2(avgZ, hyp));
160
+ return {
161
+ lat: roundToNDecimals(centralLat, 13),
162
+ lon: roundToNDecimals(centralLon, 13)
163
+ };
164
+ };
165
+ /**
166
+ * Compute the longitude delta from a distance, in meters.
167
+ *
168
+ * This requires the latitude: we want to compute the horizontal delta
169
+ * on the Earth surface. As it is a sphere (kind of), this delta won't be
170
+ * the same depending on whether it is on the equator (min variation)
171
+ * or on the poles (max variation), for instance.
172
+ *
173
+ * @param {number} latitude - The latitude
174
+ * @param {number} distance - The distance in meters
175
+ * @returns {number} the longitude delta degrees
176
+ */
177
+
178
+
179
+ exports.computeSphericalCenter = computeSphericalCenter;
180
+
181
+ var deltaLongitude = function deltaLongitude(latitude, distance) {
182
+ var phi = degreesToRadians(latitude);
183
+ var deltaLambda = distance / (EARTH_RADIUS_M * Math.cos(phi));
184
+ return roundToNDecimals(radiansToDegrees(deltaLambda), 13);
185
+ };
186
+ /**
187
+ * Compute the latitude delta from a distance, in meters.
188
+ *
189
+ * The reasoning is rather simple: there are 360° of latitudes of same distance.
190
+ * Then, it consists of computing 1 degree distance, and divide the
191
+ * given distance by this value.
192
+ *
193
+ * @param {number} distance - The distance in meters
194
+ * @returns {number} The delta latitude degrees
195
+ */
196
+
197
+
198
+ exports.deltaLongitude = deltaLongitude;
199
+
200
+ var deltaLatitude = function deltaLatitude(distance) {
201
+ var distOneLatDegree = EARTH_CIRCUMFERENCE_M / 360; // 111 319 meters per degree
59
202
 
60
- var distance = 6371 * 1000 * c;
61
- return Math.round(distance * 100) / 100;
203
+ var deltaLat = distance / distOneLatDegree;
204
+ return roundToNDecimals(deltaLat, 13);
62
205
  };
63
206
 
64
- exports.geodesicDistance = geodesicDistance;
207
+ exports.deltaLatitude = deltaLatitude;
@@ -1,4 +1,10 @@
1
- const { geodesicDistance, computeSpeed } = require('./geo')
1
+ const {
2
+ geodesicDistance,
3
+ computeSpeed,
4
+ computeSphericalCenter,
5
+ deltaLongitude,
6
+ deltaLatitude
7
+ } = require('./geo')
2
8
 
3
9
  describe('geodesicDistance', () => {
4
10
  it('should return 0 for 2 points with same coordinates', () => {
@@ -15,7 +21,7 @@ describe('geodesicDistance', () => {
15
21
  it('should return the correct Paris-NY distance', () => {
16
22
  const paris = { lat: 48.8566, lon: 2.3522 }
17
23
  const NY = { lat: 40.7128, lon: -74.006 }
18
- expect(geodesicDistance(paris, NY)).toEqual(5837240.9)
24
+ expect(geodesicDistance(paris, NY)).toEqual(5843779.97)
19
25
  })
20
26
  })
21
27
 
@@ -30,3 +36,57 @@ describe('speed', () => {
30
36
  expect(computeSpeed(10000, 1800)).toEqual(5.56)
31
37
  })
32
38
  })
39
+
40
+ describe('computeSphericalCenter', () => {
41
+ it('should return null when coordinates are missing', () => {
42
+ expect(computeSphericalCenter([])).toEqual(null)
43
+ })
44
+ it('should return the correct coordinates when there are all equals', () => {
45
+ expect(computeSphericalCenter([{ lat: 10, lon: 10 }])).toEqual({
46
+ lat: 10,
47
+ lon: 10
48
+ })
49
+ expect(
50
+ computeSphericalCenter([
51
+ { lat: 0.0, lon: 0.0 },
52
+ { lat: 0.0, lon: 0.0 },
53
+ { lat: 0.0, lon: 0.0 }
54
+ ])
55
+ ).toEqual({ lat: 0, lon: 0 })
56
+ })
57
+ it('should return correct center for n points', () => {
58
+ expect(
59
+ computeSphericalCenter([{ lat: 10, lon: 10 }, { lat: 10, lon: 10 }])
60
+ ).toEqual({ lat: 10, lon: 10 })
61
+
62
+ expect(
63
+ computeSphericalCenter([{ lat: 48, lon: 2 }, { lat: 48, lon: 3 }])
64
+ ).toEqual({ lat: 48.0010848667078, lon: 2.5 })
65
+
66
+ expect(
67
+ computeSphericalCenter([
68
+ { lat: 10, lon: 10 },
69
+ { lat: 20, lon: 20 },
70
+ { lat: 30, lon: 30 }
71
+ ])
72
+ ).toEqual({ lat: 20.1866988557583, lon: 19.5721919393414 })
73
+ })
74
+ })
75
+
76
+ describe('delta longitude and latitude', () => {
77
+ it('null distance should give a null delta', () => {
78
+ expect(deltaLongitude(46.66, 0)).toEqual(0)
79
+ expect(deltaLatitude(0)).toEqual(0)
80
+ })
81
+
82
+ it('should give the correct longitude delta', () => {
83
+ expect(deltaLongitude(46.66, 200)).toEqual(0.002617749975)
84
+ expect(deltaLongitude(0, 200)).toEqual(0.0017966305682) // equator
85
+ expect(deltaLongitude(89.99, 200)).toEqual(10.2939349426746) // north pole
86
+ expect(deltaLongitude(-89.99, 200)).toEqual(10.2939349426746) // south pole
87
+ })
88
+ it('should give the correct latitude delta', () => {
89
+ expect(deltaLatitude(200)).toEqual(0.0017966313163)
90
+ expect(deltaLatitude(2000)).toEqual(0.0179663131628) // contrarily to the longitude, this is linear
91
+ })
92
+ })
@@ -5,7 +5,7 @@ var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWild
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.user = exports.paper = exports.dacc = exports.sharing = exports.timeseries = exports.document = exports.contact = exports.utils = exports.permission = exports.note = exports.account = exports.konnectorFolder = exports.folder = exports.file = exports.applications = exports.instance = exports.trigger = exports.accounts = exports.triggers = void 0;
8
+ exports.geo = exports.user = exports.paper = exports.dacc = exports.sharing = exports.timeseries = exports.document = exports.contact = exports.utils = exports.permission = exports.note = exports.account = exports.konnectorFolder = exports.folder = exports.file = exports.applications = exports.instance = exports.trigger = exports.accounts = exports.triggers = void 0;
9
9
 
10
10
  var trigger = _interopRequireWildcard(require("./trigger"));
11
11
 
@@ -74,6 +74,10 @@ exports.paper = paper;
74
74
  var user = _interopRequireWildcard(require("./user"));
75
75
 
76
76
  exports.user = user;
77
+
78
+ var geo = _interopRequireWildcard(require("./geo"));
79
+
80
+ exports.geo = geo;
77
81
  // For backward compatibility before 9.0.0
78
82
  var triggers = trigger;
79
83
  exports.triggers = triggers;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-client",
3
- "version": "41.4.1",
3
+ "version": "41.6.0",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.node.js",
6
6
  "browser": "dist/index.js",
@@ -75,5 +75,5 @@
75
75
  "react-native-ios11-devicecheck": "https://github.com/cozy/react-native-devicecheck#app-attest-v0.1"
76
76
  },
77
77
  "sideEffects": false,
78
- "gitHead": "973c0aa500d04e1463b5a7a9f4f3d5a8d80e2bd9"
78
+ "gitHead": "a44be14889f120cfc81e28797ce35a789929dc8a"
79
79
  }
@@ -1,2 +1,5 @@
1
1
  export function computeSpeed(distance: number, duration: number): number;
2
2
  export function geodesicDistance(point1: import("../types").Coordinates, point2: import("../types").Coordinates): number;
3
+ export function computeSphericalCenter(coordinates: Array<import("../types").Coordinates>): import("../types").Coordinates;
4
+ export function deltaLongitude(latitude: number, distance: number): number;
5
+ export function deltaLatitude(distance: number): number;
@@ -17,4 +17,5 @@ import * as sharing from "./sharing";
17
17
  import * as dacc from "./dacc";
18
18
  import * as paper from "./paper";
19
19
  import * as user from "./user";
20
- export { trigger, instance, applications, file, folder, konnectorFolder, note, account, permission, utils, contact, document, timeseries, sharing, dacc, paper, user };
20
+ import * as geo from "./geo";
21
+ export { trigger, instance, applications, file, folder, konnectorFolder, note, account, permission, utils, contact, document, timeseries, sharing, dacc, paper, user, geo };