@platecms/delta-client 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.
Files changed (176) hide show
  1. package/README.md +8 -2
  2. package/package.json +17 -9
  3. package/src/__generated__/fragment-masking.ts +87 -0
  4. package/src/__generated__/gql.ts +142 -0
  5. package/src/__generated__/graphql.ts +3787 -0
  6. package/src/__generated__/{index.d.ts → index.ts} +1 -1
  7. package/src/api/fetchContentExperiences.spec.ts +82 -0
  8. package/src/api/fetchContentExperiences.ts +42 -0
  9. package/src/api/fetchContentItems.spec.ts +62 -0
  10. package/src/api/fetchContentItems.ts +42 -0
  11. package/src/api/fetchPathParts.spec.ts +62 -0
  12. package/src/api/fetchPathParts.ts +42 -0
  13. package/src/api/fetchTags.spec.ts +61 -0
  14. package/src/api/fetchTags.ts +38 -0
  15. package/src/api/index.ts +4 -0
  16. package/src/api/types.ts +35 -0
  17. package/src/apollo/index.ts +69 -0
  18. package/src/app/utils/paginated-response.type.ts +19 -0
  19. package/src/factories/BaseFactory.ts +19 -0
  20. package/src/factories/ChannelFactory.ts +19 -0
  21. package/src/factories/ContentExperienceFactory.ts +54 -0
  22. package/src/factories/PathPartFactory.ts +26 -0
  23. package/src/factories/TagFactory.ts +30 -0
  24. package/src/graphql/content-experiences/content-experiences.query.gql +82 -0
  25. package/src/graphql/content-items/content-items.query.gql +47 -0
  26. package/src/graphql/fragments/asset.fragment.gql +10 -0
  27. package/src/graphql/fragments/asset.fragments.gql +10 -0
  28. package/src/graphql/fragments/building-block-field-fullfillment.fragments.gql +45 -0
  29. package/src/graphql/fragments/building-block-field.fragments.gql +8 -0
  30. package/src/graphql/fragments/building-block.fragments.gql +11 -0
  31. package/src/graphql/fragments/content-experience.fragments.gql +8 -0
  32. package/src/graphql/fragments/content-field.fragments.gql +7 -0
  33. package/src/graphql/fragments/content-item.fragments.gql +11 -0
  34. package/src/graphql/fragments/content-type.fragments.gql +11 -0
  35. package/src/graphql/fragments/content-values.fragments.gql +33 -0
  36. package/src/graphql/fragments/experience-component.fragments.gql +13 -0
  37. package/src/graphql/fragments/grid-placement.fragments.gql +7 -0
  38. package/src/graphql/fragments/path-part.fragments.gql +8 -0
  39. package/src/graphql/fragments/tag.fragment.gql +17 -0
  40. package/src/graphql/path-parts/path-parts.query.gql +52 -0
  41. package/src/graphql/tags/tag.query.gql +17 -0
  42. package/src/graphql/tags/tags.fragments.gql +17 -0
  43. package/src/index.ts +1 -0
  44. package/src/schema/index.spec.ts +211 -0
  45. package/src/schema/{index.d.ts → index.ts} +4 -0
  46. package/src/schema/lib/schemas/array.spec.ts +126 -0
  47. package/src/schema/lib/schemas/array.ts +46 -0
  48. package/src/schema/lib/schemas/asset.spec.ts +101 -0
  49. package/src/schema/lib/schemas/asset.ts +13 -0
  50. package/src/schema/lib/schemas/baseSchema.ts +42 -0
  51. package/src/schema/lib/schemas/boolean.spec.ts +65 -0
  52. package/src/schema/lib/schemas/boolean.ts +12 -0
  53. package/src/schema/lib/schemas/buildingBlock.spec.ts +56 -0
  54. package/src/schema/lib/schemas/buildingBlock.ts +39 -0
  55. package/src/schema/lib/schemas/contentItem.spec.ts +61 -0
  56. package/src/schema/lib/schemas/contentItem.ts +39 -0
  57. package/src/schema/lib/schemas/contentType.spec.ts +113 -0
  58. package/src/schema/lib/schemas/contentType.ts +13 -0
  59. package/src/schema/lib/schemas/date.spec.ts +82 -0
  60. package/src/schema/lib/schemas/date.ts +17 -0
  61. package/src/schema/lib/schemas/gridPlacement.spec.ts +77 -0
  62. package/src/schema/lib/schemas/gridPlacement.ts +13 -0
  63. package/src/schema/lib/schemas/index.ts +66 -0
  64. package/src/schema/lib/schemas/number.spec.ts +65 -0
  65. package/src/schema/lib/schemas/number.ts +12 -0
  66. package/src/schema/lib/schemas/pathPart.spec.ts +120 -0
  67. package/src/schema/lib/schemas/pathPart.ts +13 -0
  68. package/src/schema/lib/schemas/smartText.spec.ts +105 -0
  69. package/src/schema/lib/schemas/smartText.ts +13 -0
  70. package/src/schema/lib/schemas/string.spec.ts +65 -0
  71. package/src/schema/lib/schemas/string.ts +12 -0
  72. package/src/schema/lib/schemas/tag.spec.ts +89 -0
  73. package/src/schema/lib/schemas/tag.ts +13 -0
  74. package/src/schema/lib/utils/index.spec.ts +160 -0
  75. package/src/schema/lib/utils/index.ts +11 -0
  76. package/src/schema/lib/utils/isContentValue.spec.ts +111 -0
  77. package/src/schema/lib/utils/isContentValue.ts +17 -0
  78. package/src/schema/lib/utils/isPrimitiveValue.spec.ts +38 -0
  79. package/src/schema/lib/utils/isPrimitiveValue.ts +7 -0
  80. package/src/slate/index.ts +95 -0
  81. package/src/utils/index.ts +15 -0
  82. package/src/utils/lib/connectors/BaseConnector.ts +26 -0
  83. package/src/utils/lib/connectors/WindowConnector.ts +70 -0
  84. package/src/utils/lib/events/ConnectorEvents.ts +89 -0
  85. package/src/utils/lib/events/EventEmitter.ts +19 -0
  86. package/src/__generated__/fragment-masking.d.ts +0 -19
  87. package/src/__generated__/fragment-masking.js +0 -22
  88. package/src/__generated__/fragment-masking.js.map +0 -1
  89. package/src/__generated__/gql.d.ts +0 -12
  90. package/src/__generated__/gql.js +0 -13
  91. package/src/__generated__/gql.js.map +0 -1
  92. package/src/__generated__/graphql.d.ts +0 -2157
  93. package/src/__generated__/graphql.js +0 -70
  94. package/src/__generated__/graphql.js.map +0 -1
  95. package/src/__generated__/index.js +0 -6
  96. package/src/__generated__/index.js.map +0 -1
  97. package/src/api/index.d.ts +0 -2
  98. package/src/api/index.js +0 -4
  99. package/src/api/index.js.map +0 -1
  100. package/src/apollo/index.d.ts +0 -7
  101. package/src/apollo/index.js +0 -40
  102. package/src/apollo/index.js.map +0 -1
  103. package/src/index.d.ts +0 -1
  104. package/src/index.js +0 -3
  105. package/src/index.js.map +0 -1
  106. package/src/schema/index.js +0 -18
  107. package/src/schema/index.js.map +0 -1
  108. package/src/schema/lib/schemas/array.d.ts +0 -6
  109. package/src/schema/lib/schemas/array.js +0 -20
  110. package/src/schema/lib/schemas/array.js.map +0 -1
  111. package/src/schema/lib/schemas/asset.d.ts +0 -5
  112. package/src/schema/lib/schemas/asset.js +0 -14
  113. package/src/schema/lib/schemas/asset.js.map +0 -1
  114. package/src/schema/lib/schemas/baseSchema.d.ts +0 -10
  115. package/src/schema/lib/schemas/baseSchema.js +0 -36
  116. package/src/schema/lib/schemas/baseSchema.js.map +0 -1
  117. package/src/schema/lib/schemas/boolean.d.ts +0 -4
  118. package/src/schema/lib/schemas/boolean.js +0 -15
  119. package/src/schema/lib/schemas/boolean.js.map +0 -1
  120. package/src/schema/lib/schemas/buildingBlock.d.ts +0 -7
  121. package/src/schema/lib/schemas/buildingBlock.js +0 -26
  122. package/src/schema/lib/schemas/buildingBlock.js.map +0 -1
  123. package/src/schema/lib/schemas/contentItem.d.ts +0 -6
  124. package/src/schema/lib/schemas/contentItem.js +0 -26
  125. package/src/schema/lib/schemas/contentItem.js.map +0 -1
  126. package/src/schema/lib/schemas/contentType.d.ts +0 -5
  127. package/src/schema/lib/schemas/contentType.js +0 -14
  128. package/src/schema/lib/schemas/contentType.js.map +0 -1
  129. package/src/schema/lib/schemas/date.d.ts +0 -5
  130. package/src/schema/lib/schemas/date.js +0 -19
  131. package/src/schema/lib/schemas/date.js.map +0 -1
  132. package/src/schema/lib/schemas/gridPlacement.d.ts +0 -5
  133. package/src/schema/lib/schemas/gridPlacement.js +0 -14
  134. package/src/schema/lib/schemas/gridPlacement.js.map +0 -1
  135. package/src/schema/lib/schemas/index.d.ts +0 -55
  136. package/src/schema/lib/schemas/index.js +0 -33
  137. package/src/schema/lib/schemas/index.js.map +0 -1
  138. package/src/schema/lib/schemas/number.d.ts +0 -4
  139. package/src/schema/lib/schemas/number.js +0 -15
  140. package/src/schema/lib/schemas/number.js.map +0 -1
  141. package/src/schema/lib/schemas/pathPart.d.ts +0 -5
  142. package/src/schema/lib/schemas/pathPart.js +0 -14
  143. package/src/schema/lib/schemas/pathPart.js.map +0 -1
  144. package/src/schema/lib/schemas/smartText.d.ts +0 -5
  145. package/src/schema/lib/schemas/smartText.js +0 -14
  146. package/src/schema/lib/schemas/smartText.js.map +0 -1
  147. package/src/schema/lib/schemas/string.d.ts +0 -4
  148. package/src/schema/lib/schemas/string.js +0 -15
  149. package/src/schema/lib/schemas/string.js.map +0 -1
  150. package/src/schema/lib/schemas/tag.d.ts +0 -5
  151. package/src/schema/lib/schemas/tag.js +0 -14
  152. package/src/schema/lib/schemas/tag.js.map +0 -1
  153. package/src/schema/lib/utils/isContentValue.d.ts +0 -2
  154. package/src/schema/lib/utils/isContentValue.js +0 -17
  155. package/src/schema/lib/utils/isContentValue.js.map +0 -1
  156. package/src/schema/lib/utils/isPrimitiveValue.d.ts +0 -5
  157. package/src/schema/lib/utils/isPrimitiveValue.js +0 -8
  158. package/src/schema/lib/utils/isPrimitiveValue.js.map +0 -1
  159. package/src/slate/index.d.ts +0 -65
  160. package/src/slate/index.js +0 -3
  161. package/src/slate/index.js.map +0 -1
  162. package/src/utils/index.d.ts +0 -6
  163. package/src/utils/index.js +0 -6
  164. package/src/utils/index.js.map +0 -1
  165. package/src/utils/lib/connectors/BaseConnector.d.ts +0 -16
  166. package/src/utils/lib/connectors/BaseConnector.js +0 -17
  167. package/src/utils/lib/connectors/BaseConnector.js.map +0 -1
  168. package/src/utils/lib/connectors/WindowConnector.d.ts +0 -10
  169. package/src/utils/lib/connectors/WindowConnector.js +0 -53
  170. package/src/utils/lib/connectors/WindowConnector.js.map +0 -1
  171. package/src/utils/lib/events/ConnectorEvents.d.ts +0 -63
  172. package/src/utils/lib/events/ConnectorEvents.js +0 -24
  173. package/src/utils/lib/events/ConnectorEvents.js.map +0 -1
  174. package/src/utils/lib/events/EventEmitter.d.ts +0 -7
  175. package/src/utils/lib/events/EventEmitter.js +0 -21
  176. package/src/utils/lib/events/EventEmitter.js.map +0 -1
