Comparing version 0.1.1 to 0.2.0
"use strict"; | ||
function dedent() { | ||
var strings = arguments[0]; | ||
var result = ""; | ||
for (var i = 0; i < strings.length; i++) { | ||
var next = strings.raw[i]. | ||
replace(/\\\n[ \t]*/g, ""). | ||
replace(/\n[ \t]*/g, "\n"); | ||
result += next; | ||
if (i + 1 < arguments.length) { | ||
result += arguments[i + 1]; | ||
function dedent(strings, ...values) { | ||
// first, perform interpolation | ||
let result = ""; | ||
for (let i = 0; i < strings.length; i++) { | ||
// join lines when there is a suppressed newline | ||
result += strings.raw[i].replace(/\\\n[ \t]*/g, ""); | ||
if (i < values.length) { | ||
result += values[i]; | ||
} | ||
} | ||
return result; | ||
// now strip indentation | ||
let lines = result.trim().split("\n"); | ||
let mindent = null; | ||
lines.forEach(l => { | ||
if (m = l.match(/^ +/)) { | ||
let indent = m[0].length; | ||
if (!mindent) { | ||
// this is the first indented line | ||
mindent = indent; | ||
} else { | ||
mindent = Math.min(mindent, indent); | ||
} | ||
} | ||
}); | ||
if (mindent === null) return result; | ||
return lines.map(l => l[0] === " " ? l.slice(mindent) : l).join("\n"); | ||
}; | ||
module.exports = dedent; |
{ | ||
"name": "dedent", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "An ES6 string tag that strips indentation from multi-line strings", | ||
@@ -5,0 +5,0 @@ "main": "dedent.js", |
@@ -11,12 +11,39 @@ # Dedent | ||
function usageExample() { | ||
return 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.`; | ||
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.`; | ||
let second = dd` | ||
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. | ||
`; | ||
return first + "\n\n" + second; | ||
} | ||
``` | ||
usageExample().indexOf(' '); //=> -1 | ||
``` | ||
> console.log(usageExample()); | ||
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. | ||
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. | ||
``` | ||
## License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3995
29
49