elections-types 1.0.11 → 1.0.13

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.
@@ -9,7 +9,7 @@ export declare class Election {
9
9
  readonly name: string;
10
10
  readonly office_id: OfficeID;
11
11
  readonly state_id: StateID;
12
- readonly district?: string;
12
+ readonly district?: number;
13
13
  readonly candidate_ids?: string[];
14
14
  readonly committee_ids?: string[];
15
15
  constructor(election: any);
@@ -3,17 +3,19 @@ const office_ids = ["H", "S"];
3
3
  export function is_office_id(office_id) {
4
4
  return office_ids.includes(office_id);
5
5
  }
6
+ const state_ids_without_district = ["AK", "DE", "ND", "SD", "VT", "WY"];
6
7
  export function is_election_id(election_id) {
7
8
  const parts = election_id.split("-");
8
9
  const year = Number(parts[0]);
9
10
  const office_id = parts[1];
10
11
  const state_id = parts[2];
11
12
  const district = Number(parts[3]);
12
- return ((parts.length === 4 && office_id === "H" || parts.length === 3) &&
13
+ return (parts.length <= 4 &&
13
14
  Number.isInteger(year) && year > 1700 && year < 3000 &&
14
15
  is_office_id(office_id) &&
15
16
  is_state_id(state_id) &&
16
- (office_id === "S" || Number.isInteger(district) && district < 100));
17
+ ((office_id === "S" || state_ids_without_district.includes(state_id)) && district === undefined ||
18
+ !(office_id === "S" || state_ids_without_district.includes(state_id)) && Number.isInteger(district) && district < 100));
17
19
  }
18
20
  export class Election {
19
21
  election_id;
@@ -41,7 +43,7 @@ export class Election {
41
43
  typeof election.name === "string" &&
42
44
  is_office_id(election.office_id) &&
43
45
  is_state_id(election.state_id) &&
44
- (election.district === undefined && typeof election.district === "string") &&
46
+ (election.district === undefined || typeof election.district === "number") &&
45
47
  (election.candidate_ids === undefined || Array.isArray(election.candidate_ids) && election.candidate_ids.every((candidate_id) => typeof candidate_id === "string")) &&
46
48
  (election.committee_ids === undefined || Array.isArray(election.committee_ids) && election.committee_ids.every((committee_id) => typeof committee_id === "string")));
47
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elections-types",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
@@ -8,6 +8,8 @@ export function is_office_id(office_id : any) : office_id is OfficeID {
8
8
  return office_ids.includes(office_id)
9
9
  }
10
10
 
11
+ const state_ids_without_district : StateID[] = ["AK", "DE", "ND", "SD", "VT", "WY"]
12
+
11
13
  export type ElectionID = string
12
14
 
13
15
  export function is_election_id(election_id : any) : election_id is ElectionID {
@@ -17,11 +19,12 @@ export function is_election_id(election_id : any) : election_id is ElectionID {
17
19
  const state_id = parts[2]
18
20
  const district = Number(parts[3])
19
21
  return (
20
- (parts.length === 4 && office_id === "H" || parts.length === 3) &&
22
+ parts.length <= 4 &&
21
23
  Number.isInteger(year) && year > 1700 && year < 3000 &&
22
24
  is_office_id(office_id) &&
23
25
  is_state_id(state_id) &&
24
- (office_id === "S" || Number.isInteger(district) && district < 100)
26
+ ((office_id === "S" || state_ids_without_district.includes(state_id)) && district === undefined ||
27
+ !(office_id === "S" || state_ids_without_district.includes(state_id)) && Number.isInteger(district) && district < 100)
25
28
  )
26
29
  }
27
30
 
@@ -30,7 +33,7 @@ export class Election {
30
33
  readonly name : string
31
34
  readonly office_id : OfficeID
32
35
  readonly state_id : StateID
33
- readonly district? : string
36
+ readonly district? : number
34
37
  readonly candidate_ids? : string[]
35
38
  readonly committee_ids? : string[]
36
39
 
@@ -54,7 +57,7 @@ export class Election {
54
57
  typeof election.name === "string" &&
55
58
  is_office_id(election.office_id) &&
56
59
  is_state_id(election.state_id) &&
57
- (election.district === undefined && typeof election.district === "string") &&
60
+ (election.district === undefined || typeof election.district === "number") &&
58
61
  (election.candidate_ids === undefined || Array.isArray(election.candidate_ids) && election.candidate_ids.every((candidate_id : any) => typeof candidate_id === "string")) &&
59
62
  (election.committee_ids === undefined || Array.isArray(election.committee_ids) && election.committee_ids.every((committee_id : any) => typeof committee_id === "string"))
60
63
  )