Skip to content

Commit 2595288

Browse files
committed
initial commit
0 parents  commit 2595288

12 files changed

+488
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"stage": 0
3+
}

.gitignore

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Created by https://www.gitignore.io/api/node
2+
3+
### Node ###
4+
# Logs
5+
logs
6+
*.log
7+
npm-debug.log*
8+
9+
src
10+
11+
# Runtime data
12+
pids
13+
*.pid
14+
*.seed
15+
16+
# Directory for instrumented libs generated by jscoverage/JSCover
17+
lib-cov
18+
19+
# Coverage directory used by tools like istanbul
20+
coverage
21+
22+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
23+
.grunt
24+
25+
# node-waf configuration
26+
.lock-wscript
27+
28+
# Compiled binary addons (http://nodejs.org/api/addons.html)
29+
build/Release
30+
31+
# Dependency directory
32+
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
33+
node_modules

.istanbul.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
instrumentation:
2+
root: lib

.travis.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: node_js
2+
node_js:
3+
- "0.12"
4+
- "0.11"
5+
- "0.10"
6+
- "iojs"
7+
after_script: npm run coveralls

lib/.DS_Store

6 KB
Binary file not shown.

lib/index.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
2+
export default function tags(opts) {
3+
const settings = {
4+
trim: true,
5+
oneLine: false,
6+
stripIndent: false,
7+
includeArrays: false,
8+
...opts
9+
}
10+
// return a tag function that transforms our template
11+
return function tag(template, ...expressions) {
12+
// join the parts necessary to re-construct the template
13+
let temp = template.reduce((accumulator, part, i) => {
14+
let expression = expressions[i - 1];
15+
if (settings.includeArrays && Array.isArray(expression)) {
16+
const sep = settings.includeArrays.separator || '';
17+
const con = settings.includeArrays.conjunction;
18+
// inline arrays, making sure to include item separator
19+
expression = expression.join(sep + accumulator.match(/(\s+)$/)[1]);
20+
if (con) {
21+
// replace the last separator with the conjunction
22+
const sepIndex = expression.lastIndexOf(sep);
23+
expression = expression.substr(0, sepIndex) + ' ' + con + expression.substr(sepIndex + 1);
24+
}
25+
}
26+
return accumulator + expression + part;
27+
});
28+
// replace any newlines with spaces if we just want
29+
// a one liner
30+
if (settings.oneLine) temp = temp.replace(/(?:\s+)/g, ' ');
31+
if (settings.stripIndent) {
32+
// strip leading indents
33+
const match = temp.match(/^[ \t]*(?=\S)/gm);
34+
const indent = Math.min.apply(Math, match.map(el => el.length));
35+
const regexp = new RegExp('^[ \\t]{' + indent + '}', 'gm');
36+
temp = indent > 0 ? temp.replace(regexp, '') : temp;
37+
}
38+
// trim leading and trailing whitespace
39+
if (settings.trim) temp = temp.trim();
40+
return temp;
41+
}
42+
}
43+
44+
export const html = tags({
45+
stripIndent: true,
46+
includeArrays: true
47+
});
48+
49+
export const oneLine = tags({
50+
oneLine: true
51+
});
52+
53+
export const inlineLists = tags({
54+
includeArrays: true
55+
});
56+
57+
export const stripIndent = tags({
58+
stripIndent: true
59+
});
60+
61+
export const commaLists = tags({
62+
includeArrays: {
63+
separator: ','
64+
}
65+
});
66+
67+
export const commaListsOr = tags({
68+
includeArrays: {
69+
separator: ',',
70+
conjunction: 'or'
71+
}
72+
});
73+
74+
export const commaListsAnd = tags({
75+
includeArrays: {
76+
separator: ',',
77+
conjunction: 'and'
78+
}
79+
});
80+
81+
export const oneLineCommaLists = tags({
82+
includeArrays: {
83+
separator: ','
84+
},
85+
oneLine: true
86+
});
87+
88+
export const oneLineCommaListsOr = tags({
89+
includeArrays: {
90+
separator: ',',
91+
conjunction: 'or'
92+
},
93+
oneLine: true
94+
});
95+
96+
export const oneLineCommaListsAnd = tags({
97+
includeArrays: {
98+
separator: ',',
99+
conjunction: 'and'
100+
},
101+
oneLine: true
102+
});

license.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
License (MIT)
2+
-------------
3+
4+
Copyright © Declan de Wet
5+
6+
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:
7+
8+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9+
10+
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.

package.json

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"name": "common-tags",
3+
"description": "a few common utility template tags for ES2015",
4+
"version": "0.0.0",
5+
"keywords": [
6+
"es2015",
7+
"es6",
8+
"babel",
9+
"template",
10+
"tag",
11+
"tagged",
12+
"literal",
13+
"string",
14+
"strings",
15+
"multi",
16+
"single",
17+
"one",
18+
"line",
19+
"multiline",
20+
"singleline",
21+
"oneline",
22+
"es6-tag",
23+
"es2015-tag",
24+
"strip",
25+
"indent",
26+
"indents",
27+
"normalize",
28+
"array"
29+
],
30+
"scripts": {
31+
"release": "npm publish",
32+
"prerelease": "mv lib src && babel src -d lib --stage 0",
33+
"postrelease": "rm -rf lib && mv src lib",
34+
"test": "mocha",
35+
"coverage": "babel-node ./node_modules/.bin/isparta cover _mocha",
36+
"coveralls": "coveralls < coverage/lcov.info",
37+
"precoveralls": "npm run coverage"
38+
},
39+
"main": "lib",
40+
"license": "MIT",
41+
"repository": {
42+
"type": "git",
43+
"url": "https://github.com/declandewet/common-tags"
44+
},
45+
"bugs": {
46+
"url": "http://github.com/declandewet/common-tags/issues",
47+
"email": "[email protected]"
48+
},
49+
"directories": {
50+
"lib": "lib"
51+
},
52+
"devDependencies": {
53+
"babel": "^5.8.23",
54+
"chai": "^3.4.0",
55+
"coveralls": "^2.11.4",
56+
"isparta": "^3.1.0",
57+
"mocha": "^2.3.3"
58+
}
59+
}

0 commit comments

Comments
 (0)