Skip to content

Commit

Permalink
fix: Fix trim bug in trimCode
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan committed Jul 15, 2019
1 parent f81eb1c commit ddbc47a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/bin/readme-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import _ from 'lodash';
import glob from 'glob';

const trimCode = (code) => {
let lines = code.trim().split('\n');
let lines = code.replace(/^\n/, '').trimEnd().split('\n');

const indendation = lines[lines.length - 1].match(/^\s+/);
const firsLineIndentation = lines[0].match(/^\s+/);
const lastLineIndentation = lines[lines.length - 1].match(/^\s+/);

const indentSize = indendation ? indendation[0].length : 0;
const firstIndentSize = firsLineIndentation ? firsLineIndentation[0].length : 0;
const lastIndentSize = lastLineIndentation ? lastLineIndentation[0].length : 0;

lines = lines.map((line, index) => {
if (index === 0) {
return line;
return line.slice(Math.min(firstIndentSize, lastIndentSize));
}

return line.slice(indentSize);
return line.slice(lastIndentSize);
});

return lines.join('\n');
Expand Down

0 comments on commit ddbc47a

Please sign in to comment.