@mailwoman/tiger 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/README.md +3 -0
  2. package/block-group.ts +151 -0
  3. package/class-code.ts +1026 -0
  4. package/constants.ts +252 -0
  5. package/county-subdivision.ts +154 -0
  6. package/county.ts +135 -0
  7. package/files.ts +251 -0
  8. package/functional-status.ts +64 -0
  9. package/geoid.ts +678 -0
  10. package/index.ts +18 -0
  11. package/legal-statistical-area.ts +222 -0
  12. package/out/block-group.d.ts +135 -0
  13. package/out/block-group.d.ts.map +1 -0
  14. package/out/block-group.js +7 -0
  15. package/out/block-group.js.map +1 -0
  16. package/out/class-code.d.ts +863 -0
  17. package/out/class-code.d.ts.map +1 -0
  18. package/out/class-code.js +864 -0
  19. package/out/class-code.js.map +1 -0
  20. package/out/constants.d.ts +220 -0
  21. package/out/constants.d.ts.map +1 -0
  22. package/out/constants.js +98 -0
  23. package/out/constants.js.map +1 -0
  24. package/out/county-subdivision.d.ts +139 -0
  25. package/out/county-subdivision.d.ts.map +1 -0
  26. package/out/county-subdivision.js +7 -0
  27. package/out/county-subdivision.js.map +1 -0
  28. package/out/county.d.ts +120 -0
  29. package/out/county.d.ts.map +1 -0
  30. package/out/county.js +9 -0
  31. package/out/county.js.map +1 -0
  32. package/out/files.d.ts +118 -0
  33. package/out/files.d.ts.map +1 -0
  34. package/out/files.js +142 -0
  35. package/out/files.js.map +1 -0
  36. package/out/functional-status.d.ts +64 -0
  37. package/out/functional-status.d.ts.map +1 -0
  38. package/out/functional-status.js +65 -0
  39. package/out/functional-status.js.map +1 -0
  40. package/out/geoid.d.ts +438 -0
  41. package/out/geoid.d.ts.map +1 -0
  42. package/out/geoid.js +255 -0
  43. package/out/geoid.js.map +1 -0
  44. package/out/index.d.ts +18 -0
  45. package/out/index.d.ts.map +1 -0
  46. package/out/index.js +18 -0
  47. package/out/index.js.map +1 -0
  48. package/out/legal-statistical-area.d.ts +222 -0
  49. package/out/legal-statistical-area.d.ts.map +1 -0
  50. package/out/legal-statistical-area.js +223 -0
  51. package/out/legal-statistical-area.js.map +1 -0
  52. package/out/sdk/fetch.d.ts.map +1 -0
  53. package/out/sdk/fetch.js.map +1 -0
  54. package/out/sdk/index.d.ts +7 -0
  55. package/out/sdk/index.d.ts.map +1 -0
  56. package/out/sdk/index.js +7 -0
  57. package/out/sdk/index.js.map +1 -0
  58. package/out/sdk/redistricting.d.ts.map +1 -0
  59. package/out/sdk/redistricting.js.map +1 -0
  60. package/out/sdk/schema.d.ts.map +1 -0
  61. package/out/sdk/schema.js.map +1 -0
  62. package/out/sdk/state/TIGERTabulatedBlockEntity.d.ts +13 -0
  63. package/out/sdk/state/TIGERTabulatedBlockEntity.d.ts.map +1 -0
  64. package/out/sdk/state/TIGERTabulatedBlockEntity.js +22 -0
  65. package/out/sdk/state/TIGERTabulatedBlockEntity.js.map +1 -0
  66. package/out/sdk/state/TIGERTractEntity.d.ts +31 -0
  67. package/out/sdk/state/TIGERTractEntity.d.ts.map +1 -0
  68. package/out/sdk/state/TIGERTractEntity.js +7 -0
  69. package/out/sdk/state/TIGERTractEntity.js.map +1 -0
  70. package/out/sdk/state/block-operations.d.ts +36 -0
  71. package/out/sdk/state/block-operations.d.ts.map +1 -0
  72. package/out/sdk/state/block-operations.js +37 -0
  73. package/out/sdk/state/block-operations.js.map +1 -0
  74. package/out/sdk/state/index.d.ts +9 -0
  75. package/out/sdk/state/index.d.ts.map +1 -0
  76. package/out/sdk/state/index.js +9 -0
  77. package/out/sdk/state/index.js.map +1 -0
  78. package/out/state.d.ts +595 -0
  79. package/out/state.d.ts.map +1 -0
  80. package/out/state.js +384 -0
  81. package/out/state.js.map +1 -0
  82. package/out/tabulation-block.d.ts +43 -0
  83. package/out/tabulation-block.d.ts.map +1 -0
  84. package/out/tabulation-block.js +9 -0
  85. package/out/tabulation-block.js.map +1 -0
  86. package/out/tract.d.ts +36 -0
  87. package/out/tract.d.ts.map +1 -0
  88. package/out/tract.js +8 -0
  89. package/out/tract.js.map +1 -0
  90. package/out/tsconfig.tsbuildinfo +1 -0
  91. package/package.json +41 -0
  92. package/sdk/data/nation/index.json +14642 -0
  93. package/sdk/index.ts +7 -0
  94. package/sdk/initialize.sql +161 -0
  95. package/sdk/state/TIGERTabulatedBlockEntity.ts +25 -0
  96. package/sdk/state/TIGERTractEntity.ts +34 -0
  97. package/sdk/state/block-operations.ts +69 -0
  98. package/sdk/state/index.ts +9 -0
  99. package/state.ts +553 -0
  100. package/tabulation-block.ts +68 -0
  101. package/tract.ts +52 -0
  102. package/tsconfig.json +16 -0
  103. package/typedoc.json +5 -0
