@prismicio/types-internal 0.2.6 → 0.2.7

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.
@@ -4,5 +4,14 @@ declare type DocumentData = Map<WidgetKey, O.Option<WidgetContent>>;
4
4
  declare const DocumentData: {
5
5
  getWidget(key: WidgetKey, widgets: Map<WidgetKey, unknown>, widgetTypes: Map<WidgetKey, string>, widgetPositions: Map<WidgetKey, number>, parsed: Map<string, O.Option<WidgetContent>>): O.Option<WidgetContent>;
6
6
  fromJson(widgets: Map<WidgetKey, unknown>, widgetTypes: Map<WidgetKey, string>, widgetPositions: Map<WidgetKey, number>): DocumentData;
7
+ partitionData(data: {
8
+ [p: string]: unknown;
9
+ }): {
10
+ types: Map<string, string>;
11
+ positions: Map<string, number>;
12
+ widgets: Map<WidgetKey, unknown>;
13
+ slugs: ReadonlyArray<string>;
14
+ uid: string | undefined;
15
+ };
7
16
  };
8
17
  export default DocumentData;
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
+ const Either_1 = require("fp-ts/lib/Either");
4
5
  const O = (0, tslib_1.__importStar)(require("fp-ts/lib/Option"));
6
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
5
7
  const widgets_1 = require("./widgets");
6
8
  function computeWidget(key, widgets, widgetTypes, widgetPositions) {
7
9
  const widget = widgets.get(key);
@@ -24,5 +26,45 @@ const DocumentData = {
24
26
  return new Map([...Array.from(acc.entries()), [widgetKey, parsedWidget]]);
25
27
  }, new Map());
26
28
  },
29
+ partitionData(data) {
30
+ const fields = Object.entries(data);
31
+ const { types, positions, widgets } = fields.reduce((acc, [k, v]) => {
32
+ if (k.endsWith("_TYPE")) {
33
+ const decodedValue = t.string.decode(v);
34
+ if ((0, Either_1.isRight)(decodedValue)) {
35
+ return {
36
+ ...acc,
37
+ types: acc.types.set(k.substring(0, k.length - 5), decodedValue.right),
38
+ };
39
+ }
40
+ }
41
+ if (k.endsWith("_POSITION") && typeof v === "number") {
42
+ return {
43
+ ...acc,
44
+ positions: acc.positions.set(k.substring(0, k.length - 9), v),
45
+ };
46
+ }
47
+ if (!k.endsWith("_POSITION") && !k.endsWith("_TYPE")) {
48
+ return {
49
+ ...acc,
50
+ widgets: acc.widgets.set(k, v),
51
+ };
52
+ }
53
+ return acc;
54
+ }, {
55
+ types: new Map(),
56
+ positions: new Map(),
57
+ widgets: new Map(),
58
+ });
59
+ const slugs = data["slugs_INTERNAL"] || [];
60
+ const uid = data["uid"];
61
+ return {
62
+ widgets,
63
+ types,
64
+ positions,
65
+ uid,
66
+ slugs,
67
+ };
68
+ },
27
69
  };
28
70
  exports.default = DocumentData;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -1,4 +1,6 @@
1
+ import { isRight } from "fp-ts/lib/Either"
1
2
  import * as O from "fp-ts/lib/Option"
3
+ import * as t from "io-ts"
2
4
 
3
5
  import { type WidgetContent, type WidgetKey, Widget } from "./widgets"
4
6
 
@@ -49,6 +51,62 @@ const DocumentData = {
49
51
  return new Map([...Array.from(acc.entries()), [widgetKey, parsedWidget]])
50
52
  }, new Map<WidgetKey, O.Option<WidgetContent>>())
51
53
  },
54
+
55
+ partitionData(data: { [p: string]: unknown }): {
56
+ types: Map<string, string>
57
+ positions: Map<string, number>
58
+ widgets: Map<WidgetKey, unknown>
59
+ slugs: ReadonlyArray<string>
60
+ uid: string | undefined
61
+ } {
62
+ const fields: [string, unknown][] = Object.entries(data)
63
+
64
+ const { types, positions, widgets } = fields.reduce(
65
+ (acc, [k, v]) => {
66
+ if (k.endsWith("_TYPE")) {
67
+ const decodedValue = t.string.decode(v)
68
+ if (isRight(decodedValue)) {
69
+ return {
70
+ ...acc,
71
+ types: acc.types.set(
72
+ k.substring(0, k.length - 5),
73
+ decodedValue.right,
74
+ ),
75
+ }
76
+ }
77
+ }
78
+ if (k.endsWith("_POSITION") && typeof v === "number") {
79
+ return {
80
+ ...acc,
81
+ positions: acc.positions.set(k.substring(0, k.length - 9), v),
82
+ }
83
+ }
84
+ if (!k.endsWith("_POSITION") && !k.endsWith("_TYPE")) {
85
+ return {
86
+ ...acc,
87
+ widgets: acc.widgets.set(k, v),
88
+ }
89
+ }
90
+ return acc
91
+ },
92
+ {
93
+ types: new Map<string, string>(),
94
+ positions: new Map<string, number>(),
95
+ widgets: new Map<string, unknown>(),
96
+ },
97
+ )
98
+
99
+ const slugs = (data["slugs_INTERNAL"] as string[]) || []
100
+ const uid = data["uid"] as string | undefined
101
+
102
+ return {
103
+ widgets,
104
+ types,
105
+ positions,
106
+ uid,
107
+ slugs,
108
+ }
109
+ },
52
110
  }
53
111
 
54
112
  export default DocumentData