-
-
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.
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
- Loading branch information
1 parent
559267d
commit 8eea639
Showing
5 changed files
with
56 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* @file Unit Tests - parseJSON | ||
* @module tsconfig-utils/internal/tests/unit/parseJSON | ||
*/ | ||
|
||
import testSubject from '../parse-json' | ||
|
||
describe('unit:internal/parseJSON', () => { | ||
it('should return parsed JSON value', () => { | ||
// Arrange | ||
const json: string = '{\n\t// Rainbows\n\t"unicorn": "cake"\n}' | ||
|
||
// Act + Expect | ||
expect(testSubject(json)).to.deep.equal({ unicorn: 'cake' }) | ||
}) | ||
}) |
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,28 @@ | ||
/** | ||
* @file Internal - parseJSON | ||
* @module tsconfig-utils/internal/parseJSON | ||
*/ | ||
|
||
import type { JsonValue } from '@flex-development/tutils' | ||
import stripBom from 'strip-bom' | ||
import stripComments from 'strip-json-comments' | ||
import validateString from './validate-string' | ||
|
||
/** | ||
* Parses a JSON string. | ||
* | ||
* Strips comments and [UTF-8 byte order marks (BOM)][1] before parsing. | ||
* | ||
* [1]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8 | ||
* | ||
* @template T - Parsed JSON value type | ||
* | ||
* @param {string} json - JSON string to parse | ||
* @return {T} Parsed JSON value | ||
*/ | ||
function parseJSON<T extends JsonValue>(json: string): T { | ||
validateString(json, 'json') | ||
return JSON.parse(stripComments(stripBom(json), { whitespace: false })) as T | ||
} | ||
|
||
export default parseJSON |
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