trim-margin
Advanced tools
Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "trim-margin", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "\"trimMargin\" like Kotlin and \"stripMargin\" like Scala.", | ||
@@ -5,0 +5,0 @@ "main": "src/js/index.js", |
# trim-margin | ||
`trimMargin` like Kotlin and `stripMargin` like Scala. | ||
## Features | ||
* Indent in a string literal. | ||
* Selectable APIs: `trimMargin` or `stripMargin`. | ||
* Use as tagged template literals. | ||
* Inject to string type. | ||
## install | ||
``` | ||
$ npm i -S trim-margin | ||
``` | ||
## Usage | ||
```js | ||
const { | ||
trimMargin, | ||
tm, | ||
inject, | ||
} = require("trim-margin"); | ||
console.log(trimMargin(` | ||
|trim | ||
| indent | ||
| spaces`)); | ||
// => "\ntrim\n indent\n spaces | ||
const template = ` | template`; | ||
const literal = ` | literal`; | ||
console.log(tm`\ | ||
|tagged | ||
${template} | ||
|${literal}`); | ||
// => "tagged\n template\n | literal" | ||
inject(); | ||
console.log(`\ | ||
|inject | ||
| to | ||
| string`.trimMargin()); | ||
// => "inject\n to\n string" | ||
``` | ||
## API | ||
### trimMargin(str, [delimiter]) | ||
Trim indent spaces. | ||
#### str | ||
Indented string. | ||
#### delimiter | ||
Indent delimiter. | ||
This is used as an argument to a `RegExp` object. | ||
defalut: `"\\|"` | ||
### stripMargin(str, [delimiter]) | ||
Same `trimMargin`. | ||
### tm | ||
Use as Tagged template literals. | ||
Same `trimMargin(literal)`. | ||
### sm | ||
Same `tm`. | ||
### inject | ||
Inject to `string.prototype`: `trimMargin` and `stripMargin`. | ||
You can use it like method of string type. | ||
### injectTrimMargin | ||
Inject to `string.prototype`: `trimMargin`. | ||
You can use it like method of string type. | ||
### injectStripMargin | ||
Inject to `string.prototype`: `stripMargin`. | ||
You can use it like method of string type. | ||
### injectAt(methodName) | ||
Inject to `string.prototype`. | ||
You can use it like method of string type. | ||
#### methodName | ||
Method name injected into string type. | ||
## License | ||
MIT © tee-talog | ||
@@ -25,4 +25,5 @@ const { defalutDelimiter } = require("./config"); | ||
} | ||
const marged = strings.reduce((prev, current, index) => [prev, values[index - 1], current]); | ||
return stripMargin((typeof marged === "string") ? marged : marged.join("")); | ||
return stripMargin( | ||
strings.reduce( | ||
(prev, current, index) => prev + values[index - 1] + current)); | ||
}; | ||
@@ -29,0 +30,0 @@ |
@@ -8,7 +8,30 @@ const { | ||
const methodName = "testMethod"; | ||
const deleteInjectedMethods = () => { | ||
String.prototype.stripMargin = undefined; | ||
String.prototype.trimMargin = undefined; | ||
String.prototype[methodName] = undefined; | ||
}; | ||
describe("inject", () => { | ||
test("NOP", () => { | ||
expect("").toBe(""); | ||
beforeEach(() => { | ||
inject(); | ||
injectTo(methodName); | ||
}); | ||
afterEach(() => { | ||
deleteInjectedMethods(); | ||
}); | ||
test("inject stripMargin", () => { | ||
expect(`a`.stripMargin()).toBe("a"); | ||
}); | ||
test("inject trimMargin", () => { | ||
expect(`a`.trimMargin()).toBe("a"); | ||
}); | ||
test("inject original method name", () => { | ||
expect(`a`[methodName]()).toBe("a"); | ||
}); | ||
}); | ||
@@ -51,4 +51,27 @@ const { stripMargin, sm } = require("../js/stripMargin"); | ||
describe("sm tag", () => { | ||
test("non-embedded", () => { | ||
expect(sm` | ||
|a | ||
|`).toBe("\na\n"); | ||
}); | ||
describe("indented embedded variable", () => { | ||
test("first variable", () => { | ||
const aaa = ` |aaa`; | ||
const bbb = ` | bbb`; | ||
expect(sm`${aaa} | ||
|${bbb} | ||
|ccc`) | ||
.toBe("aaa\n | bbb\nccc"); | ||
}); | ||
test("last variable", () => { | ||
const aaa = ` |aaa`; | ||
const bbb = ` | bbb`; | ||
expect(sm`\ | ||
${aaa} | ||
|${bbb}`) | ||
.toBe("aaa\n | bbb"); | ||
}); | ||
}); | ||
}); | ||
7292
169
102