run-seq
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

run-seq

Build status Test coverage NPM version NPM Downloads Prettier Conventional Commits

run a series of tasks with next controller

It's useful for splitting multiple related tasks.

Installation

npm install run-seq
# or use yarn 
yarn add run-seq

Usage

import runSeq from 'run-seq'
const tree = {
  tagName: 'p',
  nodes: [
    {
      tagName: 'img',
      data: {
        src: './img.png'
      }
    },
    {
      tagName: 'text',
      text: 'lalalala'
    },
    {
      tagName: 'p',
      nodes: [
        {
          tagName: 'img',
          data: {
            src: './img.png'
          }
        },
        {
          tagName: 'text',
          text: 'lalalala'
        }
      ]
    }
  ]
}
 
const html = runSeq(
  [
    // 0
    (node, next) => {
      if (!node) return ''
      if (!Array.isArray(node)) {
        node = [node]
      }
 
      return node.map(node => next(node)).join('\n')
    },
    // 1
    (node, next) => {
      switch (node.tagName) {
        case 'img':
          return `<img src="${node.data.src}"/>`
        case 'p':
          // `next.all` is processing the all sequence (0-3)
          return `<p>${next.all(node.nodes)}</p>`
      }
      // `next` is processing the next task (2)
      return next(node)
    },
    // 2
    (node, next) => {
      switch (node.tagName) {
        case 'text':
          return node.text
      }
      return next(node)
    },
    // 3
    node => ''
  ],
  [tree]
)
 
html // => <p><img src="./img.png"><span>lalalala</span><p><img src="./img.png"><span>lalalala</span></p></p>

Contributing

  • Fork it!
  • Create your new branch:
    git checkout -b feature-new or git checkout -b fix-which-bug
  • Start your magic work now
  • Make sure npm test passes
  • Commit your changes:
    git commit -am 'feat: some description (close #123)' or git commit -am 'fix: some description (fix #123)'
  • Push to the branch: git push
  • Submit a pull request :)

Authors

This library is written and maintained by imcuttle, moyuyc95@gmail.com.

License

MIT - imcuttle 🐟

Readme

Keywords

Package Sidebar

Install

npm i run-seq

Weekly Downloads

12

Version

1.0.4

License

MIT

Unpacked Size

27.1 kB

Total Files

22

Last publish

Collaborators

  • moyuyc