Skip to content

Instantly share code, notes, and snippets.

@billymoon
Last active January 20, 2022 11:48
Show Gist options
  • Save billymoon/d9aac3414590c2aa7756da624fe22571 to your computer and use it in GitHub Desktop.
Save billymoon/d9aac3414590c2aa7756da624fe22571 to your computer and use it in GitHub Desktop.
pseudo-random-data-generator-demo
import faker from "https://cdn.jsdelivr.net/npm/@faker-js/[email protected]/dist/faker.min.js"; ///**import and configure faker**
faker.locales[faker.locale].name.male_prefix = ["Mr.", "Dr."];
faker.locales[faker.locale].name.female_prefix = ["Mrs.", "Miss.", "Dr."];
window.faker = faker;
({ ///**Generate some fake data**
name: faker.name.firstName(1),
age: faker.datatype.number() % 100
});
faker.seed(50); ///**Make it repeatable**
({
name: faker.name.firstName(1),
age: faker.datatype.number() % 100
});
const SEED_VALUE = 50; ///**The Problem**
faker.seed(SEED_VALUE);
({
name: faker.name.firstName(1),
age: faker.datatype.number() % 100
});
faker.seed(SEED_VALUE);
({
name: faker.name.firstName(1),
title: faker.name.prefix(1),
age: faker.datatype.number() % 100
});
const seededFaker = getSeededFaker(50); ///**Consistently repeatable values**
({ ///**Unique address for data items**
name: seededFaker("NAME").name.firstName(1),
age: seededFaker("AGE").datatype.number() % 100,
});
({ ///**Adding item does not change other values**
name: seededFaker("NAME").name.firstName(1),
title: seededFaker("TITLE").name.prefix(1),
age: seededFaker("AGE").datatype.number() % 100,
});
new Array(500).fill(null).map((_, index) => { ///**Generate lots of data**
const seededFaker = getSeededFaker(index);
return {
name: seededFaker("NAME").name.firstName(1),
title: seededFaker("TITLE").name.prefix(1),
age: seededFaker("AGE").datatype.number() % 100,
iban: seededFaker("iban").finance.iban(),
bitcoin: seededFaker("bitcoin").finance.bitcoinAddress(),
}
});
// hash function to convert string into integer ///**The Solution**
const murmurHash3 = (str) => {
for (var i = 0, h = 1779033703 ^ str.length; i < str.length; i++) {
h = Math.imul(h ^ str.charCodeAt(i), 3432918353);
h = h << 13 | h >>> 19;
}
h = Math.imul(h ^ (h >>> 16), 2246822507);
h = Math.imul(h ^ (h >>> 13), 3266489909);
return (h ^= h >>> 16) >>> 0;
}
// return faker instance that combines seed and path
const getSeededFaker = (seed) =>
(path) => {
const seededPathString = JSON.stringify([seed, path]);
const seedNumber = murmurHash3(seededPathString);
faker.seed(seedNumber);
return faker;
};
window.murmurHash3 = murmurHash3
window.getSeededFaker = getSeededFaker
const seededFaker = getSeededFaker(50); ///**Populate system with consistent data**
const user = {
name: seededFaker("NAME").name.firstName(1),
title: seededFaker("TITLE").name.prefix(1),
age: seededFaker("AGE").datatype.number() % 100,
};
`INSERT INTO users (name, title, age) VALUES ('${user.name}', '${user.title}', ${user.age});`;
`injectIntoLDAP("${user.name}", "${user.title}");`;
const seededFaker = getSeededFaker(37); ///**Generate data for cucumber tests**
const test = `
Feature: Welcoming Login
As a user
I want to be greeted upon login
So that I can feel warm and fuzzy inside
Scenario: Login to app
Given I register user with email "${seededFaker("NAME").name.firstName(1).toLowerCase()}@example.com" and password "${seededFaker("PASSWORD").internet.password()}"
And I navigate to "/login"
When I input "${seededFaker("NAME").name.firstName(1).toLowerCase()}@example.com" into email form input
And I input "${seededFaker("PASSWORD").internet.password()}" into password form input
And I press the login button
Then I expect the title to be "Hello ${seededFaker("NAME").name.firstName(1)}, have a nice day!"
`;
const seededFaker = getSeededFaker(50); ///**Develop new feature**
const user = {
name: seededFaker("NAME").name.firstName(1),
title: seededFaker("TITLE").name.prefix(1),
age: seededFaker("AGE").datatype.number() % 100,
iban: seededFaker("iban").finance.iban(),
bitcoin: seededFaker("bitcoin").finance.bitcoinAddress(),
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment