g45t345rt/minifaker

Feature Request: Add support for seeds passed to functions

jasikpark opened this issue · 5 comments

I've found it useful to have fake data generated based on an input number, for example we use https://miragejs.com/ and it has support for an ID manager, so we use a different package to generate base32 numbers based on an input integer so that we can get stable but smartly generated data for our storybook snapshots.

Then when testing manually we don't need to set a seed and we can get the fully random mock data available.

Would it be possible to have an API like:

ip(4) // returns 192.1.4.3
ip(5) // returns 54.21.5.93
ip(4) // returns 192.1.4.3

I could imagine I might get a proxy to this by using setSeed before every time I want to call a function like this, but I'm unsure that will get me the result I want.

Forgot to export setSeed for cjs. So, i've pushed a quick fix 1.34.1 to npm. You can update to latest and use it.

const minifaker = require('minifaker')

minifaker.setSeed('1')
minifaker.ip() // 69.40.53.68
minifaker.setSeed('2')
minifaker.ip() // 182.11.30.237
minifaker.setSeed('1')
minifaker.ip() // 69.40.53.68

I also created a quick test file to see if it works and it does https://github.com/g45t345rt/minifaker/blob/master/test/setSeed.test.ts

cool cool i'll try that out.. i'll probably make a wrapper function for those for myself

function myIP(n) {
  setSeed(n)
  return ip()
}

You can also pass the func instead of creating a wrapper for each function.

function seedFunc(hash, func) {
  setSeed(hash)
  return func()
}

I wouldn't mind adding this in the library.

You can also pass the func instead of creating a wrapper for each function.

function seedFunc(hash, func) {

  setSeed(hash)

  return func()

}

I wouldn't mind adding this in the library.

yep i was probably going to use a higher ordered function like that to create the function defs though now that i think about it maybe i'll just use it like that?

const reproducibleFake = (fn: Function) => ((seed: number) => {
	minifaker.setSeed(seed);
	return fn();
});

const reproducibleIP = reproducibleFake(minifaker.ip);