@duoquote/polytini 0.1.0 → 0.1.1

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 (68) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +120 -120
  3. package/dist/types/cli.d.cts +2 -2
  4. package/dist/types/deckgl.d.cts +72 -60
  5. package/dist/types/flexible.d.cts +92 -80
  6. package/dist/types/geojson.jsr.d.cts +111 -0
  7. package/dist/types/google.d.cts +48 -48
  8. package/dist/types/google.jsr.d.cts +72 -60
  9. package/dist/types/index.d.cts +109 -87
  10. package/dist/types/leaflet-auto.d.cts +4 -4
  11. package/dist/types/leaflet-auto.jsr.d.cts +2 -2
  12. package/dist/types/leaflet.d.cts +39 -39
  13. package/dist/types/leaflet.jsr.d.cts +15 -13
  14. package/dist/types/mapbox.d.cts +50 -50
  15. package/dist/types/mapbox.jsr.d.cts +5 -0
  16. package/dist/types/maplibre.d.cts +54 -54
  17. package/dist/types/maplibre.jsr.d.cts +92 -0
  18. package/dist/types/openlayers.d.cts +23 -23
  19. package/dist/types/openlayers.jsr.d.cts +42 -0
  20. package/dist/types/presets.d.cts +70 -66
  21. package/dist/types/turf.d.cts +33 -33
  22. package/dist/types/turf.jsr.d.cts +46 -0
  23. package/dist/types/wasm.d.cts +42 -33
  24. package/package.json +200 -200
  25. package/src/browser.js +15 -15
  26. package/src/bun.js +4 -4
  27. package/src/cli-lib.js +160 -160
  28. package/src/cli.js +7 -7
  29. package/src/core.js +268 -268
  30. package/src/deckgl.js +176 -176
  31. package/src/deno.js +12 -12
  32. package/src/error.js +15 -15
  33. package/src/fast.js +179 -179
  34. package/src/flexible.js +283 -283
  35. package/src/google.js +70 -70
  36. package/src/leaflet-auto.js +11 -11
  37. package/src/leaflet.js +44 -44
  38. package/src/mapbox.js +10 -10
  39. package/src/maplibre.js +57 -57
  40. package/src/node.js +17 -17
  41. package/src/openlayers.js +172 -172
  42. package/src/presets.js +242 -242
  43. package/src/text.js +111 -111
  44. package/src/turf.js +80 -80
  45. package/src/wasm-bytes.js +2 -2
  46. package/src/wasm.js +235 -235
  47. package/src/web.js +9 -9
  48. package/types/cli.d.ts +2 -2
  49. package/types/deckgl.d.ts +72 -60
  50. package/types/flexible.d.ts +92 -80
  51. package/types/geojson.jsr.d.ts +111 -0
  52. package/types/google.d.ts +48 -48
  53. package/types/google.jsr.d.ts +72 -60
  54. package/types/index.d.ts +109 -87
  55. package/types/leaflet-auto.d.ts +4 -4
  56. package/types/leaflet-auto.jsr.d.ts +2 -2
  57. package/types/leaflet.d.ts +39 -39
  58. package/types/leaflet.jsr.d.ts +15 -13
  59. package/types/mapbox.d.ts +50 -50
  60. package/types/mapbox.jsr.d.ts +5 -0
  61. package/types/maplibre.d.ts +54 -54
  62. package/types/maplibre.jsr.d.ts +92 -0
  63. package/types/openlayers.d.ts +23 -23
  64. package/types/openlayers.jsr.d.ts +42 -0
  65. package/types/presets.d.ts +70 -66
  66. package/types/turf.d.ts +33 -33
  67. package/types/turf.jsr.d.ts +46 -0
  68. package/types/wasm.d.ts +42 -33
