@molochko-dev/data 1.4.29 → 1.4.30

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.
@@ -1,19 +1,3 @@
1
1
  import { IAnimal } from '../types';
2
- import { ANIMAL_GENDER_ENUM, ANIMAL_STATUS_ENUM, EVENT_TYPES_ENUM } from '../enums';
3
- export declare class AnimalForEventFilter {
4
- animals: IAnimal[];
5
- eventType: EVENT_TYPES_ENUM;
6
- constructor(animals: IAnimal[], eventType: EVENT_TYPES_ENUM);
7
- genderFilter(animal: IAnimal, availableGender: ANIMAL_GENDER_ENUM): Boolean;
8
- ageFilter(animal: IAnimal, availableMinAge: number): Boolean;
9
- statusFilter(animal: IAnimal, availableStatuses: ANIMAL_STATUS_ENUM[]): Boolean;
10
- abortFilter(): IAnimal[];
11
- neosemFilter(): IAnimal[];
12
- osemenenieFilter(): IAnimal[];
13
- otelFilter(): IAnimal[];
14
- recheckFilter(): IAnimal[];
15
- suhostoiFilter(): IAnimal[];
16
- pozSuhostoiFilter(): IAnimal[];
17
- outputFilter(): IAnimal[];
18
- filter(): IAnimal[];
19
- }
2
+ import { EVENT_TYPES_ENUM } from '../enums';
3
+ export declare function filter(animals: IAnimal[], eventType: EVENT_TYPES_ENUM): IAnimal[];
@@ -1,168 +1,105 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AnimalForEventFilter = void 0;
3
+ exports.filter = void 0;
4
4
  const enums_1 = require("../enums");
5
5
  const date_1 = require("../helpers/date");
6
6
  const status = enums_1.ANIMAL_STATUS_ENUM;
7
7
  const defaultCowAge = 12;
