@platecms/delta-castscript 0.6.0 → 0.8.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": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "description": "Helper function for quickly creating CAST based trees.",
5
5
  "license": "UNLICENSED",
6
6
  "publishConfig": {
@@ -17,12 +17,11 @@
17
17
  "src/**/*"
18
18
  ],
19
19
  "dependencies": {
20
- "@platecms/delta-cast": "0.6.0",
21
- "@platecms/delta-plate-resource-notation": "0.6.0",
20
+ "@platecms/delta-cast": "0.8.0",
21
+ "@platecms/delta-plate-resource-notation": "0.8.0",
22
22
  "class-transformer": "0.5.1",
23
23
  "lodash": "4.17.21",
24
24
  "reflect-metadata": "0.2.2",
25
25
  "tslib": "2.8.1"
26
- },
27
- "type": "commonjs"
26
+ }
28
27
  }
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 "lodash";
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
+ }
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,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