Skip to content

Commit

Permalink
feat: 🎸 add indent utility
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo committed Apr 1, 2021
1 parent f7b388e commit 78be41c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,32 @@ export function revertOriginalPhpTagInHtml(content) {
);
}

export function indent(content, level, options) {
const lines = content.split('\n');
return _.map(lines, (line, index) => {
if (!line.match(/\w/)) {
return line;
}

const ignoreFirstLine = optional(options).ignoreFirstLine || false;

if (ignoreFirstLine && index === 0) {
return line;
}

const originalLineWhitespaces = detectIndent(line).amount;
const indentChar = optional(options).useTabs ? '\t' : ' ';
const indentSize = optional(options).indentSize || 4;
const whitespaces = originalLineWhitespaces + indentSize * level;

if (whitespaces < 0) {
return line;
}

return indentChar.repeat(whitespaces) + line.trimLeft();
}).join('\n');
}

export function unindent(directive, content, level, options) {
const lines = content.split('\n');
return _.map(lines, (line) => {
Expand Down

0 comments on commit 78be41c

Please sign in to comment.