I’m happy to announce faker, a library for generating fake but realistic data: names, addresses, emails, phone numbers, companies, dates, finance data, lorem ipsum and more, in 77 locales.
opam install faker
As far as I could find, OCaml didn’t have a faker-style library. Property-testing generators like QCheck give you random values, but not plausible, localised fixtures for demos, seeding a development database or snapshot tests.
faker is a port of @faker-js/faker 10.6.0, with seed-for-seed parity: for the same seed, every method returns exactly what faker-js returns. It uses the same MersenneTwister19937 generator and the same algorithms, so an OCaml service and a JS/TS service can share fixtures.
let () =
let f = Faker.create ~seed:42 () in
print_endline (Faker.Person.full_name f);
print_endline (Faker.Internet.email f);
print_endline (Faker.Location.city f);
print_endline
(Faker.Helpers.fake "{{person.firstName}} works as a {{person.jobTitle}}" f);
let de = Faker.Locales.De.faker () in
Faker.seed de 42;
print_endline (Faker.Person.full_name de);
let ja = Faker.Locales.Ja.faker () in
Faker.seed ja 42;
print_endline (Faker.Person.full_name ja)
Nikita Crist
Moises.Sipes-Effertz18@gmail.com
Lolaville
Jeannette works as a Forward Implementation Analyst
Melinda Dragu
吉川 結衣
The equivalent faker-js code prints the same six lines.
What’s in it
- All 26 faker-js modules (
Faker.Person,Faker.Internet,Faker.Location,Faker.Date,Faker.Finance,Faker.Commerce, …) and all 77 locales. - The API mirrors faker-js, so the faker-js API reference documents every method. The faker instance is always the last argument, and options are optional labelled arguments:
faker.number.int({ min: 1, max: 10 })becomesFaker.Number.int ~min:1 ~max:10 f. - No dependencies beyond the stdlib and
unix. Locales are linked on demand, so an English-only program doesn’t carry the other 76.
How parity is checked
dune test compares the OCaml output with fixtures generated by running faker-js itself: 3 seeds for every case, plus a sweep of every method in every locale (about 72,000 checks). Getting them all to pass meant solving a few problems that may interest people here:
- FMA contraction. On arm64, the OCaml compiler fuses
a *. b +. cinto a single fused multiply-add, which rounds differently from V8. The port uses an explicit multiply where it matters. - Evaluation order. OCaml evaluates function arguments and record fields right to left, so every random draw is bound with
letin faker-js’s order. - UTF-16 semantics. JS string lengths, case mapping and NFKD normalisation are emulated on UTF-8 strings, with tables generated from Node.
- Floating-point maths.
location.nearbyGPSCoordinateuses a port of V8’s fdlibmsin/cos. The one known platform dependence isMath.powin the exponential distributor: Node calls the system libm, so faker-js itself rounds the last bit differently on FreeBSD. opam-repo-ci found this, and 0.1.1 is the fix.
The porting notes are in docs/PORTING.md, and the remaining differences are listed in the README under “Known differences”.
Links
- Package and API docs: faker 0.1.1 (latest) · OCaml Package
- Source: GitHub - barteo/faker-ocaml: Generate massive amounts of fake (but realistic) data for testing and development — OCaml port of @faker-js/faker with seed-for-seed parity · GitHub
- Issues: Issues · barteo/faker-ocaml · GitHub
All the data and algorithms come from faker-js, so many thanks to the faker-js team and contributors. Feedback and bug reports are very welcome, especially any case where the output differs from faker-js.