elections-types 1.0.78 → 1.0.79

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.
@@ -2,7 +2,7 @@ import { CandidateAux } from "./Candidate.js";
2
2
  import { CommitteeAux } from "./Committee.js";
3
3
  import { ExpenditureAux } from "./Expenditure.js";
4
4
  import { StateID } from "./State.js";
5
- declare const office_ids: readonly ["H", "S"];
5
+ declare const office_ids: readonly ["H", "S", "P"];
6
6
  export type OfficeID = typeof office_ids[number];
7
7
  export declare function is_office_id(office_id: any): office_id is OfficeID;
8
8
  export type ElectionID = string;
@@ -11,7 +11,7 @@ export declare class Election {
11
11
  readonly election_id: ElectionID;
12
12
  readonly name: string;
13
13
  readonly office_id: OfficeID;
14
- readonly state_id: StateID;
14
+ readonly state_id?: StateID;
15
15
  readonly district?: number;
16
16
  readonly [x: string]: any;
17
17
  constructor(election: Election);
@@ -2,7 +2,7 @@ import { CandidateAux } from "./Candidate.js";
2
2
  import { CommitteeAux } from "./Committee.js";
3
3
  import { ExpenditureAux } from "./Expenditure.js";
4
4
  import { is_state_id } from "./State.js";
5
- const office_ids = ["H", "S"];
5
+ const office_ids = ["H", "S", "P"];
6
6
  export function is_office_id(office_id) {
7
7
  return office_ids.includes(office_id);
8
8
  }
@@ -13,12 +13,28 @@ export function is_election_id(election_id) {
13
13
  const office_id = parts[1];
14
14
  const state_id = parts[2];
15
15
  const district = Number(parts[3]);
16
- return (parts.length <= 4 &&
17
- Number.isInteger(year) && year > 1700 && year < 3000 &&
18
- is_office_id(office_id) &&
19
- is_state_id(state_id) &&
20
- ((office_id === "S" || state_ids_without_district.includes(state_id)) && parts[3] === undefined ||
21
- !(office_id === "S" || state_ids_without_district.includes(state_id)) && Number.isInteger(district) && district < 100));
16
+ if (!(Number.isInteger(year) && year > 1700 && year < 3000 && is_office_id(office_id))) {
17
+ return false;
18
+ }
19
+ if (office_id === "P" && parts.length === 2) {
20
+ return true;
21
+ }
22
+ if (!is_state_id(state_id)) {
23
+ return false;
24
+ }
25
+ if (office_id === "S" && parts.length === 3) {
26
+ return true;
27
+ }
28
+ if (office_id !== "H") {
29
+ return false;
30
+ }
31
+ if (!(state_ids_without_district.includes(state_id) && parts.length === 3 || parts.length === 4)) {
32
+ return false;
33
+ }
34
+ if (!Number.isInteger(district) && district < 100) {
35
+ return false;
36
+ }
37
+ return true;
22
38
  }
23
39
  export class Election {
24
40
  election_id;
@@ -41,7 +57,7 @@ export class Election {
41
57
  is_election_id(election.election_id) &&
42
58
  typeof election.name === "string" &&
43
59
  is_office_id(election.office_id) &&
44
- is_state_id(election.state_id) &&
60
+ (election.state_id === undefined || is_state_id(election.state_id)) &&
45
61
  (election.district === undefined || typeof election.district === "number"));
46
62
  }
47
63
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elections-types",
3
- "version": "1.0.78",
3
+ "version": "1.0.79",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
@@ -3,7 +3,7 @@ import { Committee, CommitteeAux } from "./Committee"
3
3
  import { ExpenditureAux } from "./Expenditure"
4
4
  import { is_state_id, state_ids, StateID } from "./State"
5
5
 
6
- const office_ids = ["H", "S"] as const
6
+ const office_ids = ["H", "S", "P"] as const
7
7
 
8
8
  export type OfficeID = typeof office_ids[number]
9
9
 
@@ -21,21 +21,35 @@ export function is_election_id(election_id : any) : election_id is ElectionID {
21
21
  const office_id = parts[1]
22
22
  const state_id = parts[2]
23
23
  const district = Number(parts[3])
24
- return (
25
- parts.length <= 4 &&
26
- Number.isInteger(year) && year > 1700 && year < 3000 &&
27
- is_office_id(office_id) &&
28
- is_state_id(state_id) &&
29
- ((office_id === "S" || state_ids_without_district.includes(state_id)) && parts[3] === undefined ||
30
- !(office_id === "S" || state_ids_without_district.includes(state_id)) && Number.isInteger(district) && district < 100)
31
- )
24
+ if (!(Number.isInteger(year) && year > 1700 && year < 3000 && is_office_id(office_id))) {
25
+ return false
26
+ }
27
+ if (office_id === "P" && parts.length === 2) {
28
+ return true
29
+ }
30
+ if (!is_state_id(state_id)) {
31
+ return false
32
+ }
33
+ if (office_id === "S" && parts.length === 3) {
34
+ return true
35
+ }
36
+ if (office_id !== "H") {
37
+ return false
38
+ }
39
+ if (!(state_ids_without_district.includes(state_id) && parts.length === 3 || parts.length === 4)) {
40
+ return false
41
+ }
42
+ if (!Number.isInteger(district) && district < 100) {
43
+ return false
44
+ }
45
+ return true
32
46
  }
33
47
 
34
48
  export class Election {
35
49
  readonly election_id : ElectionID
36
50
  readonly name : string
37
51
  readonly office_id : OfficeID
38
- readonly state_id : StateID
52
+ readonly state_id? : StateID
39
53
  readonly district? : number
40
54
  readonly [x : string] : any
41
55
 
@@ -56,7 +70,7 @@ export class Election {
56
70
  is_election_id(election.election_id) &&
57
71
  typeof election.name === "string" &&
58
72
  is_office_id(election.office_id) &&
59
- is_state_id(election.state_id) &&
73
+ (election.state_id === undefined || is_state_id(election.state_id)) &&
60
74
  (election.district === undefined || typeof election.district === "number")
61
75
  )
62
76
  }