Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Replace internal fake patterns with a resolver function #3176

Open
ST-DDT opened this issue Oct 12, 2024 · 1 comment
Open

Proposal: Replace internal fake patterns with a resolver function #3176

ST-DDT opened this issue Oct 12, 2024 · 1 comment
Labels
c: feature Request for new feature m: helpers Something is referring to the helpers module p: 1-normal Nothing urgent
Milestone

Comments

@ST-DDT
Copy link
Member

ST-DDT commented Oct 12, 2024

Continuation of/Requires: #2667, #2838

Partially inspired by: #1832, #2664


Clear and concise description of the problem

Glossary:

  • fake patterns: String patterns used by faker.helpers.fake
  • resolver functions: functions that take fakerCore as an argument and return a value based on a seed

Currently, some faker internal methods use fake patterns to generate some complex values based on certain parameters.
However parsing fake patterns is slow and error prone.

Suggested solution

Introduce a new helper method: faker.helpers.resolve(fns: ResolverFunction[])

type ResolverFunction<T> = (core: FakerCore) => T;

function resolve<T>(core: FakerCore, fns: ResolverFunction<T>[]): T{
    const fn = arrayElement(core, fns);
    return fn(core);
}

This could later be extended to support parameters as well.

Fake-Usage (OLD)

export const last_name_pattern = [
	'{{person.last_name}}',
	'{{person.last_name}}-{{person.last_name}}',
];

function lastName(core: FakerCore) {
	return fake(core, core.locale.person.last_name_pattern);
}

Resolve-Usage (NEW)

export const last_name_resolvers = [
	(core) => arrayElement(core, core.locale.person.last_name),
	(core) => arrayElement(core, core.locale.person.last_name) + '-' + arrayElement(core, core.locale.person.last_name),
];

function lastName(core: FakerCore) {
	return resolve(core, core.locale.person.last_name_resolvers);
}

Pros

  • Compile time errors instead of runtime errors
  • Supports auto completion when creating the resolver functions
  • Significantly better performance
  • No issues with json encoded parameters

Cons

  • Breaking change for locale data
  • Methods that use the new resolve patterns are harder to populate using a DB/file/string only store

Affected Methods

  • company.name
  • food.description
  • food.dish
  • location.zipCode
  • location.city
  • location.street
  • location.streetAddress
  • location.secondayAddress
  • person.lastName
  • person.bio
  • person.jobTitle

And potentially:

  • finance.iban
  • ....

Alternatives

Alternatively, we could follow this pattern for each method individually.

export const last_name_resolvers = [
	(core) => arrayElement(core, core.locale.person.last_name),
	(core) => arrayElement(core, core.locale.person.last_name) + '-' + arrayElement(core, core.locale.person.last_name),
];

function lastName(core: FakerCore) {
    const lastNameResolver = arrayElement(core, core.locale.person.last_name_resolvers);
	return lastNameResolver(core);
}
@ST-DDT ST-DDT added c: feature Request for new feature p: 1-normal Nothing urgent m: helpers Something is referring to the helpers module labels Oct 12, 2024
@ST-DDT ST-DDT added this to the v10.0 milestone Oct 12, 2024
@matthewmayer
Copy link
Contributor

Hmm, i'm not really a big fan of this idea. I like the fact that the data sources for most methods are simple arrays. Also, I think looking at how Faker uses fake patterns internally is educational in figuring out how you might use it yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: feature Request for new feature m: helpers Something is referring to the helpers module p: 1-normal Nothing urgent
Projects
None yet
Development

No branches or pull requests

2 participants