Skip to content

Commit e8646ba

Browse files
committed
feature(align-spaces) speed up
1 parent b46464c commit e8646ba

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/align-spaces.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ module.exports = (str) => {
1818
return array.join('\n');
1919
};
2020

21+
module.exports.isAligned = (str) => {
22+
check(str);
23+
24+
const array = str.split('\n');
25+
const n = array.length - 1;
26+
27+
for (let i = 0; i < n; i++) {
28+
const str = array[i];
29+
30+
if (!i || isNotSpaceString(str))
31+
continue;
32+
33+
if (str !== getSpaces(array[i + 1]))
34+
return false;
35+
}
36+
37+
return true;
38+
};
39+
2140
function isNotSpaceString(s) {
2241
let i = s.length;
2342

test/align-spaces.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,24 @@ test('align-spaces', (t) => {
2929
t.end();
3030
});
3131

32+
test('align-spaces: isAligned', (t) => {
33+
const result = alignSpaces.isAligned(notAligned);
34+
35+
t.notOk(result, 'should be false');
36+
t.end();
37+
});
38+
39+
test.only('align-spaces: isAligned: ok', (t) => {
40+
debugger;
41+
const result = alignSpaces.isAligned([
42+
'function calc() {',
43+
' const result = [];',
44+
' ',
45+
' return result;',
46+
'}'
47+
].join('\n'));
48+
49+
t.ok(result, 'should be true');
50+
t.end();
51+
});
52+

0 commit comments

Comments
 (0)