@@ -0,0 +1,120 @@
1
+ import { describe, it } from "vitest";
2
+ import { NumberSchema } from "./number";
3
+ import { PathPartSchema } from "./pathPart";
4
+
5
+ describe("PathPart schema", () => {
6
+ it("should parse to a path part when parsing a path part content value", () => {
7
+ const schema = new PathPartSchema();
8
+
9
+ const result = schema.parse([
10
+ {
11
+ linkedPathPart: {
12
+ prn: "123",
13
+ name: "title",
14
+ path: "title",
15
+ contentFields: [
16
+ {
17
+ name: "title",
18
+ },
19
+ ],
20
+ },
21
+ },
22
+ ]);
23
+
24
+ expect(result).toEqual({
25
+ prn: "123",
26
+ name: "title",
27
+ path: "title",
28
+ contentFields: [
29
+ {
30
+ name: "title",
31
+ },
32
+ ],
33
+ });
34
+ });
35
+
36
+ it("should return null when parsing an a non path part content value", () => {
37
+ const schema = new PathPartSchema();
38
+
39
+ const result = schema.parse([
40
+ {
41
+ primitiveValue: "hello",
42
+ },
43
+ ]);
44
+
45
+ expect(result).toEqual(null);
46
+ });
47
+
48
+ describe("when placeholders are enabled", () => {
49
+ it("should return the placeholder when parsing an empty array", () => {
50
+ const schema = new PathPartSchema().placeholder({
51
+ prn: "123",
52
+ name: "title",
53
+ path: "title",
54
+ contentFields: [
55
+ {
56
+ name: "title",
57
+ },
58
+ ],
59
+ });
60
+
61
+ const result = schema.parse([], {
62
+ placeholders: true,
63
+ });
64
+
65
+ expect(result).toEqual({
66
+ prn: "123",
67
+ name: "title",
68
+ path: "title",
69
+ contentFields: [
70
+ {
71
+ name: "title",
72
+ },
73
+ ],
74
+ });
75
+ });
76
+
77
+ it("should return the placeholder when parsing an non path part content value", () => {
78
+ const schema = new PathPartSchema().placeholder({
79
+ prn: "123",
80
+ name: "title",
81
+ path: "title",
82
+ contentFields: [
83
+ {
84
+ name: "title",
85
+ },
86
+ ],
87
+ });
88
+
89
+ const result = schema.parse(
90
+ [
91
+ {
92
+ primitiveValue: true,
93
+ },
94
+ ],
95
+ {
96
+ placeholders: true,
97
+ },
98
+ );
99
+
100
+ expect(result).toEqual({
101
+ prn: "123",
102
+ name: "title",
103
+ path: "title",
104
+ contentFields: [
105
+ {
106
+ name: "title",
107
+ },
108
+ ],
109
+ });
110
+ });
111
+ });
112
+
113
+ describe("when nullable is disabled", () => {
114
+ it("should throw an error when parsing an non path part content value", () => {
115
+ const schema = new NumberSchema().nullable(false);
116
+
117
+ expect(() => schema.parse([{ primitiveValue: "hello" }])).toThrow("Data could not be found and is not nullable");
118
+ });
119
+ });
120
+ });
@@ -0,0 +1,13 @@
1
+ import { PathPart } from "../../../__generated__/graphql";
2
+ import { isContentValue } from "../utils/isContentValue";
3
+ import { BaseSchema } from "./baseSchema";
4
+
5
+ export class PathPartSchema extends BaseSchema<PathPart | null, unknown> {
6
+ protected override findValue(data: unknown): PathPart | null {
7
+ if (isContentValue(data) && "linkedPathPart" in data) {
8
+ return (data as { linkedPathPart: PathPart }).linkedPathPart;
9
+ }
10
+
11
+ return null;
12
+ }
13
+ }
@@ -0,0 +1,105 @@
1
+ import { describe, it } from "vitest";
2
+ import { SmartTextSchema } from "./smartText";
3
+ import { c } from "@platecms/delta-castscript";
4
+
5
+ describe("SmartText schema", () => {
6
+ it("should parse to a smart text when parsing a smart text content value", () => {
7
+ const schema = new SmartTextSchema();
8
+
9
+ const result = schema.parse([
10
+ {
11
+ interpolatedSmartText: c("root", [c("paragraph", "hello")]),
12
+ },
13
+ ]);
14
+
15
+ expect(result).toEqual({
16
+ type: "root",
17
+ children: [
18
+ {
19
+ type: "paragraph",
20
+ children: [
21
+ {
22
+ type: "text",
23
+ value: "hello",
24
+ },
25
+ ],
26
+ },
27
+ ],
28
+ });
29
+ });
30
+
31
+ it("should return null when parsing an a non smart text content value", () => {
32
+ const schema = new SmartTextSchema();
33
+
34
+ const result = schema.parse([
35
+ {
36
+ primitiveValue: "hello",
37
+ },
38
+ ]);
39
+
40
+ expect(result).toEqual(null);
41
+ });
42
+
43
+ describe("when placeholders are enabled", () => {
44
+ it("should return the placeholder when parsing an empty array", () => {
45
+ const schema = new SmartTextSchema().placeholder(c("root", [c("paragraph", "placeholder")]));
46
+
47
+ const result = schema.parse([], {
48
+ placeholders: true,
49
+ });
50
+
51
+ expect(result).toEqual({
52
+ type: "root",
53
+ children: [
54
+ {
55
+ type: "paragraph",
56
+ children: [
57
+ {
58
+ type: "text",
59
+ value: "placeholder",
60
+ },
61
+ ],
62
+ },
63
+ ],
64
+ });
65
+ });
66
+
67
+ it("should return the placeholder when parsing an non smart text content value", () => {
68
+ const schema = new SmartTextSchema().placeholder(c("root", [c("paragraph", "placeholder")]));
69
+
70
+ const result = schema.parse(
71
+ [
72
+ {
73
+ primitiveValue: true,
74
+ },
75
+ ],
76
+ {
77
+ placeholders: true,
78
+ },
79
+ );
80
+
81
+ expect(result).toEqual({
82
+ type: "root",
83
+ children: [
84
+ {
85
+ type: "paragraph",
86
+ children: [
87
+ {
88
+ type: "text",
89
+ value: "placeholder",
90
+ },
91
+ ],
92
+ },
93
+ ],
94
+ });
95
+ });
96
+ });
97
+
98
+ describe("when nullable is disabled", () => {
99
+ it("should throw an error when parsing an non smart text content value", () => {
100
+ const schema = new SmartTextSchema().nullable(false);
101
+
102
+ expect(() => schema.parse([{ primitiveValue: 42 }])).toThrow("Data could not be found and is not nullable");
103
+ });
104
+ });
105
+ });
@@ -0,0 +1,13 @@
1
+ import { Root as SmartText } from "@platecms/delta-cast";
2
+ import { BaseSchema } from "./baseSchema";
3
+ import { isContentValue } from "../utils/isContentValue";
4
+
5
+ export class SmartTextSchema extends BaseSchema<SmartText | null, unknown> {
6
+ protected override findValue(data: unknown): SmartText | null {
7
+ if (isContentValue(data) && "interpolatedSmartText" in data) {
8
+ return (data as { interpolatedSmartText: SmartText }).interpolatedSmartText;
9
+ }
10
+
11
+ return null;
12
+ }
13
+ }
@@ -0,0 +1,65 @@
1
+ import { describe, it } from "vitest";
2
+ import { StringSchema } from "./string";
3
+
4
+ describe("String schema", () => {
5
+ it("should parse to a string when parsing a string content value", () => {
6
+ const schema = new StringSchema();
7
+
8
+ const result = schema.parse([
9
+ {
10
+ primitiveValue: "hello",
11
+ },
12
+ ]);
13
+
14
+ expect(result).toEqual("hello");
15
+ });
16
+
17
+ it("should return null when parsing an a non string content value", () => {
18
+ const schema = new StringSchema();
19
+
20
+ const result = schema.parse([
21
+ {
22
+ primitiveValue: 42,
23
+ },
24
+ ]);
25
+
26
+ expect(result).toEqual(null);
27
+ });
28
+
29
+ describe("when placeholders are enabled", () => {
30
+ it("should return the placeholder when parsing an empty array", () => {
31
+ const schema = new StringSchema().placeholder("hello");
32
+
33
+ const result = schema.parse([], {
34
+ placeholders: true,
35
+ });
36
+
37
+ expect(result).toEqual("hello");
38
+ });
39
+
40
+ it("should return the placeholder when parsing an non string content value", () => {
41
+ const schema = new StringSchema().placeholder("hello");
42
+
43
+ const result = schema.parse(
44
+ [
45
+ {
46
+ primitiveValue: true,
47
+ },
48
+ ],
49
+ {
50
+ placeholders: true,
51
+ },
52
+ );
53
+
54
+ expect(result).toEqual("hello");
55
+ });
56
+ });
57
+
58
+ describe("when nullable is disabled", () => {
59
+ it("should throw an error when parsing an non string content value", () => {
60
+ const schema = new StringSchema().nullable(false);
61
+
62
+ expect(() => schema.parse([{ primitiveValue: 42 }])).toThrow("Data could not be found and is not nullable");
63
+ });
64
+ });
65
+ });
@@ -0,0 +1,12 @@
1
+ import { BaseSchema } from "./baseSchema";
2
+ import { isPrimitiveValue } from "../utils/isPrimitiveValue";
3
+
4
+ export class StringSchema extends BaseSchema<string | null, unknown> {
5
+ protected override findValue(data: unknown): string | null {
6
+ if (isPrimitiveValue(data) && typeof data.primitiveValue === "string") {
7
+ return data.primitiveValue;
8
+ }
9
+
10
+ return null;
11
+ }
12
+ }
@@ -0,0 +1,89 @@
1
+ import { describe, it } from "vitest";
2
+ import { TagSchema } from "./tag";
3
+
4
+ describe("Tag schema", () => {
5
+ it("should parse to a tag when parsing a tag content value", () => {
6
+ const schema = new TagSchema();
7
+
8
+ const result = schema.parse([
9
+ {
10
+ linkedTag: {
11
+ prn: "123",
12
+ name: "title",
13
+ path: "title",
14
+ },
15
+ },
16
+ ]);
17
+
18
+ expect(result).toEqual({
19
+ prn: "123",
20
+ name: "title",
21
+ path: "title",
22
+ });
23
+ });
24
+
25
+ it("should return null when parsing an a non tag content value", () => {
26
+ const schema = new TagSchema();
27
+
28
+ const result = schema.parse([
29
+ {
30
+ primitiveValue: "hello",
31
+ },
32
+ ]);
33
+
34
+ expect(result).toEqual(null);
35
+ });
36
+
37
+ describe("when placeholders are enabled", () => {
38
+ it("should return the placeholder when parsing an empty array", () => {
39
+ const schema = new TagSchema().placeholder({
40
+ prn: "123",
41
+ name: "title",
42
+ path: "title",
43
+ });
44
+
45
+ const result = schema.parse([], {
46
+ placeholders: true,
47
+ });
48
+
49
+ expect(result).toEqual({
50
+ prn: "123",
51
+ name: "title",
52
+ path: "title",
53
+ });
54
+ });
55
+
56
+ it("should return the placeholder when parsing an non tag content value", () => {
57
+ const schema = new TagSchema().placeholder({
58
+ prn: "123",
59
+ name: "title",
60
+ path: "title",
61
+ });
62
+
63
+ const result = schema.parse(
64
+ [
65
+ {
66
+ primitiveValue: true,
67
+ },
68
+ ],
69
+ {
70
+ placeholders: true,
71
+ },
72
+ );
73
+
74
+ expect(result).toEqual({
75
+ prn: "123",
76
+ name: "title",
77
+ path: "title",
78
+ });
79
+ });
80
+ });
81
+
82
+ describe("when nullable is disabled", () => {
83
+ it("should throw an error when parsing an non tag content value", () => {
84
+ const schema = new TagSchema().nullable(false);
85
+
86
+ expect(() => schema.parse([{ primitiveValue: 42 }])).toThrow("Data could not be found and is not nullable");
87
+ });
88
+ });
89
+ });
@@ -0,0 +1,13 @@
1
+ import { Tag } from "../../../__generated__/graphql";
2
+ import { isContentValue } from "../utils/isContentValue";
3
+ import { BaseSchema } from "./baseSchema";
4
+
5
+ export class TagSchema extends BaseSchema<Tag | null, unknown> {
6
+ protected override findValue(data: unknown): Tag | null {
7
+ if (isContentValue(data) && "linkedTag" in data) {
8
+ return (data as { linkedTag: Tag }).linkedTag;
9
+ }
10
+
11
+ return null;
12
+ }
13
+ }
@@ -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
+ }