-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: replace deindent test dependency (#2799)
- Loading branch information
Showing
25 changed files
with
169 additions
and
126 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Naive deindent - takes in a string and: | ||
* 1. finds the minimal indentation across all lines of content | ||
* 2. removes the minimal whitespace for each line | ||
* 3. attempts to remove first and last line in case of empty lines to improve usage | ||
* | ||
* NOTICE: treat tab (\t) as a single character - all lines are expected to be indented in the same format | ||
*/ | ||
export function deindent(text: string) { | ||
if (!text) { | ||
return text; | ||
} | ||
const lines = text.split('\n'); | ||
let min = text.length; | ||
for (const line of lines) { | ||
if (!line || !line.trim()) { | ||
continue; | ||
} | ||
const indent = line.match(/^[\s\t]+/); | ||
const indentSize = indent?.[0].length || 0; | ||
if (indentSize < min) { | ||
min = indentSize; | ||
} | ||
} | ||
return lines | ||
.map((line) => line.slice(min)) | ||
.join('\n') | ||
.trim(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { expect } from 'chai'; | ||
import { deindent } from '@stylable/core-test-kit'; | ||
|
||
describe('helpers/deindent', () => { | ||
it(`should trim single line`, () => { | ||
const result = deindent(` A `); | ||
expect(result, 'empty lines').to.eql(`A`); | ||
}); | ||
it(`should trim first and last empty lines`, () => { | ||
const result = deindent(` | ||
A | ||
`); | ||
expect(result, 'empty lines').to.eql(`A`); | ||
}); | ||
it(`should preserve first and last lines with context`, () => { | ||
const result = deindent(`X | ||
A | ||
Y`); | ||
expect(result, 'empty lines').to.eql(`X | ||
A | ||
Y`); | ||
}); | ||
it(`should remove all indentation`, () => { | ||
const result = deindent(` | ||
A | ||
B | ||
`); | ||
expect(result).to.eql(`A\nB`); | ||
}); | ||
it(`should preserve relative indentation`, () => { | ||
const result = deindent(` | ||
A | ||
B | ||
C | ||
`); | ||
expect(result).to.eql(`A\n\tB\n C`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.