@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/core.js CHANGED
@@ -1,268 +1,268 @@
1
- // The portable codec. Every entry point builds on this file, and the web
2
- // entry is only this file. It uses no Node, Bun or Deno globals.
3
- //
4
- // Each public function has a fast loop for the common case (2D points,
5
- // precision up to 6, valid input) and falls back to the exact `slow*`
6
- // functions for anything else: other dimensions, huge values, objects,
7
- // and every kind of bad input. The slow functions do the same Number math
8
- // as Google's algorithm description, so both paths give identical output.
9
-
10
- import { PolylineError, fail } from './error.js';
11
-
12
- export { PolylineError, fail };
13
-
14
- // Options of the current call, set by opts() and read right after it, so the
15
- // hot paths never allocate. A plain object on purpose: writing exported
16
- // module-level `let` bindings cost about 30 ns per call on V8, this costs
17
- // nothing measurable.
18
- export const st = { swap: false, strict: true, dims: 2, zf: 1e5 };
19
-
20
- /**
21
- * Parses options, fills `st` and returns the factor. Kept tiny so V8 inlines
22
- * it into every caller; with the object branch in the same function it
23
- * wasn't inlined, and decoding 10 points got about 25% slower.
24
- */
25
- export function opts(o) {
26
- st.swap = false;
27
- st.strict = true;
28
- st.dims = 2;
29
- return o && typeof o == 'object' ? objOpts(o) : (st.zf = o == null ? 1e5 : pow(o));
30
- }
31
-
32
- function objOpts(o) {
33
- const { order, dimensions: d } = o;
34
- if (order == 'lnglat') st.swap = true;
35
- else if (order != null && order != 'latlng') bad('order');
36
- if (o.strict === false) st.strict = false;
37
- if (d != null) st.dims = d >= 1 && d <= 16 && d % 1 == 0 ? d : bad('dimensions');
38
- const f = o.factor != null ? num(o.factor) : pow(o.precision ?? 5);
39
- st.zf = o.zFactor != null ? num(o.zFactor) : f;
40
- return f;
41
- }
42
-
43
- const bad = (name) => fail('option', 'Invalid ' + name);
44
- const num = (f) => (f > 0 && f < 1 / 0 ? f : bad('factor'));
45
- const pow = (p) => (typeof p == 'number' && p >= 0 && p <= 15 && p % 1 == 0 ? 10 ** p : bad('precision'));
46
-
47
- // Scaled values below this fit the 32-bit math of the fast loops, deltas and
48
- // zigzag included. It covers every valid coordinate up to precision 6.
49
- export const LIMIT = 2 ** 29;
50
-
51
- /**
52
- * Exact decoder for any input. Returns points, or a flat Float64Array.
53
- * Strict mode throws on invalid characters, on a string that ends inside a
54
- * value or a point, and on values past 2^53. Otherwise it skips invalid
55
- * characters and drops an incomplete last point.
56
- */
57
- export function slowDecode(str, f, flat) {
58
- if (typeof str != 'string') fail('input', 'Expected a string');
59
- const { dims: d, strict, zf } = st, sw = st.swap && d > 1, values = [];
60
- let v = 0, scale = 1, i = 0, c;
61
- for (; i < str.length; i++) {
62
- c = str.charCodeAt(i) - 63;
63
- if (c < 0 || c > 63) {
64
- if (strict) fail('char', 'Invalid character', i);
65
- continue;
66
- }
67
- v += (c & 31) * scale;
68
- scale *= 32;
69
- if (v > 2 ** 53 && strict) fail('overflow', 'Value too large', i);
70
- if (c < 32) {
71
- values.push(v % 2 ? -(v + 1) / 2 : v / 2);
72
- v = 0;
73
- scale = 1;
74
- }
75
- }
76
- if (strict && (scale > 1 || values.length % d)) fail('truncated', 'Truncated', i);
77
- const n = (values.length / d) | 0, sum = Array(d).fill(0), out = flat ? new Float64Array(n * d) : Array(n);
78
- for (let j = 0, k = 0; j < n; j++) {
79
- const p = flat ? out : Array(d);
80
- for (let a = 0; a < d; a++, k++) {
81
- sum[a] += values[k];
82
- p[(flat ? k - a : 0) + (sw && a < 2 ? 1 - a : a)] = sum[a] / (a < 2 ? f : zf);
83
- }
84
- if (!flat) out[j] = p;
85
- }
86
- return out;
87
- }
88
-
89
- /**
90
- * Exact encoder for any input: [a, b] tuples (or any array-like), objects
91
- * with lat/latitude and lng/lon/longitude, or a flat array of numbers.
92
- * Rounds half away from zero, like Google's reference implementation.
93
- */
94
- export function slowEncode(points, f, flat) {
95
- if (points == null || !(points.length >= 0)) fail('input', 'Expected an array');
96
- const { dims: d, strict, zf } = st, sw = st.swap && d > 1, last = Array(d).fill(0), n = flat ? points.length / d : points.length;
97
- if (n % 1 && strict) fail('input', 'Length not a multiple of ' + d);
98
- let s = '';
99
- for (let i = 0; i < (n | 0); i++) {
100
- const p = flat ? 0 : points[i];
101
- for (let a = 0; a < d; a++) {
102
- const b = sw && a < 2 ? 1 - a : a;
103
- let x = flat ? points[i * d + b] : p == null ? p : p.length >= 0 ? p[b] : a ? (a > 1 ? p.alt ?? p.altitude : p.lng ?? p.lon ?? p.longitude) : p.lat ?? p.latitude;
104
- // null and undefined are missing, not 0. Numeric strings are numbers.
105
- x = x == null ? NaN : +x * (a < 2 ? f : zf);
106
- if (!(x - x == 0)) {
107
- if (strict) fail('coordinate', 'Invalid coordinate', i);
108
- x = 0;
109
- }
110
- x = Math.sign(x) * Math.round(Math.abs(x));
111
- let z = x - last[a];
112
- last[a] = x;
113
- z = z < 0 ? -z * 2 - 1 : z * 2;
114
- if (z > 2 ** 53 && strict) fail('overflow', 'Value too large', i);
115
- for (; z >= 32; z = Math.floor(z / 32)) s += String.fromCharCode((z % 32) + 95);
116
- s += String.fromCharCode(z + 63);
117
- }
118
- }
119
- return s;
120
- }
121
-
122
- const decodeWith = (str, o, flat) => {
123
- const f = opts(o), out = st.dims == 2 && typeof str == 'string' && decodeLoop(str, f, st.swap, flat);
124
- return out ? (st.swap && !flat ? flip(out) : out) : slowDecode(str, f, flat);
125
- };
126
-
127
- // The fast loops always build [lat, lng]; lnglat output is flipped in place
128
- // afterwards. A per-point `swap ? [b, a] : [a, b]` cost the default order
129
- // about 5%, and the extra pass is small next to allocating the points.
130
- export const flip = (out) => {
131
- for (const p of out) {
132
- const t = p[0];
133
- p[0] = p[1];
134
- p[1] = t;
135
- }
136
- return out;
137
- };
138
-
139
- /** Decodes to [lat, lng] tuples. */
140
- export const decode = (str, o) => decodeWith(str, o, 0);
141
-
142
- /** Decodes to a flat Float64Array: [lat0, lng0, lat1, lng1, ...]. */
143
- export const decodeFlat = (str, o) => decodeWith(str, o, 1);
144
-
145
- // The 2D fast loop, or null when the input needs slowDecode. A separate,
146
- // self-contained function because V8 optimized it better that way: with the
147
- // option and type checks in the same function, 10 point lines decoded about
148
- // 15% slower.
149
- export function decodeLoop(str, f, sw, flat) {
150
- const len = str.length, a = +sw;
151
- let i = 0, n = 0, lat = 0, lng = 0, bad = 0, c, v, s, out;
152
- if (flat) {
153
- // Every value ends with exactly one character below code 95.
154
- for (; i < len; i++) n += (str.charCodeAt(i) - 95) >>> 31;
155
- out = new Float64Array(n & ~1);
156
- i = n = 0;
157
- } else out = len < 64 ? [] : Array(len >> 1);
158
- // One value is one or more 5-bit chunks. `bad` collects every character
159
- // code: anything outside 0..63 after the -63 shift sets a high bit, and
160
- // so does a 7th chunk in one value (25 - s < 0). NaN past the end of
161
- // a truncated string ends the value and leaves i past len.
162
- while (i < len) {
163
- c = str.charCodeAt(i++) - 63;
164
- bad |= c;
165
- v = c & 31;
166
- for (s = 5; c >= 32; s += 5) {
167
- c = str.charCodeAt(i++) - 63;
168
- bad |= c | (25 - s);
169
- v |= (c & 31) << s;
170
- }
171
- lat += (v >>> 1) ^ -(v & 1);
172
- c = str.charCodeAt(i++) - 63;
173
- bad |= c;
174
- v = c & 31;
175
- for (s = 5; c >= 32; s += 5) {
176
- c = str.charCodeAt(i++) - 63;
177
- bad |= c | (25 - s);
178
- v |= (c & 31) << s;
179
- }
180
- lng += (v >>> 1) ^ -(v & 1);
181
- if (flat) {
182
- out[n + a] = lat / f;
183
- out[n + 1 - a] = lng / f;
184
- n += 2;
185
- } else out[n++] = [lat / f, lng / f];
186
- }
187
- if (bad & ~63 || i != len) return null;
188
- if (out.length != n) out.length = n;
189
- return out;
190
- }
191
-
192
- // Encoded characters for every zigzag value below 1024, so one or two
193
- // characters cost one string concatenation instead of a loop. Built by a
194
- // pure call, so bundles that never encode can drop it.
195
- const CHARS = /* @__PURE__ */ Array.from({ length: 1024 }, (_, z) => chars(z));
196
-
197
- export function chars(z) {
198
- let s = '';
199
- for (; z >= 32; z >>>= 5) s += String.fromCharCode((z & 31) + 95);
200
- return s + String.fromCharCode(z + 63);
201
- }
202
-
203
- const encodeWith = (src, o, flat) => {
204
- const f = opts(o);
205
- return (st.dims == 2 && f <= 1e6 && (flat ? src != null && !(src.length % 2) : Array.isArray(src)) ? encodeLoop(src, f, +st.swap, flat) : null) ?? slowEncode(src, f, flat);
206
- };
207
-
208
- /** Encodes [lat, lng] tuples, or objects with lat and lng. */
209
- export const encode = (points, o) => encodeWith(points, o, 0);
210
-
211
- /** Encodes a flat array of numbers: [lat0, lng0, lat1, lng1, ...]. */
212
- export const encodeFlat = (values, o) => encodeWith(values, o, 1);
213
-
214
- // The 2D fast loop, or null when the input needs slowEncode.
215
- function encodeLoop(src, f, a, flat) {
216
- const n = flat ? src.length >> 1 : src.length;
217
- let s = '', i = 0, la = 0, lb = 0, x, y, p, q, z, pt;
218
- try {
219
- for (; i < n; i++) {
220
- if (flat) {
221
- x = src[i * 2 + a];
222
- y = src[i * 2 + 1 - a];
223
- } else {
224
- pt = src[i];
225
- x = pt[a];
226
- y = pt[1 - a];
227
- }
228
- // Anything but numbers goes the slow way, which coerces numeric
229
- // strings and rejects null. The range check is also false for NaN.
230
- if (typeof x != 'number' || typeof y != 'number' || !((x *= f) < LIMIT && x > -LIMIT && (y *= f) < LIMIT && y > -LIMIT)) return null;
231
- // Round half away from zero. floor(|x| + 0.5) is exact here except for
232
- // |x| = 0.49999999999999994, where the addition rounds up to 1, so any
233
- // |x| < 0.5 is set to 0 afterwards. Math.round would be exact on its own,
234
- // but it measured 40% slower on JavaScriptCore.
235
- p = x < 0 ? -((0.5 - x) | 0) : (x + 0.5) | 0;
236
- q = y < 0 ? -((0.5 - y) | 0) : (y + 0.5) | 0;
237
- if (x < 0.5 && x > -0.5) p = 0;
238
- if (y < 0.5 && y > -0.5) q = 0;
239
- z = p - la;
240
- z = (z << 1) ^ (z >> 31);
241
- s += z < 1024 ? CHARS[z] : chars(z);
242
- z = q - lb;
243
- z = (z << 1) ^ (z >> 31);
244
- s += z < 1024 ? CHARS[z] : chars(z);
245
- la = p;
246
- lb = q;
247
- }
248
- } catch {
249
- // A null point: let slowEncode report it.
250
- return null;
251
- }
252
- return s;
253
- }
254
-
255
- // Helpers built on top of an entry point's own decode and encode, so every
256
- // entry uses its fastest paths. Entries call these with /* @__PURE__ */, so
257
- // bundlers drop the ones nobody imports.
258
- const lngLat = (o) => ({ ...(typeof o == 'number' ? { precision: o } : o), order: 'lnglat' });
259
-
260
- /** {lat, lng} objects, the shape Google Maps and Leaflet take. */
261
- export const latLngs = (decode) => (str, o) => decode(str, o).map(([lat, lng]) => ({ lat, lng }));
262
-
263
- export const toGeo = (decode) => (str, o) => ({ type: 'LineString', coordinates: decode(str, lngLat(o)) });
264
-
265
- export const fromGeo = (encode) => (g, o) => {
266
- if (g?.type == 'Feature') g = g.geometry;
267
- return g?.type == 'LineString' ? encode(g.coordinates, lngLat(o)) : fail('input', 'Expected a LineString');
268
- };
1
+ // The portable codec. Every entry point builds on this file, and the web
2
+ // entry is only this file. It uses no Node, Bun or Deno globals.
3
+ //
4
+ // Each public function has a fast loop for the common case (2D points,
5
+ // precision up to 6, valid input) and falls back to the exact `slow*`
6
+ // functions for anything else: other dimensions, huge values, objects,
7
+ // and every kind of bad input. The slow functions do the same Number math
8
+ // as Google's algorithm description, so both paths give identical output.
9
+
10
+ import { PolylineError, fail } from './error.js';
11
+
12
+ export { PolylineError, fail };
13
+
14
+ // Options of the current call, set by opts() and read right after it, so the
15
+ // hot paths never allocate. A plain object on purpose: writing exported
16
+ // module-level `let` bindings cost about 30 ns per call on V8, this costs
17
+ // nothing measurable.
18
+ export const st = { swap: false, strict: true, dims: 2, zf: 1e5 };
19
+
20
+ /**
21
+ * Parses options, fills `st` and returns the factor. Kept tiny so V8 inlines
22
+ * it into every caller; with the object branch in the same function it
23
+ * wasn't inlined, and decoding 10 points got about 25% slower.
24
+ */
25
+ export function opts(o) {
26
+ st.swap = false;
27
+ st.strict = true;
28
+ st.dims = 2;
29
+ return o && typeof o == 'object' ? objOpts(o) : (st.zf = o == null ? 1e5 : pow(o));
30
+ }
31
+
32
+ function objOpts(o) {
33
+ const { order, dimensions: d } = o;
34
+ if (order == 'lnglat') st.swap = true;
35
+ else if (order != null && order != 'latlng') bad('order');
36
+ if (o.strict === false) st.strict = false;
37
+ if (d != null) st.dims = d >= 1 && d <= 16 && d % 1 == 0 ? d : bad('dimensions');
38
+ const f = o.factor != null ? num(o.factor) : pow(o.precision ?? 5);
39
+ st.zf = o.zFactor != null ? num(o.zFactor) : f;
40
+ return f;
41
+ }
42
+
43
+ const bad = (name) => fail('option', 'Invalid ' + name);
44
+ const num = (f) => (f > 0 && f < 1 / 0 ? f : bad('factor'));
45
+ const pow = (p) => (typeof p == 'number' && p >= 0 && p <= 15 && p % 1 == 0 ? 10 ** p : bad('precision'));
46
+
47
+ // Scaled values below this fit the 32-bit math of the fast loops, deltas and
48
+ // zigzag included. It covers every valid coordinate up to precision 6.
49
+ export const LIMIT = 2 ** 29;
50
+
51
+ /**
52
+ * Exact decoder for any input. Returns points, or a flat Float64Array.
53
+ * Strict mode throws on invalid characters, on a string that ends inside a
54
+ * value or a point, and on values past 2^53. Otherwise it skips invalid
55
+ * characters and drops an incomplete last point.
56
+ */
57
+ export function slowDecode(str, f, flat) {
58
+ if (typeof str != 'string') fail('input', 'Expected a string');
59
+ const { dims: d, strict, zf } = st, sw = st.swap && d > 1, values = [];
60
+ let v = 0, scale = 1, i = 0, c;
61
+ for (; i < str.length; i++) {
62
+ c = str.charCodeAt(i) - 63;
63
+ if (c < 0 || c > 63) {
64
+ if (strict) fail('char', 'Invalid character', i);
65
+ continue;
66
+ }
67
+ v += (c & 31) * scale;
68
+ scale *= 32;
69
+ if (v > 2 ** 53 && strict) fail('overflow', 'Value too large', i);
70
+ if (c < 32) {
71
+ values.push(v % 2 ? -(v + 1) / 2 : v / 2);
72
+ v = 0;
73
+ scale = 1;
74
+ }
75
+ }
76
+ if (strict && (scale > 1 || values.length % d)) fail('truncated', 'Truncated', i);
77
+ const n = (values.length / d) | 0, sum = Array(d).fill(0), out = flat ? new Float64Array(n * d) : Array(n);
78
+ for (let j = 0, k = 0; j < n; j++) {
79
+ const p = flat ? out : Array(d);
80
+ for (let a = 0; a < d; a++, k++) {
81
+ sum[a] += values[k];
82
+ p[(flat ? k - a : 0) + (sw && a < 2 ? 1 - a : a)] = sum[a] / (a < 2 ? f : zf);
83
+ }
84
+ if (!flat) out[j] = p;
85
+ }
86
+ return out;
87
+ }
88
+
89
+ /**
90
+ * Exact encoder for any input: [a, b] tuples (or any array-like), objects
91
+ * with lat/latitude and lng/lon/longitude, or a flat array of numbers.
92
+ * Rounds half away from zero, like Google's reference implementation.
93
+ */
94
+ export function slowEncode(points, f, flat) {
95
+ if (points == null || !(points.length >= 0)) fail('input', 'Expected an array');
96
+ const { dims: d, strict, zf } = st, sw = st.swap && d > 1, last = Array(d).fill(0), n = flat ? points.length / d : points.length;
97
+ if (n % 1 && strict) fail('input', 'Length not a multiple of ' + d);
98
+ let s = '';
99
+ for (let i = 0; i < (n | 0); i++) {
100
+ const p = flat ? 0 : points[i];
101
+ for (let a = 0; a < d; a++) {
102
+ const b = sw && a < 2 ? 1 - a : a;
103
+ let x = flat ? points[i * d + b] : p == null ? p : p.length >= 0 ? p[b] : a ? (a > 1 ? p.alt ?? p.altitude : p.lng ?? p.lon ?? p.longitude) : p.lat ?? p.latitude;
104
+ // null and undefined are missing, not 0. Numeric strings are numbers.
105
+ x = x == null ? NaN : +x * (a < 2 ? f : zf);
106
+ if (!(x - x == 0)) {
107
+ if (strict) fail('coordinate', 'Invalid coordinate', i);
108
+ x = 0;
109
+ }
110
+ x = Math.sign(x) * Math.round(Math.abs(x));
111
+ let z = x - last[a];
112
+ last[a] = x;
113
+ z = z < 0 ? -z * 2 - 1 : z * 2;
114
+ if (z > 2 ** 53 && strict) fail('overflow', 'Value too large', i);
115
+ for (; z >= 32; z = Math.floor(z / 32)) s += String.fromCharCode((z % 32) + 95);
116
+ s += String.fromCharCode(z + 63);
117
+ }
118
+ }
119
+ return s;
120
+ }
121
+
122
+ const decodeWith = (str, o, flat) => {
123
+ const f = opts(o), out = st.dims == 2 && typeof str == 'string' && decodeLoop(str, f, st.swap, flat);
124
+ return out ? (st.swap && !flat ? flip(out) : out) : slowDecode(str, f, flat);
125
+ };
126
+
127
+ // The fast loops always build [lat, lng]; lnglat output is flipped in place
128
+ // afterwards. A per-point `swap ? [b, a] : [a, b]` cost the default order
129
+ // about 5%, and the extra pass is small next to allocating the points.
130
+ export const flip = (out) => {
131
+ for (const p of out) {
132
+ const t = p[0];
133
+ p[0] = p[1];
134
+ p[1] = t;
135
+ }
136
+ return out;
137
+ };
138
+
139
+ /** Decodes to [lat, lng] tuples. */
140
+ export const decode = (str, o) => decodeWith(str, o, 0);
141
+
142
+ /** Decodes to a flat Float64Array: [lat0, lng0, lat1, lng1, ...]. */
143
+ export const decodeFlat = (str, o) => decodeWith(str, o, 1);
144
+
145
+ // The 2D fast loop, or null when the input needs slowDecode. A separate,
146
+ // self-contained function because V8 optimized it better that way: with the
147
+ // option and type checks in the same function, 10 point lines decoded about
148
+ // 15% slower.
149
+ export function decodeLoop(str, f, sw, flat) {
150
+ const len = str.length, a = +sw;
151
+ let i = 0, n = 0, lat = 0, lng = 0, bad = 0, c, v, s, out;
152
+ if (flat) {
153
+ // Every value ends with exactly one character below code 95.
154
+ for (; i < len; i++) n += (str.charCodeAt(i) - 95) >>> 31;
155
+ out = new Float64Array(n & ~1);
156
+ i = n = 0;
157
+ } else out = len < 64 ? [] : Array(len >> 1);
158
+ // One value is one or more 5-bit chunks. `bad` collects every character
159
+ // code: anything outside 0..63 after the -63 shift sets a high bit, and
160
+ // so does a 7th chunk in one value (25 - s < 0). NaN past the end of
161
+ // a truncated string ends the value and leaves i past len.
162
+ while (i < len) {
163
+ c = str.charCodeAt(i++) - 63;
164
+ bad |= c;
165
+ v = c & 31;
166
+ for (s = 5; c >= 32; s += 5) {
167
+ c = str.charCodeAt(i++) - 63;
168
+ bad |= c | (25 - s);
169
+ v |= (c & 31) << s;
170
+ }
171
+ lat += (v >>> 1) ^ -(v & 1);
172
+ c = str.charCodeAt(i++) - 63;
173
+ bad |= c;
174
+ v = c & 31;
175
+ for (s = 5; c >= 32; s += 5) {
176
+ c = str.charCodeAt(i++) - 63;
177
+ bad |= c | (25 - s);
178
+ v |= (c & 31) << s;
179
+ }
180
+ lng += (v >>> 1) ^ -(v & 1);
181
+ if (flat) {
182
+ out[n + a] = lat / f;
183
+ out[n + 1 - a] = lng / f;
184
+ n += 2;
185
+ } else out[n++] = [lat / f, lng / f];
186
+ }
187
+ if (bad & ~63 || i != len) return null;
188
+ if (out.length != n) out.length = n;
189
+ return out;
190
+ }
191
+
192
+ // Encoded characters for every zigzag value below 1024, so one or two
193
+ // characters cost one string concatenation instead of a loop. Built by a
194
+ // pure call, so bundles that never encode can drop it.
195
+ const CHARS = /* @__PURE__ */ Array.from({ length: 1024 }, (_, z) => chars(z));
196
+
197
+ export function chars(z) {
198
+ let s = '';
199
+ for (; z >= 32; z >>>= 5) s += String.fromCharCode((z & 31) + 95);
200
+ return s + String.fromCharCode(z + 63);
201
+ }
202
+
203
+ const encodeWith = (src, o, flat) => {
204
+ const f = opts(o);
205
+ return (st.dims == 2 && f <= 1e6 && (flat ? src != null && !(src.length % 2) : Array.isArray(src)) ? encodeLoop(src, f, +st.swap, flat) : null) ?? slowEncode(src, f, flat);
206
+ };
207
+
208
+ /** Encodes [lat, lng] tuples, or objects with lat and lng. */
209
+ export const encode = (points, o) => encodeWith(points, o, 0);
210
+
211
+ /** Encodes a flat array of numbers: [lat0, lng0, lat1, lng1, ...]. */
212
+ export const encodeFlat = (values, o) => encodeWith(values, o, 1);
213
+
214
+ // The 2D fast loop, or null when the input needs slowEncode.
215
+ function encodeLoop(src, f, a, flat) {
216
+ const n = flat ? src.length >> 1 : src.length;
217
+ let s = '', i = 0, la = 0, lb = 0, x, y, p, q, z, pt;
218
+ try {
219
+ for (; i < n; i++) {
220
+ if (flat) {
221
+ x = src[i * 2 + a];
222
+ y = src[i * 2 + 1 - a];
223
+ } else {
224
+ pt = src[i];
225
+ x = pt[a];
226
+ y = pt[1 - a];
227
+ }
228
+ // Anything but numbers goes the slow way, which coerces numeric
229
+ // strings and rejects null. The range check is also false for NaN.
230
+ if (typeof x != 'number' || typeof y != 'number' || !((x *= f) < LIMIT && x > -LIMIT && (y *= f) < LIMIT && y > -LIMIT)) return null;
231
+ // Round half away from zero. floor(|x| + 0.5) is exact here except for
232
+ // |x| = 0.49999999999999994, where the addition rounds up to 1, so any
233
+ // |x| < 0.5 is set to 0 afterwards. Math.round would be exact on its own,
234
+ // but it measured 40% slower on JavaScriptCore.
235
+ p = x < 0 ? -((0.5 - x) | 0) : (x + 0.5) | 0;
236
+ q = y < 0 ? -((0.5 - y) | 0) : (y + 0.5) | 0;
237
+ if (x < 0.5 && x > -0.5) p = 0;
238
+ if (y < 0.5 && y > -0.5) q = 0;
239
+ z = p - la;
240
+ z = (z << 1) ^ (z >> 31);
241
+ s += z < 1024 ? CHARS[z] : chars(z);
242
+ z = q - lb;
243
+ z = (z << 1) ^ (z >> 31);
244
+ s += z < 1024 ? CHARS[z] : chars(z);
245
+ la = p;
246
+ lb = q;
247
+ }
248
+ } catch {
249
+ // A null point: let slowEncode report it.
250
+ return null;
251
+ }
252
+ return s;
253
+ }
254
+
255
+ // Helpers built on top of an entry point's own decode and encode, so every
256
+ // entry uses its fastest paths. Entries call these with /* @__PURE__ */, so
257
+ // bundlers drop the ones nobody imports.
258
+ const lngLat = (o) => ({ ...(typeof o == 'number' ? { precision: o } : o), order: 'lnglat' });
259
+
260
+ /** {lat, lng} objects, the shape Google Maps and Leaflet take. */
261
+ export const latLngs = (decode) => (str, o) => decode(str, o).map(([lat, lng]) => ({ lat, lng }));
262
+
263
+ export const toGeo = (decode) => (str, o) => ({ type: 'LineString', coordinates: decode(str, lngLat(o)) });
264
+
265
+ export const fromGeo = (encode) => (g, o) => {
266
+ if (g?.type == 'Feature') g = g.geometry;
267
+ return g?.type == 'LineString' ? encode(g.coordinates, lngLat(o)) : fail('input', 'Expected a LineString');
268
+ };