1 | # Endent [![stability][stability-image]][stability-index]
|
2 |
|
3 | [![npm][npm-image]][npm-url]
|
4 | [![travis][travis-image]][travis-url]
|
5 | [![dm][dm-image]][npm-url]
|
6 | [![js-standard-style][code-style]][standard]
|
7 |
|
8 | [stability-image]: https://img.shields.io/badge/stability-stable-green.svg
|
9 | [stability-index]: https://nodejs.org/api/documentation.html#documentation_stability_index
|
10 | [npm-image]: https://img.shields.io/npm/v/endent.svg?style=flat-square
|
11 | [npm-url]: https://www.npmjs.com/package/endent
|
12 | [travis-image]: https://img.shields.io/travis/indentjs/endent.svg?style=flat-square
|
13 | [travis-url]: https://travis-ci.org/indentjs/endent
|
14 | [dm-image]: http://img.shields.io/npm/dm/endent.svg?style=flat-square
|
15 | [code-style]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
|
16 | [standard]: https://github.com/feross/standard
|
17 |
|
18 | An ES6 string tag that makes indentation right, adds some key features to [dedent](https://github.com/dmnd/dedent).
|
19 |
|
20 | ## Feature
|
21 |
|
22 | ### Pretty object
|
23 |
|
24 | ```ts
|
25 | import dedent from "dedent";
|
26 | import endent from "endent";
|
27 |
|
28 | var someobj = {
|
29 | contact: {
|
30 | jack: 123456,
|
31 | tom: 654321,
|
32 | },
|
33 | color: "blue",
|
34 | };
|
35 |
|
36 | var somejson = '["bear", "fish", "dog", "cat"]';
|
37 |
|
38 | var awfulTmpl = dedent`
|
39 | function store (state, emitter) {
|
40 | state["someobj"] = ${JSON.stringify(someobj, null, 2)}
|
41 | state["somejson"] = ${JSON.stringify(JSON.parse(somejson), null, 2)}
|
42 | }
|
43 | `;
|
44 | // use endent.pretty(value) when value is object or array.
|
45 | var prettyTmpl = endent`
|
46 | function store (state, emitter) {
|
47 | state["someobj"] = ${endent.pretty(someobj)}
|
48 | state["somejson"] = ${somejson}
|
49 | }
|
50 | `;
|
51 |
|
52 | console.log(awfulTmpl + "\n\n" + prettyTmpl);
|
53 | ```
|
54 |
|
55 | ```ts
|
56 | // awfulTmpl
|
57 | function store(state, emitter) {
|
58 | state["someobj"] = {
|
59 | contact: {
|
60 | jack: 123456,
|
61 | tom: 654321,
|
62 | },
|
63 | color: "blue",
|
64 | }
|
65 | state["somejson"] = [
|
66 | "bear",
|
67 | "fish",
|
68 | "dog",
|
69 | "cat"
|
70 | ]
|
71 | }
|
72 |
|
73 | // prettyTmpl
|
74 | function store(state, emitter) {
|
75 | state["someobj"] = {
|
76 | contact: {
|
77 | jack: 123456,
|
78 | tom: 654321,
|
79 | },
|
80 | color: "blue",
|
81 | }
|
82 | state["somejson"] = [
|
83 | "bear",
|
84 | "fish",
|
85 | "dog",
|
86 | "cat"
|
87 | ]
|
88 | }
|
89 | ```
|
90 |
|
91 | ### Endows suitable indentation for multiline interpolation
|
92 |
|
93 | ```ts
|
94 | var dependencies = ["jquery", "underscore", "bootstrap"];
|
95 | var dependencyTmpl = ``;
|
96 | dependencies.forEach((d, i) => {
|
97 | dependencyTmpl += `var ${d} = require("${d}")\n`;
|
98 | });
|
99 |
|
100 | var awfulTmpl = dedent`
|
101 | ;(function () {
|
102 | ${dependencyTmpl}
|
103 | })()
|
104 | `;
|
105 |
|
106 | var prettyTmpl = endent`
|
107 | ;(function () {
|
108 | ${dependencyTmpl}
|
109 | })()
|
110 | `;
|
111 |
|
112 | console.log(awfulTmpl + "\n\n" + prettyTmpl);
|
113 | ```
|
114 |
|
115 | ```js
|
116 | // awfulTmpl
|
117 | (function () {
|
118 | var jquery = require("jquery");
|
119 | var underscore = require("underscore");
|
120 | var bootstrap = require("bootstrap");
|
121 | })();
|
122 |
|
123 | // prettyTmpl
|
124 | (function () {
|
125 | var jquery = require("jquery");
|
126 | var underscore = require("underscore");
|
127 | var bootstrap = require("bootstrap");
|
128 | })();
|
129 | ```
|
130 |
|
131 | ## License
|
132 |
|
133 | MIT
|