@molochko-dev/data 1.4.34 → 1.4.36

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.
@@ -3,102 +3,119 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.filter = void 0;
4
4
  const enums_1 = require("../enums");
5
5
  const date_1 = require("../helpers/date");
6
- const status = enums_1.COW_STATUS;
7
- const defaultCowAge = 12;
8
6
  /*
9
7
 
10
8
  const filtered = filter(animals, eventType)
11
9
 
12
10
  */
13
- function ageFilter(animal, availableMinAge /*month*/) {
14
- const months = (0, date_1.getMonthFromDate)(animal.dob.toISOString());
15
- return months > availableMinAge;
16
- }
17
- function statusFilter(animal, availableStatuses) {
18
- return availableStatuses.includes(animal.status);
19
- }
20
- function genderFilter(animal, availableGender) {
21
- return animal.gender === availableGender;
22
- }
23
- function abortFilter(animals) {
24
- const result = animals.filter((animal) => genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow));
25
- return result;
26
- }
27
- function neosemFilter(animals) {
28
- return abortFilter(animals);
29
- }
30
- function osemenenieFilter(animals) {
31
- const result = animals.filter((animal) => {
32
- const genderOk = genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow);
33
- const statusOk = statusFilter(animal, [status.HEIFER, status.BRED, status.OPEN, status.FRESH]);
34
- const ageOk = ageFilter(animal, defaultCowAge);
35
- return genderOk && statusOk && ageOk && animal;
36
- });
37
- return result;
38
- }
39
- function otelFilter(animals) {
40
- const result = animals.filter((animal) => {
41
- const genderOk = genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow);
42
- const statusOk = statusFilter(animal, [status.DRY, status.PREGNANT]);
43
- const ageOk = ageFilter(animal, defaultCowAge);
44
- return genderOk && statusOk && ageOk && animal;
45
- });
46
- return result;
47
- }
48
- function recheckFilter(animals) {
49
- const result = animals.filter((animal) => {
50
- const genderOk = genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow);
51
- const ageOk = ageFilter(animal, defaultCowAge);
52
- let availableStatus = [status.BRED, status.PREGNANT, status.DRY];
53
- if (animal.animalEventRelation) {
54
- const previousRechecks = animal.animalEventRelation.filter((row) => {
55
- return row.eventDetail.type === enums_1.EVENT_TYPES_ENUM.recheck;
56
- });
57
- if (previousRechecks.length == 0) {
58
- availableStatus = [status.BRED];
11
+ class AnimalEventFilter {
12
+ constructor() {
13
+ this.status = enums_1.COW_STATUS;
14
+ this.defaultCowAge = 12;
15
+ }
16
+ ageFilter(animal, availableMinAge /*month*/) {
17
+ const months = (0, date_1.getMonthFromDate)(animal.dob.toISOString());
18
+ return months > availableMinAge;
19
+ }
20
+ statusFilter(animal, availableStatuses) {
21
+ return availableStatuses.includes(animal.status);
22
+ }
23
+ genderFilter(animal, availableGender) {
24
+ return animal.gender === availableGender;
25
+ }
26
+ abortFilter(animals) {
27
+ const result = animals.filter((animal) => this.genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow));
28
+ return result;
29
+ }
30
+ neosemFilter(animals) {
31
+ return this.abortFilter(animals);
32
+ }
33
+ osemenenieFilter(animals) {
34
+ const result = animals.filter((animal) => {
35
+ const genderOk = this.genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow);
36
+ const statusOk = this.statusFilter(animal, [
37
+ this.status.HEIFER,
38
+ this.status.BRED,
39
+ this.status.OPEN,
40
+ this.status.FRESH,
41
+ ]);
42
+ const ageOk = this.ageFilter(animal, this.defaultCowAge);
43
+ return genderOk && statusOk && ageOk && animal;
44
+ });
45
+ return result;
46
+ }
47
+ otelFilter(animals) {
48
+ const result = animals.filter((animal) => {
49
+ const genderOk = this.genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow);
50
+ const statusOk = this.statusFilter(animal, [this.status.DRY, this.status.PREGNANT]);
51
+ const ageOk = this.ageFilter(animal, this.defaultCowAge);
52
+ return genderOk && statusOk && ageOk && animal;
53
+ });
54
+ return result;
55
+ }
56
+ recheckFilter(animals) {
57
+ const result = animals.filter((animal) => {
58
+ const genderOk = this.genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow);
59
+ const ageOk = this.ageFilter(animal, this.defaultCowAge);
60
+ let availableStatus = [this.status.BRED, this.status.PREGNANT, this.status.DRY];
61
+ if (animal.animalEventRelation) {
62
+ const previousRechecks = animal.animalEventRelation.filter((row) => {
63
+ return row.eventDetail.type === enums_1.EVENT_TYPES_ENUM.recheck;
64
+ });
65
+ if (previousRechecks.length == 0) {
66
+ availableStatus = [this.status.BRED];
67
+ }
59
68
  }
60
- }
61
- const statusOk = statusFilter(animal, availableStatus);
62
- return genderOk && statusOk && ageOk && animal;
63
- });
64
- return result;
65
- }
66
- function suhostoiFilter(animals) {
67
- return abortFilter(animals);
68
- }
69
- function pozSuhostoiFilter(animals) {
70
- const result = animals.filter((animal) => {
71
- const genderOk = genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow);
72
- const statusOk = statusFilter(animal, [status.DRY]);
73
- const ageOk = ageFilter(animal, defaultCowAge);
74
- return genderOk && statusOk && ageOk && animal;
75
- });
76
- return result;
77
- }
78
- function transferOutFilter(animals) {
79
- return animals;
80
- }
81
- function perevodFilter(animals) {
82
- return animals;
83
- }
84
- function filter(animals, eventType) {
85
- const filters = {
86
- [enums_1.EVENT_TYPES_ENUM.abort]: abortFilter,
87
- [enums_1.EVENT_TYPES_ENUM.neosem]: neosemFilter,
88
- [enums_1.EVENT_TYPES_ENUM.osemenenie]: osemenenieFilter,
89
- [enums_1.EVENT_TYPES_ENUM.otel]: otelFilter,
90
- [enums_1.EVENT_TYPES_ENUM.recheck]: recheckFilter,
91
- [enums_1.EVENT_TYPES_ENUM.suhostoi]: suhostoiFilter,
92
- [enums_1.EVENT_TYPES_ENUM.pozSuhostoi]: pozSuhostoiFilter,
93
- [enums_1.EVENT_TYPES_ENUM.perevod]: perevodFilter,
94
- [enums_1.EVENT_TYPES_ENUM.transferOut]: transferOutFilter,
95
- };
96
- try {
97
- const result = filters[eventType](animals);
69
+ const statusOk = this.statusFilter(animal, availableStatus);
70
+ return genderOk && statusOk && ageOk && animal;
71
+ });
98
72
  return result;
