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

Any way to dry up chained methods? #379

Open
drewpereli opened this issue Mar 11, 2024 · 0 comments
Open

Any way to dry up chained methods? #379

drewpereli opened this issue Mar 11, 2024 · 0 comments

Comments

@drewpereli
Copy link

First of all, great library, thank you. I'm wondering if there's a good way to dry up some of my chaining. Say I have something like this:

new TypeIt('#my-el')
  .type('abc')
  .move(null, { instant: true })
  .type('\n')
  .move(null, { instant: true })
  .type('123')
  .move(null, { instant: true })
  .type('\n')
  .move(null, { instant: true })
  .type('456')
  .go();

Is it possible to encapsulate the

  .move(null, { instant: true })
  .type('\n')
  .move(null, { instant: true })

stuff into its own function somehow?

I know this works:

let t = new TypeIt('#my-el').type('abc');

t = typeAtStart(t, '123');
t = typeAtStart(t, '456');

t.go();

function typeAtStart(typeIt: TypeIt, text: string) {
  return typeIt
    .move(null, { instant: true })
    .type('\n')
    .move(null, { instant: true })
    .type(text);
}

But I'm wondering if there's a more idiomatic way to do it, maybe without having to break up the chain.

I tried doing it like this:

new TypeIt('#$my-el')
  .type('abc')
  .exec((t) => typeAtStart(t, '123'))
  .exec((t) => typeAtStart(t, '456'))
  .go();

function typeAtStart(typeIt: TypeIt, text: string) {
  return typeIt
    .move(null, { instant: true })
    .type('\n')
    .move(null, { instant: true })
    .type(text);
}

But that doesn't seem to work.

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