Skip to content

Any way to dry up chained methods? #379

Open

Description

@drewpereli

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions