@platecms/delta-castscript 1.0.0 → 1.3.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platecms/delta-castscript",
3
- "version": "1.0.0",
3
+ "version": "1.3.0",
4
4
  "description": "Helper function for quickly creating CAST based trees.",
5
5
  "license": "UNLICENSED",
6
6
  "publishConfig": {
@@ -17,12 +17,10 @@
17
17
  "src/**/*"
18
18
  ],
19
19
  "dependencies": {
20
- "@platecms/delta-cast": "1.0.0",
21
- "@platecms/delta-plate-resource-notation": "1.0.0",
20
+ "@platecms/delta-cast": "1.3.0",
21
+ "@platecms/delta-plate-resource-notation": "1.3.0",
22
22
  "class-transformer": "0.5.1",
23
- "lodash": "4.17.21",
24
23
  "reflect-metadata": "0.2.2",
25
24
  "tslib": "2.8.1"
26
- },
27
- "type": "commonjs"
25
+ }
28
26
  }
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { createC } from "./lib";
2
+
3
+ /*
4
+ eslint-disable-next-line id-length, @typescript-eslint/naming-convention -- we use c as a variable name to create as compact functions as possible
5
+ */
6
+ export const c = createC();
@@ -0,0 +1,52 @@
1
+ import { c } from "../index";
2
+
3
+ describe("create c", () => {
4
+ it("should create a content node", () => {
5
+ const result = c("paragraph", "hello");
6
+
7
+ expect(result).toEqual({
8
+ type: "paragraph",
9
+ children: [
10
+ {
11
+ type: "text",
12
+ value: "hello",
13
+ },
14
+ ],
15
+ });
16
+ });
17
+
18
+ it("should create a content node with children", () => {
19
+ const result = c("heading", { level: 1 }, [c("text", "hello")]);
20
+
21
+ expect(result).toEqual({
22
+ type: "heading",
23
+ level: 1,
24
+ children: [
25
+ {
26
+ type: "text",
27
+ value: "hello",
28
+ },
29
+ ],
30
+ });
31
+ });
32
+
33
+ it("should create a content node with root and children", () => {
34
+ const result = c("root", [c("heading", "welcome", { level: 1 })]);
35
+
36
+ expect(result).toEqual({
37
+ type: "root",
38
+ children: [
39
+ {
40
+ type: "heading",
41
+ level: 1,
42
+ children: [
43
+ {
44
+ type: "text",
45
+ value: "welcome",
46
+ },
47
+ ],
48
+ },
49
+ ],
50
+ });
51
+ });
52
+ });
@@ -0,0 +1,75 @@
1
+ import { Content, Root } from "@platecms/delta-cast";
2
+ import { isObject, isUndefined } from "../schema/lib/utils";
3
+
4
+ type Children = Content[];
5
+ type Properties = Record<string, unknown>;
6
+ type Text = string;
7
+
8
+ type Args = Children | Properties | Text;
9
+
10
+ type NodeType = Content["type"] | "root";
11
+ interface Node {
12
+ [key: string]: unknown;
13
+ type: NodeType;
14
+ value?: string;
15
+ children?: Children;
16
+ }
17
+
18
+ function isTextArgument(arg: Args): arg is string {
19
+ return typeof arg === "string";
20
+ }
21
+
22
+ function isObjectArgument(arg: Args): arg is Record<string, unknown> {
23
+ return isObject(arg);
24
+ }
25
+
26
+ function isChildrenArgument(arg: Args): arg is Content[] {
27
+ return Array.isArray(arg);
28
+ }
29
+
30
+ function addChild(node: Node, child: Content): void {
31
+ node.children ??= [];
32
+
33
+ node.children.push(child);
34
+ }
35
+
36
+ export function createC(): <T extends NodeType>(
37
+ type: T,
38
+ ...args: [Children | Properties | Text, (Children | Properties)?, Children?]
39
+ ) => T extends "root" ? Root : Content {
40
+ function create<T extends NodeType>(
41
+ type: T,
42
+ ...args: [Children | Properties | Text, (Children | Properties)?, Children?]
43
+ ): T extends "root" ? Root : Content {
44
+ const node: Node = {
45
+ type,
46
+ };
47
+
48
+ args.forEach((arg) => {
49
+ if (isUndefined(arg)) {
50
+ return;
51
+ }
52
+
53
+ if (isTextArgument(arg)) {
54
+ if (type === "text") {
55
+ node.value = arg;
56
+ } else {
57
+ addChild(node, {
58
+ type: "text",
59
+ value: arg,
60
+ });
61
+ }
62
+ } else if (isChildrenArgument(arg)) {
63
+ for (const child of arg) {
64
+ addChild(node, child);
65
+ }
66
+ } else if (isObjectArgument(arg)) {
67
+ Object.assign(node, arg);
68
+ }
69
+ });
70
+
71
+ return node as unknown as T extends "root" ? Root : Content;
72
+ }
73
+
74
+ return create;
75
+ }
@@ -0,0 +1,261 @@
1
+ import { compact, first, isArray, isObject, isUndefined } 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: number[] = [1, 2, 3];
107
+ expect(isArray(value)).toBe(true);
108
+ });
109
+
110
+ it("should return true for array of strings", () => {
111
+ const value: string[] = ["a", "b", "c"];
112
+ expect(isArray(value)).toBe(true);
113
+ });
114
+
115
+ it("should return true for array of objects", () => {
116
+ const value: { id: number }[] = [{ 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: { key: string } = { key: "value" };
132
+ expect(isArray(value)).toBe(false);
133
+ });
134
+
135
+ it("should return false for string", () => {
136
+ const value = "hello";
137
+ expect(isArray(value)).toBe(false);
138
+ });
139
+
140
+ it("should return false for number", () => {
141
+ const value = 42;
142
+ expect(isArray(value)).toBe(false);
143
+ });
144
+
145
+ it("should return false for boolean", () => {
146
+ const value = 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 = new Date();
158
+ expect(isArray(value)).toBe(false);
159
+ });
160
+ });
161
+
162
+ describe("isObject", () => {
163
+ it("should return true for empty object", () => {
164
+ const value = {};
165
+ expect(isObject(value)).toBe(true);
166
+ });
167
+
168
+ it("should return true for object with properties", () => {
169
+ const value = { key: "value" };
170
+ expect(isObject(value)).toBe(true);
171
+ });
172
+
173
+ it("should return false for null", () => {
174
+ const value = null;
175
+ expect(isObject(value)).toBe(false);
176
+ });
177
+
178
+ it("should return false for undefined", () => {
179
+ const value = undefined;
180
+ expect(isObject(value)).toBe(false);
181
+ });
182
+
183
+ it("should return true for array", () => {
184
+ const value = [1, 2, 3];
185
+ expect(isObject(value)).toBe(true);
186
+ });
187
+
188
+ it("should return true for function", () => {
189
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
190
+ function value(): void {}
191
+ expect(isObject(value)).toBe(true);
192
+ });
193
+
194
+ it("should return true for Date object", () => {
195
+ const value = new Date();
196
+ expect(isObject(value)).toBe(true);
197
+ });
198
+
199
+ it("should return false for number", () => {
200
+ const value = 42;
201
+ expect(isObject(value)).toBe(false);
202
+ });
203
+
204
+ it("should return false for boolean", () => {
205
+ const value = true;
206
+ expect(isObject(value)).toBe(false);
207
+ });
208
+
209
+ it("should return false for string", () => {
210
+ const value = "hello";
211
+ expect(isObject(value)).toBe(false);
212
+ });
213
+ });
214
+
215
+ describe("isUndefined", () => {
216
+ it("should return true for undefined", () => {
217
+ const value = undefined;
218
+ expect(isUndefined(value)).toBe(true);
219
+ });
220
+
221
+ it("should return false for null", () => {
222
+ const value = null;
223
+ expect(isUndefined(value)).toBe(false);
224
+ });
225
+
226
+ it("should return false for string", () => {
227
+ const value = "hello";
228
+ expect(isUndefined(value)).toBe(false);
229
+ });
230
+
231
+ it("should return false for number", () => {
232
+ const value = 42;
233
+ expect(isUndefined(value)).toBe(false);
234
+ });
235
+
236
+ it("should return false for boolean", () => {
237
+ const value = true;
238
+ expect(isUndefined(value)).toBe(false);
239
+ });
240
+
241
+ it("should return false for function", () => {
242
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
243
+ function value(): void {}
244
+ expect(isUndefined(value)).toBe(false);
245
+ });
246
+
247
+ it("should return false for Date object", () => {
248
+ const value = new Date();
249
+ expect(isUndefined(value)).toBe(false);
250
+ });
251
+
252
+ it("should return false for null", () => {
253
+ const value = null;
254
+ expect(isUndefined(value)).toBe(false);
255
+ });
256
+
257
+ it("should return false for undefined", () => {
258
+ const value = undefined;
259
+ expect(isUndefined(value)).toBe(true);
260
+ });
261
+ });
@@ -0,0 +1,19 @@
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
+ }
12
+
13
+ export function isObject(value: unknown): value is object {
14
+ return value != null && (typeof value === "object" || typeof value === "function");
15
+ }
16
+
17
+ export function isUndefined(value: unknown): value is undefined {
18
+ return value === undefined;
19
+ }
package/src/index.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export declare const c: <T extends "paragraph" | "heading" | "list" | "listItem" | "blockquote" | "code" | "text" | "bold" | "italic" | "underline" | "strikethrough" | "inlineCode" | "highlight" | "contentValue" | "externalLink" | "internalLink" | "root">(type: T, args_0: string | {
2
- [x: string]: unknown;
3
- } | import("generated/dist/packages/cast/src").Content[], args_1?: {
4
- [x: string]: unknown;
5
- } | import("generated/dist/packages/cast/src").Content[] | undefined, args_2?: import("generated/dist/packages/cast/src").Content[] | undefined) => T extends "root" ? import("generated/dist/packages/cast/src").Root : import("generated/dist/packages/cast/src").Content;
package/src/index.js DELETED
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.c = void 0;
4
- const lib_1 = require("./lib");
5
- exports.c = (0, lib_1.createC)();
6
- //# sourceMappingURL=index.js.map
package/src/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/castscript/src/index.ts"],"names":[],"mappings":";;;AAAA,+BAAgC;AAKnB,QAAA,CAAC,GAAG,IAAA,aAAO,GAAE,CAAC"}
@@ -1,7 +0,0 @@
1
- import { Content, Root } from "@platecms/delta-cast";
2
- type Children = Content[];
3
- type Properties = Record<string, unknown>;
4
- type Text = string;
5
- type NodeType = Content["type"] | "root";
6
- export declare function createC(): <T extends NodeType>(type: T, ...args: [Children | Properties | Text, (Children | Properties)?, Children?]) => T extends "root" ? Root : Content;
7
- export {};
package/src/lib/index.js DELETED
@@ -1,51 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createC = createC;
4
- const lodash_1 = require("lodash");
5
- function isTextArgument(arg) {
6
- return typeof arg === "string";
7
- }
8
- function isObjectArgument(arg) {
9
- return (0, lodash_1.isObject)(arg);
10
- }
11
- function isChildrenArgument(arg) {
12
- return Array.isArray(arg);
13
- }
14
- function addChild(node, child) {
15
- node.children ??= [];
16
- node.children.push(child);
17
- }
18
- function createC() {
19
- function create(type, ...args) {
20
- const node = {
21
- type,
22
- };
23
- args.forEach((arg) => {
24
- if ((0, lodash_1.isUndefined)(arg)) {
25
- return;
26
- }
27
- if (isTextArgument(arg)) {
28
- if (type === "text") {
29
- node.value = arg;
30
- }
31
- else {
32
- addChild(node, {
33
- type: "text",
34
- value: arg,
35
- });
36
- }
37
- }
38
- else if (isChildrenArgument(arg)) {
39
- for (const child of arg) {
40
- addChild(node, child);
41
- }
42
- }
43
- else if (isObjectArgument(arg)) {
44
- Object.assign(node, arg);
45
- }
46
- });
47
- return node;
48
- }
49
- return create;
50
- }
51
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/castscript/src/lib/index.ts"],"names":[],"mappings":";;AAmCA,0BAuCC;AAzED,mCAA+C;AAgB/C,SAAS,cAAc,CAAC,GAAS;IAC/B,OAAO,OAAO,GAAG,KAAK,QAAQ,CAAC;AACjC,CAAC;AAED,SAAS,gBAAgB,CAAC,GAAS;IACjC,OAAO,IAAA,iBAAQ,EAAC,GAAG,CAAC,CAAC;AACvB,CAAC;AAED,SAAS,kBAAkB,CAAC,GAAS;IACnC,OAAO,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AAC5B,CAAC;AAED,SAAS,QAAQ,CAAC,IAAU,EAAE,KAAc;IAC1C,IAAI,CAAC,QAAQ,KAAK,EAAE,CAAC;IAErB,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC5B,CAAC;AAED,SAAgB,OAAO;IAIrB,SAAS,MAAM,CACb,IAAO,EACP,GAAG,IAAyE;QAE5E,MAAM,IAAI,GAAS;YACjB,IAAI;SACL,CAAC;QAEF,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YACnB,IAAI,IAAA,oBAAW,EAAC,GAAG,CAAC,EAAE,CAAC;gBACrB,OAAO;YACT,CAAC;YAED,IAAI,cAAc,CAAC,GAAG,CAAC,EAAE,CAAC;gBACxB,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;oBACpB,IAAI,CAAC,KAAK,GAAG,GAAG,CAAC;gBACnB,CAAC;qBAAM,CAAC;oBACN,QAAQ,CAAC,IAAI,EAAE;wBACb,IAAI,EAAE,MAAM;wBACZ,KAAK,EAAE,GAAG;qBACX,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;iBAAM,IAAI,kBAAkB,CAAC,GAAG,CAAC,EAAE,CAAC;gBACnC,KAAK,MAAM,KAAK,IAAI,GAAG,EAAE,CAAC;oBACxB,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;gBACxB,CAAC;YACH,CAAC;iBAAM,IAAI,gBAAgB,CAAC,GAAG,CAAC,EAAE,CAAC;gBACjC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;YAC3B,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,OAAO,IAAoD,CAAC;IAC9D,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}