dyo-tools 0.3.0 → 0.3.2

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,90 +1,90 @@
1
- import DYOToolsComponent from './DTComponent';
2
- import { DTModuleToObject } from '../types';
3
- import DYOToolsMaster from './DTMaster';
4
-
5
- export default class DYOToolsModule extends DYOToolsComponent {
6
- /**
7
- * Defining component type to "module".
8
- */
9
- protected _componentType: string = 'module';
10
-
11
- /**
12
- * Defining if the module is enabled or not.
13
- * @protected
14
- */
15
- protected _enabled: boolean;
16
-
17
- /**
18
- * Applying the parent constructor, and initializing _enabled property to true by default.
19
- *
20
- * @param key
21
- */
22
- constructor(key?: string) {
23
- super(key);
24
-
25
- this._enabled = true;
26
- }
27
-
28
- /**
29
- * Returns Master component from context, or undefined.
30
- */
31
- getMaster(): DYOToolsMaster | undefined {
32
- return this.getContext<DYOToolsMaster>('master');
33
- }
34
-
35
- /**
36
- * Getter for the _enabled property.
37
- */
38
- isEnabled(): boolean {
39
- return this._enabled;
40
- }
41
-
42
- /**
43
- * Change _enabled property to true.
44
- */
45
- enable(): void {
46
- this._enabled = true;
47
- }
48
-
49
- /**
50
- * Change _enabled property to false.
51
- */
52
- disable(): void {
53
- this._enabled = false;
54
- }
55
-
56
- /**
57
- * Return String representation of the Module instance.
58
- *
59
- * @returns String representation of the Module.
60
- */
61
- toString(): string {
62
- let extraLabel: string = '';
63
-
64
- if (!this._enabled) {
65
- extraLabel = ' - Disabled';
66
- }
67
-
68
- return `Component ${this._key} - Type: Module${extraLabel}`;
69
- }
70
-
71
- /**
72
- * Return JSON Object representation of the Module instance.
73
- *
74
- * JSON Object returned has the following structure :
75
- * * **id** : _id property of the Module.
76
- * * **key** : _key property of the Module.
77
- * * **type** : _componentType property of the Module.
78
- * * **enabled** : _enabled property of the Module.
79
- *
80
- * @returns JSON Object representation of the Module.
81
- */
82
- toObject(): DTModuleToObject {
83
- return {
84
- id: this._id,
85
- key: this._key,
86
- type: this._componentType,
87
- enabled: this._enabled,
88
- };
89
- }
90
- }
1
+ import DYOToolsComponent from './DTComponent';
2
+ import { DTModuleToObject } from '../types';
3
+ import DYOToolsMaster from './DTMaster';
4
+
5
+ export default class DYOToolsModule extends DYOToolsComponent {
6
+ /**
7
+ * Defining component type to "module".
8
+ */
9
+ protected _componentType: string = 'module';
10
+
11
+ /**
12
+ * Defining if the module is enabled or not.
13
+ * @protected
14
+ */
15
+ protected _enabled: boolean;
16
+
17
+ /**
18
+ * Applying the parent constructor, and initializing _enabled property to true by default.
19
+ *
20
+ * @param key
21
+ */
22
+ constructor(key?: string) {
23
+ super(key);
24
+
25
+ this._enabled = true;
26
+ }
27
+
28
+ /**
29
+ * Returns Master component from context, or undefined.
30
+ */
31
+ getMaster(): DYOToolsMaster | undefined {
32
+ return this.getContext<DYOToolsMaster>('master');
33
+ }
34
+
35
+ /**
36
+ * Getter for the _enabled property.
37
+ */
38
+ isEnabled(): boolean {
39
+ return this._enabled;
40
+ }
41
+
42
+ /**
43
+ * Change _enabled property to true.
44
+ */
45
+ enable(): void {
46
+ this._enabled = true;
47
+ }
48
+
49
+ /**
50
+ * Change _enabled property to false.
51
+ */
52
+ disable(): void {
53
+ this._enabled = false;
54
+ }
55
+
56
+ /**
57
+ * Return String representation of the Module instance.
58
+ *
59
+ * @returns String representation of the Module.
60
+ */
61
+ toString(): string {
62
+ let extraLabel: string = '';
63
+
64
+ if (!this._enabled) {
65
+ extraLabel = ' - Disabled';
66
+ }
67
+
68
+ return `Component ${this._key} - Type: Module${extraLabel}`;
69
+ }
70
+
71
+ /**
72
+ * Return JSON Object representation of the Module instance.
73
+ *
74
+ * JSON Object returned has the following structure :
75
+ * * **id** : _id property of the Module.
76
+ * * **key** : _key property of the Module.
77
+ * * **type** : _componentType property of the Module.
78
+ * * **enabled** : _enabled property of the Module.
79
+ *
80
+ * @returns JSON Object representation of the Module.
81
+ */
82
+ toObject(): DTModuleToObject {
83
+ return {
84
+ id: this._id,
85
+ key: this._key,
86
+ type: this._componentType,
87
+ enabled: this._enabled,
88
+ };
89
+ }
90
+ }
package/src/index.ts CHANGED
@@ -1,17 +1,17 @@
1
- // All exports
2
- // CORE
3
- export { default as DTComponent } from './core/DTComponent';
4
- export { default as DTComponentWithMeta } from './core/DTComponentWithMeta';
5
- export { default as DTComponentPhysical } from './core/DTComponentPhysical';
6
- export { default as DTElement } from './core/DTElement';
7
- export { default as DTBunch } from './core/DTBunch';
8
- export { default as DTManager } from './core/DTManager';
9
- export { default as DTMaster } from './core/DTMaster';
10
- export { default as DTError } from './core/DTError';
11
- export { default as DTAction } from './core/DTAction';
12
- export { default as DTModule } from './core/DTModule';
13
-
14
- // LIBS
15
- // PLAYER
16
- export { default as DTPlayer } from './libs/player/DTPlayer.element';
17
- export { default as DTPlayerManager } from './libs/player/DTPlayer.manager';
1
+ // All exports
2
+ // CORE
3
+ export { default as DTComponent } from './core/DTComponent';
4
+ export { default as DTComponentWithMeta } from './core/DTComponentWithMeta';
5
+ export { default as DTComponentPhysical } from './core/DTComponentPhysical';
6
+ export { default as DTElement } from './core/DTElement';
7
+ export { default as DTBunch } from './core/DTBunch';
8
+ export { default as DTManager } from './core/DTManager';
9
+ export { default as DTMaster } from './core/DTMaster';
10
+ export { default as DTError } from './core/DTError';
11
+ export { default as DTAction } from './core/DTAction';
12
+ export { default as DTModule } from './core/DTModule';
13
+
14
+ // LIBS
15
+ // PLAYER
16
+ export { default as DTPlayer } from './libs/player/DTPlayer.element';
17
+ export { default as DTPlayerManager } from './libs/player/DTPlayer.manager';
@@ -1,175 +1,175 @@
1
- import {
2
- DYOFinderComponentType,
3
- DYOFinderConfiguration,
4
- DYOFinderFilterOperator,
5
- DYOFinderFilters,
6
- FilterOperatorType,
7
- StandardPrimitiveType,
8
- StandardPrimitiveTypeWithArray,
9
- } from '../types';
10
-
11
- export default class DYOFinder {
12
- /**
13
- * Current DTComponent associated to the Finder.
14
- */
15
- protected _component: DYOFinderComponentType;
16
-
17
- /**
18
- * Current DYOFinder configuration applied.
19
- */
20
- protected _configuration: DYOFinderConfiguration;
21
-
22
- /**
23
- * Set _component and _configuration properties.
24
- *
25
- * @param component
26
- * @param configuration
27
- */
28
- constructor(component: DYOFinderComponentType, configuration: DYOFinderConfiguration) {
29
- this._component = component;
30
- this._configuration = configuration;
31
- }
32
-
33
- /**
34
- * Getter for _component property.
35
- */
36
- getComponent(): DYOFinderComponentType {
37
- return this._component;
38
- }
39
-
40
- /**
41
- * Return an array of DTComponent from *_component* **_items** property filtered with a **filters** argument.
42
- *
43
- * Search filters can be applied on properties depending on the current _configuration provided.
44
- *
45
- * For each search filter provided, an object of specific operators is applied :
46
- * * **BASIC OPERATORS**
47
- * * **$eq** : The property must be strict equal to the filter value.
48
- * * **$in** : The property must be included into the filter array.
49
- * * **$nin** : The property must not be included into the filter array.
50
- * * **$ne** : The property must be different to the filter value.
51
- * * **EXTENDED OPERATORS** (meta only)
52
- * * **$lte** : Number property only. The property must be lower or equal than the filter value.
53
- * * **$gte** : Number property only. The property must be higher or equal than the filter array.
54
- * * **$contains** : Array property only. The property must contain the filter value.
55
- * * **$ncontains** : Array property only. The property must not contain the filter value.
56
- *
57
- * If many operators and / or many properties are passed into the **filters** argument, the logic operator applied is
58
- * **AND**.
59
- *
60
- * @param filters Filters Object. The format is :
61
- * { [property_1] : { [operator_1] : filter_value, [operator_2] : filter_value_2, ... }, [property_2] : { ... }, ... }
62
- *
63
- * For **objectSearch** properties, you have to pass the object key before the operator :
64
- * { [property_1]: { [object_key1] : { [operator_1] : filter_value_1, ... }, [object_key2] : { ... }, ... }, ... }
65
- * @returns Array of DTComponent instance corresponding to the filters. Empty if no filter or invalid ones are passed.
66
- */
67
- execute<ITEM>(filters: DYOFinderFilters): ITEM[] {
68
- const items = this._component.getAll();
69
- const filteredItems = [];
70
-
71
- for (const item of items) {
72
- let validItem = !!(Object.keys(filters).length);
73
-
74
- for (const [propKey, configProp] of Object.entries(this._configuration)) {
75
- if (filters[propKey]) {
76
- if (configProp.objectSearch) {
77
- const objectValue = configProp.getValue(item, this.getComponent());
78
- if (objectValue) {
79
- for (const [filterK, filterV] of Object.entries(filters[propKey])) {
80
- const metaValue = Object.prototype.hasOwnProperty.call(objectValue, filterK) ? objectValue[filterK] : undefined;
81
- if (!this.checkAllValidFiltersForProp(metaValue, filterV, configProp.operators)) {
82
- validItem = false;
83
- break;
84
- }
85
- }
86
- } else {
87
- validItem = false;
88
- }
89
- } else if (!this.checkAllValidFiltersForProp(
90
- (configProp.getValue(item, this.getComponent()) as StandardPrimitiveType),
91
- filters[propKey],
92
- configProp.operators,
93
- )) {
94
- validItem = false;
95
- }
96
- }
97
- }
98
-
99
- if (validItem) {
100
- filteredItems.push(item);
101
- }
102
- }
103
-
104
- return filteredItems;
105
- }
106
-
107
- private checkAllValidFiltersForProp = (
108
- itemProp: StandardPrimitiveTypeWithArray,
109
- operators: Partial<DYOFinderFilterOperator>,
110
- validOperators: FilterOperatorType[],
111
- ) => {
112
- if (Object.keys(operators).length) {
113
- for (const operator of Object.keys(operators)) {
114
- if (!validOperators.includes(operator as FilterOperatorType)
115
- || !this.validFiltersForItem(itemProp, operators[operator], operator as FilterOperatorType)) {
116
- return false;
117
- }
118
- }
119
- return true;
120
- }
121
- return false;
122
- };
123
-
124
- private validFiltersForItem(
125
- itemProp: StandardPrimitiveTypeWithArray,
126
- filter: StandardPrimitiveTypeWithArray,
127
- operator: FilterOperatorType,
128
- ): boolean {
129
- // $eq Filter
130
- if (operator === FilterOperatorType.EQ) {
131
- return itemProp === filter;
132
- }
133
- // $in Filter
134
- if (operator === FilterOperatorType.IN) {
135
- return filter ? (filter as Array<StandardPrimitiveType>).includes(itemProp as StandardPrimitiveType) : false;
136
- }
137
- // $nin Filter
138
- if (operator === FilterOperatorType.NIN) {
139
- return filter ? !(filter as Array<StandardPrimitiveType>).includes(itemProp as StandardPrimitiveType) : false;
140
- }
141
- // $ne Filter
142
- /* c8 ignore next */
143
- if (operator === FilterOperatorType.NE) {
144
- return itemProp !== filter;
145
- }
146
- // $lte Filter
147
- if (operator === FilterOperatorType.LTE) {
148
- if (typeof itemProp === 'number' && typeof filter === 'number') {
149
- return itemProp <= filter;
150
- }
151
- return false;
152
- }
153
- // $gte Filter
154
- if (operator === FilterOperatorType.GTE) {
155
- if (typeof itemProp === 'number' && typeof filter === 'number') {
156
- return itemProp >= filter;
157
- }
158
- return false;
159
- }
160
- // $contains Filter
161
- if (operator === FilterOperatorType.CONTAINS) {
162
- return (itemProp && Array.isArray(itemProp))
163
- ? (itemProp as Array<StandardPrimitiveType>).includes(filter as StandardPrimitiveType)
164
- : false;
165
- }
166
- // $ncontains Filter
167
- if (operator === FilterOperatorType.NCONTAINS) {
168
- return (itemProp && Array.isArray(itemProp))
169
- ? !(itemProp as Array<StandardPrimitiveType>).includes(filter as StandardPrimitiveType)
170
- : false;
171
- }
172
-
173
- return false;
174
- }
175
- }
1
+ import {
2
+ DYOFinderComponentType,
3
+ DYOFinderConfiguration,
4
+ DYOFinderFilterOperator,
5
+ DYOFinderFilters,
6
+ FilterOperatorType,
7
+ StandardPrimitiveType,
8
+ StandardPrimitiveTypeWithArray,
9
+ } from '../types';
10
+
11
+ export default class DYOFinder {
12
+ /**
13
+ * Current DTComponent associated to the Finder.
14
+ */
15
+ protected _component: DYOFinderComponentType;
16
+
17
+ /**
18
+ * Current DYOFinder configuration applied.
19
+ */
20
+ protected _configuration: DYOFinderConfiguration;
21
+
22
+ /**
23
+ * Set _component and _configuration properties.
24
+ *
25
+ * @param component
26
+ * @param configuration
27
+ */
28
+ constructor(component: DYOFinderComponentType, configuration: DYOFinderConfiguration) {
29
+ this._component = component;
30
+ this._configuration = configuration;
31
+ }
32
+
33
+ /**
34
+ * Getter for _component property.
35
+ */
36
+ getComponent(): DYOFinderComponentType {
37
+ return this._component;
38
+ }
39
+
40
+ /**
41
+ * Return an array of DTComponent from *_component* **_items** property filtered with a **filters** argument.
42
+ *
43
+ * Search filters can be applied on properties depending on the current _configuration provided.
44
+ *
45
+ * For each search filter provided, an object of specific operators is applied :
46
+ * * **BASIC OPERATORS**
47
+ * * **$eq** : The property must be strict equal to the filter value.
48
+ * * **$in** : The property must be included into the filter array.
49
+ * * **$nin** : The property must not be included into the filter array.
50
+ * * **$ne** : The property must be different to the filter value.
51
+ * * **EXTENDED OPERATORS** (meta only)
52
+ * * **$lte** : Number property only. The property must be lower or equal than the filter value.
53
+ * * **$gte** : Number property only. The property must be higher or equal than the filter array.
54
+ * * **$contains** : Array property only. The property must contain the filter value.
55
+ * * **$ncontains** : Array property only. The property must not contain the filter value.
56
+ *
57
+ * If many operators and / or many properties are passed into the **filters** argument, the logic operator applied is
58
+ * **AND**.
59
+ *
60
+ * @param filters Filters Object. The format is :
61
+ * { [property_1] : { [operator_1] : filter_value, [operator_2] : filter_value_2, ... }, [property_2] : { ... }, ... }
62
+ *
63
+ * For **objectSearch** properties, you have to pass the object key before the operator :
64
+ * { [property_1]: { [object_key1] : { [operator_1] : filter_value_1, ... }, [object_key2] : { ... }, ... }, ... }
65
+ * @returns Array of DTComponent instance corresponding to the filters. Empty if no filter or invalid ones are passed.
66
+ */
67
+ execute<ITEM>(filters: DYOFinderFilters): ITEM[] {
68
+ const items = this._component.getAll();
69
+ const filteredItems = [];
70
+
71
+ for (const item of items) {
72
+ let validItem = !!(Object.keys(filters).length);
73
+
74
+ for (const [propKey, configProp] of Object.entries(this._configuration)) {
75
+ if (filters[propKey]) {
76
+ if (configProp.objectSearch) {
77
+ const objectValue = configProp.getValue(item, this.getComponent());
78
+ if (objectValue) {
79
+ for (const [filterK, filterV] of Object.entries(filters[propKey])) {
80
+ const metaValue = Object.prototype.hasOwnProperty.call(objectValue, filterK) ? objectValue[filterK] : undefined;
81
+ if (!this.checkAllValidFiltersForProp(metaValue, filterV, configProp.operators)) {
82
+ validItem = false;
83
+ break;
84
+ }
85
+ }
86
+ } else {
87
+ validItem = false;
88
+ }
89
+ } else if (!this.checkAllValidFiltersForProp(
90
+ (configProp.getValue(item, this.getComponent()) as StandardPrimitiveType),
91
+ filters[propKey],
92
+ configProp.operators,
93
+ )) {
94
+ validItem = false;
95
+ }
96
+ }
97
+ }
98
+
99
+ if (validItem) {
100
+ filteredItems.push(item);
101
+ }
102
+ }
103
+
104
+ return filteredItems;
105
+ }
106
+
107
+ private checkAllValidFiltersForProp = (
108
+ itemProp: StandardPrimitiveTypeWithArray,
109
+ operators: Partial<DYOFinderFilterOperator>,
110
+ validOperators: FilterOperatorType[],
111
+ ) => {
112
+ if (Object.keys(operators).length) {
113
+ for (const operator of Object.keys(operators)) {
114
+ if (!validOperators.includes(operator as FilterOperatorType)
115
+ || !this.validFiltersForItem(itemProp, operators[operator], operator as FilterOperatorType)) {
116
+ return false;
117
+ }
118
+ }
119
+ return true;
120
+ }
121
+ return false;
122
+ };
123
+
124
+ private validFiltersForItem(
125
+ itemProp: StandardPrimitiveTypeWithArray,
126
+ filter: StandardPrimitiveTypeWithArray,
127
+ operator: FilterOperatorType,
128
+ ): boolean {
129
+ // $eq Filter
130
+ if (operator === FilterOperatorType.EQ) {
131
+ return itemProp === filter;
132
+ }
133
+ // $in Filter
134
+ if (operator === FilterOperatorType.IN) {
135
+ return filter ? (filter as Array<StandardPrimitiveType>).includes(itemProp as StandardPrimitiveType) : false;
136
+ }
137
+ // $nin Filter
138
+ if (operator === FilterOperatorType.NIN) {
139
+ return filter ? !(filter as Array<StandardPrimitiveType>).includes(itemProp as StandardPrimitiveType) : false;
140
+ }
141
+ // $ne Filter
142
+ /* c8 ignore next */
143
+ if (operator === FilterOperatorType.NE) {
144
+ return itemProp !== filter;
145
+ }
146
+ // $lte Filter
147
+ if (operator === FilterOperatorType.LTE) {
148
+ if (typeof itemProp === 'number' && typeof filter === 'number') {
149
+ return itemProp <= filter;
150
+ }
151
+ return false;
152
+ }
153
+ // $gte Filter
154
+ if (operator === FilterOperatorType.GTE) {
155
+ if (typeof itemProp === 'number' && typeof filter === 'number') {
156
+ return itemProp >= filter;
157
+ }
158
+ return false;
159
+ }
160
+ // $contains Filter
161
+ if (operator === FilterOperatorType.CONTAINS) {
162
+ return (itemProp && Array.isArray(itemProp))
163
+ ? (itemProp as Array<StandardPrimitiveType>).includes(filter as StandardPrimitiveType)
164
+ : false;
165
+ }
166
+ // $ncontains Filter
167
+ if (operator === FilterOperatorType.NCONTAINS) {
168
+ return (itemProp && Array.isArray(itemProp))
169
+ ? !(itemProp as Array<StandardPrimitiveType>).includes(filter as StandardPrimitiveType)
170
+ : false;
171
+ }
172
+
173
+ return false;
174
+ }
175
+ }
@@ -1,9 +1,9 @@
1
- import { DTAcceptedMetaData } from '../../types';
2
- import DYOToolsElement from '../../core/DTElement';
3
-
4
- export default class DYOToolsPlayer<IComponentMeta extends DTAcceptedMetaData = DTAcceptedMetaData> extends DYOToolsElement<IComponentMeta> {
5
- /**
6
- * Defining component domain to "player".
7
- */
8
- protected _domain = 'player';
9
- }
1
+ import { DTAcceptedMetaData } from '../../types';
2
+ import DYOToolsElement from '../../core/DTElement';
3
+
4
+ export default class DYOToolsPlayer<IComponentMeta extends DTAcceptedMetaData = DTAcceptedMetaData> extends DYOToolsElement<IComponentMeta> {
5
+ /**
6
+ * Defining component domain to "player".
7
+ */
8
+ protected _domain = 'player';
9
+ }