@technobuddha/library
Version:
A large library of useful functions
19 lines (18 loc) • 630 B
JavaScript
import isNil from 'lodash/isNil';
import escapeRegExp from 'lodash/escapeRegExp';
import { space } from '../constants';
/**
* Determine the indentation level of text
*
* @param input The indented text
* @param __namedParameters see {@link Options}
* @default indenter space
* @returns The minimum amount of indentation on each line
*/
export function getIndent(input, { indenter = space } = {}) {
const matches = new RegExp(`^(${escapeRegExp(indenter)})+`, 'ugm').exec(input);
if (isNil(matches))
return 0;
return (Math.min(...matches.map(m => m.length))) / indenter.length;
}
export default getIndent;