pinia-orm-edge 1.7.3-28257628.bbb8bab
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.
- package/LICENSE +21 -0
- package/README.md +96 -0
- package/dist/casts.cjs +124 -0
- package/dist/casts.d.cts +66 -0
- package/dist/casts.d.mts +66 -0
- package/dist/casts.d.ts +66 -0
- package/dist/casts.mjs +118 -0
- package/dist/decorators.cjs +219 -0
- package/dist/decorators.d.cts +107 -0
- package/dist/decorators.d.mts +107 -0
- package/dist/decorators.d.ts +107 -0
- package/dist/decorators.mjs +198 -0
- package/dist/helpers.cjs +69 -0
- package/dist/helpers.d.cts +54 -0
- package/dist/helpers.d.mts +54 -0
- package/dist/helpers.d.ts +54 -0
- package/dist/helpers.mjs +61 -0
- package/dist/index.cjs +3592 -0
- package/dist/index.d.cts +49 -0
- package/dist/index.d.mts +49 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.mjs +3561 -0
- package/dist/nanoid/async.cjs +42 -0
- package/dist/nanoid/async.d.cts +29 -0
- package/dist/nanoid/async.d.mts +29 -0
- package/dist/nanoid/async.d.ts +29 -0
- package/dist/nanoid/async.mjs +39 -0
- package/dist/nanoid/index.cjs +42 -0
- package/dist/nanoid/index.d.cts +24 -0
- package/dist/nanoid/index.d.mts +24 -0
- package/dist/nanoid/index.d.ts +24 -0
- package/dist/nanoid/index.mjs +39 -0
- package/dist/nanoid/non-secure.cjs +42 -0
- package/dist/nanoid/non-secure.d.cts +24 -0
- package/dist/nanoid/non-secure.d.mts +24 -0
- package/dist/nanoid/non-secure.d.ts +24 -0
- package/dist/nanoid/non-secure.mjs +39 -0
- package/dist/shared/pinia-orm.1bd7d299.mjs +153 -0
- package/dist/shared/pinia-orm.4ff2e12f.mjs +66 -0
- package/dist/shared/pinia-orm.876a7cdc.d.cts +1981 -0
- package/dist/shared/pinia-orm.876a7cdc.d.mts +1981 -0
- package/dist/shared/pinia-orm.876a7cdc.d.ts +1981 -0
- package/dist/shared/pinia-orm.a62c4fc4.cjs +167 -0
- package/dist/shared/pinia-orm.a7e3e0f3.cjs +68 -0
- package/dist/uuid/v1.cjs +41 -0
- package/dist/uuid/v1.d.cts +25 -0
- package/dist/uuid/v1.d.mts +25 -0
- package/dist/uuid/v1.d.ts +25 -0
- package/dist/uuid/v1.mjs +38 -0
- package/dist/uuid/v4.cjs +41 -0
- package/dist/uuid/v4.d.cts +25 -0
- package/dist/uuid/v4.d.mts +25 -0
- package/dist/uuid/v4.d.ts +25 -0
- package/dist/uuid/v4.mjs +38 -0
- package/package.json +126 -0
@@ -0,0 +1,198 @@
|
|
1
|
+
function Attr(value) {
|
2
|
+
return (target, propertyKey) => {
|
3
|
+
const self = target.$self();
|
4
|
+
self.setRegistry(propertyKey, () => self.attr(value));
|
5
|
+
};
|
6
|
+
}
|
7
|
+
|
8
|
+
function Str(value, options = {}) {
|
9
|
+
return (target, propertyKey) => {
|
10
|
+
const self = target.$self();
|
11
|
+
self.setRegistry(propertyKey, () => {
|
12
|
+
const attr = self.string(value);
|
13
|
+
if (options.notNullable) {
|
14
|
+
attr.notNullable();
|
15
|
+
}
|
16
|
+
return attr;
|
17
|
+
});
|
18
|
+
};
|
19
|
+
}
|
20
|
+
|
21
|
+
function Num(value, options = {}) {
|
22
|
+
return (target, propertyKey) => {
|
23
|
+
const self = target.$self();
|
24
|
+
self.setRegistry(propertyKey, () => {
|
25
|
+
const attr = self.number(value);
|
26
|
+
if (options.notNullable) {
|
27
|
+
attr.notNullable();
|
28
|
+
}
|
29
|
+
return attr;
|
30
|
+
});
|
31
|
+
};
|
32
|
+
}
|
33
|
+
|
34
|
+
function Bool(value, options = {}) {
|
35
|
+
return (target, propertyKey) => {
|
36
|
+
const self = target.$self();
|
37
|
+
self.setRegistry(propertyKey, () => {
|
38
|
+
const attr = self.boolean(value);
|
39
|
+
if (options.notNullable) {
|
40
|
+
attr.notNullable();
|
41
|
+
}
|
42
|
+
return attr;
|
43
|
+
});
|
44
|
+
};
|
45
|
+
}
|
46
|
+
|
47
|
+
function Uid(options) {
|
48
|
+
return (target, propertyKey) => {
|
49
|
+
const self = target.$self();
|
50
|
+
self.setRegistry(propertyKey, () => self.uid(options));
|
51
|
+
};
|
52
|
+
}
|
53
|
+
|
54
|
+
function HasOne(related, foreignKey, localKey) {
|
55
|
+
return (target, propertyKey) => {
|
56
|
+
const self = target.$self();
|
57
|
+
self.setRegistry(
|
58
|
+
propertyKey,
|
59
|
+
() => self.hasOne(related(), foreignKey, localKey)
|
60
|
+
);
|
61
|
+
};
|
62
|
+
}
|
63
|
+
|
64
|
+
function BelongsTo(related, foreignKey, ownerKey) {
|
65
|
+
return (target, propertyKey) => {
|
66
|
+
const self = target.$self();
|
67
|
+
self.setRegistry(
|
68
|
+
propertyKey,
|
69
|
+
() => self.belongsTo(related(), foreignKey, ownerKey)
|
70
|
+
);
|
71
|
+
};
|
72
|
+
}
|
73
|
+
|
74
|
+
function BelongsToMany(related, pivot, foreignPivotKey, relatedPivotKey, parentKey, relatedKey) {
|
75
|
+
return (target, propertyKey) => {
|
76
|
+
const self = target.$self();
|
77
|
+
self.setRegistry(
|
78
|
+
propertyKey,
|
79
|
+
() => self.belongsToMany(related(), pivot(), foreignPivotKey, relatedPivotKey, parentKey, relatedKey)
|
80
|
+
);
|
81
|
+
};
|
82
|
+
}
|
83
|
+
|
84
|
+
function HasMany(related, foreignKey, localKey) {
|
85
|
+
return (target, propertyKey) => {
|
86
|
+
const self = target.$self();
|
87
|
+
self.setRegistry(
|
88
|
+
propertyKey,
|
89
|
+
() => self.hasMany(related(), foreignKey, localKey)
|
90
|
+
);
|
91
|
+
};
|
92
|
+
}
|
93
|
+
|
94
|
+
function HasManyBy(related, foreignKey, ownerKey) {
|
95
|
+
return (target, propertyKey) => {
|
96
|
+
const self = target.$self();
|
97
|
+
self.setRegistry(
|
98
|
+
propertyKey,
|
99
|
+
() => self.hasManyBy(related(), foreignKey, ownerKey)
|
100
|
+
);
|
101
|
+
};
|
102
|
+
}
|
103
|
+
|
104
|
+
function HasManyThrough(related, through, firstKey, secondKey, localKey, secondLocalKey) {
|
105
|
+
return (target, propertyKey) => {
|
106
|
+
const self = target.$self();
|
107
|
+
self.setRegistry(
|
108
|
+
propertyKey,
|
109
|
+
() => self.hasManyThrough(related(), through(), firstKey, secondKey, localKey, secondLocalKey)
|
110
|
+
);
|
111
|
+
};
|
112
|
+
}
|
113
|
+
|
114
|
+
function MorphOne(related, id, type, localKey) {
|
115
|
+
return (target, propertyKey) => {
|
116
|
+
const self = target.$self();
|
117
|
+
self.setRegistry(
|
118
|
+
propertyKey,
|
119
|
+
() => self.morphOne(related(), id, type, localKey)
|
120
|
+
);
|
121
|
+
};
|
122
|
+
}
|
123
|
+
|
124
|
+
function MorphTo(related, id, type, ownerKey) {
|
125
|
+
return (target, propertyKey) => {
|
126
|
+
const self = target.$self();
|
127
|
+
self.setRegistry(
|
128
|
+
propertyKey,
|
129
|
+
() => self.morphTo(related(), id, type, ownerKey)
|
130
|
+
);
|
131
|
+
};
|
132
|
+
}
|
133
|
+
|
134
|
+
function MorphToMany(related, pivot, relatedId, id, type, parentKey, relatedKey) {
|
135
|
+
return (target, propertyKey) => {
|
136
|
+
const self = target.$self();
|
137
|
+
self.setRegistry(
|
138
|
+
propertyKey,
|
139
|
+
() => self.morphToMany(related(), pivot(), relatedId, id, type, parentKey, relatedKey)
|
140
|
+
);
|
141
|
+
};
|
142
|
+
}
|
143
|
+
|
144
|
+
function MorphMany(related, id, type, localKey) {
|
145
|
+
return (target, propertyKey) => {
|
146
|
+
const self = target.$self();
|
147
|
+
self.setRegistry(
|
148
|
+
propertyKey,
|
149
|
+
() => self.morphMany(related(), id, type, localKey)
|
150
|
+
);
|
151
|
+
};
|
152
|
+
}
|
153
|
+
|
154
|
+
function OnDelete(mode) {
|
155
|
+
return (target, propertyKey) => {
|
156
|
+
const self = target.$self();
|
157
|
+
self.setFieldDeleteMode(propertyKey, mode);
|
158
|
+
};
|
159
|
+
}
|
160
|
+
|
161
|
+
function Cast(to) {
|
162
|
+
return (target, propertyKey) => {
|
163
|
+
const self = target.$self();
|
164
|
+
self.setCast(propertyKey, to());
|
165
|
+
};
|
166
|
+
}
|
167
|
+
|
168
|
+
function Mutate(get, set) {
|
169
|
+
return (target, propertyKey) => {
|
170
|
+
const self = target.$self();
|
171
|
+
self.setMutator(propertyKey, { get, set });
|
172
|
+
};
|
173
|
+
}
|
174
|
+
|
175
|
+
function Hidden() {
|
176
|
+
return (target, propertyKey) => {
|
177
|
+
const self = target.$self();
|
178
|
+
self.setHidden(propertyKey);
|
179
|
+
};
|
180
|
+
}
|
181
|
+
|
182
|
+
function NonEnumerable(target, propertyKey) {
|
183
|
+
const descriptor = Object.getOwnPropertyDescriptor(target, propertyKey) || /* @__PURE__ */ Object.create(null);
|
184
|
+
if (descriptor.enumerable !== false) {
|
185
|
+
Object.defineProperty(target, propertyKey, {
|
186
|
+
enumerable: false,
|
187
|
+
set(value) {
|
188
|
+
Object.defineProperty(this, propertyKey, {
|
189
|
+
enumerable: false,
|
190
|
+
writable: true,
|
191
|
+
value
|
192
|
+
});
|
193
|
+
}
|
194
|
+
});
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
export { Attr, BelongsTo, BelongsToMany, Bool, Cast, HasMany, HasManyBy, HasManyThrough, HasOne, Hidden, MorphMany, MorphOne, MorphTo, MorphToMany, Mutate, NonEnumerable, Num, OnDelete, Str, Uid };
|
package/dist/helpers.cjs
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
const Utils = require('./shared/pinia-orm.a62c4fc4.cjs');
|
4
|
+
|
5
|
+
function useSum(models, field) {
|
6
|
+
return models.reduce((sum, item) => {
|
7
|
+
if (typeof item[field] === "number") {
|
8
|
+
sum += item[field];
|
9
|
+
}
|
10
|
+
return sum;
|
11
|
+
}, 0);
|
12
|
+
}
|
13
|
+
|
14
|
+
function usePluck(models, field) {
|
15
|
+
return models.map((model) => Utils.getValue(model, field)).filter((item) => item);
|
16
|
+
}
|
17
|
+
|
18
|
+
function useMax(models, field) {
|
19
|
+
const numbers = usePluck(models, field).filter((item) => typeof item === "number");
|
20
|
+
return numbers.length === 0 ? 0 : Math.max(...numbers);
|
21
|
+
}
|
22
|
+
|
23
|
+
function useMin(models, field) {
|
24
|
+
const numbers = usePluck(models, field).filter((item) => typeof item === "number");
|
25
|
+
return numbers.length === 0 ? 0 : Math.min(...numbers);
|
26
|
+
}
|
27
|
+
|
28
|
+
function useKeys(models) {
|
29
|
+
return models.map((model) => model[model.$getLocalKey()]);
|
30
|
+
}
|
31
|
+
|
32
|
+
function useGroupBy(models, fields) {
|
33
|
+
const grouped = {};
|
34
|
+
const props = Array.isArray(fields) ? fields : [fields];
|
35
|
+
models.forEach((model) => {
|
36
|
+
const key = props.length === 1 ? model[props[0]] : `[${props.map((field) => model[field]).toString()}]`;
|
37
|
+
grouped[key] = (grouped[key] || []).concat(model);
|
38
|
+
});
|
39
|
+
return grouped;
|
40
|
+
}
|
41
|
+
|
42
|
+
function useSortBy(collection, sort, flags) {
|
43
|
+
const directions = [];
|
44
|
+
const iteratees = [];
|
45
|
+
typeof sort === "function" && iteratees.push(sort) && directions.push("asc");
|
46
|
+
Array.isArray(sort) && sort.forEach((item) => iteratees.push(item[0]) && directions.push(item[1]));
|
47
|
+
iteratees.length === 0 && iteratees.push(sort);
|
48
|
+
return Utils.orderBy(collection, iteratees, directions, flags);
|
49
|
+
}
|
50
|
+
|
51
|
+
function useCollect(models) {
|
52
|
+
return {
|
53
|
+
sum: (field) => useSum(models, field),
|
54
|
+
min: (field) => useMin(models, field),
|
55
|
+
max: (field) => useMax(models, field),
|
56
|
+
pluck: (field) => usePluck(models, field),
|
57
|
+
groupBy: (fields) => useGroupBy(models, fields),
|
58
|
+
sortBy: (sort, flags = "SORT_REGULAR") => useSortBy(models, sort, flags),
|
59
|
+
keys: () => useKeys(models)
|
60
|
+
};
|
61
|
+
}
|
62
|
+
|
63
|
+
exports.useCollect = useCollect;
|
64
|
+
exports.useGroupBy = useGroupBy;
|
65
|
+
exports.useKeys = useKeys;
|
66
|
+
exports.useMax = useMax;
|
67
|
+
exports.useMin = useMin;
|
68
|
+
exports.usePluck = usePluck;
|
69
|
+
exports.useSum = useSum;
|
@@ -0,0 +1,54 @@
|
|
1
|
+
import { M as Model, b as Collection } from './shared/pinia-orm.876a7cdc.cjs';
|
2
|
+
import 'pinia';
|
3
|
+
import '@pinia-orm/normalizr';
|
4
|
+
import '@/composables';
|
5
|
+
|
6
|
+
type SortFlags = 'SORT_REGULAR' | 'SORT_FLAG_CASE';
|
7
|
+
|
8
|
+
type sorting<T> = ((record: T) => any) | string | [string, 'asc' | 'desc'][];
|
9
|
+
|
10
|
+
interface UseCollect<M extends Model = Model> {
|
11
|
+
sum: (field: string) => number;
|
12
|
+
min: (field: string) => number;
|
13
|
+
max: (field: string) => number;
|
14
|
+
pluck: (field: string) => any[];
|
15
|
+
groupBy: (fields: string[] | string) => Record<string, Collection<M>>;
|
16
|
+
sortBy: (sort: sorting<M>, flags?: SortFlags) => M[];
|
17
|
+
keys: () => string[];
|
18
|
+
}
|
19
|
+
/**
|
20
|
+
* Return all possible helper functions for the collection
|
21
|
+
*/
|
22
|
+
declare function useCollect<M extends Model = Model>(models: Collection<M>): UseCollect<M>;
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Get the sum value of the specified filed.
|
26
|
+
*/
|
27
|
+
declare function useSum(models: Collection, field: string): number;
|
28
|
+
|
29
|
+
/**
|
30
|
+
* The useGroupBy method groups the collection's items by a given key.
|
31
|
+
*/
|
32
|
+
declare function useGroupBy<T extends Record<string, any>>(models: T[], fields: string[] | string): Record<string, T[]>;
|
33
|
+
|
34
|
+
/**
|
35
|
+
* The keys method returns all of the collection's primary keys
|
36
|
+
*/
|
37
|
+
declare function useKeys(models: Collection): string[];
|
38
|
+
|
39
|
+
/**
|
40
|
+
* Get the min value of the specified filed.
|
41
|
+
*/
|
42
|
+
declare function useMin(models: Collection, field: string): number;
|
43
|
+
|
44
|
+
/**
|
45
|
+
* Get the max value of the specified filed.
|
46
|
+
*/
|
47
|
+
declare function useMax(models: Collection, field: string): number;
|
48
|
+
|
49
|
+
/**
|
50
|
+
* The pluck method retrieves all of the values for a given key.
|
51
|
+
*/
|
52
|
+
declare function usePluck(models: Collection, field: string): any[];
|
53
|
+
|
54
|
+
export { type UseCollect, useCollect, useGroupBy, useKeys, useMax, useMin, usePluck, useSum };
|
@@ -0,0 +1,54 @@
|
|
1
|
+
import { M as Model, b as Collection } from './shared/pinia-orm.876a7cdc.mjs';
|
2
|
+
import 'pinia';
|
3
|
+
import '@pinia-orm/normalizr';
|
4
|
+
import '@/composables';
|
5
|
+
|
6
|
+
type SortFlags = 'SORT_REGULAR' | 'SORT_FLAG_CASE';
|
7
|
+
|
8
|
+
type sorting<T> = ((record: T) => any) | string | [string, 'asc' | 'desc'][];
|
9
|
+
|
10
|
+
interface UseCollect<M extends Model = Model> {
|
11
|
+
sum: (field: string) => number;
|
12
|
+
min: (field: string) => number;
|
13
|
+
max: (field: string) => number;
|
14
|
+
pluck: (field: string) => any[];
|
15
|
+
groupBy: (fields: string[] | string) => Record<string, Collection<M>>;
|
16
|
+
sortBy: (sort: sorting<M>, flags?: SortFlags) => M[];
|
17
|
+
keys: () => string[];
|
18
|
+
}
|
19
|
+
/**
|
20
|
+
* Return all possible helper functions for the collection
|
21
|
+
*/
|
22
|
+
declare function useCollect<M extends Model = Model>(models: Collection<M>): UseCollect<M>;
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Get the sum value of the specified filed.
|
26
|
+
*/
|
27
|
+
declare function useSum(models: Collection, field: string): number;
|
28
|
+
|
29
|
+
/**
|
30
|
+
* The useGroupBy method groups the collection's items by a given key.
|
31
|
+
*/
|
32
|
+
declare function useGroupBy<T extends Record<string, any>>(models: T[], fields: string[] | string): Record<string, T[]>;
|
33
|
+
|
34
|
+
/**
|
35
|
+
* The keys method returns all of the collection's primary keys
|
36
|
+
*/
|
37
|
+
declare function useKeys(models: Collection): string[];
|
38
|
+
|
39
|
+
/**
|
40
|
+
* Get the min value of the specified filed.
|
41
|
+
*/
|
42
|
+
declare function useMin(models: Collection, field: string): number;
|
43
|
+
|
44
|
+
/**
|
45
|
+
* Get the max value of the specified filed.
|
46
|
+
*/
|
47
|
+
declare function useMax(models: Collection, field: string): number;
|
48
|
+
|
49
|
+
/**
|
50
|
+
* The pluck method retrieves all of the values for a given key.
|
51
|
+
*/
|
52
|
+
declare function usePluck(models: Collection, field: string): any[];
|
53
|
+
|
54
|
+
export { type UseCollect, useCollect, useGroupBy, useKeys, useMax, useMin, usePluck, useSum };
|
@@ -0,0 +1,54 @@
|
|
1
|
+
import { M as Model, b as Collection } from './shared/pinia-orm.876a7cdc.js';
|
2
|
+
import 'pinia';
|
3
|
+
import '@pinia-orm/normalizr';
|
4
|
+
import '@/composables';
|
5
|
+
|
6
|
+
type SortFlags = 'SORT_REGULAR' | 'SORT_FLAG_CASE';
|
7
|
+
|
8
|
+
type sorting<T> = ((record: T) => any) | string | [string, 'asc' | 'desc'][];
|
9
|
+
|
10
|
+
interface UseCollect<M extends Model = Model> {
|
11
|
+
sum: (field: string) => number;
|
12
|
+
min: (field: string) => number;
|
13
|
+
max: (field: string) => number;
|
14
|
+
pluck: (field: string) => any[];
|
15
|
+
groupBy: (fields: string[] | string) => Record<string, Collection<M>>;
|
16
|
+
sortBy: (sort: sorting<M>, flags?: SortFlags) => M[];
|
17
|
+
keys: () => string[];
|
18
|
+
}
|
19
|
+
/**
|
20
|
+
* Return all possible helper functions for the collection
|
21
|
+
*/
|
22
|
+
declare function useCollect<M extends Model = Model>(models: Collection<M>): UseCollect<M>;
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Get the sum value of the specified filed.
|
26
|
+
*/
|
27
|
+
declare function useSum(models: Collection, field: string): number;
|
28
|
+
|
29
|
+
/**
|
30
|
+
* The useGroupBy method groups the collection's items by a given key.
|
31
|
+
*/
|
32
|
+
declare function useGroupBy<T extends Record<string, any>>(models: T[], fields: string[] | string): Record<string, T[]>;
|
33
|
+
|
34
|
+
/**
|
35
|
+
* The keys method returns all of the collection's primary keys
|
36
|
+
*/
|
37
|
+
declare function useKeys(models: Collection): string[];
|
38
|
+
|
39
|
+
/**
|
40
|
+
* Get the min value of the specified filed.
|
41
|
+
*/
|
42
|
+
declare function useMin(models: Collection, field: string): number;
|
43
|
+
|
44
|
+
/**
|
45
|
+
* Get the max value of the specified filed.
|
46
|
+
*/
|
47
|
+
declare function useMax(models: Collection, field: string): number;
|
48
|
+
|
49
|
+
/**
|
50
|
+
* The pluck method retrieves all of the values for a given key.
|
51
|
+
*/
|
52
|
+
declare function usePluck(models: Collection, field: string): any[];
|
53
|
+
|
54
|
+
export { type UseCollect, useCollect, useGroupBy, useKeys, useMax, useMin, usePluck, useSum };
|
package/dist/helpers.mjs
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
import { k as getValue, o as orderBy } from './shared/pinia-orm.1bd7d299.mjs';
|
2
|
+
|
3
|
+
function useSum(models, field) {
|
4
|
+
return models.reduce((sum, item) => {
|
5
|
+
if (typeof item[field] === "number") {
|
6
|
+
sum += item[field];
|
7
|
+
}
|
8
|
+
return sum;
|
9
|
+
}, 0);
|
10
|
+
}
|
11
|
+
|
12
|
+
function usePluck(models, field) {
|
13
|
+
return models.map((model) => getValue(model, field)).filter((item) => item);
|
14
|
+
}
|
15
|
+
|
16
|
+
function useMax(models, field) {
|
17
|
+
const numbers = usePluck(models, field).filter((item) => typeof item === "number");
|
18
|
+
return numbers.length === 0 ? 0 : Math.max(...numbers);
|
19
|
+
}
|
20
|
+
|
21
|
+
function useMin(models, field) {
|
22
|
+
const numbers = usePluck(models, field).filter((item) => typeof item === "number");
|
23
|
+
return numbers.length === 0 ? 0 : Math.min(...numbers);
|
24
|
+
}
|
25
|
+
|
26
|
+
function useKeys(models) {
|
27
|
+
return models.map((model) => model[model.$getLocalKey()]);
|
28
|
+
}
|
29
|
+
|
30
|
+
function useGroupBy(models, fields) {
|
31
|
+
const grouped = {};
|
32
|
+
const props = Array.isArray(fields) ? fields : [fields];
|
33
|
+
models.forEach((model) => {
|
34
|
+
const key = props.length === 1 ? model[props[0]] : `[${props.map((field) => model[field]).toString()}]`;
|
35
|
+
grouped[key] = (grouped[key] || []).concat(model);
|
36
|
+
});
|
37
|
+
return grouped;
|
38
|
+
}
|
39
|
+
|
40
|
+
function useSortBy(collection, sort, flags) {
|
41
|
+
const directions = [];
|
42
|
+
const iteratees = [];
|
43
|
+
typeof sort === "function" && iteratees.push(sort) && directions.push("asc");
|
44
|
+
Array.isArray(sort) && sort.forEach((item) => iteratees.push(item[0]) && directions.push(item[1]));
|
45
|
+
iteratees.length === 0 && iteratees.push(sort);
|
46
|
+
return orderBy(collection, iteratees, directions, flags);
|
47
|
+
}
|
48
|
+
|
49
|
+
function useCollect(models) {
|
50
|
+
return {
|
51
|
+
sum: (field) => useSum(models, field),
|
52
|
+
min: (field) => useMin(models, field),
|
53
|
+
max: (field) => useMax(models, field),
|
54
|
+
pluck: (field) => usePluck(models, field),
|
55
|
+
groupBy: (fields) => useGroupBy(models, fields),
|
56
|
+
sortBy: (sort, flags = "SORT_REGULAR") => useSortBy(models, sort, flags),
|
57
|
+
keys: () => useKeys(models)
|
58
|
+
};
|
59
|
+
}
|
60
|
+
|
61
|
+
export { useCollect, useGroupBy, useKeys, useMax, useMin, usePluck, useSum };
|