indent-string
Advanced tools
Comparing version 3.2.0 to 4.0.0
36
index.js
'use strict'; | ||
module.exports = (str, count, opts) => { | ||
// Support older versions: use the third parameter as options.indent | ||
// TODO: Remove the workaround in the next major version | ||
const options = typeof opts === 'object' ? Object.assign({indent: ' '}, opts) : {indent: opts || ' '}; | ||
count = count === undefined ? 1 : count; | ||
if (typeof str !== 'string') { | ||
throw new TypeError(`Expected \`input\` to be a \`string\`, got \`${typeof str}\``); | ||
module.exports = (string, count = 1, options) => { | ||
options = { | ||
indent: ' ', | ||
includeEmptyLines: false, | ||
...options | ||
}; | ||
if (typeof string !== 'string') { | ||
throw new TypeError( | ||
`Expected \`input\` to be a \`string\`, got \`${typeof string}\`` | ||
); | ||
} | ||
if (typeof count !== 'number') { | ||
throw new TypeError(`Expected \`count\` to be a \`number\`, got \`${typeof count}\``); | ||
throw new TypeError( | ||
`Expected \`count\` to be a \`number\`, got \`${typeof count}\`` | ||
); | ||
} | ||
if (typeof options.indent !== 'string') { | ||
throw new TypeError(`Expected \`options.indent\` to be a \`string\`, got \`${typeof options.indent}\``); | ||
throw new TypeError( | ||
`Expected \`options.indent\` to be a \`string\`, got \`${typeof options.indent}\`` | ||
); | ||
} | ||
if (count === 0) { | ||
return str; | ||
return string; | ||
} | ||
const regex = options.includeEmptyLines ? /^/mg : /^(?!\s*$)/mg; | ||
return str.replace(regex, options.indent.repeat(count)); | ||
} | ||
; | ||
const regex = options.includeEmptyLines ? /^/gm : /^(?!\s*$)/gm; | ||
return string.replace(regex, options.indent.repeat(count)); | ||
}; |
{ | ||
"name": "indent-string", | ||
"version": "3.2.0", | ||
"description": "Indent each line in a string", | ||
"license": "MIT", | ||
"repository": "sindresorhus/indent-string", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"indent", | ||
"string", | ||
"str", | ||
"pad", | ||
"align", | ||
"line", | ||
"text", | ||
"each", | ||
"every" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
"name": "indent-string", | ||
"version": "4.0.0", | ||
"description": "Indent each line in a string", | ||
"license": "MIT", | ||
"repository": "sindresorhus/indent-string", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"indent", | ||
"string", | ||
"pad", | ||
"align", | ||
"line", | ||
"text", | ||
"each", | ||
"every" | ||
], | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -19,8 +19,6 @@ # indent-string [![Build Status](https://travis-ci.org/sindresorhus/indent-string.svg?branch=master)](https://travis-ci.org/sindresorhus/indent-string) | ||
indentString('Unicorns\nRainbows', 4); | ||
//=> ' Unicorns' | ||
//=> ' Rainbows' | ||
//=> ' Unicorns\n Rainbows' | ||
indentString('Unicorns\nRainbows', 4, {indent: '♥'}); | ||
//=> '♥♥♥♥Unicorns' | ||
//=> '♥♥♥♥Rainbows' | ||
//=> '♥♥♥♥Unicorns\n♥♥♥♥Rainbows' | ||
``` | ||
@@ -31,9 +29,9 @@ | ||
### indentString(input, [count], [options]) | ||
### indentString(string, [count], [options]) | ||
#### input | ||
#### string | ||
Type: `string` | ||
String you want to indent. | ||
The string to indent. | ||
@@ -45,7 +43,7 @@ #### count | ||
How many times you want `indent` repeated. | ||
How many times you want `options.indent` repeated. | ||
#### options | ||
Type: `Object`<br> | ||
Type: `object` | ||
@@ -57,3 +55,3 @@ ##### indent | ||
String to use for the indent. | ||
The string to use for the indent. | ||
@@ -60,0 +58,0 @@ ##### includeEmptyLines |
4398
5
61
3
71