99
73
  }
100
- catch (err) {
101
- throw new Error(`AnimalForEventFilter.filter.${eventType} not implemented`);
74
+ suhostoiFilter(animals) {
75
+ return this.abortFilter(animals);
102
76
  }
77
+ pozSuhostoiFilter(animals) {
78
+ const result = animals.filter((animal) => {
79
+ const genderOk = this.genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow);
80
+ const statusOk = this.statusFilter(animal, [this.status.DRY]);
81
+ const ageOk = this.ageFilter(animal, this.defaultCowAge);
82
+ return genderOk && statusOk && ageOk && animal;
83
+ });
84
+ return result;
85
+ }
86
+ transferOutFilter(animals) {
87
+ return animals;
88
+ }
89
+ perevodFilter(animals) {
90
+ return animals;
91
+ }
92
+ filter(animals, eventType) {
93
+ switch (eventType) {
94
+ case enums_1.EVENT_TYPES_ENUM.abort:
95
+ return this.abortFilter(animals);
96
+ case enums_1.EVENT_TYPES_ENUM.neosem:
97
+ return this.neosemFilter(animals);
98
+ case enums_1.EVENT_TYPES_ENUM.osemenenie:
99
+ return this.osemenenieFilter(animals);
100
+ case enums_1.EVENT_TYPES_ENUM.otel:
101
+ return this.otelFilter(animals);
102
+ case enums_1.EVENT_TYPES_ENUM.recheck:
103
+ return this.recheckFilter(animals);
104
+ case enums_1.EVENT_TYPES_ENUM.suhostoi:
105
+ return this.suhostoiFilter(animals);
106
+ case enums_1.EVENT_TYPES_ENUM.pozSuhostoi:
107
+ return this.pozSuhostoiFilter(animals);
108
+ case enums_1.EVENT_TYPES_ENUM.perevod:
109
+ return this.perevodFilter(animals);
110
+ case enums_1.EVENT_TYPES_ENUM.transferOut:
111
+ return this.transferOutFilter(animals);
112
+ default:
113
+ throw new Error(`AnimalForEventFilter.filter.${eventType} not implemented`);
114
+ }
115
+ }
116
+ }
117
+ const f = new AnimalEventFilter();
118
+ function filter(animals, eventType) {
119
+ return f.filter(animals, eventType);
103
120
  }
