依赖文件
This commit is contained in:
+4
@@ -0,0 +1,4 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "node"
|
||||
- "8"
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2017 Zhou Hancheng (z308114274@gmail.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
# Endent [![stability][stability-image]][stability-index]
|
||||
[![npm][npm-image]][npm-url]
|
||||
[![travis][travis-image]][travis-url]
|
||||
[![dm][dm-image]][npm-url]
|
||||
[![js-standard-style][code-style]][standard]
|
||||
|
||||
[stability-image]: https://img.shields.io/badge/stability-stable-green.svg
|
||||
[stability-index]: https://nodejs.org/api/documentation.html#documentation_stability_index
|
||||
[npm-image]: https://img.shields.io/npm/v/endent.svg?style=flat-square
|
||||
[npm-url]: https://www.npmjs.com/package/endent
|
||||
[travis-image]: https://img.shields.io/travis/indentjs/endent.svg?style=flat-square
|
||||
[travis-url]: https://travis-ci.org/indentjs/endent
|
||||
[dm-image]: http://img.shields.io/npm/dm/endent.svg?style=flat-square
|
||||
[code-style]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
|
||||
[standard]: https://github.com/feross/standard
|
||||
|
||||
An ES6 string tag that makes indentation right, adds some key features to [dedent](https://github.com/dmnd/dedent).
|
||||
|
||||
## Feature
|
||||
|
||||
### pretty object
|
||||
|
||||
```js
|
||||
import dedent from "dedent"
|
||||
import endent from "endent"
|
||||
|
||||
var someobj = {
|
||||
contact: {
|
||||
jack: 123456,
|
||||
tom: 654321
|
||||
},
|
||||
color: "blue"
|
||||
}
|
||||
|
||||
var somejson = '["bear", "fish", "dog", "cat"]'
|
||||
|
||||
var awfulTmpl = dedent`
|
||||
function store (state, emitter) {
|
||||
state["someobj"] = ${JSON.stringify(someobj, null, 2)}
|
||||
state["somejson"] = ${JSON.stringify(JSON.parse(somejson), null, 2)}
|
||||
}
|
||||
`
|
||||
// use endent.pretty(value) when value is object or array.
|
||||
var prettyTmpl = endent`
|
||||
function store (state, emitter) {
|
||||
state["someobj"] = ${endent.pretty(someobj)}
|
||||
state["somejson"] = ${somejson}
|
||||
}
|
||||
`
|
||||
|
||||
console.log(awfulTmpl + "\n\n" + prettyTmpl)
|
||||
```
|
||||
|
||||
```js
|
||||
// awfulTmpl
|
||||
function store (state, emitter) {
|
||||
state["someobj"] = {
|
||||
"contact": {
|
||||
"jack": 123456,
|
||||
"tom": 654321
|
||||
},
|
||||
"color": "blue"
|
||||
}
|
||||
state["somejson"] = [
|
||||
"bear",
|
||||
"fish",
|
||||
"dog"
|
||||
]
|
||||
}
|
||||
|
||||
// prettyTmpl
|
||||
function store (state, emitter) {
|
||||
state["someobj"] = {
|
||||
"contact": {
|
||||
"jack": 123456,
|
||||
"tom": 654321
|
||||
},
|
||||
"color": "blue"
|
||||
}
|
||||
state["somejson"] = [
|
||||
"bear",
|
||||
"fish",
|
||||
"dog",
|
||||
"cat"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### endows suitable indentation for multiline interpolation
|
||||
|
||||
```js
|
||||
var dependencies = ['jquery', 'underscore', 'bootstrap']
|
||||
var dependencyTmpl = ``
|
||||
dependencies.forEach((d, i) => {
|
||||
dependencyTmpl += `var ${d} = require("${d}")\n`
|
||||
})
|
||||
|
||||
var awfulTmpl = dedent`
|
||||
;(function () {
|
||||
${dependencyTmpl}
|
||||
})()
|
||||
`
|
||||
|
||||
var prettyTmpl = endent`
|
||||
;(function () {
|
||||
${dependencyTmpl}
|
||||
})()
|
||||
`
|
||||
|
||||
console.log(awfulTmpl + "\n\n" + prettyTmpl)
|
||||
```
|
||||
|
||||
```js
|
||||
// awfulTmpl
|
||||
;(function () {
|
||||
var jquery = require('jquery')
|
||||
var underscore = require('underscore')
|
||||
var bootstrap = require('bootstrap')
|
||||
})()
|
||||
|
||||
|
||||
// prettyTmpl
|
||||
;(function () {
|
||||
var jquery = require('jquery')
|
||||
var underscore = require('underscore')
|
||||
var bootstrap = require('bootstrap')
|
||||
})()
|
||||
|
||||
```
|
||||
## License
|
||||
|
||||
MIT
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
const dedent = require('dedent')
|
||||
const objectorarray = require('objectorarray')
|
||||
const parse = require('fast-json-parse')
|
||||
|
||||
module.exports = endent
|
||||
|
||||
const ENDENT_ID = 'twhZNwxI1aFG3r4'
|
||||
|
||||
function endent (strings, ...values) {
|
||||
strings = [].concat(strings)
|
||||
|
||||
let result = ''
|
||||
|
||||
for (let i = 0; i < strings.length; i++) {
|
||||
result += strings[i]
|
||||
|
||||
if (i < values.length) {
|
||||
let value = values[i]
|
||||
let isJson = false
|
||||
|
||||
if (parse(value).value) {
|
||||
value = parse(value).value
|
||||
isJson = true
|
||||
}
|
||||
|
||||
if ((value && value[ENDENT_ID]) || isJson) {
|
||||
let rawlines = result.split('\n')
|
||||
let l = rawlines[rawlines.length - 1].search(/\S/)
|
||||
let endentation = l > 0 ? ' '.repeat(l) : ''
|
||||
let valueJson = isJson ? JSON.stringify(value, null, 2) : value[ENDENT_ID]
|
||||
let valueLines = valueJson.split('\n')
|
||||
|
||||
valueLines.forEach((l, index) => {
|
||||
if (index > 0) {
|
||||
result += ('\n' + endentation + l)
|
||||
} else {
|
||||
result += l
|
||||
}
|
||||
})
|
||||
} else if (typeof value === 'string' && value.includes('\n')) {
|
||||
let endentations = result.match(/(?:^|\n)( *)$/)
|
||||
|
||||
if (typeof value === 'string') {
|
||||
let endentation = endentations ? endentations[1] : ''
|
||||
result += value
|
||||
.split('\n')
|
||||
.map((str, i) => {
|
||||
str = (ENDENT_ID + str)
|
||||
return i === 0 ? str : `${endentation}${str}`
|
||||
})
|
||||
.join('\n')
|
||||
} else {
|
||||
result += value
|
||||
}
|
||||
} else {
|
||||
result += value
|
||||
}
|
||||
}
|
||||
}
|
||||
result = dedent(result)
|
||||
return result.split(ENDENT_ID).join('')
|
||||
}
|
||||
|
||||
endent.pretty = (data) => {
|
||||
return objectorarray(data) ? {[ENDENT_ID]: JSON.stringify(data, null, 2)} : data
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"name": "endent",
|
||||
"version": "1.4.1",
|
||||
"description": "➡️ An ES6 string tag that makes indentation right",
|
||||
"main": "endent.js",
|
||||
"scripts": {
|
||||
"test": "standard && tape test.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/ZhouHansen/endent.git"
|
||||
},
|
||||
"author": {
|
||||
"name": "zhouhancheng",
|
||||
"email": "z308114274@gmail.com"
|
||||
},
|
||||
"keywords": [
|
||||
"dedent",
|
||||
"tag",
|
||||
"multi-line string"
|
||||
],
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/ZhouHansen/endent/issues"
|
||||
},
|
||||
"homepage": "https://github.com/ZhouHansen/endent#readme",
|
||||
"dependencies": {
|
||||
"dedent": "^0.7.0",
|
||||
"fast-json-parse": "^1.0.3",
|
||||
"objectorarray": "^1.0.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"standard": "^11.0.1",
|
||||
"tape": "^4.8.0"
|
||||
}
|
||||
}
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
var test = require('tape')
|
||||
var endent = require('./')
|
||||
|
||||
test('object', t => {
|
||||
t.plan(1)
|
||||
|
||||
var json = JSON.stringify(JSON.parse('[ "abc" ]'), null, 2)
|
||||
|
||||
var someobj = {
|
||||
contact: {
|
||||
jack: '123456',
|
||||
tom: '654321'
|
||||
},
|
||||
name: 'template',
|
||||
color: 'blue',
|
||||
animals: ['bear', 'fish', 'dog', 'cat']
|
||||
}
|
||||
|
||||
var colors = ['red', 'pink', 'white']
|
||||
var objectName = 'someobj'
|
||||
|
||||
var dependencies = ['jquery', 'underscore', 'bootstrap']
|
||||
var dependencyTmpl = ``
|
||||
|
||||
dependencies.forEach((d, i) => {
|
||||
dependencyTmpl += `var ${d} = require("${d}")\n`
|
||||
})
|
||||
|
||||
var jsFile = endent`
|
||||
${dependencyTmpl}
|
||||
module.exports = store
|
||||
|
||||
function store (state, emitter) {
|
||||
emitter.on("DOMContentLoaded", function () {
|
||||
state["json"] = ${json}
|
||||
state["${objectName}"] = ${endent.pretty(someobj)}
|
||||
state["colors"] = ${endent.pretty(colors)}
|
||||
state["name"] = "${endent.pretty('jack')}"
|
||||
state["name2"] = "${'tom'}"
|
||||
state["number"] = ${endent.pretty(123)}
|
||||
state["number2"] = ${123}
|
||||
state["Iamundefined"] = ${endent.pretty()}
|
||||
state["Iamnull"] = ${endent.pretty(null)}
|
||||
state["Iamregexp"] = ${endent.pretty(/abc/)}
|
||||
})
|
||||
}
|
||||
`
|
||||
|
||||
t.equal(jsFile, `var jquery = require("jquery")
|
||||
var underscore = require("underscore")
|
||||
var bootstrap = require("bootstrap")
|
||||
|
||||
module.exports = store
|
||||
|
||||
function store (state, emitter) {
|
||||
emitter.on("DOMContentLoaded", function () {
|
||||
state["json"] = [
|
||||
"abc"
|
||||
]
|
||||
state["someobj"] = {
|
||||
"contact": {
|
||||
"jack": "123456",
|
||||
"tom": "654321"
|
||||
},
|
||||
"name": "template",
|
||||
"color": "blue",
|
||||
"animals": [
|
||||
"bear",
|
||||
"fish",
|
||||
"dog",
|
||||
"cat"
|
||||
]
|
||||
}
|
||||
state["colors"] = [
|
||||
"red",
|
||||
"pink",
|
||||
"white"
|
||||
]
|
||||
state["name"] = "jack"
|
||||
state["name2"] = "tom"
|
||||
state["number"] = 123
|
||||
state["number2"] = 123
|
||||
state["Iamundefined"] = undefined
|
||||
state["Iamnull"] = null
|
||||
state["Iamregexp"] = /abc/
|
||||
})
|
||||
}`)
|
||||
})
|
||||
|
||||
test('string', t => {
|
||||
t.plan(1)
|
||||
const a = `
|
||||
hello
|
||||
world`
|
||||
const b = endent`
|
||||
foo.
|
||||
${a}
|
||||
bar.`
|
||||
t.equal(b, `foo.
|
||||
|
||||
hello
|
||||
world
|
||||
bar.`)
|
||||
})
|
||||
|
||||
test('issue#1', t => {
|
||||
t.plan(1)
|
||||
const a = '"test"'
|
||||
const r = endent`
|
||||
{
|
||||
${a}: null
|
||||
}
|
||||
`
|
||||
t.equal(r, `{
|
||||
"test": null
|
||||
}`)
|
||||
})
|
||||
|
||||
test('issue#2', t => {
|
||||
t.plan(1)
|
||||
|
||||
const r = endent`
|
||||
foo.
|
||||
x=${'hello\n world'}
|
||||
bar.
|
||||
`
|
||||
console.log(r)
|
||||
t.equal(r, `foo.
|
||||
x=hello
|
||||
world
|
||||
bar.`)
|
||||
})
|
||||
|
||||
test('tab', t => {
|
||||
t.plan(1)
|
||||
t.equal(endent`foo\tbar`, 'foo\tbar')
|
||||
})
|
||||
Reference in New Issue
Block a user