package/out/geoid.d.ts ADDED
@@ -0,0 +1,438 @@
1
+ /**
2
+ * @copyright Sister Software.
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ */
6
+ import type { Tagged } from "type-fest";
7
+ import { AdminLevel1Code, FIPSStateCode } from "./state.js";
8
+ /**
9
+ * A 4-digit number assigned by the Census to uniquely identify a block.
10
+ *
11
+ * ```txt
12
+ * (State Code)━━┳┓ ┏┳┳┳┳┳━━(Tract Code)
13
+ * 482012231001050
14
+ * ┗╋┛ ┗┻┻┻━(Block Code)
15
+ * (County Code)
16
+ * ```
17
+ *
18
+ * @category Census
19
+ * @category FIPS
20
+ * @type {string}
21
+ * @see {@linkcode FIPSBlockGroupCode} for a less specific level.
22
+ * @see {@linkcode FIPSBlockGeoID} for the full GeoID.
23
+ * @pattern ^\d{4}$
24
+ * @minLength 4
25
+ * @maxLength 4
26
+ * @title FIPS Block Code
27
+ */
28
+ export type FIPSBlockCode = Tagged<string, "FIPSBlockCode">;
29
+ /**
30
+ * A 15-digit number assigned by the Census to uniquely identify a block.
31
+ *
32
+ * ```txt
33
+ * (State Code)━━┳┓ ┏┳┳┳┳┳━━(Tract Code)
34
+ * 482012231001050
35
+ * ┗╋┛ ┗┻┻┻━(Block Code)
36
+ * (County Code)
37
+ * ```
38
+ *
39
+ * @category Census
40
+ * @category FIPS
41
+ * @type {string}
42
+ * @see {@linkcode FIPSBlockCode} for the 4-digit version.
43
+ * @pattern ^\d{15}$
44
+ * @minLength 15
45
+ * @maxLength 15
46
+ * @title FIPS Block Geo ID
47
+ */
48
+ export type FIPSBlockGeoID = Tagged<string, "FIPSBlockGeoID">;
49
+ /**
50
+ * A 1-digit number assigned by the Census to uniquely identify a block group.
51
+ *
52
+ * ```txt
53
+ * (State Code)━━┳┓ ┏┳┳┳┳┳━━(Tract Code)
54
+ * 060133740002###
55
+ * ┗╋┛ ┗━(Block Group Code)
56
+ * (County Code)
57
+ * ```
58
+ *
59
+ * @category Census
60
+ * @category FIPS
61
+ * @type {string}
62
+ * @see {@linkcode FIPSBlockCode} for a more specific level.
63
+ * @see {@linkcode FIPSTractCode} for a less specific level.
64
+ * @pattern ^\d{1}$
65
+ * @minLength 1
66
+ * @maxLength 1
67
+ * @title FIPS Block Group Code
68
+ */
69
+ export type FIPSBlockGroupCode = Tagged<string, "FIPSBlockGroupCode">;
70
+ /**
71
+ * A 6-digit number assigned by the Census to uniquely identify a tract.
72
+ *
73
+ * ```txt
74
+ * (State Code)━━┳┓ ┏┳┳┳┳┳━━(Tract Code)
75
+ * 06013374000####
76
+ * ┗╋┛
77
+ * (County Code)
78
+ * ```
79
+ *
80
+ * @category Census
81
+ * @category FIPS
82
+ * @type {string}
83
+ * @see {@linkcode FIPSBlockGroupCode} for a more specific level.
84
+ * @see {@linkcode FIPSCountySubDivisionCode} for a less specific level.
85
+ * @pattern ^\d{6}$
86
+ * @minLength 6
87
+ * @maxLength 6
88
+ * @title FIPS Tract Code
89
+ */
90
+ export type FIPSTractCode = Tagged<string, "FIPSTractCode">;
91
+ /**
92
+ * An 11-digit number assigned by the Census to uniquely identify a tract.
93
+ *
94
+ * ```txt
95
+ * (State Code)━━┳┓ ┏┳┳┳┳┳━━(Tract Code)
96
+ * 06013374000####
97
+ * ┗╋┛
98
+ * (County Code)
99
+ * ```
100
+ *
101
+ * @category Census
102
+ * @category FIPS
103
+ * @type {string}
104
+ * @see {@linkcode FIPSBlockCode} for the 4-digit version.
105
+ * @pattern ^\d{6}$
106
+ * @minLength 6
107
+ * @maxLength 6
108
+ * @title FIPS Tract Geo ID
109
+ */
110
+ export type FIPSTractGeoID = Tagged<string, "FIPSTractGeoID">;
111
+ /**
112
+ * A 5-digit number assigned by the Census to uniquely identify a county sub-division.
113
+ *
114
+ * ```txt
115
+ * (State Code)━━┳┓ ┏┳┳┳┳━━(County Sub-Division Code)
116
+ * 0601337400#####
117
+ * ┗╋┛
118
+ * (County Code)
119
+ * ```
120
+ *
121
+ * @category Census
122
+ * @category FIPS
123
+ * @type {string}
124
+ * @see {@linkcode FIPSTractCode} for a more specific level.
125
+ * @see {@linkcode FIPSCountyCode} for a less specific level.
126
+ * @pattern ^\d{5}$
127
+ * @minLength 5
128
+ * @maxLength 5
129
+ * @title FIPS County Sub-Division Code
130
+ */
131
+ export type FIPSCountySubDivisionCode = Tagged<string, "FIPSCountySubDivisionCode">;
132
+ /**
133
+ * A 3-digit number assigned by the Census to uniquely identify a county.
134
+ *
135
+ * ```txt
136
+ * (State Code)━━┳┓
137
+ * 06013##########
138
+ * ┗┻┻━(County Code)
139
+ * ```
140
+ *
141
+ * @category Census
142
+ * @category FIPS
143
+ * @type {string}
144
+ * @see {@linkcode FIPSCountySubDivisionCode} for a more specific level.
145
+ * @see {@linkcode AdminLevel1Code} for a less specific level.
146
+ * @pattern ^\d{3}$
147
+ * @minLength 3
148
+ * @maxLength 3
149
+ * @title FIPS County Code
150
+ */
151
+ export type FIPSCountyCode = Tagged<string, "FIPSCountyCode">;
152
+ /**
153
+ * A 5-digit number assigned by the Census to uniquely identify a place.
154
+ *
155
+ * ```txt
156
+ * (State Code)━━┳┓
157
+ * 4835000########
158
+ * ┗┻┻┻┻━(Place Code)
159
+ * ```
160
+ *
161
+ * @category Census
162
+ * @category FIPS
163
+ * @type {string}
164
+ * @minLength 5
165
+ * @maxLength 5
166
+ * @pattern ^\d{5}$
167
+ * @title FIPS Place Code
168
+ */
169
+ export type FIPSPlaceCode = Tagged<string, "FIPSPlaceCode">;
170
+ /**
171
+ * A 2-digit number assigned by the Census to uniquely identify a congressional district.
172
+ *
173
+ * ```txt
174
+ * (State Code)━━┳┓
175
+ * 0902···········
176
+ * ┗┻━(Congressional District Code)
177
+ * ```
178
+ *
179
+ * @category Census
180
+ * @category FIPS
181
+ * @type {string}
182
+ * @see {@linkcode AdminLevel1Code} for a less specific level.
183
+ * @see {@linkcode FIPSPlaceCode} for a more specific level.
184
+ * @pattern ^\d{2}$
185
+ * @minLength 2
186
+ * @maxLength 2
187
+ * @title FIPS Congressional District Code
188
+ */
189
+ export type FIPSCongressionalDistrictCode = Tagged<string, "FIPSConressionalDistrictCode">;
190
+ /**
191
+ * A part of a GeoID to it's respective name.
192
+ *
193
+ * @internal
194
+ */
195
+ export declare enum GeoIDPart {
196
+ /**
197
+ * The state code part of a GeoID.
198
+ *
199
+ * Sometimes considered the administrative area level 1 code.
200
+ */
201
+ State = "state_code",
202
+ /**
203
+ * The county code part of a GeoID.
204
+ *
205
+ * Sometimes considered the administrative area level 2 code.
206
+ */
207
+ County = "county_code",
208
+ /**
209
+ * The county subdivision code part of a GeoID.
210
+ *
211
+ * Sometimes considered the administrative area level 3 code.
212
+ */
213
+ CountySubDivision = "county_sub_division_code",
214
+ /**
215
+ * The congressional district code part of a GeoID.
216
+ */
217
+ CongressionalDistrict = "congressional_district_code",
218
+ /**
219
+ * A place as defined by the Census, such as a city or town.
220
+ */
221
+ Place = "place_code",
222
+ /**
223
+ * The tract code part of a GeoID. The third most granular part.
224
+ */
225
+ Tract = "tract_code",
226
+ /**
227
+ * The block group code part of a GeoID. The second most granular part.
228
+ */
229
+ BlockGroup = "block_group_code",
230
+ /**
231
+ * The block code part of a GeoID. The most granular part.
232
+ */
233
+ Block = "block_code"
234
+ }
235
+ /**
236
+ * Mapping of GeoID parts to their respective FIPS codes types.
237
+ *
238
+ * @internal
239
+ */
240
+ export type GeoIDPartMapping = {
241
+ [GeoIDPart.State]: FIPSStateCode;
242
+ [GeoIDPart.County]: FIPSCountyCode;
243
+ [GeoIDPart.CountySubDivision]: FIPSCountySubDivisionCode;
244
+ [GeoIDPart.CongressionalDistrict]: FIPSCongressionalDistrictCode;
245
+ [GeoIDPart.Place]: FIPSPlaceCode;
246
+ [GeoIDPart.Tract]: FIPSTractCode;
247
+ [GeoIDPart.BlockGroup]: FIPSBlockGroupCode;
248
+ [GeoIDPart.Block]: FIPSBlockCode;
249
+ };
250
+ /**
251
+ * Record of GeoID parts and their respective lengths.
252
+ *
253
+ * Note that the length of a GeoID is the sum of the lengths of its parts.
254
+ *
255
+ * @internal
256
+ */
257
+ export declare const GeoIDPartLength: {
258
+ readonly state_code: 2;
259
+ readonly county_code: 3;
260
+ readonly county_sub_division_code: 5;
261
+ readonly congressional_district_code: 2;
262
+ readonly place_code: 5;
263
+ readonly tract_code: 6;
264
+ readonly block_group_code: 1;
265
+ readonly block_code: 4;
266
+ };
267
+ /**
268
+ * A GeoID parsed to the block level. The most granular level.
269
+ *
270
+ * @title Parsed GeoID Block Level
271
+ * @public
272
+ */
273
+ export interface ParsedGeoIDBlockLevel {
274
+ [GeoIDPart.State]: FIPSStateCode;
275
+ [GeoIDPart.County]: FIPSCountyCode;
276
+ [GeoIDPart.CountySubDivision]: FIPSCountySubDivisionCode;
277
+ [GeoIDPart.Tract]: FIPSTractCode;
278
+ [GeoIDPart.Place]: FIPSPlaceCode;
279
+ [GeoIDPart.BlockGroup]: FIPSBlockGroupCode;
280
+ [GeoIDPart.Block]: FIPSBlockCode;
281
+ [GeoIDPart.BlockGroup]: FIPSBlockGroupCode;
282
+ [GeoIDPart.CongressionalDistrict]: FIPSCongressionalDistrictCode;
283
+ }
284
+ /**
285
+ * A GeoID parsed to the block group level. The second most granular level.
286
+ *
287
+ * @internal
288
+ */
289
+ export type ParsedGeoIDBlockGroupLevel = {
290
+ [GeoIDPart.State]: AdminLevel1Code;
291
+ [GeoIDPart.County]: FIPSCountyCode;
292
+ [GeoIDPart.CountySubDivision]: FIPSCountySubDivisionCode;
293
+ [GeoIDPart.Tract]: FIPSTractCode;
294
+ [GeoIDPart.BlockGroup]: FIPSBlockGroupCode;
295
+ [GeoIDPart.Block]: undefined;
296
+ };
297
+ /**
298
+ * A GeoID parsed to the tract level. The third most granular level.
299
+ *
300
+ * @internal
301
+ */
302
+ export type ParsedGeoIDTractLevel = {
303
+ [GeoIDPart.State]: AdminLevel1Code;
304
+ [GeoIDPart.County]: FIPSCountyCode;
305
+ [GeoIDPart.CountySubDivision]: FIPSCountySubDivisionCode;
306
+ [GeoIDPart.Tract]: FIPSTractCode;
307
+ [GeoIDPart.BlockGroup]: undefined;
308
+ [GeoIDPart.Block]: undefined;
309
+ };
310
+ /**
311
+ * A GeoID parsed to a partial level.
312
+ *
313
+ * @internal
314
+ */
315
+ export type ParsedGeoIDPartial = {
316
+ [P in GeoIDPart]?: string;
317
+ };
318
+ /**
319
+ * A GeoID parsed to the county-subdivision level. The fourth most granular level.
320
+ *
321
+ * @internal
322
+ */
323
+ export type ParsedGeoIDCountySubDivisionLevel = {
324
+ [GeoIDPart.State]: AdminLevel1Code;
325
+ [GeoIDPart.County]: FIPSCountyCode;
326
+ [GeoIDPart.CountySubDivision]: FIPSCountySubDivisionCode;
327
+ [GeoIDPart.Tract]: undefined;
328
+ [GeoIDPart.BlockGroup]: undefined;
329
+ [GeoIDPart.Block]: undefined;
330
+ };
331
+ /**
332
+ * A GeoID parsed to the county level. The fifth most granular level.
333
+ *
334
+ * @internal
335
+ */
336
+ export type ParsedGeoIDCountyLevel = {
337
+ [GeoIDPart.State]: AdminLevel1Code;
338
+ [GeoIDPart.County]: FIPSCountyCode;
339
+ [GeoIDPart.CountySubDivision]: undefined;
340
+ [GeoIDPart.Tract]: undefined;
341
+ [GeoIDPart.BlockGroup]: undefined;
342
+ [GeoIDPart.Block]: undefined;
343
+ };
344
+ /**
345
+ * A GeoID parsed to the state level. The least granular level.
346
+ *
347
+ * @internal
348
+ */
349
+ export type ParsedGeoIDStateLevel = {
350
+ [GeoIDPart.State]: AdminLevel1Code;
351
+ [GeoIDPart.County]: undefined;
352
+ [GeoIDPart.CountySubDivision]: undefined;
353
+ [GeoIDPart.Tract]: undefined;
354
+ [GeoIDPart.BlockGroup]: undefined;
355
+ [GeoIDPart.Block]: undefined;
356
+ };
357
+ /**
358
+ * A GeoID parsed to a specific level.
359
+ *
360
+ * @internal
361
+ */
362
+ export type ParsedGeoID = ParsedGeoIDBlockLevel | ParsedGeoIDBlockGroupLevel | ParsedGeoIDTractLevel | ParsedGeoIDCountySubDivisionLevel | ParsedGeoIDCountyLevel | ParsedGeoIDStateLevel;
363
+ export declare class GeoIDParsingError extends Error {
364
+ constructor(message: string);
365
+ }
366
+ export declare class GeoIDInputMatcher<T extends GeoIDPart[] = GeoIDPart[]> {
367
+ pattern: RegExp;
368
+ components: T;
369
+ /**
370
+ * The specific length of a valid GeoID input.
371
+ */
372
+ length: number;
373
+ constructor(...geoIDComponents: T);
374
+ parse<R extends ParsedGeoIDPartial>(input: string): R | null;
375
+ test(input: string): boolean;
376
+ }
377
+ /**
378
+ * Record of GeoID input matchers, i.e. a pattern and parser for each grouping of GeoID components.
379
+ *
380
+ * @see {@linkcode GeoIDComponentMatchers} for the component-level matchers.
381
+ * @internal
382
+ */
383
+ export declare const GeoIDInputMatchers: {
384
+ readonly block_code: GeoIDInputMatcher<[GeoIDPart.State, GeoIDPart.County, GeoIDPart.Tract, GeoIDPart.Block]>;
385
+ readonly block_group_code: GeoIDInputMatcher<[GeoIDPart.State, GeoIDPart.County, GeoIDPart.Tract, GeoIDPart.BlockGroup]>;
386
+ readonly tract_code: GeoIDInputMatcher<[GeoIDPart.State, GeoIDPart.County, GeoIDPart.Tract]>;
387
+ readonly place_code: GeoIDInputMatcher<[GeoIDPart.State, GeoIDPart.County, GeoIDPart.Place]>;
388
+ readonly county_sub_division_code: GeoIDInputMatcher<[GeoIDPart.State, GeoIDPart.County, GeoIDPart.CountySubDivision]>;
389
+ readonly county_code: GeoIDInputMatcher<[GeoIDPart.State, GeoIDPart.County]>;
390
+ readonly congressional_district_code: GeoIDInputMatcher<[GeoIDPart.State, GeoIDPart.CongressionalDistrict]>;
391
+ readonly state_code: GeoIDInputMatcher<[GeoIDPart.State]>;
392
+ };
393
+ /**
394
+ * Type-predicate for checking if a value appears to be a valid GeoID component.
395
+ */
396
+ export declare function isGeoIDComponent<T extends GeoIDPart>(component: T, input: unknown): input is GeoIDPartMapping[T];
397
+ /**
398
+ * Given a block GeoID, parse it into its components.
399
+ *
400
+ * @category Census
401
+ * @category FIPS
402
+ */
403
+ export declare function parseFIPSBlockGeoID(input: FIPSBlockGeoID): ParsedGeoIDBlockLevel;
404
+ /**
405
+ * Given a tract GeoID, parse it into its components.
406
+ *
407
+ * @category Census
408
+ * @category FIPS
409
+ */
410
+ export declare function parseFIPSTractGeoID(input: FIPSTractGeoID): ParsedGeoIDTractLevel;
411
+ /**
412
+ * Given a GeoID, parse it into its components.
413
+ *
414
+ * @category Census
415
+ * @category FIPS
416
+ */
417
+ export declare function parseGeoID(input: FIPSBlockGeoID): ParsedGeoIDBlockLevel;
418
+ export declare function parseGeoID(input: FIPSBlockGroupCode): ParsedGeoIDBlockGroupLevel;
419
+ export declare function parseGeoID(input: FIPSTractCode): ParsedGeoIDTractLevel;
420
+ export declare function parseGeoID(input: FIPSCountySubDivisionCode): ParsedGeoIDCountySubDivisionLevel;
421
+ export declare function parseGeoID(input: FIPSCountyCode): ParsedGeoIDCountyLevel;
422
+ export declare function parseGeoID(input: AdminLevel1Code): ParsedGeoIDStateLevel;
423
+ export declare function parseGeoID(input: unknown): ParsedGeoID | null;
424
+ /**
425
+ * Format a parsed block GeoID back into a string.
426
+ *
427
+ * @category Census
428
+ * @category FIPS
429
+ */
430
+ export declare function formatGeoID(input: ParsedGeoIDBlockLevel): FIPSBlockGeoID;
431
+ /**
432
+ * Format a parsed GeoID back into a string.
433
+ *
434
+ * @category Census
435
+ * @category FIPS
436
+ */
437
+ export declare function formatGeoID(input: ParsedGeoIDPartial): string;
438
+ //# sourceMappingURL=geoid.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"geoid.d.ts","sourceRoot":"","sources":["../geoid.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AACvC,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAI3D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;AAE3D;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAA;AAE7D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,MAAM,kBAAkB,GAAG,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAAA;AAErE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;AAE3D;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAA;AAE7D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,MAAM,yBAAyB,GAAG,MAAM,CAAC,MAAM,EAAE,2BAA2B,CAAC,CAAA;AAEnF;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAA;AAE7D;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;AAE3D;;;;;;;;;;;;;;;;;;GAkBG;AACH,MA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AI,CAAA;AAqC9D;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,qBAAqB,GAAG,cAAc,CAAA;AACzE;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,kBAAkB,GAAG,MAAM,CAAA"}
package/out/geoid.js ADDED
@@ -0,0 +1,255 @@
1
+ /**
2
+ * @copyright Sister Software.
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ */
6
+ import { AdminLevel1Code, FIPSStateCode } from "./state.js";
7
+ //#endregion
8
+ //#region GeoID Parsing Setup
9
+ /**
10
+ * A part of a GeoID to it's respective name.
11
+ *
12
+ * @internal
13
+ */
14
+ export var GeoIDPart;
15
+ (function (GeoIDPart) {
16
+ /**
17
+ * The state code part of a GeoID.
18
+ *
19
+ * Sometimes considered the administrative area level 1 code.
20
+ */
21
+ GeoIDPart["State"] = "state_code";
22
+ /**
23
+ * The county code part of a GeoID.
24
+ *
25
+ * Sometimes considered the administrative area level 2 code.
26
+ */
27
+ GeoIDPart["County"] = "county_code";
28
+ /**
29
+ * The county subdivision code part of a GeoID.
30
+ *
31
+ * Sometimes considered the administrative area level 3 code.
32
+ */
33
+ GeoIDPart["CountySubDivision"] = "county_sub_division_code";
34
+ /**
35
+ * The congressional district code part of a GeoID.
36
+ */
37
+ GeoIDPart["CongressionalDistrict"] = "congressional_district_code";
38
+ /**
39
+ * A place as defined by the Census, such as a city or town.
40
+ */
41
+ GeoIDPart["Place"] = "place_code";
42
+ /**
43
+ * The tract code part of a GeoID. The third most granular part.
44
+ */
45
+ GeoIDPart["Tract"] = "tract_code";
46
+ /**
47
+ * The block group code part of a GeoID. The second most granular part.
48
+ */
49
+ GeoIDPart["BlockGroup"] = "block_group_code";
50
+ /**
51
+ * The block code part of a GeoID. The most granular part.
52
+ */
53
+ GeoIDPart["Block"] = "block_code";
54
+ })(GeoIDPart || (GeoIDPart = {}));
55
+ /**
56
+ * Record of GeoID parts and their respective lengths.
57
+ *
58
+ * Note that the length of a GeoID is the sum of the lengths of its parts.
59
+ *
60
+ * @internal
61
+ */
62
+ export const GeoIDPartLength = {
63
+ [GeoIDPart.State]: 2,
64
+ [GeoIDPart.County]: 3,
65
+ [GeoIDPart.CountySubDivision]: 5,
66
+ [GeoIDPart.CongressionalDistrict]: 2,
67
+ [GeoIDPart.Place]: 5,
68
+ [GeoIDPart.Tract]: 6,
69
+ [GeoIDPart.BlockGroup]: 1,
70
+ [GeoIDPart.Block]: 4,
71
+ };
72
+ export class GeoIDParsingError extends Error {
73
+ constructor(message) {
74
+ super(message);
75
+ this.name = "GeoIDParsingError";
76
+ }
77
+ }
78
+ //#endregion
79
+ //#region GeoID Input Matchers
80
+ export class GeoIDInputMatcher {
81
+ pattern;
82
+ components;
83
+ /**
84
+ * The specific length of a valid GeoID input.
85
+ */
86
+ length;
87
+ constructor(...geoIDComponents) {
88
+ this.components = geoIDComponents;
89
+ this.length = 0;
90
+ const matchedGroups = this.components.map((component) => {
91
+ const groupLength = GeoIDPartLength[component];
92
+ this.length += groupLength;
93
+ return `(\\d{${groupLength}})`;
94
+ });
95
+ this.pattern = new RegExp("^" + matchedGroups.join(""));
96
+ }
97
+ parse(input) {
98
+ const match = input.match(this.pattern);
99
+ if (!match)
100
+ return null;
101
+ const result = {};
102
+ for (let i = 0; i < this.components.length; i++) {
103
+ result[this.components[i]] = match[i + 1];
104
+ }
105
+ return result;
106
+ }
107
+ test(input) {
108
+ return this.pattern.test(input);
109
+ }
110
+ }
111
+ /**
112
+ * Record of GeoID input matchers, i.e. a pattern and parser for each grouping of GeoID components.
113
+ *
114
+ * @see {@linkcode GeoIDComponentMatchers} for the component-level matchers.
115
+ * @internal
116
+ */
117
+ export const GeoIDInputMatchers = {
118
+ [GeoIDPart.Block]: new GeoIDInputMatcher(
119
+ // ---
120
+ GeoIDPart.State, GeoIDPart.County, GeoIDPart.Tract, GeoIDPart.Block),
121
+ [GeoIDPart.BlockGroup]: new GeoIDInputMatcher(
122
+ // ---
123
+ GeoIDPart.State, GeoIDPart.County, GeoIDPart.Tract, GeoIDPart.BlockGroup),
124
+ [GeoIDPart.Tract]: new GeoIDInputMatcher(
125
+ // ---
126
+ GeoIDPart.State, GeoIDPart.County, GeoIDPart.Tract),
127
+ [GeoIDPart.Place]: new GeoIDInputMatcher(
128
+ // ---
129
+ GeoIDPart.State, GeoIDPart.County, GeoIDPart.Place),
130
+ [GeoIDPart.CountySubDivision]: new GeoIDInputMatcher(
131
+ // ---
132
+ GeoIDPart.State, GeoIDPart.County, GeoIDPart.CountySubDivision),
133
+ [GeoIDPart.County]: new GeoIDInputMatcher(
134
+ // ---
135
+ GeoIDPart.State, GeoIDPart.County),
136
+ [GeoIDPart.CongressionalDistrict]: new GeoIDInputMatcher(
137
+ // ---
138
+ GeoIDPart.State, GeoIDPart.CongressionalDistrict),
139
+ [GeoIDPart.State]: new GeoIDInputMatcher(GeoIDPart.State),
140
+ };
141
+ /**
142
+ * A record of GeoID component matchers, i.e. a pattern and parser for each GeoID component.
143
+ *
144
+ * @see {@linkcode GeoIDInputMatchers} for the input-level matchers.
145
+ * @internal
146
+ */
147
+ const GeoIDComponentMatchers = Object.fromEntries(Object.values(GeoIDPart).map((value) => {
148
+ return [value, new GeoIDInputMatcher(value)];
149
+ }));
150
+ /**
151
+ * Type-predicate for checking if a value appears to be a valid GeoID component.
152
+ */
153
+ export function isGeoIDComponent(component, input) {
154
+ if (!input)
155
+ return false;
156
+ if (typeof input !== "string")
157
+ return false;
158
+ const matcher = GeoIDComponentMatchers[component];
159
+ return matcher.test(input);
160
+ }
161
+ //#endregion
162
+ //#region GeoID Parsing
163
+ /**
164
+ * GeoID input matchers sorted by length in descending order, such that the most specific matchers
165
+ * are first.
166
+ */
167
+ const OrderedGeoIDInputMatchers = Object.values(GeoIDInputMatchers).sort((a, b) => b.length - a.length);
168
+ /**
169
+ * Given a block GeoID, parse it into its components.
170
+ *
171
+ * @category Census
172
+ * @category FIPS
173
+ */
174
+ export function parseFIPSBlockGeoID(input) {
175
+ const parsedBlock = GeoIDInputMatchers[GeoIDPart.Block].parse(input);
176
+ const { congressional_district_code } = GeoIDInputMatchers[GeoIDPart.CongressionalDistrict].parse(input);
177
+ const { county_sub_division_code } = GeoIDInputMatchers[GeoIDPart.CountySubDivision].parse(input);
178
+ const block_group_code = parsedBlock.block_code.slice(0, 1);
179
+ const parsed = {
180
+ ...parsedBlock,
181
+ congressional_district_code,
182
+ county_sub_division_code,
183
+ block_group_code,
184
+ };
185
+ return parsed;
186
+ }
187
+ /**
188
+ * Given a tract GeoID, parse it into its components.
189
+ *
190
+ * @category Census
191
+ * @category FIPS
192
+ */
193
+ export function parseFIPSTractGeoID(input) {
194
+ const parsedTract = GeoIDInputMatchers[GeoIDPart.Tract].parse(input);
195
+ const { county_sub_division_code } = GeoIDInputMatchers[GeoIDPart.CountySubDivision].parse(input);
196
+ const parsed = {
197
+ ...parsedTract,
198
+ [GeoIDPart.CountySubDivision]: county_sub_division_code,
199
+ };
200
+ return parsed;
201
+ }
202
+ export function parseGeoID(input) {
203
+ if (!input)
204
+ return null;
205
+ if (typeof input === "number")
206
+ input = input.toString();
207
+ if (typeof input !== "string")
208
+ return null;
209
+ if (input.length === GeoIDInputMatchers[GeoIDPart.Block].length) {
210
+ return parseFIPSBlockGeoID(input);
211
+ }
212
+ if (input.length === GeoIDInputMatchers[GeoIDPart.Tract].length) {
213
+ return parseFIPSTractGeoID(input);
214
+ }
215
+ for (const matcher of OrderedGeoIDInputMatchers) {
216
+ // Do we even have a chance of matching?
217
+ if (matcher.length !== input.length)
218
+ continue;
219
+ const match = matcher.parse(input);
220
+ if (!match)
221
+ continue;
222
+ Object.assign(match, {
223
+ toJSON: () => match,
224
+ });
225
+ return match;
226
+ }
227
+ return null;
228
+ }
229
+ export function formatGeoID(input) {
230
+ if (!input || typeof input !== "object")
231
+ return null;
232
+ const {
233
+ // ---
234
+ block_code, block_group_code, tract_code, state_code, county_sub_division_code, county_code, } = input;
235
+ if (block_code) {
236
+ return `${state_code}${county_code}${tract_code}${block_code}`;
237
+ }
238
+ if (block_group_code) {
239
+ return `${state_code}${county_code}${tract_code}${block_group_code}`;
240
+ }
241
+ if (tract_code) {
242
+ return `${state_code}${county_code}${tract_code}`;
243
+ }
244
+ if (county_sub_division_code) {
245
+ return `${state_code}${county_code}${county_sub_division_code}`;
246
+ }
247
+ if (county_code) {
248
+ return `${state_code}${county_code}`;
249
+ }
250
+ if (state_code)
251
+ return state_code;
252
+ return null;
253
+ }
254
+ //#endregion
255
+ //# sourceMappingURL=geoid.js.map