-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e17ec9
commit 3616cb8
Showing
3 changed files
with
28 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters