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

Get the ".last()" value generated of a module #627

Closed
wilker-dotgroup opened this issue Mar 16, 2022 · 8 comments
Closed

Get the ".last()" value generated of a module #627

wilker-dotgroup opened this issue Mar 16, 2022 · 8 comments
Labels
c: feature Request for new feature wontfix This will not be worked on
Milestone

Comments

@wilker-dotgroup
Copy link

Clear and concise description of the problem

As a developer using faker I want to use some method e.g. faker.internet.url.last() to get the latest value generated for this call.
If no previous value was generated, this method (or prop) executes like the same faker.internet.url() call.
So that helps me to avoid create an auxiliary var just to keep this value to use again in another field in the same scope.

Suggested solution

For any faker api module, the ".last()" returns the latest value generated, regardless of how the module was called.
If undefined, perform the same of no parameter call,
e.g.
faker.address.city.last() // undefined
faker.address.city.last() // same result as faker.address.city()

Alternative

No response

Additional context

No response

@wilker-dotgroup wilker-dotgroup added the s: pending triage Pending Triage label Mar 16, 2022
@Shinigami92
Copy link
Member

Interesting idea, but why don't you just safe it in a local var in your code?

@xDivisionByZerox
Copy link
Member

I love the idea of a history. But I don't see the responsibility on our side. Since our responsibility is to generate fake data. Not keep track of returned results IMO.

@wilker-dotgroup
Copy link
Author

wilker-dotgroup commented Mar 16, 2022

Well, I use some other generators and I like this approach in one of them, that is to get the last password generated. When using faker I missed something like this but there is no problem to use a var to store it.
In my code, I'm using faker to compound some POST and PUT requests and some fields need the same values. This pkg mounts the request chaining the .field('foo','bar') call, and I thought it would be better if some like .last() could be used to avoid the var "far away" from where is (re)used.

@ST-DDT
Copy link
Member

ST-DDT commented Mar 17, 2022

How about a fork() method? That way you can achieve the same.
The fork would create a new independent faker instance with the exact same values (including the seed).
That way, you can reproduce the exact same behavior as last(). It can even be used for last(number).

faker.fork().someMethod() // 'FooBar42'
faker.someMethod() // 'FooBar42'
faker.someMethod() // 'FooBar43'

Another alternative would be using a seed snapshot() and restore() mechanism.

See also: #448 (comment)

@Shinigami92
Copy link
Member

fork()
snapshot() and restore().

This actually sounds much more helpful 🤔
Because with that you could solve some even more complex tasks I think

@import-brain import-brain added c: feature Request for new feature s: needs decision Needs team/maintainer decision and removed s: pending triage Pending Triage labels Mar 17, 2022
@wilker-dotgroup
Copy link
Author

If the fork(), snapshot() and restore() doesn't create the necessity of more memory as a second instance to work I agree with that and can be used this way to get the previous values, in place to get the value directly from the actual faker instance with last().

@Shinigami92
Copy link
Member

We will try to plan something out, but for now I think this is something to target in the far future, so we can focus on the active milestones right now.
In the meanwhile, you are still able safe the value in a local var as suggested.
I personally do not want to quickly implement something, but design a well thought out draft for this feature.

@Shinigami92 Shinigami92 added this to the Future milestone Mar 17, 2022
@Shinigami92 Shinigami92 added s: needs decision Needs team/maintainer decision and removed s: needs decision Needs team/maintainer decision labels Mar 17, 2022
@ST-DDT
Copy link
Member

ST-DDT commented Mar 16, 2023

Team Decision

We won't implement it as a .last() or similar function.
Currently it is possible to achieve this via repeatedly calling faker.seed(x).
In the future we will add features to derive and potentially fork a faker instance to allow easier support for this/related usecases.

@ST-DDT ST-DDT closed this as completed Mar 16, 2023
@ST-DDT ST-DDT added wontfix This will not be worked on and removed s: needs decision Needs team/maintainer decision labels Mar 16, 2023
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 wontfix This will not be worked on
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants