doorplate 0.0.0-stage → 0.7.2
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.
- package/LICENSE-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/NOTICE +27 -0
- package/README.md +45 -2
- package/doorplate.wasm +0 -0
- package/package.json +42 -4
- package/src/index.d.ts +60 -0
- package/src/index.js +100 -0
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jeremey Bingham
|
|
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/NOTICE
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
doorplate
|
|
2
|
+
Copyright (c) 2026 Jeremey Bingham. Code licensed under MIT OR Apache-2.0.
|
|
3
|
+
|
|
4
|
+
This product includes data from third parties under their own terms:
|
|
5
|
+
|
|
6
|
+
- GeoNames postal code data (https://www.geonames.org/), licensed under the
|
|
7
|
+
Creative Commons Attribution 4.0 License
|
|
8
|
+
(https://creativecommons.org/licenses/by/4.0/). Used for the ZIP-to-place
|
|
9
|
+
tables in crates/doorplate/data/places/ (see
|
|
10
|
+
docs/places.md). The
|
|
11
|
+
GeoNames data is provided "as is" and has been reformatted (upper-cased,
|
|
12
|
+
de-accented, joined with Census names).
|
|
13
|
+
- US Census Bureau Gazetteer files and 2020 reference lists (public domain),
|
|
14
|
+
used for place names, county subdivisions and counties in
|
|
15
|
+
crates/doorplate/data/places/.
|
|
16
|
+
- Florida public-record address data, used for observed city and ZIP pairs
|
|
17
|
+
and street spellings in the curated rows of
|
|
18
|
+
crates/doorplate/data/places/ (see docs/places.md). Public record; no attribution is required.
|
|
19
|
+
- usaddress test data (MIT, datamade), vendored in tests/external/usaddress/.
|
|
20
|
+
- National Address Database (NAD), US Department of Transportation
|
|
21
|
+
(https://www.transportation.gov/gis/national-address-database), a work of the
|
|
22
|
+
US federal government (public domain). Compiled with the US Census Bureau's
|
|
23
|
+
Geography Division from the address data of its contributing state, local and
|
|
24
|
+
tribal providers. Street, city, state and ZIP values are used, with invented
|
|
25
|
+
house and unit numbers, in the benchmark suite in benchmarks/suite/ (see
|
|
26
|
+
docs/benchmark-suite.md). The suite is for parser testing; it is not a list of
|
|
27
|
+
real or deliverable addresses.
|
package/README.md
CHANGED
|
@@ -1,3 +1,46 @@
|
|
|
1
|
-
#
|
|
1
|
+
# doorplate (JavaScript)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Parse US addresses into components, a stable match key and USPS-style lines. The Rust parser compiled to
|
|
4
|
+
WebAssembly: it runs in Node and in the browser, with no dependencies and no network access. In a browser page
|
|
5
|
+
the addresses never leave the visitor's machine.
|
|
6
|
+
|
|
7
|
+
> **Beta.** The package is built from the same wasm as the
|
|
8
|
+
> [live demo](https://jeremeybingham.github.io/doorplate/demo/) and checked against the native build.
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
import { init } from "doorplate";
|
|
12
|
+
|
|
13
|
+
const dp = await init(); // once; loads doorplate.wasm
|
|
14
|
+
const r = dp.parse("123 N Main St Apt 4, Springfield IL 62704");
|
|
15
|
+
r.key; // "k6:S|123|N|MAIN|ST||IL|62704"
|
|
16
|
+
r.usps.line1; // "123 N MAIN ST APT 4"
|
|
17
|
+
r.spans; // component ranges in UTF-16 units: text.slice(start, end)
|
|
18
|
+
|
|
19
|
+
dp.parseFields({ line1: "PO Box 12", city: "Reno", state: "NV", zip: "89501" });
|
|
20
|
+
dp.parseBatch(["1 Main St, Reno NV 89501", "..."]); // { columns, rows } like `doorplate parse-file`
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`init()` takes an optional source (bytes, a `WebAssembly.Module`, a URL or a `Response`) for runtimes that cannot
|
|
24
|
+
read a sibling file, such as Cloudflare Workers. Node 18 or newer; ES modules only. Bundlers (Vite, webpack 5,
|
|
25
|
+
Next) pick up `doorplate.wasm` through `new URL(..., import.meta.url)`.
|
|
26
|
+
|
|
27
|
+
**Size and speed:** the package is 2.0 MB packed (5.6 MB unpacked, 1.5 MB with brotli), mostly place and street
|
|
28
|
+
tables. In Node, init takes about 22 ms, one `parse` about 53 µs, and `parseBatch` about 23 µs a row.
|
|
29
|
+
|
|
30
|
+
Guide: [JavaScript](https://doorplate.readthedocs.io/en/latest/javascript/). The records, the key and the
|
|
31
|
+
warning codes are the same as the command line and Python.
|
|
32
|
+
|
|
33
|
+
## Disclaimer
|
|
34
|
+
|
|
35
|
+
doorplate is provided "as is", without warranty of any kind, under the [MIT](https://github.com/jeremeybingham/doorplate/blob/main/LICENSE-MIT) or
|
|
36
|
+
[Apache-2.0](https://github.com/jeremeybingham/doorplate/blob/main/LICENSE-APACHE) license at your option; the license text is what governs. In plain
|
|
37
|
+
terms: **use it at your own risk.** Nothing here guarantees that a parse, a warning, a match key
|
|
38
|
+
or a figure is correct, complete or fit for your purpose. Accuracy and speed numbers are
|
|
39
|
+
measurements on particular datasets, not promises for yours. doorplate parses and formats
|
|
40
|
+
addresses; it does **not validate** that an address exists or can receive mail, and Pub 28
|
|
41
|
+
formatting is not certified validation. Check its output before you rely on it for anything with
|
|
42
|
+
legal, financial, safety or delivery consequences.
|
|
43
|
+
|
|
44
|
+
doorplate is an independent project. It is not affiliated with, endorsed by or certified by the
|
|
45
|
+
US Postal Service, RESO, DataMade (the authors of usaddress) or any data source it cites; those
|
|
46
|
+
names are used only to say what the output follows or is compared with.
|
package/doorplate.wasm
ADDED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "doorplate",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.7.2",
|
|
4
|
+
"description": "Parse US addresses into components, a stable match key and USPS-style lines. Rust compiled to WebAssembly; runs in Node and in the browser, nothing leaves the machine.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./src/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.d.ts",
|
|
11
|
+
"default": "./src/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./doorplate.wasm": "./doorplate.wasm",
|
|
14
|
+
"./package.json": "./package.json"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"src",
|
|
18
|
+
"doorplate.wasm",
|
|
19
|
+
"LICENSE-MIT",
|
|
20
|
+
"LICENSE-APACHE",
|
|
21
|
+
"NOTICE"
|
|
22
|
+
],
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=18"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build:wasm": "cargo build -p doorplate-wasm --target wasm32-unknown-unknown --release && cp ../../target/wasm32-unknown-unknown/release/doorplate_wasm.wasm doorplate.wasm",
|
|
29
|
+
"test": "node --test",
|
|
30
|
+
"types": "tsc --noEmit -p test/tsconfig.json"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"typescript": "~5.6.0"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT OR Apache-2.0",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/jeremeybingham/doorplate.git",
|
|
39
|
+
"directory": "crates/doorplate-js"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://doorplate.readthedocs.io/",
|
|
42
|
+
"bugs": "https://github.com/jeremeybingham/doorplate/issues",
|
|
43
|
+
"keywords": ["address", "address-parser", "usps", "geocoding", "webassembly", "wasm", "normalization", "pub28"]
|
|
44
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export type Source = ArrayBuffer | ArrayBufferView | WebAssembly.Module | URL | string | Response | Promise<Response>;
|
|
2
|
+
|
|
3
|
+
/** A component's range in the text it indexes, in UTF-16 units: `text.slice(start, end)` is the component. */
|
|
4
|
+
export interface Span {
|
|
5
|
+
/** Component name, e.g. "StreetName", "Zip5". */
|
|
6
|
+
component: string;
|
|
7
|
+
/** For `parseFields`, the field the range indexes ("line1", "line2", "city", "state", "zip"); otherwise null. */
|
|
8
|
+
field: "line1" | "line2" | "city" | "state" | "zip" | null;
|
|
9
|
+
start: number;
|
|
10
|
+
end: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** The record `doorplate parse` prints, plus `spans`. Fields not listed here may be added in later versions. */
|
|
14
|
+
export interface ParsedRecord {
|
|
15
|
+
input: string;
|
|
16
|
+
components: Record<string, string>;
|
|
17
|
+
warnings: string[];
|
|
18
|
+
unparsed: string | null;
|
|
19
|
+
key: string;
|
|
20
|
+
key_unit: string;
|
|
21
|
+
slug: string;
|
|
22
|
+
usps: { line1: string | null; line2: string | null; last_line: string | null };
|
|
23
|
+
reso: Record<string, string>;
|
|
24
|
+
place: Record<string, string> | null;
|
|
25
|
+
provenance: { library_version: string; custom_layer: string | null };
|
|
26
|
+
spans: Span[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** What `parse` returns for input it cannot parse (an empty string). */
|
|
30
|
+
export interface ParseErrorRecord {
|
|
31
|
+
input: string;
|
|
32
|
+
error: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type ParseResult = ParsedRecord | ParseErrorRecord;
|
|
36
|
+
|
|
37
|
+
export interface Fields {
|
|
38
|
+
line1?: string | null;
|
|
39
|
+
line2?: string | null;
|
|
40
|
+
city?: string | null;
|
|
41
|
+
state?: string | null;
|
|
42
|
+
zip?: string | null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface BatchResult {
|
|
46
|
+
/** The columns of `doorplate parse-file` (street_number, ..., key, key_unit, slug, warnings, unparsed). */
|
|
47
|
+
columns: string[];
|
|
48
|
+
/** One array of strings per input row, in `columns` order; null for a row with no address text. */
|
|
49
|
+
rows: (string[] | null)[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface Doorplate {
|
|
53
|
+
readonly version: string;
|
|
54
|
+
parse(address: string): ParseResult;
|
|
55
|
+
parseFields(fields: Fields): ParseResult;
|
|
56
|
+
parseBatch(rows: string[], mode?: "line"): BatchResult;
|
|
57
|
+
parseBatch(rows: (string | null)[][], mode: "fields"): BatchResult;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function init(source?: Source): Promise<Doorplate>;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// doorplate for JavaScript: the Rust parser compiled to WebAssembly (crates/doorplate-wasm), wrapped in a
|
|
2
|
+
// small loader. No dependencies, no network access besides loading doorplate.wasm itself.
|
|
3
|
+
//
|
|
4
|
+
// import { init } from "doorplate";
|
|
5
|
+
// const dp = await init();
|
|
6
|
+
// dp.parse("123 N Main St Apt 4, Springfield IL 62704").key;
|
|
7
|
+
|
|
8
|
+
const encoder = new TextEncoder();
|
|
9
|
+
const decoder = new TextDecoder();
|
|
10
|
+
|
|
11
|
+
// Rows per call to the wasm batch export. One call with 100,000 rows is several times slower per row than
|
|
12
|
+
// ten with 10,000 (one huge JSON string in and out), so `parseBatch` splits large inputs.
|
|
13
|
+
const BATCH_CHUNK = 10000;
|
|
14
|
+
|
|
15
|
+
async function readSource(source) {
|
|
16
|
+
if (source === undefined) source = new URL("../doorplate.wasm", import.meta.url);
|
|
17
|
+
source = await source;
|
|
18
|
+
if (source instanceof WebAssembly.Module) return source;
|
|
19
|
+
if (source instanceof URL || typeof source === "string") {
|
|
20
|
+
const url = new URL(source, import.meta.url);
|
|
21
|
+
if (url.protocol === "file:") {
|
|
22
|
+
const { readFile } = await import("node:fs/promises");
|
|
23
|
+
return WebAssembly.compile(await readFile(url));
|
|
24
|
+
}
|
|
25
|
+
source = await fetch(url);
|
|
26
|
+
}
|
|
27
|
+
if (typeof Response !== "undefined" && source instanceof Response) {
|
|
28
|
+
if (!source.ok) throw new Error(`doorplate: could not load the wasm file (HTTP ${source.status})`);
|
|
29
|
+
const type = source.headers.get("content-type") || "";
|
|
30
|
+
if (type.startsWith("application/wasm") && WebAssembly.compileStreaming) {
|
|
31
|
+
return WebAssembly.compileStreaming(source);
|
|
32
|
+
}
|
|
33
|
+
return WebAssembly.compile(await source.arrayBuffer());
|
|
34
|
+
}
|
|
35
|
+
return WebAssembly.compile(source); // ArrayBuffer or typed array
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Load the parser. Call once and keep the result.
|
|
40
|
+
* `source` is optional: the packaged doorplate.wasm is loaded by default. It can also be the bytes, a
|
|
41
|
+
* `WebAssembly.Module`, a URL, or a `Response` (or a promise of one), for runtimes that cannot read a
|
|
42
|
+
* sibling file.
|
|
43
|
+
*/
|
|
44
|
+
export async function init(source) {
|
|
45
|
+
const module = await readSource(source);
|
|
46
|
+
let wasm = (await WebAssembly.instantiate(module, {})).exports;
|
|
47
|
+
|
|
48
|
+
// One call: copy the input in, run, copy the result out. Memory is read fresh after the call because the
|
|
49
|
+
// buffer is replaced when the memory grows; the result buffer is reused by the next call, so it is
|
|
50
|
+
// decoded before returning.
|
|
51
|
+
function call(fn, text) {
|
|
52
|
+
const bytes = encoder.encode(text);
|
|
53
|
+
const ptr = wasm.alloc(bytes.length);
|
|
54
|
+
let out;
|
|
55
|
+
try {
|
|
56
|
+
new Uint8Array(wasm.memory.buffer, ptr, bytes.length).set(bytes);
|
|
57
|
+
const n = wasm[fn](ptr, bytes.length);
|
|
58
|
+
out = decoder.decode(new Uint8Array(wasm.memory.buffer, wasm.result_ptr(), n));
|
|
59
|
+
} catch (e) {
|
|
60
|
+
if (e instanceof WebAssembly.RuntimeError) {
|
|
61
|
+
// A trap leaves the instance unusable: start a fresh one and report.
|
|
62
|
+
WebAssembly.instantiate(module, {}).then((r) => (wasm = r.exports));
|
|
63
|
+
throw new Error("doorplate: the parser failed on this input (WebAssembly trap)", { cause: e });
|
|
64
|
+
}
|
|
65
|
+
throw e;
|
|
66
|
+
} finally {
|
|
67
|
+
try {
|
|
68
|
+
wasm.dealloc(ptr, bytes.length);
|
|
69
|
+
} catch {
|
|
70
|
+
/* the instance is being replaced */
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return JSON.parse(out);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const columns = call("parse_batch", JSON.stringify({ mode: "line", rows: [] })).columns;
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
/** The parser's version, e.g. "0.7.2". */
|
|
80
|
+
version: call("parse", "1 A St, Reno NV 89501").provenance.library_version,
|
|
81
|
+
/** Parse one address line. The record has `error` (and no `components`) when the input is empty. */
|
|
82
|
+
parse: (address) => call("parse", String(address)),
|
|
83
|
+
/** Parse separate fields: `{ line1, line2, city, state, zip }`, any of them missing or null. */
|
|
84
|
+
parseFields: (fields) => call("parse_fields", JSON.stringify(fields)),
|
|
85
|
+
/**
|
|
86
|
+
* Parse many one-line addresses (`mode: "line"`, strings) or field rows (`mode: "fields"`, arrays
|
|
87
|
+
* `[line1, line2, city, state, zip]`). Returns `{ columns, rows }`: the columns of `doorplate parse-file`,
|
|
88
|
+
* one array of strings per row, `null` for a row with no address text.
|
|
89
|
+
*/
|
|
90
|
+
parseBatch(rows, mode = "line") {
|
|
91
|
+
const out = [];
|
|
92
|
+
for (let i = 0; i < rows.length || i === 0; i += BATCH_CHUNK) {
|
|
93
|
+
const r = call("parse_batch", JSON.stringify({ mode, rows: rows.slice(i, i + BATCH_CHUNK) }));
|
|
94
|
+
if (r.error) throw new Error(`doorplate: ${r.error}`);
|
|
95
|
+
for (const row of r.rows) out.push(row);
|
|
96
|
+
}
|
|
97
|
+
return { columns, rows: out };
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|