@prismicio/mock 0.0.8 → 0.1.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 (97) hide show
  1. package/dist/api/index.cjs +348 -236
  2. package/dist/api/index.cjs.map +1 -1
  3. package/dist/api/index.d.ts +52 -6
  4. package/dist/api/index.js +348 -236
  5. package/dist/api/index.js.map +1 -1
  6. package/dist/index.cjs +893 -543
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +247 -73
  9. package/dist/index.js +885 -542
  10. package/dist/index.js.map +1 -1
  11. package/dist/model/index.cjs +398 -134
  12. package/dist/model/index.cjs.map +1 -1
  13. package/dist/model/index.d.ts +114 -28
  14. package/dist/model/index.js +398 -134
  15. package/dist/model/index.js.map +1 -1
  16. package/dist/value/index.cjs +598 -468
  17. package/dist/value/index.cjs.map +1 -1
  18. package/dist/value/index.d.ts +96 -40
  19. package/dist/value/index.js +598 -468
  20. package/dist/value/index.js.map +1 -1
  21. package/package.json +22 -20
  22. package/src/api/createAPIMockFactory.ts +56 -0
  23. package/src/api/query.ts +4 -4
  24. package/src/api/ref.ts +6 -6
  25. package/src/api/repository.ts +12 -14
  26. package/src/api/tags.ts +5 -2
  27. package/src/createMockFactory.ts +52 -0
  28. package/src/index.ts +15 -0
  29. package/src/lib/buildEmbedField.ts +28 -30
  30. package/src/lib/buildImageFieldImage.ts +19 -7
  31. package/src/lib/createFaker.ts +99 -22
  32. package/src/lib/generateCustomTypeId.ts +13 -5
  33. package/src/lib/generateFieldId.ts +13 -7
  34. package/src/lib/generateTags.ts +17 -16
  35. package/src/lib/getMockEmbedData.ts +45 -16
  36. package/src/lib/getMockImageData.ts +13 -5
  37. package/src/lib/lorem.ts +112 -0
  38. package/src/lib/valueForModel.ts +47 -30
  39. package/src/lib/valueForModelMap.ts +24 -4
  40. package/src/model/boolean.ts +6 -6
  41. package/src/model/buildMockGroupFieldMap.ts +5 -5
  42. package/src/model/color.ts +4 -4
  43. package/src/model/contentRelationship.ts +4 -4
  44. package/src/model/createModelMockFactory.ts +213 -0
  45. package/src/model/customType.ts +6 -6
  46. package/src/model/date.ts +4 -4
  47. package/src/model/embed.ts +4 -4
  48. package/src/model/geoPoint.ts +3 -3
  49. package/src/model/group.ts +7 -7
  50. package/src/model/image.ts +8 -12
  51. package/src/model/index.ts +2 -0
  52. package/src/model/integrationFields.ts +4 -4
  53. package/src/model/keyText.ts +4 -4
  54. package/src/model/link.ts +6 -6
  55. package/src/model/linkToMedia.ts +5 -5
  56. package/src/model/number.ts +4 -4
  57. package/src/model/richText.ts +8 -8
  58. package/src/model/select.ts +5 -5
  59. package/src/model/sharedSlice.ts +5 -5
  60. package/src/model/sharedSliceVariation.ts +10 -10
  61. package/src/model/slice.ts +7 -7
  62. package/src/model/sliceZone.ts +7 -4
  63. package/src/model/timestamp.ts +4 -4
  64. package/src/model/title.ts +7 -7
  65. package/src/model/uid.ts +4 -4
  66. package/src/types.ts +70 -31
  67. package/src/value/boolean.ts +4 -6
  68. package/src/value/color.ts +3 -3
  69. package/src/value/contentRelationship.ts +21 -18
  70. package/src/value/createValueMockFactory.ts +236 -0
  71. package/src/value/customType.ts +12 -12
  72. package/src/value/date.ts +6 -2
  73. package/src/value/embed.ts +23 -7
  74. package/src/value/geoPoint.ts +4 -6
  75. package/src/value/group.ts +6 -11
  76. package/src/value/image.ts +26 -18
  77. package/src/value/integrationFields.ts +12 -26
  78. package/src/value/keyText.ts +3 -5
  79. package/src/value/link.ts +11 -9
  80. package/src/value/linkToMedia.ts +9 -8
  81. package/src/value/number.ts +3 -3
  82. package/src/value/richText/embed.ts +6 -3
  83. package/src/value/richText/heading.ts +32 -27
  84. package/src/value/richText/image.ts +9 -8
  85. package/src/value/richText/index.ts +71 -48
  86. package/src/value/richText/list.ts +6 -10
  87. package/src/value/richText/oList.ts +6 -10
  88. package/src/value/richText/paragraph.ts +10 -6
  89. package/src/value/richText/preformatted.ts +11 -7
  90. package/src/value/select.ts +11 -7
  91. package/src/value/sharedSlice.ts +5 -5
  92. package/src/value/sharedSliceVariation.ts +11 -16
  93. package/src/value/slice.ts +11 -15
  94. package/src/value/sliceZone.ts +14 -18
  95. package/src/value/timestamp.ts +31 -18
  96. package/src/value/title.ts +7 -3
  97. package/src/value/uid.ts +6 -4
