@socketregistry/indent-string
Advanced tools
+31
-11
| 'use strict' | ||
| module.exports = function indentString(string, count = 1, options) { | ||
| const { includeEmptyLines = false, indent = ' ' } = { | ||
| __proto__: null, | ||
| ...options | ||
| module.exports = function indentString(input, count = 1, options) { | ||
| let includeEmptyLines = false | ||
| let indent = ' ' | ||
| if ( | ||
| // isV2: indentString(input, indent, count) - 3 args only | ||
| typeof count === 'string' && | ||
| typeof options === 'number' | ||
| ) { | ||
| indent = count | ||
| count = options | ||
| } else if ( | ||
| // isV3: indentString(input, count, indent) | ||
| typeof count === 'number' && | ||
| typeof options === 'string' | ||
| ) { | ||
| indent = options | ||
| } else if (options !== null && typeof options === 'object') { | ||
| const opts = { __proto__: null, ...options } | ||
| if (opts.includeEmptyLines !== undefined) { | ||
| includeEmptyLines = opts.includeEmptyLines | ||
| } | ||
| if (opts.indent !== undefined) { | ||
| indent = opts.indent | ||
| } | ||
| } | ||
| if (typeof string !== 'string') { | ||
| if (typeof input !== 'string') { | ||
| throw new TypeError( | ||
| `Expected \`input\` to be a \`string\`, got \`${typeof string}\`` | ||
| `Expected \`input\` to be a \`string\`, got \`${typeof input}\``, | ||
| ) | ||
@@ -15,3 +35,3 @@ } | ||
| throw new TypeError( | ||
| `Expected \`count\` to be a \`number\`, got \`${typeof count}\`` | ||
| `Expected \`count\` to be a \`number\`, got \`${typeof count}\``, | ||
| ) | ||
@@ -21,3 +41,3 @@ } | ||
| throw new RangeError( | ||
| `Expected \`count\` to be at least 0, got \`${count}\`` | ||
| `Expected \`count\` to be at least 0, got \`${count}\``, | ||
| ) | ||
@@ -27,10 +47,10 @@ } | ||
| throw new TypeError( | ||
| `Expected \`options.indent\` to be a \`string\`, got \`${typeof indent}\`` | ||
| `Expected \`options.indent\` to be a \`string\`, got \`${typeof indent}\``, | ||
| ) | ||
| } | ||
| if (count === 0) { | ||
| return string | ||
| return input | ||
| } | ||
| const regex = includeEmptyLines ? /^/gm : /^(?!\s*$)/gm | ||
| return string.replace(regex, indent.repeat(count)) | ||
| return input.replace(regex, indent.repeat(count)) | ||
| } |
+2
-2
@@ -35,4 +35,4 @@ declare type Options = { | ||
| string: string, | ||
| count?: number, | ||
| options?: Options | ||
| count?: number | undefined, | ||
| options?: Options | undefined, | ||
| ): string | ||
@@ -39,0 +39,0 @@ declare namespace indentString { |
+1
-1
| { | ||
| "name": "@socketregistry/indent-string", | ||
| "version": "1.0.13", | ||
| "version": "1.0.14", | ||
| "license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Socket.dev optimized package override for indent-string", |
7288
9%53
60.61%