• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An ES6 Tagged String Literal tag that can interpolate Node.JS streams, strings, arrays and Promises and produces a stream.

stream-template

An ES6/ES2015 Tagged String Literal tag that can interpolate Node.JS streams, strings and Promises that return either (or arrays of any of those) and produces a Node.JS stream. This allows you to join several Streams together with bits in between without having to buffer anything.

Written by Thomas Parslow (almostobsolete.net and tomparslow.co.uk) for IORad (iorad.com) and released with their kind permission.

NPM

Build Status

Warning: will only run on newer versions of Node.JS (tested on 4.0.0 and above)

Install

npm install --save stream-template

Examples

const ST = require("stream-template");

const data1 = fs.createReadStream("data1.txt");
const data2 = fs.createReadStream("data2.txt");
const output = ST`<html>
  1: <pre>${data1}</pre>
  2: <pre>${data2}</pre>
</html>`;
output.pipe(process.stdout);

Can also accept arrays (items are concatenated, array items can be any of the supported types):

const ST = require("stream-template");

const data = [
  fs.createReadStream("part1.txt"),
  fs.createReadStream("part2.txt")
];
const output = ST`Data follows: ${data}`;
output.pipe(process.stdout);

And also Promises:

const ST = require("./stream-template");
const fetch = require("node-fetch");

const email = fetch("https://api.github.com/users/almost")
  .then(r => r.json())
  .then(profile => {
    return profile.email;
  });

const output = ST`<a href="mailto:${email}">Email</a>`;
output.pipe(process.stdout);

And of course regular strings work:

const output = ST`Hello by name is ${name}`;
output.pipe(process.stdout);

I've shown each used seperated but you can do it all mixed together as well.

Encoding

By default strings are encoded as utf-8, you can change the encoding like this:

const ST = require("stream-template").encoding("utf16le");

Note that buffers and other streams are passed through as-is, the encoding only effects the template strings and interpolated strings.

Contributing

Fixed or improved stuff? Great! Send me a pull request through GitHub or get in touch on Twitter @almostobsolete or email at [email protected]

More Repositories

1

react-native-sqlite

SQLite3 bindings for React Native
Objective-C
537
star
2

react-native-html-webview

Display HTML in a UIWebView, optionally sanitizing it first
Objective-C
104
star
3

through2-concurrent

Simple Node.JS stream (streams2) Transform that runs the transform functions concurrently (with a set max concurrency)
JavaScript
74
star
4

org-daypage

Day pages for Emacs Org-Mode
Emacs Lisp
39
star
5

asyncboto

A small bit of code to make the Boto library for Amazon's AWS services work in an asynchronous (and extremely hacky) manner with Tornado. For a probably better approach see: http://blog.joshhaas.com/2011/06/marrying-boto-to-tornado-greenlets-bring-them-together/
Python
28
star
6

Haskell-Fun

Bits and pieces of Haskell code written for fun
Haskell
19
star
7

hubbub

Comments as GitHub pull requests for Jekyll (and other static site generator) websites.
JavaScript
16
star
8

react-native-sqlite-example

An example of how to read an SQLite database from React Native using react-native-sqlite
JavaScript
14
star
9

custom-ios-views-with-react-native

An example app showing a custom native component in React Native
Objective-C
13
star
10

safe-html

JavaScript
9
star
11

picprogrammer

Some minor fixes to Tetsujin's Python replacement for the PicPro programmer software.
Python
9
star
12

string-replace-stream

Replace one string with another string in a Node.js Stream.
JavaScript
9
star
13

regex-crossword-solver

A solver for a Regular Expression Crossword
Haskell
6
star
14

jquery-ebayshoppingapi

eBay Shopping API Plugin for jQuery
4
star
15

midiballs

OSX MIDI->OSC bridge for Felix's Machines
C++
3
star
16

tblc

Tiny PIC Boot Loader Command Line Tool
Python
3
star
17

sqs-readable-stream

Create a Readable Node.JS stream from an Amazon Simple Queue Service (SQS) Queue
JavaScript
3
star
18

mud.el

The beginings of a mud client using Circe's lui.el. Very basic and probably won't work for you right now.
Emacs Lisp
2
star
19

lhs-checkers

A literate Haskell program that plays Checkers.
Haskell
1
star
20

Data.Bloom

A Haskell implementation of a Bloom filter
Haskell
1
star
21

react-native-example-viewer

Component example viewer for React Native
JavaScript
1
star
22

dropbox-python

Fork of the Dropbox's Python API
Python
1
star
23

Marvin-Jabber-Client

This is a client for Jabber instant messenger servers that I wrote about 9 or 10 years ago. You probably don't want to use this, I just wanted to keep a hold of the code so I'm uploading it to Github.
Eiffel
1
star