package/src/flexible.js CHANGED
@@ -1,283 +1,283 @@
1
- // HERE's Flexible Polyline format, version 1:
2
- // https://github.com/heremaps/flexible-polyline
3
- //
4
- // A string is [version][header][data], each part made of varints written
5
- // with a URL-safe 64 character alphabet: 5 data bits per character, and a
6
- // sixth bit that says another character follows. The header holds the
7
- // precision (bits 0 to 3), the third dimension type (bits 4 to 6) and its
8
- // precision (bits 7 to 10). The data is zigzag encoded deltas, 2 or 3 values
9
- // per point, the first point relative to 0.
10
- //
11
- // Like the Google-format core, decode and encode have a fast loop for the
12
- // common case and an exact `slow*` function for the rest, which also reports
13
- // every kind of bad input. Precision 15 needs up to 64 bits, more than a
14
- // Number holds exactly, so the slow functions switch to BigInt where Number
15
- // math would lose bits.
16
- //
17
- // Errors are PolylineError with one of these codes:
18
- // 'input' decode got a non-string, or encode got no array of points
19
- // 'option' precision or thirdDimPrecision not an integer from 0 to 15,
20
- // or thirdDim not 0, 1, 2, 3, 6 or 7
21
- // 'char' a character outside the alphabet (index = its position)
22
- // 'truncated' the string ends inside the header, a value or a point
23
- // 'version' the format version is not 1, or the header has bits set
24
- // above bit 10, which version 1 doesn't define
25
- // 'overflow' a value needs more than 64 bits, the limit of the other
26
- // implementations
27
- // 'coordinate' encode got a missing or non-finite value (index = point)
28
- /* @ts-self-types="../types/flexible.d.ts" */
29
- import { PolylineError, fail } from './error.js';
30
-
31
- export { PolylineError };
32
-
33
- // Third dimension types, as stored in the header. 4 and 5 are reserved:
34
- // decode accepts them and encode refuses them, like the official code.
35
- export const ABSENT = 0;
36
- export const LEVEL = 1;
37
- export const ALTITUDE = 2;
38
- export const ELEVATION = 3;
39
- export const RESERVED1 = 4;
40
- export const RESERVED2 = 5;
41
- export const CUSTOM1 = 6;
42
- export const CUSTOM2 = 7;
43
- const TYPES = [0, 1, 2, 3, 6, 7];
44
-
45
- const E = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
46
-
47
- // Character code to 6-bit value. 64 marks a character outside the alphabet,
48
- // so OR-ing every value together and testing for > 63 finds them all.
49
- const T = new Uint8Array(128).fill(64);
50
- for (let i = 0; i < 64; i++) T[E.charCodeAt(i)] = i;
51
-
52
- // The characters of every value below 1024, so one or two characters cost
53
- // one concatenation instead of a loop.
54
- const C = [];
55
- for (let z = 0; z < 1024; z++) C[z] = z < 32 ? E[z] : E[(z & 31) | 32] + E[z >> 5];
56
-
57
- // 10 ** p from parsed literals, so every factor is exact.
58
- const P = [];
59
- for (let p = 0; p < 16; p++) P[p] = +('1e' + p);
60
-
61
- // The characters of an unsigned value below 2^53. Float math, because the
62
- // bit operators would cut it to 32 bits.
63
- const chars = (z) => {
64
- let s = '';
65
- for (; z > 31; z = Math.floor(z / 32)) s += E[(z % 32) | 32];
66
- return s + E[z];
67
- };
68
-
69
- const range = (p, name) => (typeof p == 'number' && p >= 0 && p <= 15 && p % 1 == 0 ? p : fail('option', name + ' must be an integer from 0 to 15'));
70
-
71
- /**
72
- * Reads the version and the header. Returns the header bits plus 2048 times
73
- * the index where the data starts, so decode allocates nothing for it.
74
- */
75
- function head(str) {
76
- if (typeof str != 'string') fail('input', 'Expected a string');
77
- let i = 0, k = 0, v, m, c;
78
- for (; k < 2; k++) {
79
- // Both are varints. The version is always 'B' in practice, and the
80
- // header takes 1 to 3 characters.
81
- for (v = 0, m = 1, c = 32; c & 32; m *= 32, i++) {
82
- if (i >= str.length) fail('truncated', 'Polyline ends inside the header', i);
83
- c = T[str.charCodeAt(i)];
84
- if (!(c < 64)) fail('char', 'Invalid character ' + JSON.stringify(str[i]), i);
85
- v += (c & 31) * m;
86
- }
87
- if (!k && v != 1) fail('version', 'Unsupported format version ' + v, 0);
88
- }
89
- // Also false for NaN, from a long run of continuation characters.
90
- return v < 2048 ? v + i * 2048 : fail('version', 'Invalid header', i);
91
- }
92
-
93
- /** Reads the header without decoding the points. */
94
- export function header(str) {
95
- const h = head(str) % 2048;
96
- return { precision: h & 15, thirdDim: (h >> 4) & 7, thirdDimPrecision: h >> 7 };
97
- }
98
-
99
- /**
100
- * Decodes to { precision, thirdDim, thirdDimPrecision, polyline }, the same
101
- * object @here/flexpolyline returns. Points are [lat, lng], or [lat, lng, z]
102
- * when the header has a third dimension.
103
- */
104
- export function decode(str) {
105
- const r = head(str), h = r % 2048, i = (r - h) / 2048, t = (h >> 4) & 7, f = P[h & 15], fz = P[h >> 7];
106
- return { precision: h & 15, thirdDim: t, thirdDimPrecision: h >> 7, polyline: decodeLoop(str, i, f, fz, t) || slowDecode(str, i, f, fz, t ? 3 : 2) };
107
- }
108
-
109
- // The fast loop, or null when the input needs slowDecode: an invalid
110
- // character, a truncated string, or a value of more than 10 characters.
111
- // The value reader is written out three times: a shared helper or one
112
- // generic loop over the axes measured 15 to 25% slower on V8.
113
- function decodeLoop(str, i, f, fz, three) {
114
- const len = str.length, out = [];
115
- let n = 0, a = 0, b = 0, z = 0, bad = 0, w = 0, k, c, v, s;
116
- // A code above 127 keeps its high bits in c, which marks it bad without
117
- // changing the 6 value bits. Past the end, NaN reads as T[0], also bad,
118
- // and ends the value. The first 6 characters (30 bits) use 32-bit math,
119
- // later ones float math, exact up to 10 characters (50 bits). Float
120
- // values then need float zigzag, as the bit operators cut to 32 bits.
121
- // The sums stay exact below 2^53: w bounds what the large values add up
122
- // to, and small ones would need a string of over 64 million characters.
123
- while (i < len) {
124
- k = str.charCodeAt(i++);
125
- bad |= c = T[k & 127] | (k & -128);
126
- for (v = c & 31, s = 5; c & 32; s += 5) {
127
- k = str.charCodeAt(i++);
128
- bad |= c = T[k & 127] | (k & -128);
129
- if (s < 30) v |= (c & 31) << s;
130
- else if (s < 50) w += v += (c & 31) * 2 ** s;
131
- else bad = 64;
132
- }
133
- a += s > 30 ? (v % 2 ? -(v + 1) / 2 : v / 2) : (v >>> 1) ^ -(v & 1);
134
- k = str.charCodeAt(i++);
135
- bad |= c = T[k & 127] | (k & -128);
136
- for (v = c & 31, s = 5; c & 32; s += 5) {
137
- k = str.charCodeAt(i++);
138
- bad |= c = T[k & 127] | (k & -128);
139
- if (s < 30) v |= (c & 31) << s;
140
- else if (s < 50) w += v += (c & 31) * 2 ** s;
141
- else bad = 64;
142
- }
143
- b += s > 30 ? (v % 2 ? -(v + 1) / 2 : v / 2) : (v >>> 1) ^ -(v & 1);
144
- if (three) {
145
- k = str.charCodeAt(i++);
146
- bad |= c = T[k & 127] | (k & -128);
147
- for (v = c & 31, s = 5; c & 32; s += 5) {
148
- k = str.charCodeAt(i++);
149
- bad |= c = T[k & 127] | (k & -128);
150
- if (s < 30) v |= (c & 31) << s;
151
- else if (s < 50) w += v += (c & 31) * 2 ** s;
152
- else bad = 64;
153
- }
154
- z += s > 30 ? (v % 2 ? -(v + 1) / 2 : v / 2) : (v >>> 1) ^ -(v & 1);
155
- out[n++] = [a / f, b / f, z / fz];
156
- } else out[n++] = [a / f, b / f];
157
- }
158
- return bad > 63 || i > len || w > 2 ** 52 ? null : out;
159
- }
160
-
161
- /** Exact decoder with BigInt sums, which also reports errors. */
162
- function slowDecode(str, i, f, fz, d) {
163
- const out = [], sum = [0n, 0n, 0n];
164
- let pt = [], j = 0, v = 0n, s = 0n, k, c;
165
- for (; i < str.length; i++) {
166
- k = str.charCodeAt(i);
167
- c = k < 128 ? T[k] : 64;
168
- if (c > 63) fail('char', 'Invalid character ' + JSON.stringify(str[i]), i);
169
- v |= BigInt(c & 31) << s;
170
- if (c & 32) {
171
- if ((s += 5n) > 60n) fail('overflow', 'Value too large', i);
172
- continue;
173
- }
174
- if (v >> 64n) fail('overflow', 'Value too large', i);
175
- sum[j] += v & 1n ? ~(v >> 1n) : v >> 1n;
176
- pt[j] = Number(sum[j]) / (j < 2 ? f : fz);
177
- if (++j == d) {
178
- out.push(pt);
179
- pt = [];
180
- j = 0;
181
- }
182
- v = s = 0n;
183
- }
184
- if (s || j) fail('truncated', 'Polyline ends inside a ' + (s ? 'value' : 'point'), i);
185
- return out;
186
- }
187
-
188
- /**
189
- * Encodes points. Takes the @here/flexpolyline argument,
190
- * { polyline, precision, thirdDim, thirdDimPrecision }, or the points with
191
- * those fields in `options`, which win over the object's. Points are
192
- * [lat, lng] or [lat, lng, z] arrays, or one flat array of numbers. Rounds
193
- * half away from zero.
194
- */
195
- export function encode(input, options) {
196
- const obj = input != null && typeof input == 'object' && !(input.length >= 0), a = obj ? input : {}, o = options || {};
197
- const pts = obj ? input.polyline : input;
198
- const p = range(o.precision ?? a.precision ?? 5, 'precision');
199
- const q = range(o.thirdDimPrecision ?? a.thirdDimPrecision ?? 0, 'thirdDimPrecision');
200
- const t = o.thirdDim ?? a.thirdDim ?? 0;
201
- if (!TYPES.includes(t)) fail('option', 'thirdDim must be 0, 1, 2, 3, 6 or 7');
202
- if (pts == null || !(pts.length >= 0)) fail('input', 'Expected an array of points');
203
- const s = 'B' + chars((q << 7) | (t << 4) | p), d = t ? 3 : 2, flat = typeof pts[0] == 'number';
204
- if (flat && pts.length % d) fail('input', 'Flat array length is not a multiple of ' + d);
205
- return (!flat && encodeLoop(pts, P[p], P[q], t, s)) || slowEncode(pts, P[p], P[q], d, flat, s);
206
- }
207
-
208
- // Scaled values below this keep deltas and zigzag values in 32-bit math.
209
- // It covers coordinates up to precision 6.
210
- const LIMIT = 2 ** 29;
211
-
212
- // Rounded values below this keep deltas and zigzag values below 2^53.
213
- const WIDE = 2 ** 51;
214
-
215
- // The fast loop for arrays of points, or undefined when the input needs
216
- // slowEncode: a value that is missing, not finite or too large.
217
- function encodeLoop(pts, f, fz, three, s) {
218
- const n = pts.length;
219
- let i = 0, la = 0, lb = 0, lz = 0, x, y, w, p, q, r, z, pt;
220
- try {
221
- for (; i < n; i++) {
222
- pt = pts[i];
223
- x = pt[0] * f;
224
- y = pt[1] * f;
225
- w = three ? pt[2] * fz : 0;
226
- // Also false for NaN, so missing values go the slow way.
227
- if (!(x < LIMIT && x > -LIMIT && y < LIMIT && y > -LIMIT && w < LIMIT && w > -LIMIT)) return;
228
- // Round half away from zero. Below LIMIT, adding 0.5 and truncating
229
- // gives the exact result.
230
- p = x < 0 ? -((0.5 - x) | 0) : (x + 0.5) | 0;
231
- q = y < 0 ? -((0.5 - y) | 0) : (y + 0.5) | 0;
232
- z = p - la;
233
- z = (z << 1) ^ (z >> 31);
234
- s += z < 1024 ? C[z] : chars(z);
235
- z = q - lb;
236
- z = (z << 1) ^ (z >> 31);
237
- s += z < 1024 ? C[z] : chars(z);
238
- la = p;
239
- lb = q;
240
- if (three) {
241
- r = w < 0 ? -((0.5 - w) | 0) : (w + 0.5) | 0;
242
- z = r - lz;
243
- z = (z << 1) ^ (z >> 31);
244
- s += z < 1024 ? C[z] : chars(z);
245
- lz = r;
246
- }
247
- }
248
- } catch {
249
- return;
250
- }
251
- return s;
252
- }
253
-
254
- /**
255
- * Exact encoder for any input. Rounded values are whole doubles, exact at
256
- * any size, so they stay Numbers. Their deltas and zigzag values use float
257
- * math below WIDE, where it is exact, and BigInt above.
258
- */
259
- function slowEncode(pts, f, fz, d, flat, s) {
260
- const n = flat ? pts.length / d : pts.length, last = [0, 0, 0];
261
- for (let i = 0; i < n; i++) {
262
- const pt = flat ? null : pts[i];
263
- if (!flat && pt == null) fail('coordinate', 'Invalid point', i);
264
- for (let a = 0; a < d; a++) {
265
- const x = (flat ? pts[i * d + a] : pt[a]) * (a < 2 ? f : fz);
266
- if (!(x - x == 0)) fail('coordinate', 'Invalid coordinate', i);
267
- // Math.round of the magnitude is exact at any size. Adding 0.5 and
268
- // flooring would round 2^52 + 1 up to 2^52 + 2.
269
- const r = x < 0 ? -Math.round(-x) : Math.round(x), l = last[a];
270
- let z = r - l;
271
- last[a] = r;
272
- if (r < WIDE && r > -WIDE && l < WIDE && l > -WIDE) s += chars(z < 0 ? -2 * z - 1 : 2 * z);
273
- else {
274
- z = BigInt(r) - BigInt(l);
275
- z = z < 0n ? ~(z << 1n) : z << 1n;
276
- if (z >> 64n) fail('overflow', 'Value too large', i);
277
- for (; z > 31n; z >>= 5n) s += E[Number(z & 31n) | 32];
278
- s += E[Number(z)];
279
- }
280
- }
281
- }
282
- return s;
283
- }
1
+ // HERE's Flexible Polyline format, version 1:
2
+ // https://github.com/heremaps/flexible-polyline
3
+ //
4
+ // A string is [version][header][data], each part made of varints written
5
+ // with a URL-safe 64 character alphabet: 5 data bits per character, and a
6
+ // sixth bit that says another character follows. The header holds the
7
+ // precision (bits 0 to 3), the third dimension type (bits 4 to 6) and its
8
+ // precision (bits 7 to 10). The data is zigzag encoded deltas, 2 or 3 values
9
+ // per point, the first point relative to 0.
10
+ //
11
+ // Like the Google-format core, decode and encode have a fast loop for the
12
+ // common case and an exact `slow*` function for the rest, which also reports
13
+ // every kind of bad input. Precision 15 needs up to 64 bits, more than a
14
+ // Number holds exactly, so the slow functions switch to BigInt where Number
15
+ // math would lose bits.
16
+ //
17
+ // Errors are PolylineError with one of these codes:
18
+ // 'input' decode got a non-string, or encode got no array of points
19
+ // 'option' precision or thirdDimPrecision not an integer from 0 to 15,
20
+ // or thirdDim not 0, 1, 2, 3, 6 or 7
21
+ // 'char' a character outside the alphabet (index = its position)
22
+ // 'truncated' the string ends inside the header, a value or a point
23
+ // 'version' the format version is not 1, or the header has bits set
24
+ // above bit 10, which version 1 doesn't define
25
+ // 'overflow' a value needs more than 64 bits, the limit of the other
26
+ // implementations
27
+ // 'coordinate' encode got a missing or non-finite value (index = point)
28
+ /* @ts-self-types="../types/flexible.d.ts" */
29
+ import { PolylineError, fail } from './error.js';
30
+
31
+ export { PolylineError };
32
+
33
+ // Third dimension types, as stored in the header. 4 and 5 are reserved:
34
+ // decode accepts them and encode refuses them, like the official code.
35
+ export const ABSENT = 0;
36
+ export const LEVEL = 1;
37
+ export const ALTITUDE = 2;
38
+ export const ELEVATION = 3;
39
+ export const RESERVED1 = 4;
40
+ export const RESERVED2 = 5;
41
+ export const CUSTOM1 = 6;
42
+ export const CUSTOM2 = 7;
43
+ const TYPES = [0, 1, 2, 3, 6, 7];
44
+
45
+ const E = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
46
+
47
+ // Character code to 6-bit value. 64 marks a character outside the alphabet,
48
+ // so OR-ing every value together and testing for > 63 finds them all.
49
+ const T = new Uint8Array(128).fill(64);
50
+ for (let i = 0; i < 64; i++) T[E.charCodeAt(i)] = i;
51
+
52
+ // The characters of every value below 1024, so one or two characters cost
53
+ // one concatenation instead of a loop.
54
+ const C = [];
55
+ for (let z = 0; z < 1024; z++) C[z] = z < 32 ? E[z] : E[(z & 31) | 32] + E[z >> 5];
56
+
57
+ // 10 ** p from parsed literals, so every factor is exact.
58
+ const P = [];
59
+ for (let p = 0; p < 16; p++) P[p] = +('1e' + p);
60
+
61
+ // The characters of an unsigned value below 2^53. Float math, because the
62
+ // bit operators would cut it to 32 bits.
63
+ const chars = (z) => {
64
+ let s = '';
65
+ for (; z > 31; z = Math.floor(z / 32)) s += E[(z % 32) | 32];
66
+ return s + E[z];
67
+ };
68
+
69
+ const range = (p, name) => (typeof p == 'number' && p >= 0 && p <= 15 && p % 1 == 0 ? p : fail('option', name + ' must be an integer from 0 to 15'));
70
+
71
+ /**
72
+ * Reads the version and the header. Returns the header bits plus 2048 times
73
+ * the index where the data starts, so decode allocates nothing for it.
74
+ */
75
+ function head(str) {
76
+ if (typeof str != 'string') fail('input', 'Expected a string');
77
+ let i = 0, k = 0, v, m, c;
78
+ for (; k < 2; k++) {
79
+ // Both are varints. The version is always 'B' in practice, and the
80
+ // header takes 1 to 3 characters.
81
+ for (v = 0, m = 1, c = 32; c & 32; m *= 32, i++) {
82
+ if (i >= str.length) fail('truncated', 'Polyline ends inside the header', i);
83
+ c = T[str.charCodeAt(i)];
84
+ if (!(c < 64)) fail('char', 'Invalid character ' + JSON.stringify(str[i]), i);
85
+ v += (c & 31) * m;
86
+ }
87
+ if (!k && v != 1) fail('version', 'Unsupported format version ' + v, 0);
88
+ }
89
+ // Also false for NaN, from a long run of continuation characters.
90
+ return v < 2048 ? v + i * 2048 : fail('version', 'Invalid header', i);
91
+ }
92
+
93
+ /** Reads the header without decoding the points. */
94
+ export function header(str) {
95
+ const h = head(str) % 2048;
96
+ return { precision: h & 15, thirdDim: (h >> 4) & 7, thirdDimPrecision: h >> 7 };
97
+ }
98
+
99
+ /**
100
+ * Decodes to { precision, thirdDim, thirdDimPrecision, polyline }, the same
101
+ * object @here/flexpolyline returns. Points are [lat, lng], or [lat, lng, z]
102
+ * when the header has a third dimension.
103
+ */
104
+ export function decode(str) {
105
+ const r = head(str), h = r % 2048, i = (r - h) / 2048, t = (h >> 4) & 7, f = P[h & 15], fz = P[h >> 7];
106
+ return { precision: h & 15, thirdDim: t, thirdDimPrecision: h >> 7, polyline: decodeLoop(str, i, f, fz, t) || slowDecode(str, i, f, fz, t ? 3 : 2) };
107
+ }
108
+
109
+ // The fast loop, or null when the input needs slowDecode: an invalid
110
+ // character, a truncated string, or a value of more than 10 characters.
111
+ // The value reader is written out three times: a shared helper or one
112
+ // generic loop over the axes measured 15 to 25% slower on V8.
113
+ function decodeLoop(str, i, f, fz, three) {
114
+ const len = str.length, out = [];
115
+ let n = 0, a = 0, b = 0, z = 0, bad = 0, w = 0, k, c, v, s;
116
+ // A code above 127 keeps its high bits in c, which marks it bad without
117
+ // changing the 6 value bits. Past the end, NaN reads as T[0], also bad,
118
+ // and ends the value. The first 6 characters (30 bits) use 32-bit math,
119
+ // later ones float math, exact up to 10 characters (50 bits). Float
120
+ // values then need float zigzag, as the bit operators cut to 32 bits.
121
+ // The sums stay exact below 2^53: w bounds what the large values add up
122
+ // to, and small ones would need a string of over 64 million characters.
123
+ while (i < len) {
124
+ k = str.charCodeAt(i++);
125
+ bad |= c = T[k & 127] | (k & -128);
126
+ for (v = c & 31, s = 5; c & 32; s += 5) {
127
+ k = str.charCodeAt(i++);
128
+ bad |= c = T[k & 127] | (k & -128);
129
+ if (s < 30) v |= (c & 31) << s;
130
+ else if (s < 50) w += v += (c & 31) * 2 ** s;
131
+ else bad = 64;
132
+ }
133
+ a += s > 30 ? (v % 2 ? -(v + 1) / 2 : v / 2) : (v >>> 1) ^ -(v & 1);
134
+ k = str.charCodeAt(i++);
135
+ bad |= c = T[k & 127] | (k & -128);
136
+ for (v = c & 31, s = 5; c & 32; s += 5) {
137
+ k = str.charCodeAt(i++);
138
+ bad |= c = T[k & 127] | (k & -128);
139
+ if (s < 30) v |= (c & 31) << s;
140
+ else if (s < 50) w += v += (c & 31) * 2 ** s;
141
+ else bad = 64;
142
+ }
143
+ b += s > 30 ? (v % 2 ? -(v + 1) / 2 : v / 2) : (v >>> 1) ^ -(v & 1);
144
+ if (three) {
145
+ k = str.charCodeAt(i++);
146
+ bad |= c = T[k & 127] | (k & -128);
147
+ for (v = c & 31, s = 5; c & 32; s += 5) {
148
+ k = str.charCodeAt(i++);
149
+ bad |= c = T[k & 127] | (k & -128);
150
+ if (s < 30) v |= (c & 31) << s;
151
+ else if (s < 50) w += v += (c & 31) * 2 ** s;
152
+ else bad = 64;
153
+ }
154
+ z += s > 30 ? (v % 2 ? -(v + 1) / 2 : v / 2) : (v >>> 1) ^ -(v & 1);
155
+ out[n++] = [a / f, b / f, z / fz];
156
+ } else out[n++] = [a / f, b / f];
157
+ }
158
+ return bad > 63 || i > len || w > 2 ** 52 ? null : out;
159
+ }
160
+
161
+ /** Exact decoder with BigInt sums, which also reports errors. */
162
+ function slowDecode(str, i, f, fz, d) {
163
+ const out = [], sum = [0n, 0n, 0n];
164
+ let pt = [], j = 0, v = 0n, s = 0n, k, c;
165
+ for (; i < str.length; i++) {
166
+ k = str.charCodeAt(i);
167
+ c = k < 128 ? T[k] : 64;
168
+ if (c > 63) fail('char', 'Invalid character ' + JSON.stringify(str[i]), i);
169
+ v |= BigInt(c & 31) << s;
170
+ if (c & 32) {
171
+ if ((s += 5n) > 60n) fail('overflow', 'Value too large', i);
172
+ continue;
173
+ }
174
+ if (v >> 64n) fail('overflow', 'Value too large', i);
175
+ sum[j] += v & 1n ? ~(v >> 1n) : v >> 1n;
176
+ pt[j] = Number(sum[j]) / (j < 2 ? f : fz);
177
+ if (++j == d) {
178
+ out.push(pt);
179
+ pt = [];
180
+ j = 0;
181
+ }
182
+ v = s = 0n;
183
+ }
184
+ if (s || j) fail('truncated', 'Polyline ends inside a ' + (s ? 'value' : 'point'), i);
185
+ return out;
186
+ }
187
+
188
+ /**
189
+ * Encodes points. Takes the @here/flexpolyline argument,
190
+ * { polyline, precision, thirdDim, thirdDimPrecision }, or the points with
191
+ * those fields in `options`, which win over the object's. Points are
192
+ * [lat, lng] or [lat, lng, z] arrays, or one flat array of numbers. Rounds
193
+ * half away from zero.
194
+ */
195
+ export function encode(input, options) {
196
+ const obj = input != null && typeof input == 'object' && !(input.length >= 0), a = obj ? input : {}, o = options || {};
197
+ const pts = obj ? input.polyline : input;
198
+ const p = range(o.precision ?? a.precision ?? 5, 'precision');
199
+ const q = range(o.thirdDimPrecision ?? a.thirdDimPrecision ?? 0, 'thirdDimPrecision');
200
+ const t = o.thirdDim ?? a.thirdDim ?? 0;
201
+ if (!TYPES.includes(t)) fail('option', 'thirdDim must be 0, 1, 2, 3, 6 or 7');
202
+ if (pts == null || !(pts.length >= 0)) fail('input', 'Expected an array of points');
203
+ const s = 'B' + chars((q << 7) | (t << 4) | p), d = t ? 3 : 2, flat = typeof pts[0] == 'number';
204
+ if (flat && pts.length % d) fail('input', 'Flat array length is not a multiple of ' + d);
205
+ return (!flat && encodeLoop(pts, P[p], P[q], t, s)) || slowEncode(pts, P[p], P[q], d, flat, s);
206
+ }
207
+
208
+ // Scaled values below this keep deltas and zigzag values in 32-bit math.
209
+ // It covers coordinates up to precision 6.
210
+ const LIMIT = 2 ** 29;
211
+
212
+ // Rounded values below this keep deltas and zigzag values below 2^53.
213
+ const WIDE = 2 ** 51;
214
+
215
+ // The fast loop for arrays of points, or undefined when the input needs
216
+ // slowEncode: a value that is missing, not finite or too large.
217
+ function encodeLoop(pts, f, fz, three, s) {
218
+ const n = pts.length;
219
+ let i = 0, la = 0, lb = 0, lz = 0, x, y, w, p, q, r, z, pt;
220
+ try {
221
+ for (; i < n; i++) {
222
+ pt = pts[i];
223
+ x = pt[0] * f;
224
+ y = pt[1] * f;
225
+ w = three ? pt[2] * fz : 0;
226
+ // Also false for NaN, so missing values go the slow way.
227
+ if (!(x < LIMIT && x > -LIMIT && y < LIMIT && y > -LIMIT && w < LIMIT && w > -LIMIT)) return;
228
+ // Round half away from zero. Below LIMIT, adding 0.5 and truncating
229
+ // gives the exact result.
230
+ p = x < 0 ? -((0.5 - x) | 0) : (x + 0.5) | 0;
231
+ q = y < 0 ? -((0.5 - y) | 0) : (y + 0.5) | 0;
232
+ z = p - la;
233
+ z = (z << 1) ^ (z >> 31);
234
+ s += z < 1024 ? C[z] : chars(z);
235
+ z = q - lb;
236
+ z = (z << 1) ^ (z >> 31);
237
+ s += z < 1024 ? C[z] : chars(z);
238
+ la = p;
239
+ lb = q;
240
+ if (three) {
241
+ r = w < 0 ? -((0.5 - w) | 0) : (w + 0.5) | 0;
242
+ z = r - lz;
243
+ z = (z << 1) ^ (z >> 31);
244
+ s += z < 1024 ? C[z] : chars(z);
245
+ lz = r;
246
+ }
247
+ }
248
+ } catch {
249
+ return;
250
+ }
251
+ return s;
252
+ }
253
+
254
+ /**
255
+ * Exact encoder for any input. Rounded values are whole doubles, exact at
256
+ * any size, so they stay Numbers. Their deltas and zigzag values use float
257
+ * math below WIDE, where it is exact, and BigInt above.
258
+ */
259
+ function slowEncode(pts, f, fz, d, flat, s) {
260
+ const n = flat ? pts.length / d : pts.length, last = [0, 0, 0];
261
+ for (let i = 0; i < n; i++) {
262
+ const pt = flat ? null : pts[i];
263
+ if (!flat && pt == null) fail('coordinate', 'Invalid point', i);
264
+ for (let a = 0; a < d; a++) {
265
+ const x = (flat ? pts[i * d + a] : pt[a]) * (a < 2 ? f : fz);
266
+ if (!(x - x == 0)) fail('coordinate', 'Invalid coordinate', i);
267
+ // Math.round of the magnitude is exact at any size. Adding 0.5 and
268
+ // flooring would round 2^52 + 1 up to 2^52 + 2.
269
+ const r = x < 0 ? -Math.round(-x) : Math.round(x), l = last[a];
270
+ let z = r - l;
271
+ last[a] = r;
272
+ if (r < WIDE && r > -WIDE && l < WIDE && l > -WIDE) s += chars(z < 0 ? -2 * z - 1 : 2 * z);
273
+ else {
274
+ z = BigInt(r) - BigInt(l);
275
+ z = z < 0n ? ~(z << 1n) : z << 1n;
276
+ if (z >> 64n) fail('overflow', 'Value too large', i);
277
+ for (; z > 31n; z >>= 5n) s += E[Number(z & 31n) | 32];
278
+ s += E[Number(z)];
279
+ }
280
+ }
281
+ }
282
+ return s;
283
+ }