Coder Social home page Coder Social logo

stream-tag's Introduction

stream-tag

A tagged template literal utility for Node streams

Works in node.js and the browser, if streams are polyfilled.

This utility is a small tagged-template-literal function that enables interpolation of strings, numbers, Buffers, promises, and streams into a new stream.

This is highly useful when constructing stream templates that will be emitted by a server-side rendering service for instance. One example of such use-case is React's streamed SSR, where it's useful to combine its stream with other variables into an HTML output template.

Installation

yarn add stream-tag
# or
npm install stream-tag

Usage

const streamTag = require('stream-tag');

const stream = streamTag`
  one
  ${'two'}
  ${3}
  ${Promise.resolve('four')}
  ${Promise.resolve(5)}
  ${Buffer.from('six')}
  ${streamTag`seven`}
`;

// This is what stream emits
const output = `
  one
  two
  3
  four
  5
  six
  seven
`;

API

streamTag(templateStringArr, ...interpolations)

Can be called as a tagged template literal. Returns a stream emitting the template with all interpolations combined into the output.

templateStringArr

Type: string[]

interpolations

Type: Array<void | string | number | Buffer | ReadableStream | Promise>

Promises may only resolve to all other values in the array above.

Related

After writing this I noticed that there's already the execellent stream-template library, which does about the same.

Licence

MIT

stream-tag's People

Contributors

kitten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

stefanmaric

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.