dedent
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -8,7 +8,15 @@ "use strict"; | ||
var raw = undefined; | ||
if (typeof strings === "string") { | ||
// dedent can be used as a plain function | ||
raw = [strings]; | ||
} else { | ||
raw = strings.raw; | ||
} | ||
// first, perform interpolation | ||
var result = ""; | ||
for (var i = 0; i < strings.length; i++) { | ||
for (var i = 0; i < raw.length; i++) { | ||
// join lines when there is a suppressed newline | ||
result += strings.raw[i].replace(/\\\n[ \t]*/g, ""); | ||
result += raw[i].replace(/\\\n[ \t]*/g, ""); | ||
if (i < values.length) { | ||
@@ -19,8 +27,11 @@ result += values[i]; | ||
// dedent eats leading and trailing whitespace too | ||
result = result.trim(); | ||
// now strip indentation | ||
var lines = result.trim().split("\n"); | ||
var lines = result.split("\n"); | ||
var mindent = null; | ||
lines.forEach(function (l) { | ||
var m = undefined; | ||
if (m = l.match(/^ +/)) { | ||
var m = l.match(/^ +/); | ||
if (m) { | ||
var indent = m[0].length; | ||
@@ -35,8 +46,9 @@ if (!mindent) { | ||
}); | ||
if (mindent === null) return result; | ||
return lines.map(function (l) { | ||
if (mindent === null) { | ||
return result; | ||
}return lines.map(function (l) { | ||
return l[0] === " " ? l.slice(mindent) : l; | ||
}).join("\n"); | ||
}; | ||
} | ||
module.exports = dedent; |
{ | ||
"name": "dedent", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "An ES6 string tag that strips indentation from multi-line strings", | ||
@@ -31,13 +31,15 @@ "main": "dist/dedent.js", | ||
"devDependencies": { | ||
"6to5": "^2.11.3", | ||
"6to5-jest": "^1.0.0", | ||
"babel-eslint": "^2.0.2", | ||
"babel-jest": "^5.0.1", | ||
"eslint": "^0.18.0", | ||
"gulp": "^3.8.10", | ||
"gulp-6to5": "^2.0.2", | ||
"gulp-babel": "^5.1.0", | ||
"jest-cli": "^0.2.1" | ||
}, | ||
"scripts": { | ||
"test": "jest" | ||
"test": "jest", | ||
"lint": "eslint dedent.js __tests__" | ||
}, | ||
"jest": { | ||
"scriptPreprocessor": "<rootDir>/node_modules/6to5-jest", | ||
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest", | ||
"testFileExtensions": [ | ||
@@ -44,0 +46,0 @@ "js" |
@@ -8,10 +8,11 @@ # Dedent | ||
```js | ||
var dd = require('dedent'); | ||
import dedent from "dedent"; | ||
function usageExample() { | ||
let first = dd`A string that gets so long you need to break it over multiple | ||
lines. Luckily dedent is here to keep it readable without | ||
lots of spaces ending up in the string itself.`; | ||
const first = dedent`A string that gets so long you need to break it over | ||
multiple lines. Luckily dedent is here to keep it | ||
readable without lots of spaces ending up in the string | ||
itself.`; | ||
let second = dd` | ||
const second = dedent` | ||
Leading and trailing lines will be trimmed, so you can write something like | ||
@@ -27,3 +28,7 @@ this and have it work as you expect: | ||
return first + "\n\n" + second; | ||
const third = dedent(` | ||
Wait! I lied. Dedent can also be used as a function. | ||
`); | ||
return first + "\n\n" + second + "\n\n" third; | ||
} | ||
@@ -37,5 +42,6 @@ ``` | ||
``` | ||
A string that gets so long you need to break it over multiple | ||
lines. Luckily 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 dedent is here to keep it | ||
readable without lots of spaces ending up in the string | ||
itself. | ||
@@ -50,2 +56,4 @@ Leading and trailing lines will be trimmed, so you can write something like | ||
That's all. | ||
Wait! I lied. Dedent can also be used as a function. | ||
``` | ||
@@ -52,0 +60,0 @@ |
4800
44
60
6