@platecms/delta-cast-util-to-plaintext 1.4.2 → 1.5.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.
- package/package.json +4 -5
- package/src/lib/index.ts +1 -1
- package/src/lib/utils/index.spec.ts +160 -0
- package/src/lib/utils/index.ts +11 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platecms/delta-cast-util-to-plaintext",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Utility to convert CAST trees to plaintext.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"publishConfig": {
|
|
@@ -17,12 +17,11 @@
|
|
|
17
17
|
"src/**/*"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@platecms/delta-cast": "1.
|
|
21
|
-
"@platecms/delta-plate-resource-notation": "1.
|
|
20
|
+
"@platecms/delta-cast": "1.5.0",
|
|
21
|
+
"@platecms/delta-plate-resource-notation": "1.5.0",
|
|
22
22
|
"class-transformer": "0.5.1",
|
|
23
23
|
"reflect-metadata": "0.2.2",
|
|
24
24
|
"tslib": "2.8.1",
|
|
25
|
-
"zwitch": "2.0.4"
|
|
26
|
-
"lodash": "4.17.21"
|
|
25
|
+
"zwitch": "2.0.4"
|
|
27
26
|
}
|
|
28
27
|
}
|
package/src/lib/index.ts
CHANGED
|
@@ -17,8 +17,8 @@ import {
|
|
|
17
17
|
Text,
|
|
18
18
|
Underline,
|
|
19
19
|
} from "@platecms/delta-cast";
|
|
20
|
-
import { isArray } from "lodash";
|
|
21
20
|
import { zwitch } from "zwitch";
|
|
21
|
+
import { isArray } from "./utils";
|
|
22
22
|
|
|
23
23
|
// default separator for joining nested nodes, avoids the default of joining with a comma
|
|
24
24
|
const separator = " ";
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { compact, first, isArray } from "./index";
|
|
2
|
+
|
|
3
|
+
describe("first", () => {
|
|
4
|
+
it("should return the first element of an array with numbers", () => {
|
|
5
|
+
const array = [1, 2, 3, 4, 5];
|
|
6
|
+
const result = first(array);
|
|
7
|
+
expect(result).toBe(1);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should return the first element of an array with strings", () => {
|
|
11
|
+
const array = ["apple", "banana", "cherry"];
|
|
12
|
+
const result = first(array);
|
|
13
|
+
expect(result).toBe("apple");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("should return the first element of an array with objects", () => {
|
|
17
|
+
const obj1 = { id: 1, name: "first" };
|
|
18
|
+
const obj2 = { id: 2, name: "second" };
|
|
19
|
+
const array = [obj1, obj2];
|
|
20
|
+
const result = first(array);
|
|
21
|
+
expect(result).toBe(obj1);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should return undefined for an empty array", () => {
|
|
25
|
+
const array: number[] = [];
|
|
26
|
+
const result = first(array);
|
|
27
|
+
expect(result).toBeUndefined();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should return the first element even if it's falsy", () => {
|
|
31
|
+
const array = [0, 1, 2];
|
|
32
|
+
const result = first(array);
|
|
33
|
+
expect(result).toBe(0);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should work with arrays containing undefined", () => {
|
|
37
|
+
const array = [undefined, "second", "third"];
|
|
38
|
+
const result = first(array);
|
|
39
|
+
expect(result).toBeUndefined();
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe("compact", () => {
|
|
44
|
+
it("should remove null values from array", () => {
|
|
45
|
+
const array = [1, null, 2, null, 3];
|
|
46
|
+
const result = compact(array);
|
|
47
|
+
expect(result).toEqual([1, 2, 3]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("should remove undefined values from array", () => {
|
|
51
|
+
const array = [1, undefined, 2, undefined, 3];
|
|
52
|
+
const result = compact(array);
|
|
53
|
+
expect(result).toEqual([1, 2, 3]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("should remove false values from array", () => {
|
|
57
|
+
const array = [true, false, true, false];
|
|
58
|
+
const result = compact(array);
|
|
59
|
+
expect(result).toEqual([true, true]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("should remove empty strings from array", () => {
|
|
63
|
+
const array = ["hello", "", "world", ""];
|
|
64
|
+
const result = compact(array);
|
|
65
|
+
expect(result).toEqual(["hello", "world"]);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("should remove zero values from array", () => {
|
|
69
|
+
const array = [1, 0, 2, 0, 3];
|
|
70
|
+
const result = compact(array);
|
|
71
|
+
expect(result).toEqual([1, 2, 3]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should remove all falsy values in mixed array", () => {
|
|
75
|
+
const array = [1, null, "hello", undefined, false, "", 0, 2, "world"];
|
|
76
|
+
const result = compact(array);
|
|
77
|
+
expect(result).toEqual([1, "hello", 2, "world"]);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("should return empty array when all values are falsy", () => {
|
|
81
|
+
const array = [null, undefined, false, "", 0];
|
|
82
|
+
const result = compact(array);
|
|
83
|
+
expect(result).toEqual([]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("should return same array when no falsy values", () => {
|
|
87
|
+
const array = [1, 2, "hello", true, { id: 1 }];
|
|
88
|
+
const result = compact(array);
|
|
89
|
+
expect(result).toEqual([1, 2, "hello", true, { id: 1 }]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("should work with empty array", () => {
|
|
93
|
+
const array: (string | null)[] = [];
|
|
94
|
+
const result = compact(array);
|
|
95
|
+
expect(result).toEqual([]);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe("isArray", () => {
|
|
100
|
+
it("should return true for empty array", () => {
|
|
101
|
+
const value: unknown = [];
|
|
102
|
+
expect(isArray(value)).toBe(true);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("should return true for array of numbers", () => {
|
|
106
|
+
const value: unknown = [1, 2, 3];
|
|
107
|
+
expect(isArray(value)).toBe(true);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("should return true for array of strings", () => {
|
|
111
|
+
const value: unknown = ["a", "b", "c"];
|
|
112
|
+
expect(isArray(value)).toBe(true);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("should return true for array of objects", () => {
|
|
116
|
+
const value: unknown = [{ id: 1 }, { id: 2 }];
|
|
117
|
+
expect(isArray(value)).toBe(true);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("should return false for null", () => {
|
|
121
|
+
const value: unknown = null;
|
|
122
|
+
expect(isArray(value)).toBe(false);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("should return false for undefined", () => {
|
|
126
|
+
const value: unknown = undefined;
|
|
127
|
+
expect(isArray(value)).toBe(false);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("should return false for plain object", () => {
|
|
131
|
+
const value: unknown = { key: "value" };
|
|
132
|
+
expect(isArray(value)).toBe(false);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("should return false for string", () => {
|
|
136
|
+
const value: unknown = "hello";
|
|
137
|
+
expect(isArray(value)).toBe(false);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("should return false for number", () => {
|
|
141
|
+
const value: unknown = 42;
|
|
142
|
+
expect(isArray(value)).toBe(false);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("should return false for boolean", () => {
|
|
146
|
+
const value: unknown = true;
|
|
147
|
+
expect(isArray(value)).toBe(false);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("should return false for function", () => {
|
|
151
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
152
|
+
function value(): void {}
|
|
153
|
+
expect(isArray(value)).toBe(false);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("should return false for Date object", () => {
|
|
157
|
+
const value: unknown = new Date();
|
|
158
|
+
expect(isArray(value)).toBe(false);
|
|
159
|
+
});
|
|
160
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function first<T>(array: T[]): T | undefined {
|
|
2
|
+
return array[0];
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function compact<T>(array: (T | "" | 0 | false | null | undefined)[]): T[] {
|
|
6
|
+
return array.filter((value) => Boolean(value)) as T[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function isArray<T>(value: unknown): value is T[] {
|
|
10
|
+
return Array.isArray(value);
|
|
11
|
+
}
|