Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
44 changes: 44 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
declare namespace indentString {
interface Options {
/**
String to use for the indent.

@default ' '
*/
readonly indent?: string;

/**
Also indent empty lines.

@default false
*/
readonly includeEmptyLines?: boolean;
}
}

/**
Indent each line in a string.

@param string - String you want to indent.
@param count - How many times you want `options.indent` repeated. Default: `1`.

@example
```
import indentString = require('indent-string');

indentString('Unicorns\nRainbows', 4);
//=> ' Unicorns'
//=> ' Rainbows'

indentString('Unicorns\nRainbows', 4, {indent: '♥'});
//=> '♥♥♥♥Unicorns'
//=> '♥♥♥♥Rainbows'
```
*/
declare function indentString(
string: string,
count?: number,
options?: indentString.Options
): string;

export = indentString;
36 changes: 22 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
'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));
};
9 changes: 9 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {expectType} from 'tsd';
import indentString = require('.');

expectType<string>(indentString('Unicorns\nRainbows'));
expectType<string>(indentString('Unicorns\nRainbows', 4));
expectType<string>(indentString('Unicorns\nRainbows', 4, {indent: '♥'}));
expectType<string>(
indentString('Unicorns\nRainbows', 4, {includeEmptyLines: true})
);
70 changes: 36 additions & 34 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
{
"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": "[email protected]",
"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": "3.2.0",
"description": "Indent each line in a string",
"license": "MIT",
"repository": "sindresorhus/indent-string",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"indent",
"string",
"str",
"pad",
"align",
"line",
"text",
"each",
"every"
],
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ String you want to indent.
Type: `number`<br>
Default: `1`

How many times you want `indent` repeated.
How many times you want `options.indent` repeated.

#### options

Type: `Object`<br>
Type: `object`<br>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about changing this. Technically, Object is correct. It's only TS that has a special object type. In normal sense, primitives are lowercase and non-primitives pascal-case. Thoughts? I haven't completely made up my mind with this or going with exact TS types.

Copy link
Contributor Author

@BendingBender BendingBender Apr 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure either. I saw that you refactored quite a few types to TS notation and thought that you've started using TS types in your docs. So this would be TypeScript's "primitive" object type that accepts any object. The Object type is the object constructor and should not be used for plain objects at least in TS.

I don't have a strong preference here, I just wanted to be consistent with TS.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Let's go with object. (See: sindresorhus/meta#1)


##### indent

Expand Down