@finema/core 1.4.25 → 1.4.26
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/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type IOption } from '../types/common';
|
|
2
|
+
export declare class ArrayHelper {
|
|
3
|
+
static toOptions(data: any[], valueAttr?: string, labelAttr?: string): IOption[];
|
|
4
|
+
static toArray<T>(value: any): Array<T | any>;
|
|
5
|
+
static isEmpty(value: any): boolean;
|
|
6
|
+
static create: (length: number) => any[];
|
|
7
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { _get } from "../utils/lodash.mjs";
|
|
2
|
+
export class ArrayHelper {
|
|
3
|
+
static toOptions(data, valueAttr = "id", labelAttr = "name") {
|
|
4
|
+
return ArrayHelper.toArray(data).map((item) => {
|
|
5
|
+
const value = _get(item, valueAttr, null);
|
|
6
|
+
return {
|
|
7
|
+
value: value || null,
|
|
8
|
+
label: _get(item, labelAttr, value)
|
|
9
|
+
};
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
static toArray(value) {
|
|
13
|
+
return Array.from(value || []);
|
|
14
|
+
}
|
|
15
|
+
static isEmpty(value) {
|
|
16
|
+
return ArrayHelper.toArray(value).length === 0;
|
|
17
|
+
}
|
|
18
|
+
static create = (length) => {
|
|
19
|
+
return Array(...Array(length));
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { describe, test, expect } from "vitest";
|
|
2
|
+
import { ArrayHelper } from "./ArrayHelper.mjs";
|
|
3
|
+
describe("ArrayHelper", () => {
|
|
4
|
+
test("toOptions default attr", () => {
|
|
5
|
+
const data = [
|
|
6
|
+
{
|
|
7
|
+
id: "1",
|
|
8
|
+
name: "long"
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
id: "2",
|
|
12
|
+
name: "long2"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
id: "3",
|
|
16
|
+
name: "long3"
|
|
17
|
+
}
|
|
18
|
+
];
|
|
19
|
+
expect(ArrayHelper.toOptions(data)).toEqual([
|
|
20
|
+
{
|
|
21
|
+
value: "1",
|
|
22
|
+
label: "long"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
value: "2",
|
|
26
|
+
label: "long2"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
value: "3",
|
|
30
|
+
label: "long3"
|
|
31
|
+
}
|
|
32
|
+
]);
|
|
33
|
+
});
|
|
34
|
+
test("toOptions custom attr", () => {
|
|
35
|
+
const data = [
|
|
36
|
+
{
|
|
37
|
+
ide: "1",
|
|
38
|
+
wtf: "long"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
ide: "2",
|
|
42
|
+
wtf: "long2"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
ide: "3",
|
|
46
|
+
wtf: "long3"
|
|
47
|
+
}
|
|
48
|
+
];
|
|
49
|
+
expect(ArrayHelper.toOptions(data, "ide", "wtf")).toEqual([
|
|
50
|
+
{
|
|
51
|
+
value: "1",
|
|
52
|
+
label: "long"
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
value: "2",
|
|
56
|
+
label: "long2"
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
value: "3",
|
|
60
|
+
label: "long3"
|
|
61
|
+
}
|
|
62
|
+
]);
|
|
63
|
+
});
|
|
64
|
+
test("toOptions custom attr nested", () => {
|
|
65
|
+
const data = [
|
|
66
|
+
{
|
|
67
|
+
ide: "1",
|
|
68
|
+
wtf: {
|
|
69
|
+
ed: "long"
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
ide: "2",
|
|
74
|
+
wtf: {
|
|
75
|
+
ed: "long2"
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
ide: "3",
|
|
80
|
+
wtf: "eiei"
|
|
81
|
+
},
|
|
82
|
+
{}
|
|
83
|
+
];
|
|
84
|
+
expect(ArrayHelper.toOptions(data, "ide", "wtf.ed")).toEqual([
|
|
85
|
+
{
|
|
86
|
+
value: "1",
|
|
87
|
+
label: "long"
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
value: "2",
|
|
91
|
+
label: "long2"
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
value: "3",
|
|
95
|
+
label: "3"
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
value: null,
|
|
99
|
+
label: null
|
|
100
|
+
}
|
|
101
|
+
]);
|
|
102
|
+
});
|
|
103
|
+
test("toArray", () => {
|
|
104
|
+
expect(ArrayHelper.toArray(null)).toEqual([]);
|
|
105
|
+
expect(ArrayHelper.toArray([])).toEqual([]);
|
|
106
|
+
expect(ArrayHelper.toArray("")).toEqual([]);
|
|
107
|
+
expect(ArrayHelper.toArray(false)).toEqual([]);
|
|
108
|
+
expect(ArrayHelper.toArray(true)).toEqual([]);
|
|
109
|
+
expect(ArrayHelper.toArray({})).toEqual([]);
|
|
110
|
+
expect(ArrayHelper.toArray([1, 2, 3])).toEqual([1, 2, 3]);
|
|
111
|
+
});
|
|
112
|
+
});
|