-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: preserve trailing whitespace in manifest
- Loading branch information
1 parent
8ac9f2e
commit 06426ec
Showing
6 changed files
with
60 additions
and
23 deletions.
There are no files selected for viewing
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
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,25 @@ | ||
/** | ||
* Information about the format of a file. | ||
* @typedef FileFormat | ||
* @property {string|number} indent Indentation characters | ||
* @property {string} trailingWhitespace Trailing whitespace at the end of the file | ||
*/ | ||
|
||
/** | ||
* Detects the indentation and trailing whitespace of a file. | ||
* | ||
* @param {string} contents contents of the file | ||
* @returns {FileFormat} Formatting of the file | ||
*/ | ||
function recognizeFormat(contents) { | ||
const indentMatch = /\n([^"]+)/.exec(contents); | ||
const trailingWhitespaceMatch = /}(\s*)$/.exec(contents); | ||
|
||
return { | ||
indent: indentMatch ? indentMatch[1] : 2, | ||
trailingWhitespace: trailingWhitespaceMatch ? trailingWhitespaceMatch[1] : "", | ||
}; | ||
} | ||
|
||
// Exports. | ||
module.exports = recognizeFormat; |
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,24 @@ | ||
/** @typedef {import('@types/jest')} */ | ||
const recognizeFormat = require("../../lib/recognizeFormat"); | ||
|
||
// Tests. | ||
describe("recognizeFormat()", () => { | ||
describe("Indentation", () => { | ||
test("Normal indentation", () => | ||
expect( | ||
recognizeFormat(`{ | ||
"a": "b", | ||
"c": { | ||
"d": "e" | ||
} | ||
}`).indent | ||
).toBe("\t")); | ||
test("No indentation", () => expect(recognizeFormat('{"a": "b"}').indent).toBe(2)); | ||
}); | ||
|
||
describe("Trailing whitespace", () => { | ||
test("No trailing whitespace", () => expect(recognizeFormat('{"a": "b"}').trailingWhitespace).toBe("")); | ||
test("Newline", () => expect(recognizeFormat('{"a": "b"}\n').trailingWhitespace).toBe("\n")); | ||
test("Multiple newlines", () => expect(recognizeFormat('{"a": "b"}\n\n').trailingWhitespace).toBe("\n\n")); | ||
}); | ||
}); |