Skip to content

Commit 628d0dc

Browse files
committed
init
0 parents  commit 628d0dc

15 files changed

+326
-0
lines changed

.editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[*.yml]
11+
indent_style = space
12+
indent_size = 2

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.github/funding.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
patreon: abranhe
2+
open_collective: abranhe
3+
custom: https://cash.me/$abranhe

.github/workflows/ci.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: build
2+
on: [push]
3+
4+
jobs:
5+
test:
6+
name: Node.js ${{ matrix.node_version }}
7+
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
node_version: [8, 10, 12]
11+
12+
steps:
13+
- uses: actions/checkout@master
14+
- name: Use Node.js ${{ matrix.node_version }}
15+
uses: actions/setup-node@v1
16+
with:
17+
version: ${{ matrix.node_version }}
18+
- name: Test
19+
run: |
20+
npm install
21+
npm test

.github/workflows/publish.yml

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: publish
2+
3+
on:
4+
push:
5+
tags:
6+
- v*
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v1
13+
- uses: actions/setup-node@v1
14+
with:
15+
node-version: 12
16+
- name: Install dependencies
17+
run: npm install
18+
- name: Build
19+
run: |
20+
npm run build
21+
npm pack
22+
mv *.tgz package.tgz
23+
- name: Upload artifact
24+
uses: actions/upload-artifact@v1
25+
with:
26+
name: artifact
27+
path: package.tgz
28+
29+
publish-npm:
30+
name: Publish to npm
31+
needs: build
32+
runs-on: ubuntu-latest
33+
steps:
34+
- name: Download artifact
35+
uses: actions/download-artifact@v1
36+
with:
37+
name: artifact
38+
- name: Extract package from artifact
39+
run: tar xf artifact/package.tgz --strip 1
40+
- uses: actions/setup-node@v1
41+
with:
42+
node-version: 12
43+
registry-url: https://registry.npmjs.org/
44+
- run: npm publish
45+
env:
46+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
47+
48+
publish-gpr:
49+
name: Publish to GH Registry
50+
needs: build
51+
runs-on: ubuntu-latest
52+
steps:
53+
- name: Download artifact
54+
uses: actions/download-artifact@v1
55+
with:
56+
name: artifact
57+
- name: Extract package from artifact
58+
run: tar xf artifact/package.tgz --strip 1
59+
- uses: actions/setup-node@v1
60+
with:
61+
node-version: 12
62+
registry-url: https://npm.pkg.github.com/
63+
scope: '@abranhe'
64+
- name: Rename package to match GitHub repository
65+
run: |-
66+
sed -i"" -E "s|\"name\": \".*\"|\"name\": \"@${GITHUB_REPOSITORY}\"|" package.json
67+
- run: echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > ~/.npmrc
68+
- run: npm publish --access public
69+
env:
70+
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
yarn.lock
3+
.DS_Store

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
node_js:
3+
- '12'
4+
- '10'
5+
- '8'
6+
7+
notifications:
8+
email: false

index.d.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
Dedenting Template Strings.
3+
4+
@param input - the template string to deindent
5+
@returns deindented string
6+
7+
@example
8+
```
9+
import dedentify from 'dedentify';
10+
11+
dedentify`
12+
this
13+
is
14+
cool
15+
`
16+
17+
// => `
18+
this
19+
is
20+
cool
21+
`
22+
```
23+
*/
24+
declare function dedentify(input: string | TemplateStringsArray): string;
25+
26+
export = dedentify;

