GithubHelp home page GithubHelp logo

tomeraberbach / gulp-windowed Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 173 KB

๐ŸชŸ Gulp plugin which processes and maps files in windows/arrays of a specified size.

Home Page: https://npm.im/gulp-windowed

License: MIT License

JavaScript 94.09% TypeScript 5.91%
gulp gulp-plugin gulp-friendly windowed paginated pagination javascript npm-module nodejs node-module

gulp-windowed's Introduction

gulp-windowed

Processes and maps files in windows/arrays of a specified size.

Install

$ npm i gulp-windowed

Usage

The following example concatenates groups of 5 markdown files. If src/posts contains files post0.md through post99.md, then the next pipe would receive a stream containing page0.md through page19.md, where page0.md is the concatenation of post0.md through post4.md. The ordering is guaranteed by the use of gulp-sort.

import gulp from 'gulp'
import sort from 'gulp-sorted'
import { windowed } from 'gulp-windowed'
import concat from 'gulp-concat'

const run = () =>
  gulp
    .src('src/posts/*.md') // Posts in markdown
    .pipe(sort()) // Sorted by filename
    .pipe(
      windowed(
        5,
        (
          files,
          i // In groups of 5
        ) => files.pipe(concat(`page${i}.md`)) // Concatenated into one page where 'i' is the window number
      )
    ) // More pipes...

Here it is used to split groups of files into different folders:

import gulp from 'gulp'
import { windowed } from 'gulp-windowed'
import rename from 'gulp-rename'

const run = () =>
  gulp.src('src/*').pipe(
    windowed(10, (files, i, done) =>
      files.pipe(
        rename({
          dirname: `folder${i}`
        })
      )
    )
  ) // More pipes...

Here it is used to skip every other file:

import gulp from 'gulp'
import { windowed } from 'gulp-windowed'

const run = () =>
  gulp
    .src('src/*')
    .pipe(windowed(1, (files, i, done) => (i % 2 === 0 ? files : done()))) // More pipes...

Notice that the stream of File objects in the callback can be returned from the callback. They are subsequently written to the resulting stream for the next pipe outside the callback. This is useful because it allows you to perform stream operations on the groups of files, while also allowing you to merge the resulting streams back together to continue performing operations.

If you'd like an array of File objects instead of a stream then import and call arrayWindowed instead.

API

windowed(n, cb) -> DuplexObjectStream<File>

Calls cb with a readable object stream (or array for arrayWindowed) containing n vinyl File objects each time n are written to it. On stream end if there are any remaining File objects because the n threshold was not met then cb is called with a readable object stream (or array for arrayWindowed) containing the remaining [0, n) File objects. The contents of the duplex object stream returned by the method depends on cb (explained below).

Parameters:

  • n : int - A number of File objects to include per window. Must be a positive integer.
  • cb : (files, i, done) -> File | Array<File> | Promise<File | Array<File> | *> | Observable<File | Array<File> | *> | ChildProcess | ReadableObjectStream<File> | undefined - A callback which either calls done or returns a File object, an array of File objects, or an asynchronous operation in some returnable format (see possible cb return types and async-done). If an asynchronous operation is returned it will be resolved. If the result of cb, through calling done or returning an asynchronous operation, is a File object or an array of File objects then they will be written to the duplex object stream returned from windowed.
    • files : ReadableObjectStream<File> | Array<File> - A readable object stream (or array for window.array) containing up to n vinyl File objects (see above from why not exactly n).
    • i : int - The current zero-based window number (i.e. the number of times cb has been called minus one).
    • done : (err, result) -> undefined - Call done(new Error(...)) for error or done(null, result) for success when performing asynchronous operations in non-returnable format or when performing strictly synchronous operations. This method does not need to be and should not be called if a valid value of the types mentioned above is returned from cb.

Pairs Well With

Contributing

Stars are always welcome!

For bugs and feature requests, please create an issue.

License

MIT ยฉ Tomer Aberbach

gulp-windowed's People

Contributors

tomeraberbach avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.