@@ -1,27 +1,28 @@
1
1
  import * as changeCase from "change-case";
2
2
 
3
- import { createFaker } from "../lib/createFaker";
3
+ import { createFaker, Faker } from "../lib/createFaker";
4
4
 
5
- import { MockValueConfig } from "../types";
5
+ import { Seed } from "../types";
6
6
 
7
7
  type GenerateTagsConfig = {
8
8
  min?: number;
9
9
  max?: number;
10
- } & Pick<MockValueConfig, "seed">;
10
+ } & (
11
+ | {
12
+ seed: Seed;
13
+ faker?: never;
14
+ }
15
+ | {
16
+ faker: Faker;
17
+ seed?: never;
18
+ }
19
+ );
11
20
 
12
21
  export const generateTags = (config: GenerateTagsConfig): string[] => {
13
- const faker = createFaker(config.seed);
22
+ const faker = config.faker || createFaker(config.seed);
14
23
 
15
- return Array(
16
- faker.datatype.number({
17
- min: config.min ?? 0,
18
- max: config.max ?? 2,
19
- }),
20
- )
21
- .fill(undefined)
22
- .map(() =>
23
- changeCase.capitalCase(
24
- faker.lorem.words(faker.datatype.number({ min: 1, max: 3 })),
25
- ),
26
- );
24
+ return Array.from(
25
+ { length: faker.range(config.min ?? 0, config.max ?? 2) },
26
+ () => changeCase.capitalCase(faker.words(faker.range(1, 3))),
27
+ );
27
28
  };
@@ -1,71 +1,100 @@
1
- import { createFaker } from "../lib/createFaker";
1
+ import { createFaker, Faker } from "../lib/createFaker";
2
2
 
3
- import { MockEmbedData, MockValueConfig } from "../types";
3
+ import { MockEmbedData, Seed } from "../types";
4
4
 