8
- class AnimalForEventFilter {
9
- constructor(animals, eventType) {
10
- this.animals = animals;
11
- this.eventType = eventType;
12
- }
13
- genderFilter(animal, availableGender) {
14
- if (animal.gender === availableGender) {
15
- return true;
16
- }
17
- return false;
18
- }
19
- ageFilter(animal, availableMinAge /*month*/) {
20
- const months = (0, date_1.getMonthFromDate)(animal.dob.toISOString());
21
- if (months > availableMinAge) {
22
- return true;
23
- }
24
- return false;
25
- }
26
- statusFilter(animal, availableStatuses) {
27
- if (availableStatuses.includes(animal.status)) {
28
- return true;
29
- }
30
- return false;
31
- }
32
- abortFilter() {
33
- const result = [];
34
- this.animals.forEach(animal => {
35
- if (this.genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow)) {
36
- result.push(animal);
37
- }
38
- });
39
- return result;
40
- }
41
- neosemFilter() {
42
- return this.abortFilter();
43
- }
44
- osemenenieFilter() {
45
- const result = [];
46
- this.animals.forEach(animal => {
47
- let genderOk = false;
48
- let statusOk = false;
49
- let ageOk = false;
50
- if (this.genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow)) {
51
- genderOk = true;
52
- }
53
- if (this.statusFilter(animal, [status.heifer, status.bred, status.open, status.fresh])) {
54
- statusOk = true;
55
- }
56
- if (this.ageFilter(animal, defaultCowAge)) {
57
- ageOk = true;
58
- }
59
- if (genderOk && statusOk && ageOk) {
60
- result.push(animal);
61
- }
62
- });
63
- return result;
64
- }
65
- otelFilter() {
66
- const result = [];
67
- this.animals.forEach(animal => {
68
- let genderOk = false;
69
- let statusOk = false;
70
- let ageOk = false;
71
- if (this.genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow)) {
72
- genderOk = true;
73
- }
74
- if (this.statusFilter(animal, [status.dry, status.pregnant])) {
75
- statusOk = true;
76
- }
77
- if (this.ageFilter(animal, defaultCowAge)) {
78
- ageOk = true;
79
- }
80
- if (genderOk && statusOk && ageOk) {
81
- result.push(animal);
82
- }
83
- });
84
- return result;
85
- }
86
- recheckFilter() {
87
- const result = [];
88
- this.animals.forEach(animal => {
89
- let genderOk = false;
90
- let statusOk = false;
91
- let ageOk = false;
92
- if (this.genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow)) {
93
- genderOk = true;
94
- }
95
- if (this.ageFilter(animal, defaultCowAge)) {
96
- ageOk = true;
97
- }
98
- let availableStatus = [status.bred, status.pregnant, status.dry];
99
- if (animal.animalEventRelation) {
100
- const previousRechecks = animal.animalEventRelation.filter((row) => {
101
- return row.eventDetail.type === enums_1.EVENT_TYPES_ENUM.recheck;
102
- });
103
- if (previousRechecks.length == 0) {
104
- availableStatus = [status.bred];
105
- }
106
- }
107
- if (this.statusFilter(animal, availableStatus)) {
108
- statusOk = true;
109
- }
110
- if (genderOk && statusOk && ageOk) {
111
- result.push(animal);
112
- }
113
- });
114
- return result;
115
- }
116
- suhostoiFilter() {
117
- return this.abortFilter();
118
- }
119
- pozSuhostoiFilter() {
120
- const result = [];
121
- this.animals.forEach(animal => {
122
- let genderOk = false;
123
- let statusOk = false;
124
- let ageOk = false;
125
- if (this.genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow)) {
126
- genderOk = true;
127
- }
128
- if (this.statusFilter(animal, [status.dry])) {
129
- statusOk = true;
130
- }
131
- if (this.ageFilter(animal, defaultCowAge)) {
132
- ageOk = true;
133
- }
134
- if (genderOk && statusOk && ageOk) {
135
- result.push(animal);
8
+ /*
9
+
10
+ const filtered = filter(animals, eventType)
11
+
12
+ */
13
+ function genderFilter(animal, availableGender) {
14
+ return animal.gender === availableGender;
15
+ }
16
+ function ageFilter(animal, availableMinAge /*month*/) {
17
+ const months = (0, date_1.getMonthFromDate)(animal.dob.toISOString());
18
+ return months > availableMinAge;
19
+ }
20
+ function statusFilter(animal, availableStatuses) {
21
+ return availableStatuses.includes(animal.status);
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];
136
59
  }
137
- });
60
+ }
61
+ const statusOk = statusFilter(animal, availableStatus);
62
+ return genderOk && statusOk && ageOk && animal;
63
+ });
64
+ console.info('result', result);
65
+ return result;
66
+ }
67
+ function suhostoiFilter(animals) {
68
+ return abortFilter(animals);
69
+ }
70
+ function pozSuhostoiFilter(animals) {
71
+ const result = animals.filter(animal => {
72
+ const genderOk = genderFilter(animal, enums_1.ANIMAL_GENDER_ENUM.cow);
73
+ const statusOk = statusFilter(animal, [status.dry]);
74
+ const ageOk = ageFilter(animal, defaultCowAge);
75
+ return genderOk && statusOk && ageOk && animal;
76
+ });
77
+ return result;
78
+ }
79
+ function transferOutFilter(animals) {
80
+ return animals;
81
+ }
82
+ function perevodFilter(animals) {
83
+ return animals;
84
+ }
85
+ function filter(animals, eventType) {
86
+ const filters = {
87
+ [enums_1.EVENT_TYPES_ENUM.abort]: abortFilter,
88
+ [enums_1.EVENT_TYPES_ENUM.neosem]: neosemFilter,
89
+ [enums_1.EVENT_TYPES_ENUM.osemenenie]: osemenenieFilter,
90
+ [enums_1.EVENT_TYPES_ENUM.otel]: otelFilter,
91
+ [enums_1.EVENT_TYPES_ENUM.recheck]: recheckFilter,
92
+ [enums_1.EVENT_TYPES_ENUM.suhostoi]: suhostoiFilter,
93
+ [enums_1.EVENT_TYPES_ENUM.pozSuhostoi]: pozSuhostoiFilter,
94
+ [enums_1.EVENT_TYPES_ENUM.perevod]: perevodFilter,
95
+ [enums_1.EVENT_TYPES_ENUM.transferOut]: transferOutFilter
96
+ };
97
+ try {
98
+ const result = filters[eventType](animals);
138
99
  return result;
139
100
  }
140
- outputFilter() {
141
- return this.animals;
142
- }
143
- filter() {
144
- switch (this.eventType) {
145
- case enums_1.EVENT_TYPES_ENUM.abort:
146
- return this.abortFilter();
147
- case enums_1.EVENT_TYPES_ENUM.neosem:
148
- return this.neosemFilter();
149
- case enums_1.EVENT_TYPES_ENUM.osemenenie:
150
- return this.osemenenieFilter();
151
- case enums_1.EVENT_TYPES_ENUM.otel:
152
- return this.otelFilter();
153
- case enums_1.EVENT_TYPES_ENUM.recheck:
154
- return this.recheckFilter();
155
- case enums_1.EVENT_TYPES_ENUM.suhostoi:
156
- return this.suhostoiFilter();
157
- case enums_1.EVENT_TYPES_ENUM.pozSuhostoi:
158
- return this.pozSuhostoiFilter();
159
- case enums_1.EVENT_TYPES_ENUM.perevod:
160
- return this.animals;
161
- case enums_1.EVENT_TYPES_ENUM.transferOut:
162
- return this.outputFilter();
163
- default:
164
- throw new Error(`AnimalForEventFilter.filter.${this.eventType} not implemented`);
165
- }
101
+ catch (err) {
102
+ throw new Error(`AnimalForEventFilter.filter.${eventType} not implemented`);
166
103
  }
167
104
  }
168
- exports.AnimalForEventFilter = AnimalForEventFilter;
105
+ exports.filter = filter;
@@ -1,2 +1,2 @@
1
- import { AnimalForEventFilter as _AnimalForEventFilter } from './animal-event-filter';
2
- export { _AnimalForEventFilter as AnimalForEventFilter };
1
+ import { filter as _filter } from './animal-event-filter';
2
+ export { _filter as filter };
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AnimalForEventFilter = void 0;
3
+ exports.filter = void 0;
4
4
  const animal_event_filter_1 = require("./animal-event-filter");
5
- Object.defineProperty(exports, "AnimalForEventFilter", { enumerable: true, get: function () { return animal_event_filter_1.AnimalForEventFilter; } });
5
+ Object.defineProperty(exports, "filter", { enumerable: true, get: function () { return animal_event_filter_1.filter; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molochko-dev/data",
3
- "version": "1.4.29",
3
+ "version": "1.4.30",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "typings/index.d.ts",
@@ -6,184 +6,122 @@ const status = ANIMAL_STATUS_ENUM
6
6
 
7
7
  const defaultCowAge = 12
8
8
 
9
- export class AnimalForEventFilter {
10
- animals: IAnimal[]
11
- eventType: EVENT_TYPES_ENUM
9
+ /*
12
10
 
13
- constructor (animals: IAnimal[], eventType: EVENT_TYPES_ENUM) {
14
- this.animals = animals
15
- this.eventType = eventType
16
- }
11
+ const filtered = filter(animals, eventType)
17
12
 
18
- genderFilter (animal: IAnimal, availableGender: ANIMAL_GENDER_ENUM): Boolean {
19
- if (animal.gender === availableGender) {
20
- return true
21
- }
22
- return false
23
- }
13
+ */
24
14
 
25
- ageFilter (animal: IAnimal, availableMinAge: number /*month*/): Boolean {
26
- const months = getMonthFromDate(animal.dob.toISOString())
27
- if ( months > availableMinAge) {
28
- return true
29
- }
30
- return false
31
- }
15
+ function genderFilter (animal: IAnimal, availableGender: ANIMAL_GENDER_ENUM): Boolean {
16
+ return animal.gender === availableGender
17
+ }
32
18
 
33
- statusFilter (animal: IAnimal, availableStatuses: ANIMAL_STATUS_ENUM[]): Boolean {
34
- if (availableStatuses.includes(animal.status)) {
35
- return true
36
- }
37
- return false
38
- }
19
+ function ageFilter (animal: IAnimal, availableMinAge: number /*month*/): Boolean {
20
+ const months = getMonthFromDate(animal.dob.toISOString())
21
+ return months > availableMinAge
22
+ }
39
23
 
40
- abortFilter () {
41
- const result: IAnimal[] = []
42
- this.animals.forEach(animal => {
43
- if (this.genderFilter(animal, ANIMAL_GENDER_ENUM.cow)) {
44
- result.push(animal)
45
- }
46
- })
47
- return result
48
- }
24
+ function statusFilter (animal: IAnimal, availableStatuses: ANIMAL_STATUS_ENUM[]): Boolean {
25
+ return availableStatuses.includes(animal.status)
26
+ }
49
27
 
50
- neosemFilter () {
51
- return this.abortFilter()
52
- }
28
+ function abortFilter (animals: IAnimal[]): IAnimal[] {
29
+ const result: IAnimal[] = animals.filter(animal => genderFilter(animal, ANIMAL_GENDER_ENUM.cow))
30
+ return result
31
+ }
53
32
 
54
- osemenenieFilter() {
55
- const result: IAnimal[] = []
56
- this.animals.forEach(animal => {
57
- let genderOk = false
58
- let statusOk = false
59
- let ageOk = false
33
+ function neosemFilter (animals: IAnimal[]): IAnimal[] {
34
+ return abortFilter(animals)
35
+ }
60
36
 
61
- if (this.genderFilter(animal, ANIMAL_GENDER_ENUM.cow)) {
62
- genderOk = true
63
- }
64
- if (this.statusFilter(animal, [status.heifer, status.bred, status.open, status.fresh])) {
65
- statusOk = true
66
- }
67
- if (this.ageFilter(animal, defaultCowAge)) {
68
- ageOk = true
69
- }
70
- if (genderOk && statusOk && ageOk) {
71
- result.push(animal)
72
- }
73
- })
74
- return result
75
- }
37
+ function osemenenieFilter(animals: IAnimal[]): IAnimal[] {
38
+ const result: IAnimal[] = animals.filter(animal => {
39
+ const genderOk = genderFilter(animal, ANIMAL_GENDER_ENUM.cow)
40
+ const statusOk = statusFilter(animal, [status.heifer, status.bred, status.open, status.fresh])
41
+ const ageOk = ageFilter(animal, defaultCowAge)
76
42
 
77
- otelFilter () {
78
- const result: IAnimal[] = []
79
- this.animals.forEach(animal => {
80
- let genderOk = false
81
- let statusOk = false
82
- let ageOk = false
43
+ return genderOk && statusOk && ageOk && animal
44
+ })
83
45
 
84
- if (this.genderFilter(animal, ANIMAL_GENDER_ENUM.cow)) {
85
- genderOk = true
86
- }
87
- if (this.statusFilter(animal, [status.dry, status.pregnant])) {
88
- statusOk = true
89
- }
90
- if (this.ageFilter(animal, defaultCowAge)) {
91
- ageOk = true
92
- }
93
- if (genderOk && statusOk && ageOk) {
94
- result.push(animal)
95
- }
96
- })
97
- return result
98
- }
46
+ return result
47
+ }
99
48
 
100
- recheckFilter () {
101
- const result: IAnimal[] = []
102
- this.animals.forEach(animal => {
103
- let genderOk = false
104
- let statusOk = false
105
- let ageOk = false
49
+ function otelFilter (animals: IAnimal[]): IAnimal[] {
50
+ const result: IAnimal[] = animals.filter(animal => {
51
+ const genderOk = genderFilter(animal, ANIMAL_GENDER_ENUM.cow)
52
+ const statusOk = statusFilter(animal, [status.dry, status.pregnant])
53
+ const ageOk = ageFilter(animal, defaultCowAge)
106
54
 
107
- if (this.genderFilter(animal, ANIMAL_GENDER_ENUM.cow)) {
108
- genderOk = true
109
- }
110
- if (this.ageFilter(animal, defaultCowAge)) {
111
- ageOk = true
112
- }
55
+ return genderOk && statusOk && ageOk && animal
56
+ })
57
+ return result
58
+ }
113
59
 
114
- let availableStatus = [status.bred, status.pregnant, status.dry]
115
- if (animal.animalEventRelation) {
116
- const previousRechecks = animal.animalEventRelation.filter((row) => {
117
- return row.eventDetail.type === EVENT_TYPES_ENUM.recheck
118
- })
119
- if (previousRechecks.length == 0) {
120
- availableStatus = [status.bred]
121
- }
60
+ function recheckFilter (animals: IAnimal[]): IAnimal[] {
61
+ const result: IAnimal[] = animals.filter(animal => {
62
+ const genderOk = genderFilter(animal, ANIMAL_GENDER_ENUM.cow)
63
+ const ageOk = ageFilter(animal, defaultCowAge)
64
+
65
+ let availableStatus = [status.bred, status.pregnant, status.dry]
66
+ if (animal.animalEventRelation) {
67
+ const previousRechecks = animal.animalEventRelation.filter((row) => {
68
+ return row.eventDetail.type === EVENT_TYPES_ENUM.recheck
69
+ })
70
+ if (previousRechecks.length == 0) {
71
+ availableStatus = [status.bred]
122
72
  }
123
- if (this.statusFilter(animal, availableStatus)) {
124
- statusOk = true
125
- }
126
- if (genderOk && statusOk && ageOk) {
127
- result.push(animal)
128
- }
129
- })
130
- return result
131
- }
73
+ }
74
+ const statusOk = statusFilter(animal, availableStatus)
75
+
76
+ return genderOk && statusOk && ageOk && animal
77
+ })
78
+ console.info('result', result)
79
+ return result
80
+ }
132
81
 
133
- suhostoiFilter () {
134
- return this.abortFilter()
135
- }
82
+ function suhostoiFilter (animals: IAnimal[]): IAnimal[] {
83
+ return abortFilter(animals)
84
+ }
136
85
 
137
- pozSuhostoiFilter () {
138
- const result: IAnimal[] = []
139
- this.animals.forEach(animal => {
140
- let genderOk = false
141
- let statusOk = false
142
- let ageOk = false
86
+ function pozSuhostoiFilter (animals: IAnimal[]): IAnimal[] {
87
+ const result: IAnimal[] = animals.filter(animal => {
88
+ const genderOk = genderFilter(animal, ANIMAL_GENDER_ENUM.cow)
89
+ const statusOk = statusFilter(animal, [status.dry])
90
+ const ageOk = ageFilter(animal, defaultCowAge)
143
91
 
144
- if (this.genderFilter(animal, ANIMAL_GENDER_ENUM.cow)) {
145
- genderOk = true
146
- }
147
- if (this.statusFilter(animal, [status.dry])) {
148
- statusOk = true
149
- }
150
- if (this.ageFilter(animal, defaultCowAge)) {
151
- ageOk = true
152
- }
153
- if (genderOk && statusOk && ageOk) {
154
- result.push(animal)
155
- }
156
- })
157
- return result
158
- }
92
+ return genderOk && statusOk && ageOk && animal
93
+ })
94
+ return result
95
+ }
159
96
 
160
- outputFilter () {
161
- return this.animals
162
- }
97
+ function transferOutFilter (animals: IAnimal[]): IAnimal[] {
98
+ return animals
99
+ }
163
100
 
164
- filter () {
165
- switch (this.eventType) {
166
- case EVENT_TYPES_ENUM.abort:
167
- return this.abortFilter()
168
- case EVENT_TYPES_ENUM.neosem:
169
- return this.neosemFilter()
170
- case EVENT_TYPES_ENUM.osemenenie:
171
- return this.osemenenieFilter()
172
- case EVENT_TYPES_ENUM.otel:
173
- return this.otelFilter()
174
- case EVENT_TYPES_ENUM.recheck:
175
- return this.recheckFilter()
176
- case EVENT_TYPES_ENUM.suhostoi:
177
- return this.suhostoiFilter()
178
- case EVENT_TYPES_ENUM.pozSuhostoi:
179
- return this.pozSuhostoiFilter()
180
- case EVENT_TYPES_ENUM.perevod:
181
- return this.animals
182
- case EVENT_TYPES_ENUM.transferOut:
183
- return this.outputFilter()
184
- default:
185
- throw new Error(`AnimalForEventFilter.filter.${this.eventType} not implemented`)
186
- }
187
- }
101
+ function perevodFilter (animals: IAnimal[]) {
102
+ return animals
103
+ }
104
+
105
+ interface IFilters {
106
+ [key: string]: (animals: IAnimal[]) => IAnimal[]
188
107
  }
189
108
 
109
+ export function filter (animals: IAnimal[], eventType: EVENT_TYPES_ENUM) {
110
+ const filters: IFilters = {
111
+ [EVENT_TYPES_ENUM.abort]: abortFilter,
112
+ [EVENT_TYPES_ENUM.neosem]: neosemFilter,
113
+ [EVENT_TYPES_ENUM.osemenenie]: osemenenieFilter,
114
+ [EVENT_TYPES_ENUM.otel]: otelFilter,
115
+ [EVENT_TYPES_ENUM.recheck]: recheckFilter,
116
+ [EVENT_TYPES_ENUM.suhostoi]: suhostoiFilter,
117
+ [EVENT_TYPES_ENUM.pozSuhostoi]: pozSuhostoiFilter,
118
+ [EVENT_TYPES_ENUM.perevod]: perevodFilter,
119
+ [EVENT_TYPES_ENUM.transferOut]: transferOutFilter
120
+ }
121
+ try {
122
+ const result = filters[eventType](animals)
123
+ return result
124
+ } catch (err) {
125
+ throw new Error(`AnimalForEventFilter.filter.${eventType} not implemented`)
126
+ }
127
+ }
@@ -1,3 +1,3 @@
1
- import { AnimalForEventFilter as _AnimalForEventFilter } from './animal-event-filter'
1
+ import { filter as _filter } from './animal-event-filter'
2
2
 
3
- export { _AnimalForEventFilter as AnimalForEventFilter }
3
+ export { _filter as filter }