@verdnatura/vn-front-lib 0.1.9 → 0.2.0

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.
@@ -0,0 +1,55 @@
1
+ function orderData(data, order) {
2
+ if (typeof order === 'function') return data.sort(data);
3
+ if (typeof order === 'string') order = [order];
4
+ if (!Array.isArray(data)) return [];
5
+ if (Array.isArray(order)) {
6
+ const orderComp = [];
7
+
8
+ for (const field of order) {
9
+ const split = field.split(/\s+/);
10
+ orderComp.push({
11
+ field: split[0],
12
+ way: split[1] === 'DESC' ? -1 : 1,
13
+ });
14
+ }
15
+
16
+ return data.sort((a, b) => sortFunc(a, b, orderComp));
17
+ }
18
+ return data;
19
+ }
20
+
21
+ function sortFunc(a, b, order) {
22
+ for (const { field, way } of order) {
23
+ const compRes = compareFunc(a[field], b[field]) * way;
24
+ if (compRes !== 0) return compRes;
25
+ }
26
+
27
+ return 0;
28
+ }
29
+
30
+ function compareFunc(a, b) {
31
+ if (a === b) return 0;
32
+ const aType = typeof a;
33
+ if (aType === typeof b) {
34
+ switch (aType) {
35
+ case 'string':
36
+ return a.localeCompare(b);
37
+ case 'number':
38
+ return a - b;
39
+ case 'boolean':
40
+ return a ? 1 : -1;
41
+ case 'object':
42
+ if (a instanceof Date && b instanceof Date)
43
+ return a.getTime() - b.getTime();
44
+ }
45
+ }
46
+
47
+ if (a === undefined) return -1;
48
+ if (b === undefined) return 1;
49
+ if (a === null) return -1;
50
+ if (b === null) return 1;
51
+
52
+ return a > b ? 1 : -1;
53
+ }
54
+
55
+ export default orderData;
@@ -1,2 +1,3 @@
1
1
  export * from './advancedFilters';
2
+ export * from './dataByOrder';
2
3
  export * from './trackAnalytics';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verdnatura/vn-front-lib",
3
- "version": "0.1.9",
3
+ "version": "0.2.0",
4
4
  "description": "Vue 3 component and composable library",
5
5
  "license": "GPL-3.0",
6
6
  "author": "Verdnatura Levante SL",