5
5
  const dataSet: MockEmbedData[] = [
6
6
  {
7
- url: "https://www.youtube.com/watch?v=fiOwHYFkUz0",
7
+ version: "1.0",
8
+ type: "video",
8
9
  embed_url: "https://www.youtube.com/embed/fiOwHYFkUz0",
9
10
  thumbnail_url: "https://i.ytimg.com/vi/fiOwHYFkUz0/hqdefault.jpg",
10
11
  thumbnail_height: 360,
11
12
  thumbnail_width: 480,
12
13
  html: '<iframe width="200" height="113" src="https://www.youtube.com/embed/fiOwHYFkUz0?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>',
14
+ width: 200,
15
+ height: 113,
13
16
  },
14
17
  {
15
- url: "https://www.youtube.com/watch?v=c-ATzcy6VkI",
18
+ version: "1.0",
19
+ type: "video",
16
20
  embed_url: "https://www.youtube.com/embed/c-ATzcy6VkI",
17
21
  thumbnail_url: "https://i.ytimg.com/vi/c-ATzcy6VkI/hqdefault.jpg",
18
22
  thumbnail_height: 360,
19
23
  thumbnail_width: 480,
20
24
  html: '<iframe width="200" height="113" src="https://www.youtube.com/embed/c-ATzcy6VkI?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>',
25
+ width: 200,
26
+ height: 113,
21
27
  },
22
28
  {
23
- url: "https://www.youtube.com/watch?v=iewZXv94XGY",
29
+ version: "1.0",
30
+ type: "video",
24
31
  embed_url: "https://www.youtube.com/watch?v=fiOwHYFkUz0",
25
32
  thumbnail_url: "https://i.ytimg.com/vi/iewZXv94XGY/hqdefault.jpg",
26
33
  thumbnail_height: 360,
27
34
  thumbnail_width: 480,
28
35
  html: '<iframe width="200" height="113" src="https://www.youtube.com/embed/iewZXv94XGY?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>',
36
+ width: 200,
37
+ height: 113,
29
38
  },
30
39
  {
31
- url: "https://twitter.com/prismicio/status/1356293316158095361",
32
- embed_url: "https://www.youtube.com/watch?v=iewZXv94XGY",
40
+ version: "1.0",
41
+ type: "rich",
42
+ embed_url: "https://twitter.com/prismicio/status/1356293316158095361",
33
43
  thumbnail_url: null,
34
44
  thumbnail_height: null,
35
45
  thumbnail_width: null,
36
46
  html: '<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Gatsby is a popular choice for Prismic users and we work hard on delivering a CMS that plays to its strengths.<br><br>But, what makes <a href="https://twitter.com/GatsbyJS?ref_src=twsrc%5Etfw">@GatsbyJS</a> so popular?<br><br>Here are some of <a href="https://twitter.com/mxstbr?ref_src=twsrc%5Etfw">@mxstbr</a>&#39;s thoughts on Gatsby&#39;s success and how they&#39;re improving developer experience.<a href="https://t.co/ZjCPvsWWUD">https://t.co/ZjCPvsWWUD</a> <a href="https://t.co/EQqzJpeNKl">pic.twitter.com/EQqzJpeNKl</a></p>&mdash; Prismic (@prismicio) <a href="https://twitter.com/prismicio/status/1356293316158095361?ref_src=twsrc%5Etfw">February 1, 2021</a></blockquote>\n<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>\n',
47
+ width: 200,
48
+ height: 113,
37
49
  },
38
50
  {
39
- url: "https://twitter.com/timbenniks/status/1304146886832594944",
51
+ type: "rich",
52
+ version: "1.0",
53
+ embed_url: "https://twitter.com/timbenniks/status/1304146886832594944",
40
54
  thumbnail_url: null,
41
55
  thumbnail_width: null,
42
56
  thumbnail_height: null,
43
- embed_url: "https://twitter.com/timbenniks/status/1304146886832594944",
44
57
  html: '<blockquote class="twitter-tweet"><p lang="en" dir="ltr">I’ve been diving deep on <a href="https://twitter.com/prismicio?ref_src=twsrc%5Etfw">@prismicio</a> <a href="https://twitter.com/hashtag/slicemachine?src=hash&amp;ref_src=twsrc%5Etfw">#slicemachine</a> today. I made all my own components and I used custom slices. It works like a charm with <a href="https://twitter.com/nuxt_js?ref_src=twsrc%5Etfw">@nuxt_js</a>. Also: I’m coding with this view. <a href="https://t.co/F0I8X9gz39">pic.twitter.com/F0I8X9gz39</a></p>&mdash; Tim Benniks (@timbenniks) <a href="https://twitter.com/timbenniks/status/1304146886832594944?ref_src=twsrc%5Etfw">September 10, 2020</a></blockquote>\n<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>\n',
58
+ width: 200,
59
+ height: 113,
45
60
  },
46
61
  {
47
- url: "https://twitter.com/prismicio/status/1354112310252630016",
62
+ version: "1.0",
63
+ type: "rich",
64
+ embed_url: "https://twitter.com/prismicio/status/1354112310252630016",
48
65
  thumbnail_url: null,
49
66
  thumbnail_width: null,
50
67
  thumbnail_height: null,
51
- embed_url: "https://twitter.com/prismicio/status/1354112310252630016",
52
68
  html: '<blockquote class="twitter-tweet"><p lang="en" dir="ltr">We&#39;re launching a new <a href="https://twitter.com/hashtag/SliceContest?src=hash&amp;ref_src=twsrc%5Etfw">#SliceContest</a> tomorrow along with Slice Machine upgrades.<br><br>Want to know more? Join us at tomorrow&#39;s Product Meet-up👇<a href="https://t.co/prYSypiAvB">https://t.co/prYSypiAvB</a><br><br>We can&#39;t tell you any further details for now, but here&#39;s a sneak peek at the prizes👀 <a href="https://t.co/fV1eoGlEBh">pic.twitter.com/fV1eoGlEBh</a></p>&mdash; Prismic (@prismicio) <a href="https://twitter.com/prismicio/status/1354112310252630016?ref_src=twsrc%5Etfw">January 26, 2021</a></blockquote>\n<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>\n',
69
+ width: 200,
70
+ height: 113,
53
71
  },
54
72
  {
55
- url: "https://twitter.com/prismicio/status/1354835716430319617",
73
+ version: "1.0",
74
+ type: "rich",
75
+ embed_url: "https://twitter.com/prismicio/status/1354835716430319617",
56
76
  thumbnail_url: null,
57
77
  thumbnail_width: null,
58
78
  thumbnail_height: null,
59
- embed_url: "https://twitter.com/prismicio/status/1354835716430319617",
60
79
  html: '<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Does anyone want to create a wildly popular website for discussing &#39;Wall Street Bets&#39; using Prismic?<br><br>It may or may not have to look a lot like <a href="https://twitter.com/hashtag/reddit?src=hash&amp;ref_src=twsrc%5Etfw">#reddit</a> and we won&#39;t make it private.<br><br>Just asking for some friends...</p>&mdash; Prismic (@prismicio) <a href="https://twitter.com/prismicio/status/1354835716430319617?ref_src=twsrc%5Etfw">January 28, 2021</a></blockquote>\n<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>\n',
80
+ width: 200,
81
+ height: 113,
61
82
  },
62
83
  ];
63
- type GetMockEmbedDataConfig = Pick<MockValueConfig, "seed">;
84
+ type GetMockEmbedDataConfig =
85
+ | {
86
+ seed: Seed;
87
+ faker?: never;
88
+ }
89
+ | {
90
+ faker: Faker;
91
+ seed?: never;
92
+ };
64
93
 
65
94
  export const getMockEmbedData = (
66
95
  config: GetMockEmbedDataConfig,
67
96
  ): MockEmbedData => {
68
- const faker = createFaker(config.seed);
97
+ const faker = config.faker || createFaker(config.seed);
69
98
 
70
- return faker.random.arrayElement(dataSet);
99
+ return faker.randomElement(dataSet);
71
100
  };
@@ -1,6 +1,6 @@
1
- import { createFaker } from "../lib/createFaker";
1
+ import { createFaker, Faker } from "../lib/createFaker";
2
2
 
3
- import { MockImageData, MockValueConfig } from "../types";
3
+ import { MockImageData, Seed } from "../types";
4
4
 