index.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const format = str => {
4+
let size = -1;
5+
6+
return str.replace(/\n(\s+)/g, (m, m1) => {
7+
if (size < 0) {
8+
size = m1.replace(/\t/g, ' ').length;
9+
}
10+
11+
return '\n' + m1.slice(Math.min(m1.length, size));
12+
});
13+
};
14+
15+
const dedentify = (callSite, ...args) => {
16+
if (typeof callSite === 'string') {
17+
return format(callSite);
18+
}
19+
20+
if (typeof callSite === 'function') {
21+
return (...args) => format(callSite(...args));
22+
}
23+
24+
const output = callSite
25+
.slice(0, args.length + 1)
26+
.map((text, i) => (i === 0 ? '' : args[i - 1]) + text)
27+
.join('');
28+
29+
return format(output);
30+
};
31+
32+
module.exports = dedentify;

index.test-d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { expectType } from 'tsd';
2+
import dedentify = require('.');
3+
4+
expectType<String>(dedentify`Awesome`);
5+
expectType<String>(dedentify('Awesome'));
6+
expectType<String>(dedentify(String.raw`Awesome`));

license

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Abraham Hernandez <[email protected]> (abranhe.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

package.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "dedentify",
3+
"version": "0.0.1",
4+
"description": "Remove indentation inside strings.",
5+
"license": "MIT",
6+
"repository": "abranhe/dedentify",
7+
"author": {
8+
"name": "Abraham Hernandez",
9+
"email": "[email protected]",
10+
"url": "https://abranhe.com"
11+
},
12+
"engines": {
13+
"node": ">=8"
14+
},
15+
"scripts": {
16+
"test": "xo && ava && tsd"
17+
},
18+
"files": [
19+
"index.js",
20+
"index.d.ts"
21+
],
22+
"keywords": [
23+
"indent",
24+
"string",
25+
"template-string"
26+
],
27+
"devDependencies": {
28+
"ava": "^2.2.0",
29+
"tsd": "^0.7.3",
30+
"xo": "^0.24.0"
31+
}
32+
}

readme.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# dedentify [![Github](https://github.com/abranhe/dedentify/workflows/build/badge.svg)](https://github.com/abranhe/dedentify) [![Travis](https://travis-ci.com/abranhe/dedentify.svg?branch=master)](https://travis-ci.com/abranhe/dedentify) [![License](https://img.shields.io/github/license/abranhe/dedentify.svg)](https://github.com/abranhe/dedentify/blob/master/license) [![npm](https://img.shields.io/npm/v/dedentify.svg?logo=npm)](https://npmjs.org/dedentify)
2+
3+
> Remove indentation inside strings.
4+
5+
## Install
6+
7+
```
8+
$ npm install dedentify
9+
```
10+
11+
<details>
12+
<summary>
13+
Other options?
14+
</summary>
15+
16+
###### npm
17+
18+
```
19+
$ npm install dedentify
20+
```
21+
22+
###### yarn
23+
24+
```
25+
$ yarn add dedentify
26+
```
27+
28+
###### Github Registry
29+
30+
```
31+
$ npm install abranhe@dedentify
32+
```
33+
34+
</details>
35+
36+
## Usage
37+
38+
```js
39+
const dedentify = require('dedentify');
40+
41+
dedentify`
42+
This
43+
is
44+
Awesome
45+
`;
46+
47+
// => `
48+
// This
49+
// is
50+
// Awesome
51+
// `
52+
```
53+
54+
## API
55+
56+
### dedentify(input)
57+
58+
#### input
59+
60+
Type: `string` | `TemplateStringsArray`
61+
62+
The template string to deindent.
63+
64+
65+
## License
66+
67+
MIT © [Abraham Hernandez](https://abranhe.com)

test.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import test from 'ava';
2+
import dedentify from '.';
3+
4+
const expected = `
5+
Star
6+
The
7+
Repo
8+
https://github.com/abranhe/dedentify
9+
`;
10+
11+
const link = 'https://github.com/abranhe/dedentify';
12+
13+
const string = `
14+
Star
15+
The
16+
Repo
17+
${link}
18+
`;
19+
20+
test('title', t => {
21+
t.is(dedentify(string), expected);
22+
t.is(dedentify`${string}`, expected);
23+
});

0 commit comments

Comments
 (0)