Faker.js: [feature request] a non-global Faker instance

Created on 19 Dec 2015  Â·  10Comments  Â·  Source: Marak/faker.js

It would be nice to be able to instantiate a Faker object with its own state. That way, seeding it doesn't inflict that seed on all other Faker.js users. Maybe they don't want my deterministic output.

Something like this:

var myFaker = new window.faker.Faker();
myFaker.seed(123);

var deterministicBsBuzz = myFaker.company.bsBuzz(); // always the same bsBuzz
var stillPseudorandomBsBuzz = window.faker.company.bsBuzz(); // still pseudorandom, unaffected by the seed, above

Most helpful comment

@Marak any progress on this one? Importing the faker constructor directly doesn't actually solve the issue as the mersenne twister appears to be a singleton, so seeding a constructed Faker instance also seeds previous ones

All 10 comments

@john-kurkowski -

If you look at the source code, you'll see Faker is actually a class which is being initialized for you.

see: https://github.com/Marak/faker.js/blob/master/lib/index.js#L20

If you wanted direct control of creating your own Faker instance you could probably do something like:

https://github.com/Marak/faker.js/blob/master/index.js#L3

Shouldn't require any code changes on our end. Let me know if that works out for you.

Glad the class is already factored out!

I think there is still a necessary code change: exporting the constructor. #321 is an example of this. Is there another or better way?

The Faker constructor is already exported.

see: https://github.com/Marak/faker.js/blob/master/lib/index.js#L133

I can see how to get it via a Node.js require, yes. But I'm following the README's browser example, trying to plainly execute build/build/faker.js. Here are the steps I followed. At the end, I open this HTML gist in my browser. The console.log output suggests there's only the lowercase window.faker exposed.

➜  /tmp  bower i Faker
bower Faker#*                   cached git://github.com/Marak/Faker.js.git#3.0.1
bower Faker#*                 validate 3.0.1 against git://github.com/Marak/Faker.js.git#*
bower faker#~3.0.1             install faker#3.0.1

faker#3.0.1 bower_components/faker
➜  /tmp  wget --quiet https://gist.githubusercontent.com/john-kurkowski/7acbad019c9f2b884b82/raw/5d2d5cdd757b10a3779773ce9cbcb3878a67c1f1/faker-global.html
➜  /tmp  open faker-global.html

@john-kurkowski

Thank you for the detailed explanation. I now understand issue.

I have reviewed #321 and have taken note.

Due to the nature of the issue I will make the necessary changes on my end with a new commit.

Thanks again for your contributions!

@Marak any progress on this one? Importing the faker constructor directly doesn't actually solve the issue as the mersenne twister appears to be a singleton, so seeding a constructed Faker instance also seeds previous ones

@Marak any progress?

I want to be able to use a number of seeds to allow for predictable fine-grain control of demo data, which means I have to be able to have multiple isolated instances of faker...

Let us know if there's anything the community can do to help speed this along!

@Marak Just checking in on the status of this feature request since it remains open!

This is going to be addressed as part of the larger concern of general project restructure for tree shaking and project size.

This is now near the top of the list of priorities for faker.

If you use typescript, you can use this code snippet to use the isolated Faker instance

import fakerStatic from 'faker'

// @ts-ignore (currently no .d.ts available yet)
import Faker from 'faker/lib'

export function createFaker(seed?: number): typeof fakerStatic {
  const faker = new Faker({ locale: 'zh_CN', localeFallback: 'en' })
  if (typeof seed !== 'undefined') {
    faker.seed(seed)
  }
  faker.locales['zh_CN'] = require('faker/lib/locales/zh_CN')
  faker.locales['en'] = require('faker/lib/locales/en')

  return faker
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

maticzav picture maticzav  Â·  3Comments

clarmso picture clarmso  Â·  3Comments

developdeez picture developdeez  Â·  5Comments

marcelorl picture marcelorl  Â·  4Comments

HoustonBass picture HoustonBass  Â·  6Comments