5
5
  const dataSet: MockImageData[] = [
6
6
  {
@@ -85,12 +85,20 @@ const dataSet: MockImageData[] = [
85
85
  },
86
86
  ];
87
87
 
88
- type GetMockImageDataConfig = Pick<MockValueConfig, "seed">;
88
+ type GetMockImageDataConfig =
89
+ | {
90
+ seed: Seed;
91
+ faker?: never;
92
+ }
93
+ | {
94
+ faker: Faker;
95
+ seed?: never;
96
+ };
89
97
 
90
98
  export const getMockImageData = (
91
99
  config: GetMockImageDataConfig,
92
100
  ): MockImageData => {
93
- const faker = createFaker(config.seed);
101
+ const faker = config.faker || createFaker(config.seed);
94
102
 
95
- return faker.random.arrayElement(dataSet);
103
+ return faker.randomElement(dataSet);
96
104
  };
@@ -0,0 +1,112 @@
1
+ /**
2
+ * The following code is a modified version of fast-lorem-ipsum from Amit Gupta.
3
+ * The changes are made to support starting the return value from a configurable
4
+ * word offset.
5
+ *
6
+ * https://github.com/amitguptagwl/fast-lorem-ipsum/tree/7f836ee59a23cc75e7ca5b1a1747ea888eab31c7
7
+ *
8
+ * MIT License
9
+ *
10
+ * Copyright (c) 2017 Amit Gupta
11
+ *
12
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ * of this software and associated documentation files (the "Software"), to deal
14
+ * in the Software without restriction, including without limitation the rights
15
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ * copies of the Software, and to permit persons to whom the Software is
17
+ * furnished to do so, subject to the following conditions:
18
+ *
19
+ * The above copyright notice and this permission notice shall be included in
20
+ * all copies or substantial portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ * SOFTWARE.
29
+ */
30
+
31
+ const wordsString =
32
+ "lorem ipsum dolor sit amet, consectetur adipiscing elit ut aliquam, purus sit amet luctus venenatis, lectus magna fringilla urna, porttitor rhoncus dolor purus non enim praesent elementum facilisis leo, vel fringilla est ullamcorper eget nulla facilisi etiam dignissim diam quis enim lobortis scelerisque fermentum dui faucibus in ornare quam viverra orci sagittis eu volutpat odio facilisis mauris sit amet massa vitae tortor condimentum lacinia quis vel eros donec ac odio tempor orci dapibus ultrices in iaculis nunc sed augue lacus, viverra vitae congue eu, consequat ac felis donec et odio pellentesque diam volutpat commodo sed egestas egestas fringilla phasellus faucibus scelerisque eleifend donec pretium vulputate sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum curabitur vitae nunc sed velit dignissim sodales ut eu sem integer vitae justo eget magna fermentum iaculis eu non diam phasellus vestibulum lorem sed risus ultricies tristique nulla aliquet enim tortor, at auctor urna nunc id cursus metus aliquam eleifend mi in nulla posuere sollicitudin aliquam ultrices sagittis orci, a scelerisque purus semper eget duis at tellus at urna condimentum mattis pellentesque id nibh tortor, id aliquet lectus proin nibh nisl, condimentum id venenatis a, condimentum vitae sapien pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas sed tempus, urna et pharetra pharetra, massa massa ultricies mi, quis hendrerit dolor magna eget est lorem ipsum dolor sit amet, consectetur adipiscing elit pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas integer eget aliquet nibh praesent tristique magna sit amet purus gravida quis blandit turpis cursus in hac habitasse platea dictumst quisque sagittis, purus sit amet volutpat consequat, mauris nunc congue nisi, vitae suscipit tellus mauris a diam maecenas sed enim ut sem viverra aliquet eget sit amet tellus cras adipiscing enim eu turpis egestas pretium aenean pharetra, magna ac placerat vestibulum, lectus mauris ultrices eros, in cursus turpis massa tincidunt dui ut ornare lectus sit amet est placerat in egestas erat imperdiet sed euismod nisi porta lorem mollis aliquam ut porttitor leo a diam sollicitudin tempor id eu nisl nunc mi ipsum, faucibus vitae aliquet nec, ullamcorper sit amet risus nullam eget felis eget nunc lobortis mattis aliquam faucibus purus in massa tempor nec feugiat nisl pretium fusce id velit ut tortor pretium viverra suspendisse potenti nullam ac tortor vitae purus faucibus ornare suspendisse sed nisi lacus, sed viverra tellus in hac habitasse platea dictumst vestibulum rhoncus est pellentesque elit ullamcorper dignissim cras tincidunt lobortis feugiat vivamus at augue eget arcu dictum varius duis at consectetur lorem donec massa sapien, faucibus et molestie ac, feugiat sed lectus vestibulum mattis ullamcorper velit sed ullamcorper morbi tincidunt ornare massa, eget egestas purus viverra accumsan in nisl nisi, scelerisque eu ultrices vitae, auctor eu augue ut lectus arcu, bibendum at varius vel, pharetra vel turpis nunc eget lorem dolor, sed viverra ipsum nunc aliquet bibendum enim, facilisis gravida neque convallis a cras semper auctor neque, vitae tempus quam pellentesque nec nam aliquam sem et tortor consequat id porta nibh venenatis cras sed felis eget velit aliquet sagittis id consectetur purus ut faucibus pulvinar elementum integer enim neque, volutpat ac tincidunt vitae, semper quis lectus nulla at volutpat diam ut venenatis tellus in metus vulputate eu scelerisque felis imperdiet proin fermentum leo vel orci porta non pulvinar neque laoreet suspendisse interdum consectetur libero, id faucibus nisl tincidunt eget nullam non nisi est, sit amet facilisis magna etiam tempor, orci eu lobortis elementum, nibh tellus molestie nunc, non blandit massa enim nec dui nunc mattis enim ut tellus elementum sagittis vitae et leo duis ut diam quam nulla porttitor massa id neque aliquam vestibulum morbi blandit cursus risus, at ultrices mi tempus imperdiet nulla malesuada pellentesque elit eget gravida cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus mauris vitae ultricies leo integer malesuada nunc vel risus commodo viverra maecenas accumsan, lacus vel facilisis volutpat, est velit egestas dui, id ornare arcu odio ut sem nulla pharetra diam sit amet nisl suscipit adipiscing bibendum est ultricies integer quis auctor elit sed vulputate mi sit amet mauris commodo quis imperdiet massa tincidunt nunc pulvinar sapien et ligula ullamcorper malesuada proin libero nunc, consequat interdum varius sit amet, mattis vulputate enim nulla aliquet porttitor lacus, luctus accumsan tortor posuere ac ut consequat semper viverra nam libero justo, laoreet sit amet cursus sit amet, dictum sit amet justo donec enim diam, vulputate ut pharetra sit amet, aliquam id diam maecenas ultricies mi eget mauris pharetra et ultrices neque ornare aenean euismod elementum nisi, quis eleifend quam adipiscing vitae proin sagittis, nisl rhoncus mattis rhoncus, urna neque viverra justo, nec ultrices dui sapien eget mi proin sed libero enim, sed faucibus turpis in eu mi bibendum neque egestas congue quisque egestas diam in arcu cursus euismod quis viverra nibh cras pulvinar mattis nunc, sed blandit libero volutpat sed cras ornare arcu dui vivamus arcu felis, bibendum ut tristique et, egestas quis ipsum suspendisse ultrices gravida dictum fusce ut placerat orci nulla pellentesque dignissim enim, sit amet venenatis urna cursus eget nunc scelerisque viverra mauris, in aliquam sem fringilla ut morbi tincidunt augue interdum velit euismod in pellentesque massa placerat duis ultricies lacus sed turpis tincidunt id aliquet risus feugiat in ante metus, dictum at tempor commodo, ullamcorper a lacus vestibulum sed arcu non odio euismod lacinia at quis risus sed vulputate odio ut enim blandit volutpat maecenas volutpat blandit aliquam etiam erat velit, scelerisque in dictum non, consectetur a erat nam at lectus urna duis convallis convallis tellus, id interdum velit laoreet id donec ultrices tincidunt arcu, non sodales neque sodales ut etiam sit amet nisl purus, in mollis nunc sed id semper risus in hendrerit gravida rutrum quisque non tellus orci, ac auctor augue mauris augue neque, gravida in fermentum et, sollicitudin ac orci phasellus egestas tellus rutrum tellus pellentesque eu tincidunt tortor aliquam nulla facilisi cras fermentum, odio eu feugiat pretium, nibh ipsum consequat nisl, vel pretium lectus quam id leo in vitae turpis massa sed elementum tempus egestas sed sed risus pretium quam vulputate dignissim suspendisse in est ante in nibh mauris, cursus mattis molestie a, iaculis at erat pellentesque adipiscing commodo elit, at imperdiet dui accumsan sit amet nulla facilisi morbi tempus iaculis urna, id volutpat lacus laoreet non curabitur gravida arcu ac tortor dignissim convallis aenean et tortor at risus viverra adipiscing at in tellus integer feugiat scelerisque varius morbi enim nunc, faucibus a pellentesque sit amet, porttitor eget dolor morbi non arcu risus, quis varius quam quisque id diam vel quam elementum pulvinar etiam non quam lacus suspendisse faucibus interdum posuere lorem ipsum dolor sit amet, consectetur adipiscing elit duis tristique sollicitudin nibh sit amet commodo nulla facilisi nullam vehicula ipsum a arcu cursus vitae congue mauris rhoncus aenean vel elit scelerisque mauris pellentesque pulvinar pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas maecenas pharetra convallis posuere morbi leo urna, molestie at elementum eu, facilisis sed odio morbi quis commodo odio aenean sed adipiscing diam donec adipiscing tristique risus nec feugiat in fermentum posuere urna nec tincidunt praesent semper feugiat nibh sed pulvinar proin gravida hendrerit lectus a molestie";
33
+
34
+ export const loremWords = wordsString.split(" ");
35
+
36
+ /**
37
+ * Returns "Lorem ipsum" text of a specified length.
38
+ *
39
+ * @example
40
+ *
41
+ * ```ts
42
+ * lorem("100w"); // 100 words
43
+ * lorem("200c"); // 200 characters
44
+ * ```
45
+ *
46
+ * @param length - Length of the returned text in the form of `${number}w` or
47
+ * `${number}c`. `w` means words, `c` means characters.
48
+ *
49
+ * @returns Lorem ipsum text.
50
+ */
51
+ export const lorem = (length: `${number}w` | `${number}c`, wordOffset = 0) => {
52
+ const count = parseInt(length.substring(0, length.length - 1));
53
+ const countType = length.charAt(length.length - 1);
54
+
55
+ if (!isNaN(count) && (countType === "w" || countType === "c")) {
56
+ // Build a list of indicies for each space in the list of
57
+ // words. Only do this on the first call of `lorem`. The list
58
+ // is cached to a property on `lorem` itself.
59
+ if (!lorem.spaceIndices.length) {
60
+ let startIndex = -1;
61
+ do {
62
+ const index = wordsString.indexOf(" ", startIndex + 1);
63
+
64
+ lorem.spaceIndices.push(index);
65
+
66
+ startIndex = index;
67
+ } while (startIndex !== -1);
68
+ }
69
+
70
+ if (countType === "c") {
71
+ return wordsString.substring(0, count);
72
+ } else {
73
+ let sentence = "";
74
+
75
+ const startSpaceIndex =
76
+ lorem.spaceIndices[(wordOffset % lorem.spaceIndices.length) - 1];
77
+
78
+ const endSpaceIndex =
79
+ lorem.spaceIndices[
80
+ (((wordOffset % lorem.spaceIndices.length) + count) %
81
+ lorem.spaceIndices.length) -
82
+ 1
83
+ ];
84
+
85
+ if (
86
+ count + (wordOffset % lorem.spaceIndices.length) >
87
+ lorem.spaceIndices.length
88
+ ) {
89
+ sentence += wordsString.substring(startSpaceIndex + 1) + " ";
90
+
91
+ const wraparoundCounts =
92
+ Math.floor(count / lorem.spaceIndices.length) - 1;
93
+ for (let i = 0; i < wraparoundCounts; i++) {
94
+ sentence += wordsString + " ";
95
+ }
96
+
97
+ sentence += wordsString.substring(0, endSpaceIndex);
98
+ } else {
99
+ sentence = wordsString.substring(startSpaceIndex + 1, endSpaceIndex);
100
+ }
101
+
102
+ if (sentence.charAt(sentence.length - 1) === ",") {
103
+ return sentence.substring(0, sentence.length - 1);
104
+ } else {
105
+ return sentence.substring(0, sentence.length);
106
+ }
107
+ }
108
+ }
109
+
110
+ throw new Error("Length must be of the form `${number}w` or `${number}c`");
111
+ };
112
+ lorem.spaceIndices = [] as number[];
@@ -1,27 +1,35 @@
1
1
  import * as prismicT from "@prismicio/types";
2
2
 
3
- import {
4
- MockValueConfig,
5
- MockValueConfigForModel,
6
- ModelValue,
7
- SetRequired,
8
- } from "../types";
3
+ import { MockValueConfigForModel, ModelValue, Seed } from "../types";
4
+ import * as value from "../value";
9
5
 
10
- type ValueForModelConfig<Model extends prismicT.CustomTypeModelField> = {
11
- config?: Omit<MockValueConfigForModel<Model>, "seed" | "model">;
12
- } & SetRequired<MockValueConfig<Model>, "model">;
6
+ import { createFaker, Faker } from "./createFaker";
13
7
 
14
- import * as value from "../value";
8
+ type ValueForModelConfig<Model extends prismicT.CustomTypeModelField> = {
9
+ model: Model;
10
+ config?: Omit<MockValueConfigForModel<Model>, "faker" | "seed" | "model">;
11
+ } & (
12
+ | {
13
+ seed: Seed;
14
+ faker?: never;
15
+ }
16
+ | {
17
+ faker: Faker;
18
+ seed?: never;
19
+ }
20
+ );
15
21
 
16
22
  export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
17
23
  config: ValueForModelConfig<Model>,
18
24
  ): ModelValue<Model> => {
25
+ const faker = config.faker || createFaker(config.seed);
26
+
19
27
  const model = config.model;
20
28
 
21
29
  switch (model.type) {
22
30
  case prismicT.CustomTypeModelFieldType.Boolean: {
23
31
  return value.boolean({
24
- seed: config.seed,
32
+ faker,
25
33
  model,
26
34
  ...config.config,
27
35
  }) as ModelValue<Model>;
@@ -29,17 +37,17 @@ export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
29
37
 
30
38
  case prismicT.CustomTypeModelFieldType.Color: {
31
39
  return value.color({
32
- seed: config.seed,
40
+ faker,
33
41
  model,
34
42
  ...config.config,
35
43
  }) as ModelValue<Model>;
36
44
  }
37
45
 
38
46
  case prismicT.CustomTypeModelFieldType.Link: {
39
- switch (model.config.select) {
47
+ switch (model.config?.select) {
40
48
  case prismicT.CustomTypeModelLinkSelectType.Document: {
41
49
  return value.contentRelationship({
42
- seed: config.seed,
50
+ faker,
43
51
  model: model as prismicT.CustomTypeModelContentRelationshipField,
44
52
  ...config.config,
45
53
  }) as ModelValue<Model>;
@@ -47,7 +55,7 @@ export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
47
55
 
48
56
  case prismicT.CustomTypeModelLinkSelectType.Media: {
49
57
  return value.linkToMedia({
50
- seed: config.seed,
58
+ faker,
51
59
  model: model as prismicT.CustomTypeModelLinkToMediaField,
52
60
  ...config.config,
53
61
  }) as ModelValue<Model>;
@@ -55,7 +63,7 @@ export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
55
63
 
56
64
  default: {
57
65
  return value.link({
58
- seed: config.seed,
66
+ faker,
59
67
  model: model as prismicT.CustomTypeModelLinkField,
60
68
  ...config.config,
61
69
  }) as ModelValue<Model>;
@@ -65,7 +73,7 @@ export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
65
73
 
66
74
  case prismicT.CustomTypeModelFieldType.Date: {
67
75
  return value.date({
68
- seed: config.seed,
76
+ faker,
69
77
  model,
70
78
  ...config.config,
71
79
  }) as ModelValue<Model>;
@@ -73,7 +81,7 @@ export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
73
81
 
74
82
  case prismicT.CustomTypeModelFieldType.Embed: {
75
83
  return value.embed({
76
- seed: config.seed,
84
+ faker,
77
85
  model,
78
86
  ...config.config,
79
87
  }) as ModelValue<Model>;
@@ -81,7 +89,7 @@ export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
81
89
 
82
90
  case prismicT.CustomTypeModelFieldType.GeoPoint: {
83
91
  return value.geoPoint({
84
- seed: config.seed,
92
+ faker,
85
93
  model,
86
94
  ...config.config,
87
95
  }) as ModelValue<Model>;
@@ -89,7 +97,7 @@ export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
89
97
 
90
98
  case prismicT.CustomTypeModelFieldType.Image: {
91
99
  return value.image({
92
- seed: config.seed,
100
+ faker,
93
101
  model,
94
102
  ...config.config,
95
103
  }) as ModelValue<Model>;
@@ -97,7 +105,7 @@ export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
97
105
 
98
106
  case prismicT.CustomTypeModelFieldType.Text: {
99
107
  return value.keyText({
100
- seed: config.seed,
108
+ faker,
101
109
  model,
102
110
  ...config.config,
103
111
  }) as ModelValue<Model>;
@@ -105,7 +113,7 @@ export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
105
113
 
106
114
  case prismicT.CustomTypeModelFieldType.Number: {
107
115
  return value.number({
108
- seed: config.seed,
116
+ faker,
109
117
  model,
110
118
  ...config.config,
111
119
  }) as ModelValue<Model>;
@@ -113,7 +121,7 @@ export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
113
121
 
114
122
  case prismicT.CustomTypeModelFieldType.Select: {
115
123
  return value.select({
116
- seed: config.seed,
124
+ faker,
117
125
  model,
118
126
  ...config.config,
119
127
  }) as ModelValue<Model>;
@@ -121,7 +129,7 @@ export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
121
129
 
122
130
  case prismicT.CustomTypeModelFieldType.Timestamp: {
123
131
  return value.timestamp({
124
- seed: config.seed,
132
+ faker,
125
133
  model,
126
134
  ...config.config,
127
135
  }) as ModelValue<Model>;
@@ -129,19 +137,21 @@ export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
129
137
 
130
138
  case prismicT.CustomTypeModelFieldType.StructuredText: {
131
139
  if (
140
+ model.config &&
132
141
  "single" in model.config &&
142
+ model.config.single &&
133
143
  model.config.single
134
144
  .split(",")
135
145
  .every((element) => /heading[1-6]/.test(element.trim()))
136
146
  ) {
137
147
  return value.title({
138
- seed: config.seed,
148
+ faker,
139
149
  model: model as prismicT.CustomTypeModelTitleField,
140
150
  ...config.config,
141
151
  }) as ModelValue<Model>;
142
152
  } else {
143
153
  return value.richText({
144
- seed: config.seed,
154
+ faker,
145
155
  model,
146
156
  ...config.config,
147
157
  }) as ModelValue<Model>;
@@ -150,7 +160,7 @@ export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
150
160
 
151
161
  case prismicT.CustomTypeModelFieldType.IntegrationFields: {
152
162
  return value.integrationFields({
153
- seed: config.seed,
163
+ faker,
154
164
  model,
155
165
  ...config.config,
156
166
  }) as ModelValue<Model>;
@@ -158,7 +168,7 @@ export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
158
168
 
159
169
  case prismicT.CustomTypeModelFieldType.UID: {
160
170
  return value.uid({
161
- seed: config.seed,
171
+ faker,
162
172
  model,
163
173
  ...config.config,
164
174
  }) as ModelValue<Model>;
@@ -166,18 +176,25 @@ export const valueForModel = <Model extends prismicT.CustomTypeModelField>(
166
176
 
167
177
  case prismicT.CustomTypeModelFieldType.Group: {
168
178
  return value.group({
169
- seed: config.seed,
179
+ faker,
170
180
  model,
171
181
  ...config.config,
172
182
  }) as ModelValue<Model>;
173
183
  }
174
184
 
185
+ case prismicT.CustomTypeModelFieldType.LegacySlices:
175
186
  case prismicT.CustomTypeModelFieldType.Slices: {
176
187
  return value.sliceZone({
177
- seed: config.seed,
188
+ faker,
178
189
  model,
179
190
  ...config.config,
180
191
  }) as ModelValue<Model>;
181
192
  }
193
+
194
+ default: {
195
+ throw new Error(
196
+ `The "${model.type}" field type is not supported in @prismicio/mock.`,
197
+ );
198
+ }
182
199
  }
183
200
  };
@@ -1,6 +1,6 @@
1
1
  import * as prismicT from "@prismicio/types";
2
2
 
3
- import { MockValueConfig, ModelValue, ModelValueMap } from "../types";
3
+ import { ModelValue, ModelValueMap, Seed } from "../types";
4
4
  import {
5
5
  MockBooleanValueConfig,
6
6
  MockColorValueConfig,
@@ -24,6 +24,7 @@ import {
24
24
  } from "../value";
25
25
 
26
26
  import { valueForModel } from "./valueForModel";
27
+ import { createFaker, Faker } from "./createFaker";
27
28
 
28
29
  const getValueConfigType = <Model extends prismicT.CustomTypeModelField>(
29
30
  model: Model,
@@ -51,7 +52,7 @@ const getValueConfigType = <Model extends prismicT.CustomTypeModelField>(
51
52
  return "image";
52
53
 
53
54
  case prismicT.CustomTypeModelFieldType.Link: {
54
- switch (model.config.select) {
55
+ switch (model.config?.select) {
55
56
  case prismicT.CustomTypeModelLinkSelectType.Document:
56
57
  return "contentRelationship";
57
58
  case prismicT.CustomTypeModelLinkSelectType.Media:
@@ -69,7 +70,9 @@ const getValueConfigType = <Model extends prismicT.CustomTypeModelField>(
69
70
 
70
71
  case prismicT.CustomTypeModelFieldType.StructuredText: {
71
72
  if (
73
+ model.config &&
72
74
  "single" in model.config &&
75
+ model.config.single &&
73
76
  model.config.single
74
77
  .split(",")
75
78
  .every((element) => /heading{1,6}/.test(element.trim()))
@@ -94,6 +97,12 @@ const getValueConfigType = <Model extends prismicT.CustomTypeModelField>(
94
97
 
95
98
  case prismicT.CustomTypeModelFieldType.Slices:
96
99
  return "sliceZone";
100
+
101
+ default: {
102
+ throw new Error(
103
+ `The "${model.type}" field type is not supported in @prismicio/mock.`,
104
+ );
105
+ }
97
106
  }
98
107
  };
99
108
 
@@ -124,13 +133,24 @@ type ValueForModelMapConfig<
124
133
  > = {
125
134
  map: ModelMap;
126
135
  configs?: ValueForModelMapConfigs;
127
- } & Omit<MockValueConfig, "model">;
136
+ } & (
137
+ | {
138
+ seed: Seed;
139
+ faker?: never;
140
+ }
141
+ | {
142
+ faker: Faker;
143
+ seed?: never;
144
+ }
145
+ );
128
146
 
129
147
  export const valueForModelMap = <
130
148
  ModelMap extends Record<string, prismicT.CustomTypeModelField>,
131
149
  >(
132
150
  config: ValueForModelMapConfig<ModelMap>,
133
151
  ): ModelValueMap<ModelMap> => {
152
+ const faker = config.faker || createFaker(config.seed);
153
+
134
154
  const result = {} as ModelValueMap<ModelMap>;
135
155
 
136
156
  for (const fieldId in config.map) {
@@ -139,7 +159,7 @@ export const valueForModelMap = <
139
159
  const fieldConfig = config.configs?.[fieldConfigType];
140
160
 
141
161
  result[fieldId] = valueForModel({
142
- seed: config.seed,
162
+ faker,
143
163
  model: fieldModel as prismicT.CustomTypeModelField,
144
164
  config: fieldConfig,
145
165
  }) as ModelValue<typeof fieldModel>;
@@ -7,15 +7,15 @@ import { MockModelConfig } from "../types";
7
7
 
8
8
  export type MockBooleanModelConfig = MockModelConfig;
9
9
 
10
- export const boolean = (
11
- config: MockBooleanModelConfig = {},
12
- ): prismicT.CustomTypeModelBooleanField => {
13
- const faker = createFaker(config.seed);
10
+ export function boolean(
11
+ config: MockBooleanModelConfig,
12
+ ): prismicT.CustomTypeModelBooleanField {
13
+ const faker = config.faker || createFaker(config.seed);
14
14
 
15
15
  return {
16
16
  type: prismicT.CustomTypeModelFieldType.Boolean,
17
17
  config: {
18
- label: changeCase.capitalCase(faker.company.bsNoun()),
18
+ label: changeCase.capitalCase(faker.word()),
19
19
  },
20
20
  };
21
- };
21
+ }