GithubHelp home page GithubHelp logo

theredoc's Introduction

theredoc

Build Status

A little template tag function that strips the leading indent from multi-line ES string templates. It also strips the first and last lines if they're just whitespace. If you're using template strings to write heredocs, you should try tagging them with theredoc to tidy them up a bit!

Install

$ npm install theredoc

Usage

const theredoc = require('theredoc')

function some () {
  function deeply () {
    function nested () {
      function code () {
        console.log(theredoc`
          I want to write multipline lines
          but don't want to mess up my indenting.
            Ok?
        `)
      }
    }
  }
}

Will look tidy in the code listing but output no superfluous indents:

I want to write multipline lines
but don't want to mess up my indenting.
  Ok?

Questions

Why should I use this?

Because good messages are often long messages, but out-of-the-box, ES template strings don't format multi-line error and console messages gracefully.

If you're familiar with """-style heredocs from CoffeeScript or the <<~-style heredoc from Ruby 2.3, you might appreciate being able to write multi-line strings with an indentation level that matches the surrounding code listing without indenting the resulting string itself.

For example, if you want to print a multi-line error message without superfluous indentation in the following situation, an unadorned template string would have to be outdented awkwardly:

        //...
      } catch (e) {
        console.error(`
Something bad happened.

  Message: ${e.message}
        `)
      }
      //...

Additionally, the newlines added to retain the formatting of the function call will be retained in the output, which you probably also don't want.

Instead, theredoc fixes both those issues by letting you write this:

        //...
      } catch (e) {
        console.error(theredoc`
          Something bad happened.

            Message: ${e.message}
        `)
      }
      //...

And the resulting console output will be:

Something bad happened.

  Message: LOL errors

With the leading 10 spaces stripped from each line and (since they only contain whitespace) having stripped the first and last lines of the template string.

If you still don't think this is nifty, I don't know what to tell you!

Why not use common-tags?

The very cool and fancy library common-tags can accomplish the same thing with the stripIndent function it exports, however there are a few downsides:

  • It will trim all leading and trailing whitespace, which one might want to preserve
  • It has a runtime dependency on babel-runtime, which drastically increases the size of its install and, because babel-runtime depends on core-js, sets a global object that might be more appropriate for an app to set than an intermediate lib
  • While common-tags is really spiffy and you should check it out to learn about all the cool transforms you can do, 99.9% of the time I really just want indent-stripping heredocs

What about tabs (\t)?

Theredoc only deals with space character indentation, sorry!

A pull request to support tabs would be considered if (a) it didn't drastically increase the module's complexity, and (b) deferred to counting spaces in the event that the given string had some lines indented with spaces and other with \t tabs.

theredoc's People

Contributors

searls avatar rosston avatar

Stargazers

David Wells avatar Brannon Hall avatar smithart avatar Roman Hossain Shaon avatar Cat  avatar Daniel Sousa avatar Matt Petty avatar Corey Ward avatar Cyril Duchon-Doris avatar Nicolas Oga avatar Arne avatar Ze-Hao Wang (Salmon) avatar Mark Allen avatar elquimista avatar Dan McGrath avatar Ger Hobbelt avatar daniel sieradski avatar Jamie Wilson avatar Daniel Sokil avatar Jacob Beltran avatar  avatar Jason La avatar Kaleb Lape avatar  avatar Cagan Sit avatar John Van Horn avatar Taylor Smith avatar Michael Mior avatar Thibaut Renoncourt avatar Tom Leenders avatar Ron Waldon-Howe avatar Christian avatar Basil Siddiqui avatar Justin Sisley avatar Bruno Oliveira avatar Marcel Mokoš avatar Karacas avatar Glavin Wiechert avatar Clay Dunston avatar Zach Holman avatar Simon Escobar Benitez avatar Simon avatar Louis-Rémi Babé avatar Russell Daly avatar Thomas Gaudin avatar David Sheldrick avatar David Furnes avatar Roman Minchyn avatar Sunny avatar Adam Luikart avatar Jérôme Macias avatar Alasdair Smith avatar Cyd La Luz avatar Hans-Kristian Koren avatar Adam Sanderson avatar  avatar Bryan Lim avatar Jiří Zelinka avatar Matthieu Lemoine avatar Fabian Mersch avatar Martin O'Grady avatar Hiroki Sato avatar Wonyoung Ju avatar Gaurav Mehla avatar Anderson Dourado avatar Ian Walter avatar Lily Skye avatar Mars Hall avatar Saneef Ansari avatar Tom Meiselbach avatar Travis LaDuke avatar Gadzhi Gadzhiev avatar Yacine Hmito avatar Mike avatar Kevin Old avatar Tyler Gaw avatar Sam Stephenson avatar Iain Reddick avatar Jo Sprague avatar

Watchers

Todd Kaufman avatar Brad Jones avatar  avatar James Cloos avatar  avatar

theredoc's Issues

Add typescript headers

When writing in typescript :

Could not find a declaration file for module 'theredoc'. '/Users/me/[project]/node_modules/theredoc/index.js' implicitly has an 'any' type.
Try npm i --save-dev @types/theredoc if it exists or add a new declaration (.d.ts) file containing declare module 'theredoc';ts(7016)

ES6 syntax

The usage of ES6 syntax in this lib is breaking in UglifyJS when bundling in webpack

ERROR in app-7d11380495e59eb43349.js from UglifyJs
SyntaxError: Name expected [app-7d11380495e59eb43349.js:132037,46]

And would fail to be parsed in old browsers

Babel plugin?

It'd be awesome to have this with no runtime cost. Could it be done as a babel plugin?

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.