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

Derivation using the existing value #55

Closed
ghost opened this issue Aug 12, 2021 · 2 comments
Closed

Derivation using the existing value #55

ghost opened this issue Aug 12, 2021 · 2 comments

Comments

@ghost
Copy link

ghost commented Aug 12, 2021

Hello! 👋

I'm trying to base the value in the derived factory on the value that was built by the original factory, but looks like this isn't possible with withDerivation or withDerivation1? (I attached an example below)

Is there any other way to achieve this? If not, would you consider adding this functionality into the library?

Example
import * as Factory from "factory.ts";

const userFactory = Factory.makeFactory({
  firstName: "Peter",
  lastName: "Parker"
});

// Doesn't work - firstName is an empty object
const userWithMiddleNameFactory = userFactory.withDerivation('firstName', (user) => {
  return user.firstName + " Benjamin"
});

// Also doesn't work
const altUserWithMiddleNameFactory = userFactory.withDerivation1(['firstName'], 'firstName', (firstName) => {
  return firstName + " Benjamin"
});

// Works for different fields
const userWithSameNamesFactory = userFactory.withDerivation('firstName', (user) => {
  return user.lastName
});

console.table({
  userWithMiddleNameFactory: userWithMiddleNameFactory.build(),
  altUserWithMiddleNameFactory: altUserWithMiddleNameFactory.build(),
  userWithSameNamesFactory: userWithSameNamesFactory.build()
});

which prints:

┌──────────────────────────────┬────────────────────────────┬──────────┐
│           (index)            │         firstName          │ lastName │
├──────────────────────────────┼────────────────────────────┼──────────┤
│  userWithMiddleNameFactory   │ '[object Object] Benjamin' │ 'Parker' │
│ altUserWithMiddleNameFactory │ '[object Object] Benjamin' │ 'Parker' │
│   userWithSameNamesFactory   │          'Parker'          │ 'Parker' │
└──────────────────────────────┴────────────────────────────┴──────────┘

p.s. This library has been super useful, thanks a lot for making it! 👏

@willryan
Copy link
Owner

@RussianCryptid should be fixed with #77 / v 1.4.0. (Use new function withSelfDerviation().)

@ghost
Copy link
Author

ghost commented Mar 31, 2023

@willryan Thank you! Just checked on the example and it's working.

@ghost ghost closed this as completed Mar 31, 2023
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant