Skip to content
This repository has been archived by the owner on May 31, 2022. It is now read-only.

Commit

Permalink
Initial release (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
MoOx committed Sep 24, 2016
1 parent 6fd8f84 commit fb289d9
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# editorconfig.org
root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2

[*.md]
# Allow <br/> from Markdown
trim_trailing_whitespace = false
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# OS X crap
.DS_Store

# no log
.log

# Node.js / npm
node_modules

# build
lib

# tests results
**/__tests__/_output*

# code coverage
.coveralls.yml
coverage
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: node_js
node_js:
- '6'

# fail asap when there is a failure
matrix:
fast_finish: true

# cache node modules
cache:
directories:
- node_modules

before_install:
# faster npm install
- npm set progress false
# remove useless/non listed deps
- npm prune

after_success: npm run coverage
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

✨ Initial release
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2016 "MoOx" Maxime Thirouin

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.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,75 @@
# multili

[![Unix Build status](https://img.shields.io/travis/MoOx/multili/master.svg?branch=master&label=unix%20build)](https://travis-ci.org/MoOx/multili)
[![Code Coverage](https://img.shields.io/coveralls/MoOx/multili/master.svg)](https://coveralls.io/github/MoOx/multili)
[![Version](https://img.shields.io/npm/v/multili.svg)](https://github.com/MoOx/multili/blob/master/CHANGELOG.md)

[![Repo on GitHub](https://img.shields.io/badge/repo-GitHub-3D76C2.svg)](https://github.com/MoOx/multili)
[![Repo on GitLab](https://img.shields.io/badge/repo-GitLab-6C488A.svg)](https://gitlab.com/MoOx/multili)
[![Repo on BitBucket](https://img.shields.io/badge/repo-BitBucket-1F5081.svg)](https://bitbucket.org/MoOx/multili)

> Function to remove indentation in multi-lines template literals (string) based on the shortest indented line.
## Installation

```console
$ npm install multili
```

## Usage

```js
import multili from "multili"

multili(`
This is a
multi-lines
string
`)

// Will produce
/*
This is a
multi-lines
string
*/

multili(`
This is a
multi-lines
string
`)

// Will produce
/*
This is a
multi-lines
string
*/


multili(`
This is a
multi-lines
string
`)

// Will produce
/*
This is a
multi-lines
string
*/
```

---

## CONTRIBUTING

* ⇄ Pull/Merge requests and ★ Stars are always welcome.
* For bugs and feature requests, please create an issue.
* Pull/Merge requests must be accompanied by passing automated tests (`$ npm test`).

## [CHANGELOG](CHANGELOG.md)

## [LICENSE](LICENSE)
56 changes: 56 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "multili",
"version": "0.0.0",
"description": "Function to remove indentation in multi-lines template literals (string) based on the indented line.",
"keywords": [
"template",
"literal",
"string",
"mutli",
"lines",
"mutli-lines",
"mutlilines"
],
"author": "Maxime Thirouin",
"license": "MIT",
"repository": "https://github.com/MoOx/multili.git",
"#repositories": "https://github.com/npm/npm/issues/11315",
"repositories": [
"https://github.com/MoOx/multili.git",
"https://gitlab.com/MoOx/multili.git",
"https://bitbucket.org/MoOx/multili.git"
],
"main": "src/index.js",
"files": [
"src",
"!**/__tests__"
],
"dependencies": {},
"devDependencies": {
"coveralls": "^2.11.9",
"eslint": "^3.0.0",
"eslint-config-i-am-meticulous": "^5.0.2",
"jest": "^15.1.1",
"npmpub": "^3.0.0"
},
"scripts": {
"lint:js": "eslint --ignore-path .gitignore --fix .",
"lint": "npm -s run lint:js",
"tests": "jest",
"pretest": "npm -s run lint",
"test": "npm -s run tests",
"coverage": "cat ./coverage/lcov.info | coveralls",
"release": "npmpub"
},
"eslintConfig": {
"env": {
"jest": true
},
"extends": [
"eslint-config-i-am-meticulous"
]
},
"jest": {
"collectCoverage": true
}
}
62 changes: 62 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const multili = require("../index.js")

it("trim spaces on first and last lines", () => {
expect(multili(`
This is a
multi-lines
string
`
))
.toEqual(`
This is a
multi-lines
string
`
)
})

it("remove global common indentation", () => {
expect(multili(`
This is a
multi-lines
string
`
))
.toEqual(`
This is a
multi-lines
string
`
)
})

it("remove common indentation based on the sortest indented line", () => {
expect(multili(`
This is a
multi-lines
string
`
))
.toEqual(`
This is a
multi-lines
string
`
)
})

it("does not remove empty lines except the first and the last", () => {
expect(multili(`
This is a
multi-lines
string
`))
.toEqual(`
This is a
multi-lines
string
`
)
})
43 changes: 43 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const whiteSpaceRE = /^\s+/
const fakeInfiniteString = { length: Infinity }

module.exports = (s) => {
const lines = s.split("\n")
let whiteSpace = fakeInfiniteString
lines.forEach((line) => {
const matches = line.match(whiteSpaceRE)
if (
line.length &&
line.replace(/\s+/, "") !== ""
) {
if (
matches &&
matches.length < line.length &&
matches[0].length < whiteSpace.length
) {
whiteSpace = matches[0]
}
else if (whiteSpace === fakeInfiniteString) {
whiteSpace = 0
}
}
})

if (lines.length) {
const firstLine = lines[0].replace(/\s+/, "")
if (firstLine === "") {
lines[0] = firstLine
}
const lastLine = lines[lines.length - 1].replace(/\s+/, "")
if (lastLine === "") {
lines[lines.length - 1] = lastLine
}
}

let result = s
if (whiteSpace !== fakeInfiniteString) {
result = lines.map((line) => line.replace(whiteSpace, "")).join("\n")
}

return result
}

0 comments on commit fb289d9

Please sign in to comment.