104
121
  exports.filter = filter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molochko-dev/data",
3
- "version": "1.4.34",
3
+ "version": "1.4.36",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "typings/index.d.ts",
@@ -2,128 +2,145 @@ import { AnimalStatus, BaseAnimal } from '../types';
2
2
  import { ANIMAL_GENDER_ENUM, COW_STATUS, EVENT_TYPES_ENUM } from '../enums';
3
3
  import { getMonthFromDate } from '../helpers/date';
4
4
 
5
- const status = COW_STATUS;
6
-
7
- const defaultCowAge = 12;
8
-
9
5
  /*
10
6
 
11
7
  const filtered = filter(animals, eventType)
12
8
 
13
9
  */
14
10
 
15
- function ageFilter<T extends BaseAnimal>(animal: T, availableMinAge: number /*month*/): boolean {
16
- const months = getMonthFromDate(animal.dob.toISOString());
17
- return months > availableMinAge;
18
- }
11
+ class AnimalEventFilter {
12
+ status = COW_STATUS;
13
+ defaultCowAge = 12;
19
14
 
20
- function statusFilter<T extends BaseAnimal>(animal: T, availableStatuses: AnimalStatus[]): boolean {
21
- return availableStatuses.includes(animal.status);
22
- }
15
+ private ageFilter<T extends BaseAnimal>(animal: T, availableMinAge: number /*month*/): boolean {
16
+ const months = getMonthFromDate(animal.dob.toISOString());
17
+ return months > availableMinAge;
18
+ }
23
19
 
24
- function genderFilter<T extends BaseAnimal>(
25
- animal: T,
26
- availableGender: ANIMAL_GENDER_ENUM,
27
- ): boolean {
28
- return animal.gender === availableGender;
29
- }
20
+ private statusFilter<T extends BaseAnimal>(
21
+ animal: T,
22
+ availableStatuses: AnimalStatus[],
23
+ ): boolean {
24
+ return availableStatuses.includes(animal.status);
25
+ }
30
26
 
31
- function abortFilter<T extends BaseAnimal>(animals: T[]): T[] {
32
- const result: T[] = animals.filter((animal) => genderFilter<T>(animal, ANIMAL_GENDER_ENUM.cow));
33
- return result;
34
- }
27
+ private genderFilter<T extends BaseAnimal>(
28
+ animal: T,
29
+ availableGender: ANIMAL_GENDER_ENUM,
30
+ ): boolean {
31
+ return animal.gender === availableGender;
32
+ }
35
33
 
36
- function neosemFilter<T extends BaseAnimal>(animals: T[]): T[] {
37
- return abortFilter(animals);
38
- }
34
+ abortFilter<T extends BaseAnimal>(animals: T[]): T[] {
35
+ const result: T[] = animals.filter((animal) =>
36
+ this.genderFilter<T>(animal, ANIMAL_GENDER_ENUM.cow),
37
+ );
38
+ return result;
39
+ }
39
40
 
40
- function osemenenieFilter<T extends BaseAnimal>(animals: T[]): T[] {
41
- const result = animals.filter((animal) => {
42
- const genderOk = genderFilter(animal, ANIMAL_GENDER_ENUM.cow);
43
- const statusOk = statusFilter(animal, [status.HEIFER, status.BRED, status.OPEN, status.FRESH]);
44
- const ageOk = ageFilter(animal, defaultCowAge);
41
+ neosemFilter<T extends BaseAnimal>(animals: T[]): T[] {
42
+ return this.abortFilter(animals);
43
+ }
45
44
 
46
- return genderOk && statusOk && ageOk && animal;
47
- });
45
+ osemenenieFilter<T extends BaseAnimal>(animals: T[]): T[] {
46
+ const result = animals.filter((animal) => {
47
+ const genderOk = this.genderFilter(animal, ANIMAL_GENDER_ENUM.cow);
48
+ const statusOk = this.statusFilter(animal, [
49
+ this.status.HEIFER,
50
+ this.status.BRED,
51
+ this.status.OPEN,
52
+ this.status.FRESH,
53
+ ]);
54
+ const ageOk = this.ageFilter(animal, this.defaultCowAge);
55
+ return genderOk && statusOk && ageOk && animal;
56
+ });
48
57
 
49
- return result;
50
- }
58
+ return result;
59
+ }
51
60
 
52
- function otelFilter<T extends BaseAnimal>(animals: T[]): T[] {
53
- const result = animals.filter((animal) => {
54
- const genderOk = genderFilter(animal, ANIMAL_GENDER_ENUM.cow);
55
- const statusOk = statusFilter(animal, [status.DRY, status.PREGNANT]);
56
- const ageOk = ageFilter(animal, defaultCowAge);
61
+ otelFilter<T extends BaseAnimal>(animals: T[]): T[] {
62
+ const result = animals.filter((animal) => {
63
+ const genderOk = this.genderFilter(animal, ANIMAL_GENDER_ENUM.cow);
64
+ const statusOk = this.statusFilter(animal, [this.status.DRY, this.status.PREGNANT]);
65
+ const ageOk = this.ageFilter(animal, this.defaultCowAge);
57
66
 
58
- return genderOk && statusOk && ageOk && animal;
59
- });
60
- return result;
61
- }
67
+ return genderOk && statusOk && ageOk && animal;
68
+ });
69
+ return result;
70
+ }
62
71
 
63
- function recheckFilter<T extends BaseAnimal>(animals: T[]): T[] {
64
- const result = animals.filter((animal) => {
65
- const genderOk = genderFilter(animal, ANIMAL_GENDER_ENUM.cow);
66
- const ageOk = ageFilter(animal, defaultCowAge);
67
-
68
- let availableStatus = [status.BRED, status.PREGNANT, status.DRY];
69
- if (animal.animalEventRelation) {
70
- const previousRechecks = animal.animalEventRelation.filter((row) => {
71
- return row.eventDetail.type === EVENT_TYPES_ENUM.recheck;
72
- });
73
- if (previousRechecks.length == 0) {
74
- availableStatus = [status.BRED];
72
+ recheckFilter<T extends BaseAnimal>(animals: T[]): T[] {
73
+ const result = animals.filter((animal) => {
74
+ const genderOk = this.genderFilter(animal, ANIMAL_GENDER_ENUM.cow);
75
+ const ageOk = this.ageFilter(animal, this.defaultCowAge);
76
+
77
+ let availableStatus = [this.status.BRED, this.status.PREGNANT, this.status.DRY];
78
+ if (animal.animalEventRelation) {
79
+ const previousRechecks = animal.animalEventRelation.filter((row) => {
80
+ return row.eventDetail.type === EVENT_TYPES_ENUM.recheck;
81
+ });
82
+ if (previousRechecks.length == 0) {
83
+ availableStatus = [this.status.BRED];
84
+ }
75
85
  }
76
- }
77
- const statusOk = statusFilter(animal, availableStatus);
86
+ const statusOk = this.statusFilter(animal, availableStatus);
78
87
 
79
- return genderOk && statusOk && ageOk && animal;
80
- });
81
- return result;
82
- }
88
+ return genderOk && statusOk && ageOk && animal;
89
+ });
90
+ return result;
91
+ }
83
92
 
84
- function suhostoiFilter<T extends BaseAnimal>(animals: T[]): T[] {
85
- return abortFilter(animals);
86
- }
93
+ suhostoiFilter<T extends BaseAnimal>(animals: T[]): T[] {
94
+ return this.abortFilter(animals);
95
+ }
87
96
 
88
- function pozSuhostoiFilter<T extends BaseAnimal>(animals: T[]): T[] {
89
- const result = animals.filter((animal) => {
90
- const genderOk = genderFilter(animal, ANIMAL_GENDER_ENUM.cow);
91
- const statusOk = statusFilter(animal, [status.DRY]);
92
- const ageOk = ageFilter(animal, defaultCowAge);
97
+ pozSuhostoiFilter<T extends BaseAnimal>(animals: T[]): T[] {
98
+ const result = animals.filter((animal) => {
99
+ const genderOk = this.genderFilter(animal, ANIMAL_GENDER_ENUM.cow);
100
+ const statusOk = this.statusFilter(animal, [this.status.DRY]);
101
+ const ageOk = this.ageFilter(animal, this.defaultCowAge);
93
102
 
94
- return genderOk && statusOk && ageOk && animal;
95
- });
96
- return result;
97
- }
103
+ return genderOk && statusOk && ageOk && animal;
104
+ });
105
+ return result;
106
+ }
98
107
 
99
- function transferOutFilter<T extends BaseAnimal>(animals: T[]): T[] {
100
- return animals;
101
- }
108
+ transferOutFilter<T extends BaseAnimal>(animals: T[]): T[] {
109
+ return animals;
110
+ }
102
111
 
103
- function perevodFilter<T extends BaseAnimal>(animals: T[]) {
104
- return animals;
105
- }
112
+ perevodFilter<T extends BaseAnimal>(animals: T[]) {
113
+ return animals;
114
+ }
106
115
 
107
- interface IFilters<T extends BaseAnimal> {
108
- [key: string]: (animals: T[]) => T[];
116
+ filter<T extends BaseAnimal>(animals: T[], eventType: EVENT_TYPES_ENUM) {
117
+ switch (eventType) {
118
+ case EVENT_TYPES_ENUM.abort:
119
+ return this.abortFilter(animals);
120
+ case EVENT_TYPES_ENUM.neosem:
121
+ return this.neosemFilter(animals);
122
+ case EVENT_TYPES_ENUM.osemenenie:
123
+ return this.osemenenieFilter(animals);
124
+ case EVENT_TYPES_ENUM.otel:
125
+ return this.otelFilter(animals);
126
+ case EVENT_TYPES_ENUM.recheck:
127
+ return this.recheckFilter(animals);
128
+ case EVENT_TYPES_ENUM.suhostoi:
129
+ return this.suhostoiFilter(animals);
130
+ case EVENT_TYPES_ENUM.pozSuhostoi:
131
+ return this.pozSuhostoiFilter(animals);
132
+ case EVENT_TYPES_ENUM.perevod:
133
+ return this.perevodFilter(animals);
134
+ case EVENT_TYPES_ENUM.transferOut:
135
+ return this.transferOutFilter(animals);
136
+ default:
137
+ throw new Error(`AnimalForEventFilter.filter.${eventType} not implemented`);
138
+ }
139
+ }
109
140
  }
110
141
 
142
+ const f = new AnimalEventFilter();
143
+
111
144
  export function filter<T extends BaseAnimal>(animals: T[], eventType: EVENT_TYPES_ENUM) {
112
- const filters: IFilters<T> = {
113
- [EVENT_TYPES_ENUM.abort]: abortFilter,
114
- [EVENT_TYPES_ENUM.neosem]: neosemFilter,
115
- [EVENT_TYPES_ENUM.osemenenie]: osemenenieFilter,
116
- [EVENT_TYPES_ENUM.otel]: otelFilter,
117
- [EVENT_TYPES_ENUM.recheck]: recheckFilter,
118
- [EVENT_TYPES_ENUM.suhostoi]: suhostoiFilter,
119
- [EVENT_TYPES_ENUM.pozSuhostoi]: pozSuhostoiFilter,
120
- [EVENT_TYPES_ENUM.perevod]: perevodFilter,
121
- [EVENT_TYPES_ENUM.transferOut]: transferOutFilter,
122
- };
123
- try {
124
- const result = filters[eventType](animals);
125
- return result;
126
- } catch (err) {
127
- throw new Error(`AnimalForEventFilter.filter.${eventType} not implemented`);
128
- }
145
+ return f.filter(animals, eventType);
129
146
  }