@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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Güven Değirmenci
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Güven Değirmenci
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,120 +1,120 @@
1
- <p align="center">
2
- <picture>
3
- <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/Duoquote/polytini/main/assets/polytini-dark.png">
4
- <img src="https://raw.githubusercontent.com/Duoquote/polytini/main/assets/polytini-light.png" width="160" alt="polytini logo: a small bird sitting on a line">
5
- </picture>
6
- </p>
7
-
8
- # polytini
9
-
10
- <p align="center">
11
- <a href="https://www.npmjs.com/package/@duoquote/polytini"><img alt="npm" src="https://img.shields.io/npm/v/@duoquote/polytini?style=flat"></a>
12
- <a href="https://jsr.io/@duoquote/polytini"><img alt="JSR" src="https://img.shields.io/jsr/v/@duoquote/polytini?style=flat"></a>
13
- <a href="https://github.com/Duoquote/polytini/actions/workflows/ci.yml"><img alt="CI" src="https://img.shields.io/github/actions/workflow/status/Duoquote/polytini/ci.yml?branch=main&style=flat&label=CI"></a>
14
- <a href="docs/platforms.md#size"><img alt="gzipped size of the default entry" src="https://raw.githubusercontent.com/Duoquote/polytini/main/assets/badges/size.svg"></a>
15
- <a href="scripts/check-deps.js"><img alt="dependencies: 0" src="https://img.shields.io/badge/dependencies-0-brightgreen?style=flat"></a>
16
- <a href="LICENSE"><img alt="license: MIT" src="https://img.shields.io/github/license/Duoquote/polytini?style=flat"></a>
17
- </p>
18
-
19
- polytini encodes and decodes [Google encoded polylines](https://developers.google.com/maps/documentation/utilities/polylinealgorithm), the strings Google Maps, OSRM, Valhalla, GraphHopper and Mapbox return for route geometry. It's strict by default, so a cut-off or corrupted string throws instead of drawing a wrong line. Node, Bun, Deno and browsers each get the code path that measured fastest there, and it has no dependencies. I wrote it while rebuilding [polyline-benchmark](https://github.com/Duoquote/polyline-benchmark), where it's compared with every other JavaScript polyline library.
20
-
21
- ## Install
22
-
23
- ```sh
24
- npm install @duoquote/polytini # or pnpm add, yarn add, bun add
25
- deno add jsr:@duoquote/polytini
26
- ```
27
-
28
- ESM and CommonJS, with TypeScript types. Node 22 or newer. Not on npm or JSR yet; [RELEASING.md](RELEASING.md) covers the first publish.
29
-
30
- ## Usage
31
-
32
- ```js
33
- import { encode, decode } from '@duoquote/polytini';
34
-
35
- encode([[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]]); // '_p~iF~ps|U_ulLnnqC_mqNvxq`@'
36
- decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@'); // [[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]]
37
- decode(osrmGeometry, { precision: 6, order: 'lnglat' }); // OSRM polyline6, as [lng, lat]
38
- ```
39
-
40
- <picture>
41
- <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/Duoquote/polyline-benchmark/main/docs/charts/speed-bun-decode-dark.svg">
42
- <img alt="Bun: decode speed of every polyline library relative to @mapbox/polyline on real-world lines; polytini is highlighted" src="https://raw.githubusercontent.com/Duoquote/polyline-benchmark/main/docs/charts/speed-bun-decode-light.svg">
43
- </picture>
44
- <picture>
45
- <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/Duoquote/polyline-benchmark/main/docs/charts/speed-bun-encode-dark.svg">
46
- <img alt="Bun: encode speed of every polyline library relative to @mapbox/polyline on real-world lines; polytini is highlighted" src="https://raw.githubusercontent.com/Duoquote/polyline-benchmark/main/docs/charts/speed-bun-encode-light.svg">
47
- </picture>
48
-
49
- ## Best at what
50
-
51
- <!-- best -->
52
- From the [benchmark](https://github.com/Duoquote/polyline-benchmark)'s 27 November 2026 run, across Bun, Node 22 to 26, Deno and Chrome. Leads under 1.3x are marked, because cells moved about 30% between rounds.
53
-
54
- | Situation | Best | Runner-up | Margin |
55
- |---|---|---|--:|
56
- | Decode short lines (10 and 100 points) | @googlemaps/polyline-codec | @mapbox/polyline | 1.05x faster, within the noise |
57
- | Decode long lines (10,000 and 100,000 points) | **@duoquote/polytini** | @googlemaps/polyline-codec | 1.14x faster, within the noise |
58
- | Decode real-world lines (median 82 points) | **@duoquote/polytini** | @googlemaps/polyline-codec | 1.22x faster, within the noise |
59
- | Encode, any length | **@duoquote/polytini** | google-polyline | 2.05x faster |
60
- | Decode to typed arrays | **@duoquote/polytini (wasm, Float64Array)** | @duoquote/polytini (Float64Array) | 1.08x faster, within the noise |
61
- | Encode from typed arrays | **@duoquote/polytini (wasm, Float64Array)** | @duoquote/polytini (Float64Array) | 1.05x faster, within the noise |
62
- | Decode 1000 lines in one call | **@duoquote/polytini (wasm decodeMany/encodeMany)** | @duoquote/polytini (wasm, Float64Array) | 1.61x faster |
63
- | Least memory per decode (10,000 points, [lat, lng] arrays) | **@duoquote/polytini (765.4 KB)** | @googlemaps/polyline-codec (766.2 KB) | 0.1% less |
64
- | Smallest bundle (gzipped) | google-polyline (551 B) | @googlemaps/polyline-codec (568 B) | 17 B smaller |
65
- | Strict validation (throws on bad input) | **@duoquote/polytini** | @lp-labs/polyline-codec | 15x faster |
66
- | HERE Flexible Polyline (encode and decode) | **@duoquote/polytini (flexible)** | @here/flexpolyline | 24x faster |
67
- <!-- /best -->
68
-
69
- ## API
70
-
71
- | Function | Does |
72
- |---|---|
73
- | `decode(str, options?)` | String to `[[lat, lng], ...]` |
74
- | `encode(points, options?)` | `[lat, lng]` tuples or `{ lat, lng }` objects to a string |
75
- | `decodeFlat` / `encodeFlat` | The same with one `Float64Array`: `[lat0, lng0, lat1, lng1, ...]` |
76
- | `decodeLatLng(str)` | String to `[{ lat, lng }, ...]` |
77
- | `toGeoJSON(str)` / `fromGeoJSON(geo)` | GeoJSON LineString in and out |
78
- | `PolylineError` | What strict mode throws, with a `code` and an `index` |
79
-
80
- Options are a precision number or `{ precision, factor, order, strict, dimensions, zFactor }`. Details, errors and rounding: [docs/api.md](docs/api.md).
81
-
82
- ## Integrations
83
-
84
- | Import | For |
85
- |---|---|
86
- | `@duoquote/polytini/presets` | Options for OSRM, Valhalla, GraphHopper, Mapbox, Strava and others |
87
- | `@duoquote/polytini/leaflet` | `L.Polyline.fromEncoded`, `toEncoded`, factories; Leaflet 1.9 and 2 |
88
- | `@duoquote/polytini/turf` | Turf Features and FeatureCollections, any GeoJSON line encoded |
89
- | `@duoquote/polytini/google` | Drop-in `decodePath` and `encodePath` for the Maps JavaScript API |
90
- | `@duoquote/polytini/openlayers` | Drop-in `ol/format/Polyline` that doesn't overwrite your geometry |
91
- | `@duoquote/polytini/maplibre`, `/mapbox` | GeoJSON sources, add a line layer, update it |
92
- | `@duoquote/polytini/deckgl` | deck.gl binary paths and polygons, no array per point |
93
- | `@duoquote/polytini/flexible` | HERE's Flexible Polyline, a drop-in for `@here/flexpolyline` |
94
- | `@duoquote/polytini/wasm` | Opt-in WebAssembly build, with batch calls |
95
-
96
- None of them import their map library; you pass it in or use what they return. Examples for each: [docs/integrations.md](docs/integrations.md).
97
-
98
- ## Notes
99
-
100
- - The precision isn't stored in the string. A `polyline6` string decoded at 5 gives coordinates ten times too big, with no error.
101
- - Strict mode throws on invalid characters, cut-off strings and NaN coordinates. Pass `strict: false` to never throw.
102
- - `/wasm` only pays off for typed arrays on long lines and for many lines in one call (`decodeMany`). For `[lat, lng]` arrays the default entry is as fast or faster. Numbers: [docs/platforms.md](docs/platforms.md#wasm).
103
- - Each runtime gets its own entry through package.json conditions (`node`, `bun`, `deno`, `browser`). Why each one exists: [docs/platforms.md](docs/platforms.md).
104
-
105
- ## CLI
106
-
107
- ```sh
108
- npx @duoquote/polytini decode '_p~iF~ps|U_ulLnnqC_mqNvxq`@'
109
- echo '[[38.5,-120.2],[40.7,-120.95]]' | npx @duoquote/polytini encode
110
- ```
111
-
112
- JSON, GeoJSON, CSV and plain lines; works with `bunx`, `pnpm dlx`, `yarn dlx` and `deno run jsr:@duoquote/polytini/cli`. All options: [docs/cli.md](docs/cli.md).
113
-
114
- ## More
115
-
116
- [Tests and coverage](docs/testing.md), [releasing](RELEASING.md), and the [benchmark](https://github.com/Duoquote/polyline-benchmark) with every table and chart.
117
-
118
- ## License
119
-
120
- MIT, see [LICENSE](LICENSE).
1
+ <p align="center">
2
+ <picture>
3
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/Duoquote/polytini/main/assets/polytini-dark.png">
4
+ <img src="https://raw.githubusercontent.com/Duoquote/polytini/main/assets/polytini-light.png" width="160" alt="polytini logo: a small bird sitting on a line">
5
+ </picture>
6
+ </p>
7
+
8
+ # polytini
9
+
10
+ <p align="center">
11
+ <a href="https://www.npmjs.com/package/@duoquote/polytini"><img alt="npm" src="https://img.shields.io/npm/v/@duoquote/polytini?style=flat"></a>
12
+ <a href="https://jsr.io/@duoquote/polytini"><img alt="JSR" src="https://img.shields.io/jsr/v/@duoquote/polytini?style=flat"></a>
13
+ <a href="https://github.com/Duoquote/polytini/actions/workflows/ci.yml"><img alt="CI" src="https://img.shields.io/github/actions/workflow/status/Duoquote/polytini/ci.yml?branch=main&style=flat&label=CI"></a>
14
+ <a href="docs/platforms.md#size"><img alt="gzipped size of the default entry" src="https://raw.githubusercontent.com/Duoquote/polytini/main/assets/badges/size.svg"></a>
15
+ <a href="scripts/check-deps.js"><img alt="dependencies: 0" src="https://img.shields.io/badge/dependencies-0-brightgreen?style=flat"></a>
16
+ <a href="LICENSE"><img alt="license: MIT" src="https://img.shields.io/github/license/Duoquote/polytini?style=flat"></a>
17
+ </p>
18
+
19
+ polytini encodes and decodes [Google encoded polylines](https://developers.google.com/maps/documentation/utilities/polylinealgorithm), the strings Google Maps, OSRM, Valhalla, GraphHopper and Mapbox return for route geometry. It's strict by default, so a cut-off or corrupted string throws instead of drawing a wrong line. Node, Bun, Deno and browsers each get the code path that measured fastest there, and it has no dependencies. I wrote it while rebuilding [polyline-benchmark](https://github.com/Duoquote/polyline-benchmark), where it's compared with every other JavaScript polyline library.
20
+
21
+ ## Install
22
+
23
+ ```sh
24
+ npm install @duoquote/polytini # or pnpm add, yarn add, bun add
25
+ deno add jsr:@duoquote/polytini
26
+ ```
27
+
28
+ ESM and CommonJS, with TypeScript types. Node 22 or newer. Not on npm or JSR yet; [RELEASING.md](RELEASING.md) covers the first publish.
29
+
30
+ ## Usage
31
+
32
+ ```js
33
+ import { encode, decode } from '@duoquote/polytini';
34
+
35
+ encode([[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]]); // '_p~iF~ps|U_ulLnnqC_mqNvxq`@'
36
+ decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@'); // [[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]]
37
+ decode(osrmGeometry, { precision: 6, order: 'lnglat' }); // OSRM polyline6, as [lng, lat]
38
+ ```
39
+
40
+ <picture>
41
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/Duoquote/polyline-benchmark/main/docs/charts/speed-bun-decode-dark.svg">
42
+ <img alt="Bun: decode speed of every polyline library relative to @mapbox/polyline on real-world lines; polytini is highlighted" src="https://raw.githubusercontent.com/Duoquote/polyline-benchmark/main/docs/charts/speed-bun-decode-light.svg">
43
+ </picture>
44
+ <picture>
45
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/Duoquote/polyline-benchmark/main/docs/charts/speed-bun-encode-dark.svg">
46
+ <img alt="Bun: encode speed of every polyline library relative to @mapbox/polyline on real-world lines; polytini is highlighted" src="https://raw.githubusercontent.com/Duoquote/polyline-benchmark/main/docs/charts/speed-bun-encode-light.svg">
47
+ </picture>
48
+
49
+ ## Best at what
50
+
51
+ <!-- best -->
52
+ From the [benchmark](https://github.com/Duoquote/polyline-benchmark)'s 27 November 2026 run, across Bun, Node 22 to 26, Deno and Chrome. Leads under 1.3x are marked, because cells moved about 30% between rounds.
53
+
54
+ | Situation | Best | Runner-up | Margin |
55
+ |---|---|---|--:|
56
+ | Decode short lines (10 and 100 points) | @googlemaps/polyline-codec | @mapbox/polyline | 1.05x faster, within the noise |
57
+ | Decode long lines (10,000 and 100,000 points) | **@duoquote/polytini** | @googlemaps/polyline-codec | 1.14x faster, within the noise |
58
+ | Decode real-world lines (median 82 points) | **@duoquote/polytini** | @googlemaps/polyline-codec | 1.22x faster, within the noise |
59
+ | Encode, any length | **@duoquote/polytini** | google-polyline | 2.05x faster |
60
+ | Decode to typed arrays | **@duoquote/polytini (wasm, Float64Array)** | @duoquote/polytini (Float64Array) | 1.08x faster, within the noise |
61
+ | Encode from typed arrays | **@duoquote/polytini (wasm, Float64Array)** | @duoquote/polytini (Float64Array) | 1.05x faster, within the noise |
62
+ | Decode 1000 lines in one call | **@duoquote/polytini (wasm decodeMany/encodeMany)** | @duoquote/polytini (wasm, Float64Array) | 1.61x faster |
63
+ | Least memory per decode (10,000 points, [lat, lng] arrays) | **@duoquote/polytini (765.4 KB)** | @googlemaps/polyline-codec (766.2 KB) | 0.1% less |
64
+ | Smallest bundle (gzipped) | google-polyline (551 B) | @googlemaps/polyline-codec (568 B) | 17 B smaller |
65
+ | Strict validation (throws on bad input) | **@duoquote/polytini** | @lp-labs/polyline-codec | 15x faster |
66
+ | HERE Flexible Polyline (encode and decode) | **@duoquote/polytini (flexible)** | @here/flexpolyline | 24x faster |
67
+ <!-- /best -->
68
+
69
+ ## API
70
+
71
+ | Function | Does |
72
+ |---|---|
73
+ | `decode(str, options?)` | String to `[[lat, lng], ...]` |
74
+ | `encode(points, options?)` | `[lat, lng]` tuples or `{ lat, lng }` objects to a string |
75
+ | `decodeFlat` / `encodeFlat` | The same with one `Float64Array`: `[lat0, lng0, lat1, lng1, ...]` |
76
+ | `decodeLatLng(str)` | String to `[{ lat, lng }, ...]` |
77
+ | `toGeoJSON(str)` / `fromGeoJSON(geo)` | GeoJSON LineString in and out |
78
+ | `PolylineError` | What strict mode throws, with a `code` and an `index` |
79
+
80
+ Options are a precision number or `{ precision, factor, order, strict, dimensions, zFactor }`. Details, errors and rounding: [docs/api.md](docs/api.md).
81
+
82
+ ## Integrations
83
+
84
+ | Import | For |
85
+ |---|---|
86
+ | `@duoquote/polytini/presets` | Options for OSRM, Valhalla, GraphHopper, Mapbox, Strava and others |
87
+ | `@duoquote/polytini/leaflet` | `L.Polyline.fromEncoded`, `toEncoded`, factories; Leaflet 1.9 and 2 |
88
+ | `@duoquote/polytini/turf` | Turf Features and FeatureCollections, any GeoJSON line encoded |
89
+ | `@duoquote/polytini/google` | Drop-in `decodePath` and `encodePath` for the Maps JavaScript API |
90
+ | `@duoquote/polytini/openlayers` | Drop-in `ol/format/Polyline` that doesn't overwrite your geometry |
91
+ | `@duoquote/polytini/maplibre`, `/mapbox` | GeoJSON sources, add a line layer, update it |
92
+ | `@duoquote/polytini/deckgl` | deck.gl binary paths and polygons, no array per point |
93
+ | `@duoquote/polytini/flexible` | HERE's Flexible Polyline, a drop-in for `@here/flexpolyline` |
94
+ | `@duoquote/polytini/wasm` | Opt-in WebAssembly build, with batch calls |
95
+
96
+ None of them import their map library; you pass it in or use what they return. Examples for each: [docs/integrations.md](docs/integrations.md).
97
+
98
+ ## Notes
99
+
100
+ - The precision isn't stored in the string. A `polyline6` string decoded at 5 gives coordinates ten times too big, with no error.
101
+ - Strict mode throws on invalid characters, cut-off strings and NaN coordinates. Pass `strict: false` to never throw.
102
+ - `/wasm` only pays off for typed arrays on long lines and for many lines in one call (`decodeMany`). For `[lat, lng]` arrays the default entry is as fast or faster. Numbers: [docs/platforms.md](docs/platforms.md#wasm).
103
+ - Each runtime gets its own entry through package.json conditions (`node`, `bun`, `deno`, `browser`). Why each one exists: [docs/platforms.md](docs/platforms.md).
104
+
105
+ ## CLI
106
+
107
+ ```sh
108
+ npx @duoquote/polytini decode '_p~iF~ps|U_ulLnnqC_mqNvxq`@'
109
+ echo '[[38.5,-120.2],[40.7,-120.95]]' | npx @duoquote/polytini encode
110
+ ```
111
+
112
+ JSON, GeoJSON, CSV and plain lines; works with `bunx`, `pnpm dlx`, `yarn dlx` and `deno run jsr:@duoquote/polytini/cli`. All options: [docs/cli.md](docs/cli.md).
113
+
114
+ ## More
115
+
116
+ [Tests and coverage](docs/testing.md), [releasing](RELEASING.md), and the [benchmark](https://github.com/Duoquote/polyline-benchmark) with every table and chart.
117
+
118
+ ## License
119
+
120
+ MIT, see [LICENSE](LICENSE).
@@ -1,2 +1,2 @@
1
- // The polytini command. Running it is the whole API.
2
- export {};
1
+ // The polytini command. Running it is the whole API.
2
+ export {};
@@ -1,60 +1,72 @@
1
- // Types for @duoquote/polytini/deckgl. The results are plain objects in
2
- // deck.gl's binary data shape, so they need no deck.gl types: they can be
3
- // passed as `data` to PathLayer, TripsLayer and SolidPolygonLayer.
4
- import type { Options } from './index.d.ts';
5
-
6
- export interface BinaryOptions extends Options {
7
- /** Float32Array instead of Float64Array. Smaller, and enough for most maps; deck.gl uses 64-bit positions only when given them. Default false. */
8
- float32?: boolean;
9
- }
10
-
11
- export interface PathOptions extends BinaryOptions {
12
- /**
13
- * For TripsLayer: the last value of each point (with dimensions 3 or 4) goes
14
- * to attributes.getTimestamps instead of the positions. The values are
15
- * divided by zFactor, like any value after the second, and always come out
16
- * as a Float32Array, because TripsLayer's timestamp attribute is 32-bit
17
- * (deck.gl 9.4 drew nothing given Float64Array timestamps). Default false.
18
- */
19
- timestamps?: boolean;
20
- }
21
-
22
- /** A deck.gl binary attribute. */
23
- export interface BinaryAttribute<T extends Float32Array | Float64Array = Float32Array | Float64Array, S extends number = number> {
24
- value: T;
25
- size: S;
26
- }
27
-
28
- export interface BinaryPaths<T extends Float32Array | Float64Array = Float32Array | Float64Array> {
29
- /** The number of paths, one per string. */
30
- length: number;
31
- /** The first vertex of each path, plus the vertex count at the end (length + 1 entries). */
32
- startIndices: Uint32Array;
33
- attributes: {
34
- /** [lng, lat] (size 2) or [lng, lat, z] (size 3) per vertex. */
35
- getPath: BinaryAttribute<T, 2 | 3>;
36
- /** One value per vertex, with the timestamps option. Always 32-bit. */
37
- getTimestamps?: BinaryAttribute<Float32Array, 1>;
38
- };
39
- }
40
-
41
- export interface BinaryPolygons<T extends Float32Array | Float64Array = Float32Array | Float64Array> {
42
- /** The number of polygons, one ring per string. */
43
- length: number;
44
- /** The first vertex of each ring, plus the vertex count at the end (length + 1 entries). */
45
- startIndices: Uint32Array;
46
- attributes: {
47
- /** [lng, lat] (size 2) or [lng, lat, z] (size 3) per vertex, every ring closed. */
48
- getPolygon: BinaryAttribute<T, 2 | 3>;
49
- };
50
- }
51
-
52
- /** Binary data for PathLayer and TripsLayer, one path per string. Pass _pathType: 'open' to skip deck.gl's normalization. */
53
- export function binaryPaths(encoded: string | string[], options: PathOptions & { float32: true }): BinaryPaths<Float32Array>;
54
- export function binaryPaths(encoded: string | string[], options?: number | (PathOptions & { float32?: false })): BinaryPaths<Float64Array>;
55
- export function binaryPaths(encoded: string | string[], options?: number | PathOptions): BinaryPaths;
56
-
57
- /** Binary data for SolidPolygonLayer, one closed ring per string (no holes). */
58
- export function binaryPolygons(encoded: string | string[], options: BinaryOptions & { float32: true }): BinaryPolygons<Float32Array>;
59
- export function binaryPolygons(encoded: string | string[], options?: number | (BinaryOptions & { float32?: false })): BinaryPolygons<Float64Array>;
60
- export function binaryPolygons(encoded: string | string[], options?: number | BinaryOptions): BinaryPolygons;
1
+ // Types for @duoquote/polytini/deckgl. The results are plain objects in
2
+ // deck.gl's binary data shape, so they need no deck.gl types: they can be
3
+ // passed as `data` to PathLayer, TripsLayer and SolidPolygonLayer.
4
+ import type { Options } from './index.d.ts';
5
+
6
+ /** Options for binaryPolygons: polytini's options plus float32. */
7
+ export interface BinaryOptions extends Options {
8
+ /** Float32Array instead of Float64Array. Smaller, and enough for most maps; deck.gl uses 64-bit positions only when given them. Default false. */
9
+ float32?: boolean;
10
+ }
11
+
12
+ /** Options for binaryPaths: BinaryOptions plus timestamps. */
13
+ export interface PathOptions extends BinaryOptions {
14
+ /**
15
+ * For TripsLayer: the last value of each point (with dimensions 3 or 4) goes
16
+ * to attributes.getTimestamps instead of the positions. The values are
17
+ * divided by zFactor, like any value after the second, and always come out
18
+ * as a Float32Array, because TripsLayer's timestamp attribute is 32-bit
19
+ * (deck.gl 9.4 drew nothing given Float64Array timestamps). Default false.
20
+ */
21
+ timestamps?: boolean;
22
+ }
23
+
24
+ /** A deck.gl binary attribute. */
25
+ export interface BinaryAttribute<T extends Float32Array | Float64Array = Float32Array | Float64Array, S extends number = number> {
26
+ /** The values, size per vertex. */
27
+ value: T;
28
+ /** Values per vertex. */
29
+ size: S;
30
+ }
31
+
32
+ /** What binaryPaths returns: `data` for PathLayer or TripsLayer. */
33
+ export interface BinaryPaths<T extends Float32Array | Float64Array = Float32Array | Float64Array> {
34
+ /** The number of paths, one per string. */
35
+ length: number;
36
+ /** The first vertex of each path, plus the vertex count at the end (length + 1 entries). */
37
+ startIndices: Uint32Array;
38
+ /** The vertex attributes. */
39
+ attributes: {
40
+ /** [lng, lat] (size 2) or [lng, lat, z] (size 3) per vertex. */
41
+ getPath: BinaryAttribute<T, 2 | 3>;
42
+ /** One value per vertex, with the timestamps option. Always 32-bit. */
43
+ getTimestamps?: BinaryAttribute<Float32Array, 1>;
44
+ };
45
+ }
46
+
47
+ /** What binaryPolygons returns: `data` for SolidPolygonLayer. */
48
+ export interface BinaryPolygons<T extends Float32Array | Float64Array = Float32Array | Float64Array> {
49
+ /** The number of polygons, one ring per string. */
50
+ length: number;
51
+ /** The first vertex of each ring, plus the vertex count at the end (length + 1 entries). */
52
+ startIndices: Uint32Array;
53
+ /** The vertex attributes. */
54
+ attributes: {
55
+ /** [lng, lat] (size 2) or [lng, lat, z] (size 3) per vertex, every ring closed. */
56
+ getPolygon: BinaryAttribute<T, 2 | 3>;
57
+ };
58
+ }
59
+
60
+ /** Binary data for PathLayer and TripsLayer, one path per string. Pass _pathType: 'open' to skip deck.gl's normalization. */
61
+ export function binaryPaths(encoded: string | string[], options: PathOptions & { float32: true }): BinaryPaths<Float32Array>;
62
+ /** binaryPaths with Float64Array positions, the default. */
63
+ export function binaryPaths(encoded: string | string[], options?: number | (PathOptions & { float32?: false })): BinaryPaths<Float64Array>;
64
+ /** binaryPaths when float32 isn't known until runtime. */
65
+ export function binaryPaths(encoded: string | string[], options?: number | PathOptions): BinaryPaths;
66
+
67
+ /** Binary data for SolidPolygonLayer, one closed ring per string (no holes). */
68
+ export function binaryPolygons(encoded: string | string[], options: BinaryOptions & { float32: true }): BinaryPolygons<Float32Array>;
69
+ /** binaryPolygons with Float64Array positions, the default. */
70
+ export function binaryPolygons(encoded: string | string[], options?: number | (BinaryOptions & { float32?: false })): BinaryPolygons<Float64Array>;
71
+ /** binaryPolygons when float32 isn't known until runtime. */
72
+ export function binaryPolygons(encoded: string | string[], options?: number | BinaryOptions): BinaryPolygons;
@@ -1,80 +1,92 @@
1
- // Types for @duoquote/polytini/flexible, HERE's Flexible Polyline format
2
- // (version 1). Separate from the Google-format entry points.
3
- import type { ErrorCode } from './index.d.ts';
4
-
5
- /** Third dimension types, as stored in the header. */
6
- export const ABSENT: 0;
7
- export const LEVEL: 1;
8
- export const ALTITUDE: 2;
9
- export const ELEVATION: 3;
10
- /** Reserved by the spec: decode accepts it, encode refuses it. */
11
- export const RESERVED1: 4;
12
- /** Reserved by the spec: decode accepts it, encode refuses it. */
13
- export const RESERVED2: 5;
14
- export const CUSTOM1: 6;
15
- export const CUSTOM2: 7;
16
-
17
- /** Any type a header can hold. */
18
- export type ThirdDimension = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
19
- /** The types encode accepts. */
20
- export type EncodableThirdDimension = 0 | 1 | 2 | 3 | 6 | 7;
21
-
22
- export interface Header {
23
- /** Decimal places of latitude and longitude, 0 to 15. */
24
- precision: number;
25
- /** What the third value means. ABSENT (0) for 2D lines. */
26
- thirdDim: ThirdDimension;
27
- /** Decimal places of the third value, 0 to 15. */
28
- thirdDimPrecision: number;
29
- }
30
-
31
- export interface DecodedResult extends Header {
32
- /** [lat, lng] points, or [lat, lng, z] when thirdDim is not ABSENT. */
33
- polyline: number[][];
34
- }
35
-
36
- export interface EncodeOptions {
37
- /** Decimal places of latitude and longitude, an integer from 0 to 15. Default 5. */
38
- precision?: number;
39
- /** Type of the third value. Default ABSENT, which ignores any third value. */
40
- thirdDim?: EncodableThirdDimension;
41
- /** Decimal places of the third value, an integer from 0 to 15. Default 0. */
42
- thirdDimPrecision?: number;
43
- }
44
-
45
- /** [lat, lng] or [lat, lng, z] points, or one flat array of numbers: [lat0, lng0, lat1, lng1, ...]. */
46
- export type Points = ArrayLike<ArrayLike<number>> | ArrayLike<number>;
47
-
48
- /** The argument of @here/flexpolyline's encode. */
49
- export interface EncodeInput extends EncodeOptions {
50
- polyline: Points;
51
- }
52
-
53
- /**
54
- * Codes of a PolylineError from this entry: the main entry's, plus 'version'
55
- * for a format version other than 1 or a header with bits set above bit 10.
56
- */
57
- export type FlexibleErrorCode = Exclude<ErrorCode, 'init'> | 'version';
58
-
59
- /** The same class as the main entry's PolylineError. */
60
- export class PolylineError extends Error {
61
- readonly name: 'PolylineError';
62
- /** What went wrong. */
63
- readonly code: FlexibleErrorCode;
64
- /** Character index (decode) or point index (encode), when there is one. */
65
- readonly index: number | undefined;
66
- constructor(code: FlexibleErrorCode, message: string, index?: number);
67
- }
68
-
69
- /**
70
- * Encodes a line. Takes @here/flexpolyline's argument, or the points with
71
- * the header fields in `options`, which win over the input object's.
72
- * Rounds half away from zero.
73
- */
74
- export function encode(input: EncodeInput | Points, options?: EncodeOptions): string;
75
-
76
- /** Decodes a line, with its header. The same result shape as @here/flexpolyline. */
77
- export function decode(str: string): DecodedResult;
78
-
79
- /** Reads only the header. */
80
- export function header(str: string): Header;
1
+ // Types for @duoquote/polytini/flexible, HERE's Flexible Polyline format
2
+ // (version 1). Separate from the Google-format entry points.
3
+ import type { ErrorCode } from './index.d.ts';
4
+
5
+ /** Third dimension types, as stored in the header. */
6
+ /** No third value: a 2D line. */
7
+ export const ABSENT: 0;
8
+ /** The third value is a level, such as a floor. */
9
+ export const LEVEL: 1;
10
+ /** The third value is altitude. */
11
+ export const ALTITUDE: 2;
12
+ /** The third value is elevation. */
13
+ export const ELEVATION: 3;
14
+ /** Reserved by the spec: decode accepts it, encode refuses it. */
15
+ export const RESERVED1: 4;
16
+ /** Reserved by the spec: decode accepts it, encode refuses it. */
17
+ export const RESERVED2: 5;
18
+ /** A third value of your own meaning. */
19
+ export const CUSTOM1: 6;
20
+ /** A second type for a third value of your own meaning. */
21
+ export const CUSTOM2: 7;
22
+
23
+ /** Any type a header can hold. */
24
+ export type ThirdDimension = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
25
+ /** The types encode accepts. */
26
+ export type EncodableThirdDimension = 0 | 1 | 2 | 3 | 6 | 7;
27
+
28
+ /** The header fields of an encoded line. */
29
+ export interface Header {
30
+ /** Decimal places of latitude and longitude, 0 to 15. */
31
+ precision: number;
32
+ /** What the third value means. ABSENT (0) for 2D lines. */
33
+ thirdDim: ThirdDimension;
34
+ /** Decimal places of the third value, 0 to 15. */
35
+ thirdDimPrecision: number;
36
+ }
37
+
38
+ /** What decode returns: the header and the points. */
39
+ export interface DecodedResult extends Header {
40
+ /** [lat, lng] points, or [lat, lng, z] when thirdDim is not ABSENT. */
41
+ polyline: number[][];
42
+ }
43
+
44
+ /** The header fields encode writes. */
45
+ export interface EncodeOptions {
46
+ /** Decimal places of latitude and longitude, an integer from 0 to 15. Default 5. */
47
+ precision?: number;
48
+ /** Type of the third value. Default ABSENT, which ignores any third value. */
49
+ thirdDim?: EncodableThirdDimension;
50
+ /** Decimal places of the third value, an integer from 0 to 15. Default 0. */
51
+ thirdDimPrecision?: number;
52
+ }
53
+
54
+ /** [lat, lng] or [lat, lng, z] points, or one flat array of numbers: [lat0, lng0, lat1, lng1, ...]. */
55
+ export type Points = ArrayLike<ArrayLike<number>> | ArrayLike<number>;
56
+
57
+ /** The argument of @here/flexpolyline's encode. */
58
+ export interface EncodeInput extends EncodeOptions {
59
+ /** The points. */
60
+ polyline: Points;
61
+ }
62
+
63
+ /**
64
+ * Codes of a PolylineError from this entry: the main entry's, plus 'version'
65
+ * for a format version other than 1 or a header with bits set above bit 10.
66
+ */
67
+ export type FlexibleErrorCode = Exclude<ErrorCode, 'init'> | 'version';
68
+
69
+ /** The same class as the main entry's PolylineError. */
70
+ export class PolylineError extends Error {
71
+ /** Always "PolylineError". */
72
+ readonly name: 'PolylineError';
73
+ /** What went wrong. */
74
+ readonly code: FlexibleErrorCode;
75
+ /** Character index (decode) or point index (encode), when there is one. */
76
+ readonly index: number | undefined;
77
+ /** Makes an error; the message gets " at <index>" appended when there's an index. */
78
+ constructor(code: FlexibleErrorCode, message: string, index?: number);
79
+ }
80
+
81
+ /**
82
+ * Encodes a line. Takes @here/flexpolyline's argument, or the points with
83
+ * the header fields in `options`, which win over the input object's.
84
+ * Rounds half away from zero.
85
+ */
86
+ export function encode(input: EncodeInput | Points, options?: EncodeOptions): string;
87
+
88
+ /** Decodes a line, with its header. The same result shape as @here/flexpolyline. */
89
+ export function decode(str: string): DecodedResult;
90
+
91
+ /** Reads only the header. */
92
+ export function header(str: string): Header;