Skip to content

Commit

Permalink
feat: support string seed
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Aug 23, 2021
1 parent 5e17ec9 commit 3616cb8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const FAKER_SEED = 1984;
import { Seed } from "./types";

export const FAKER_SEED: Seed = 1984;
29 changes: 19 additions & 10 deletions src/lib/createFaker.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import * as fakerStatic from "faker";
// @ts-expect-error - Missing .d.ts
import * as fakerLocaleEN from "faker/lib/locales/en";
import * as fakerLocaleEN from "faker/lib/locales/en/index.js";
// @ts-expect-error - Missing .d.ts
import Faker from "faker/lib";
import Faker from "faker/lib/index.js";

import { FAKER_SEED } from "../constants";

export const createFaker = (seed = FAKER_SEED): typeof fakerStatic => {
if (createFaker.cache[seed]) {
return createFaker.cache[seed];
let normalizedSeed: number | number[];
if (typeof seed === "string") {
normalizedSeed = seed.split("").map((char) => char.charCodeAt(0));
} else {
normalizedSeed = seed;
}

const seededFaker = new Faker();
seededFaker.seed(seed);
seededFaker.locales["en"] = fakerLocaleEN;
const cacheKey = JSON.stringify(normalizedSeed);

createFaker.cache[seed] = seededFaker;
if (createFaker.cache[cacheKey]) {
return createFaker.cache[cacheKey];
}

const fakerInstance = new Faker();
fakerInstance.locales["en"] = fakerLocaleEN;
fakerInstance.seed(normalizedSeed);

createFaker.cache[cacheKey] = fakerInstance;

return seededFaker;
return fakerInstance;
};
createFaker.cache = {} as Record<number, typeof fakerStatic>;
createFaker.cache = {} as Record<string, typeof fakerStatic>;
10 changes: 6 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export type SetRequired<BaseType, Keys extends keyof BaseType> = Simplify<
Required<Pick<BaseType, Keys>>
>;

export type Seed = string | number | number[];

export interface MockImageData {
url: string;
width: number;
Expand All @@ -43,11 +45,11 @@ export interface MockEmbedData {
}

export type MockRestApiConfig = {
seed?: number;
seed?: Seed;
};

export type MockModelConfig = {
seed?: number;
seed?: Seed;
};

// TODO: Add to @prismicio/types
Expand All @@ -59,7 +61,7 @@ export type PrismicModel =
| prismicT.SharedSliceModelVariation;

export type MockValueConfig<Model extends PrismicModel = PrismicModel> = {
seed?: number;
seed?: Seed;
model?: Model;
};

Expand Down Expand Up @@ -105,7 +107,7 @@ type CustomTypeModelStructuredTextField =
export type MockRichTextValueConfig<
Model extends CustomTypeModelStructuredTextField = CustomTypeModelStructuredTextField,
> = {
seed?: number;
seed?: Seed;
model?: Model;
};

Expand Down

0 comments on commit 3616cb8

Please sign in to comment.