Giter VIP home page Giter VIP logo

tiny-dedent's Introduction

tiny-dedent

Node NPM Travis David Coverage Status NPM

A Tiny module for stripping indentation from multi-line strings.

  • It's tiny! 150 bytes (gzipped)
  • No dependencies!
  • It's Fast!

Installation

yarn add tiny-dedent

NPM

npm install tiny-dedent

CDN

If you don't use a package manager, you can access tiny-dedent via unpkg (CDN), download the source, or point your package manager to the url.

Usage

import S from "tiny-dedent";

function usageExample() {
  const first = S(`A string that gets so long you need to break it over
                       multiple lines. Luckily tiny-dedent is here to keep it
                       readable without lots of spaces ending up in the string
                       itself.`);
A string that gets so long you need to break it over
multiple lines. Luckily tiny-dedent is here to keep it
readable without lots of spaces ending up in the string
itself.
const second = S(`
    Leading and trailing lines will be trimmed, so you can write something like
    this and have it work as you expect:

      * how convenient it is
      * that I can use an indented list
         - and still have it do the right thing

    That's all.
  `);
Leading and trailing lines will be trimmed, so you can write something like
this and have it work as you expect:

  * how convenient it is
  * that I can use an indented list
    - and still have it do the right thing

That's all.

Wait! I lied. Dedent can also be used as a function.

Motivation

I've been writting this replace regex over and over at the end of every template literal string, then I found there's a pretty popular package called dedent, which does exactly that. But I didn't feel like adding a dependency to do something that should be a macro, so I wrote this as a simple function.

This library is meant to be kept really really simple. It does not try to handle any edge cases, if you need something something more sophisticated take a look at the dedent package, it is around ~50 lines.

Why not a tagged function?

A tagged function has to manually make the interpolations, instead of running it natively. Passing the string it as regular function parameter leaves the hard work for the javascript engine instead of doing it in userland, in theory it should be faster, and you need ship less code.

Suggestions / Questions

File a issue on this repository

License

MIT

tiny-dedent's People

Contributors

victornpb 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.