/** * @copyright Sister Software. * @license AGPL-3.0 * @author Teffen Ellis, et al. */ import type { Tagged } from "type-fest"; import type { AdminLevel1Code, FIPSStateCode } from "./state.ts"; /** * A 4-digit number assigned by the Census to uniquely identify a block. * * ```txt * (State Code)━━┳┓ ┏┳┳┳┳┳━━(Tract Code) * 482012231001050 * ┗╋┛ ┗┻┻┻━(Block Code) * (County Code) * ``` * * @category Census * @category FIPS * @type {string} * @pattern ^\d{4}$ * @minLength 4 * @maxLength 4 * @title FIPS Block Code * @see {@linkcode FIPSBlockGroupCode} for a less specific level. * @see {@linkcode FIPSBlockGeoID} for the full GeoID. */ export type FIPSBlockCode = Tagged; /** * A 15-digit number assigned by the Census to uniquely identify a block. * * ```txt * (State Code)━━┳┓ ┏┳┳┳┳┳━━(Tract Code) * 482012231001050 * ┗╋┛ ┗┻┻┻━(Block Code) * (County Code) * ``` * * @category Census * @category FIPS * @type {string} * @pattern ^\d{15}$ * @minLength 15 * @maxLength 15 * @title FIPS Block Geo ID * @see {@linkcode FIPSBlockCode} for the 4-digit version. */ export type FIPSBlockGeoID = Tagged; /** * A 1-digit number assigned by the Census to uniquely identify a block group. * * ```txt * (State Code)━━┳┓ ┏┳┳┳┳┳━━(Tract Code) * 060133740002### * ┗╋┛ ┗━(Block Group Code) * (County Code) * ``` * * @category Census * @category FIPS * @type {string} * @pattern ^\d{1}$ * @minLength 1 * @maxLength 1 * @title FIPS Block Group Code * @see {@linkcode FIPSBlockCode} for a more specific level. * @see {@linkcode FIPSTractCode} for a less specific level. */ export type FIPSBlockGroupCode = Tagged; /** * A 6-digit number assigned by the Census to uniquely identify a tract. * * ```txt * (State Code)━━┳┓ ┏┳┳┳┳┳━━(Tract Code) * 06013374000#### * ┗╋┛ * (County Code) * ``` * * @category Census * @category FIPS * @type {string} * @pattern ^\d{6}$ * @minLength 6 * @maxLength 6 * @title FIPS Tract Code * @see {@linkcode FIPSBlockGroupCode} for a more specific level. * @see {@linkcode FIPSCountySubDivisionCode} for a less specific level. */ export type FIPSTractCode = Tagged; /** * An 11-digit number assigned by the Census to uniquely identify a tract. * * ```txt * (State Code)━━┳┓ ┏┳┳┳┳┳━━(Tract Code) * 06013374000#### * ┗╋┛ * (County Code) * ``` * * @category Census * @category FIPS * @type {string} * @pattern ^\d{6}$ * @minLength 6 * @maxLength 6 * @title FIPS Tract Geo ID * @see {@linkcode FIPSBlockCode} for the 4-digit version. */ export type FIPSTractGeoID = Tagged; /** * A 5-digit number assigned by the Census to uniquely identify a county sub-division. * * ```txt * (State Code)━━┳┓ ┏┳┳┳┳━━(County Sub-Division Code) * 0601337400##### * ┗╋┛ * (County Code) * ``` * * @category Census * @category FIPS * @type {string} * @pattern ^\d{5}$ * @minLength 5 * @maxLength 5 * @title FIPS County Sub-Division Code * @see {@linkcode FIPSTractCode} for a more specific level. * @see {@linkcode FIPSCountyCode} for a less specific level. */ export type FIPSCountySubDivisionCode = Tagged; /** * A 3-digit number assigned by the Census to uniquely identify a county. * * ```txt * (State Code)━━┳┓ * 06013########## * ┗┻┻━(County Code) * ``` * * @category Census * @category FIPS * @type {string} * @pattern ^\d{3}$ * @minLength 3 * @maxLength 3 * @title FIPS County Code * @see {@linkcode FIPSCountySubDivisionCode} for a more specific level. * @see {@linkcode AdminLevel1Code} for a less specific level. */ export type FIPSCountyCode = Tagged; /** * A 5-digit number assigned by the Census to uniquely identify a place. * * ```txt * (State Code)━━┳┓ * 4835000######## * ┗┻┻┻┻━(Place Code) * ``` * * @category Census * @category FIPS * @type {string} * @minLength 5 * @maxLength 5 * @pattern ^\d{5}$ * @title FIPS Place Code */ export type FIPSPlaceCode = Tagged; /** * A 2-digit number assigned by the Census to uniquely identify a congressional district. * * ```txt * (State Code)━━┳┓ * 0902··········· * ┗┻━(Congressional District Code) * ``` * * @category Census * @category FIPS * @type {string} * @pattern ^\d{2}$ * @minLength 2 * @maxLength 2 * @title FIPS Congressional District Code * @see {@linkcode AdminLevel1Code} for a less specific level. * @see {@linkcode FIPSPlaceCode} for a more specific level. */ export type FIPSCongressionalDistrictCode = Tagged; /** * A part of a GeoID to it's respective name. * * @internal */ export declare const GeoIDPart: { /** * The state code part of a GeoID. * * Sometimes considered the administrative area level 1 code. */ readonly State: "state_code"; /** * The county code part of a GeoID. * * Sometimes considered the administrative area level 2 code. */ readonly County: "county_code"; /** * The county subdivision code part of a GeoID. * * Sometimes considered the administrative area level 3 code. */ readonly CountySubDivision: "county_sub_division_code"; /** * The congressional district code part of a GeoID. */ readonly CongressionalDistrict: "congressional_district_code"; /** * A place as defined by the Census, such as a city or town. */ readonly Place: "place_code"; /** * The tract code part of a GeoID. The third most granular part. */ readonly Tract: "tract_code"; /** * The block group code part of a GeoID. The second most granular part. */ readonly BlockGroup: "block_group_code"; /** * The block code part of a GeoID. The most granular part. */ readonly Block: "block_code"; }; export type GeoIDPart = (typeof GeoIDPart)[keyof typeof GeoIDPart]; /** * Mapping of GeoID parts to their respective FIPS codes types. * * @internal */ export interface GeoIDPartMapping { [GeoIDPart.State]: FIPSStateCode; [GeoIDPart.County]: FIPSCountyCode; [GeoIDPart.CountySubDivision]: FIPSCountySubDivisionCode; [GeoIDPart.CongressionalDistrict]: FIPSCongressionalDistrictCode; [GeoIDPart.Place]: FIPSPlaceCode; [GeoIDPart.Tract]: FIPSTractCode; [GeoIDPart.BlockGroup]: FIPSBlockGroupCode; [GeoIDPart.Block]: FIPSBlockCode; } /** * Record of GeoID parts and their respective lengths. * * Note that the length of a GeoID is the sum of the lengths of its parts. * * @internal */ export declare const GeoIDPartLength: { readonly state_code: 2; readonly county_code: 3; readonly county_sub_division_code: 5; readonly congressional_district_code: 2; readonly place_code: 5; readonly tract_code: 6; readonly block_group_code: 1; readonly block_code: 4; }; /** * A GeoID parsed to the block level. The most granular level. * * @title Parsed GeoID Block Level * @public */ export interface ParsedGeoIDBlockLevel { [GeoIDPart.State]: FIPSStateCode; [GeoIDPart.County]: FIPSCountyCode; [GeoIDPart.CountySubDivision]: FIPSCountySubDivisionCode; [GeoIDPart.Tract]: FIPSTractCode; [GeoIDPart.Place]: FIPSPlaceCode; [GeoIDPart.BlockGroup]: FIPSBlockGroupCode; [GeoIDPart.Block]: FIPSBlockCode; [GeoIDPart.BlockGroup]: FIPSBlockGroupCode; [GeoIDPart.CongressionalDistrict]: FIPSCongressionalDistrictCode; } /** * A GeoID parsed to the block group level. The second most granular level. * * @internal */ export interface ParsedGeoIDBlockGroupLevel { [GeoIDPart.State]: AdminLevel1Code; [GeoIDPart.County]: FIPSCountyCode; [GeoIDPart.CountySubDivision]: FIPSCountySubDivisionCode; [GeoIDPart.Tract]: FIPSTractCode; [GeoIDPart.BlockGroup]: FIPSBlockGroupCode; [GeoIDPart.Block]: undefined; } /** * A GeoID parsed to the tract level. The third most granular level. * * @internal */ export interface ParsedGeoIDTractLevel { [GeoIDPart.State]: AdminLevel1Code; [GeoIDPart.County]: FIPSCountyCode; [GeoIDPart.CountySubDivision]: FIPSCountySubDivisionCode; [GeoIDPart.Tract]: FIPSTractCode; [GeoIDPart.BlockGroup]: undefined; [GeoIDPart.Block]: undefined; } /** * A GeoID parsed to a partial level. * * @internal */ export type ParsedGeoIDPartial = Partial>; /** * A GeoID parsed to the county-subdivision level. The fourth most granular level. * * @internal */ export interface ParsedGeoIDCountySubDivisionLevel { [GeoIDPart.State]: AdminLevel1Code; [GeoIDPart.County]: FIPSCountyCode; [GeoIDPart.CountySubDivision]: FIPSCountySubDivisionCode; [GeoIDPart.Tract]: undefined; [GeoIDPart.BlockGroup]: undefined; [GeoIDPart.Block]: undefined; } /** * A GeoID parsed to the county level. The fifth most granular level. * * @internal */ export interface ParsedGeoIDCountyLevel { [GeoIDPart.State]: AdminLevel1Code; [GeoIDPart.County]: FIPSCountyCode; [GeoIDPart.CountySubDivision]: undefined; [GeoIDPart.Tract]: undefined; [GeoIDPart.BlockGroup]: undefined; [GeoIDPart.Block]: undefined; } /** * A GeoID parsed to the state level. The least granular level. * * @internal */ export interface ParsedGeoIDStateLevel { [GeoIDPart.State]: AdminLevel1Code; [GeoIDPart.County]: undefined; [GeoIDPart.CountySubDivision]: undefined; [GeoIDPart.Tract]: undefined; [GeoIDPart.BlockGroup]: undefined; [GeoIDPart.Block]: undefined; } /** * A GeoID parsed to a specific level. * * @internal */ export type ParsedGeoID = ParsedGeoIDBlockLevel | ParsedGeoIDBlockGroupLevel | ParsedGeoIDTractLevel | ParsedGeoIDCountySubDivisionLevel | ParsedGeoIDCountyLevel | ParsedGeoIDStateLevel; export declare class GeoIDParsingError extends Error { constructor(message: string); } export declare class GeoIDInputMatcher { pattern: RegExp; components: T; /** * The specific length of a valid GeoID input. */ length: number; constructor(...geoIDComponents: T); parse(input: string): R | null; test(input: string): boolean; } /** * Record of GeoID input matchers, i.e. a pattern and parser for each grouping of GeoID components. * * @internal * @see {@linkcode GeoIDComponentMatchers} for the component-level matchers. */ export declare const GeoIDInputMatchers: { readonly block_code: GeoIDInputMatcher<["state_code", "county_code", "tract_code", "block_code"]>; readonly block_group_code: GeoIDInputMatcher<["state_code", "county_code", "tract_code", "block_group_code"]>; readonly tract_code: GeoIDInputMatcher<["state_code", "county_code", "tract_code"]>; readonly place_code: GeoIDInputMatcher<["state_code", "county_code", "place_code"]>; readonly county_sub_division_code: GeoIDInputMatcher<["state_code", "county_code", "county_sub_division_code"]>; readonly county_code: GeoIDInputMatcher<["state_code", "county_code"]>; readonly congressional_district_code: GeoIDInputMatcher<["state_code", "congressional_district_code"]>; readonly state_code: GeoIDInputMatcher<["state_code"]>; }; /** * Type-predicate for checking if a value appears to be a valid GeoID component. */ export declare function isGeoIDComponent(component: T, input: unknown): input is GeoIDPartMapping[T]; /** * Given a block GeoID, parse it into its components. * * @category Census * @category FIPS */ export declare function parseFIPSBlockGeoID(input: FIPSBlockGeoID): ParsedGeoIDBlockLevel; /** * Given a tract GeoID, parse it into its components. * * @category Census * @category FIPS */ export declare function parseFIPSTractGeoID(input: FIPSTractGeoID): ParsedGeoIDTractLevel; /** * Given a GeoID, parse it into its components. * * @category Census * @category FIPS */ export declare function parseGeoID(input: FIPSBlockGeoID): ParsedGeoIDBlockLevel; export declare function parseGeoID(input: FIPSBlockGroupCode): ParsedGeoIDBlockGroupLevel; export declare function parseGeoID(input: FIPSTractCode): ParsedGeoIDTractLevel; export declare function parseGeoID(input: FIPSCountySubDivisionCode): ParsedGeoIDCountySubDivisionLevel; export declare function parseGeoID(input: FIPSCountyCode): ParsedGeoIDCountyLevel; export declare function parseGeoID(input: AdminLevel1Code): ParsedGeoIDStateLevel; export declare function parseGeoID(input: unknown): ParsedGeoID | null; /** * Format a parsed block GeoID back into a string. * * @category Census * @category FIPS */ export declare function formatGeoID(input: ParsedGeoIDBlockLevel): FIPSBlockGeoID; /** * Format a parsed GeoID back into a string. * * @category Census * @category FIPS */ export declare function formatGeoID(input: ParsedGeoIDPartial): string; //# sourceMappingURL=geoid.d.ts.map