Skip to content

Commit

Permalink
feat(internal): parseJSON
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Feb 4, 2023
1 parent 559267d commit 8eea639
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
"@flex-development/pathe": "1.0.3",
"@flex-development/tsconfig-types": "2.0.3",
"@flex-development/tutils": "6.0.0-alpha.10",
"json5": "2.2.3",
"strip-bom": "5.0.0"
"strip-bom": "5.0.0",
"strip-json-comments": "5.0.0"
},
"devDependencies": {
"@commitlint/cli": "17.4.2",
Expand Down
16 changes: 16 additions & 0 deletions src/internal/__tests__/parse-json.spec.ts
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' })
})
})
1 change: 1 addition & 0 deletions src/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

export { default as isDirectory } from './is-directory'
export { default as isFile } from './is-file'
export { default as parseJSON } from './parse-json'
export { default as readFile } from './read-file'
export { default as validateFunction } from './validate-function'
export { default as validateString } from './validate-string'
Expand Down
28 changes: 28 additions & 0 deletions src/internal/parse-json.ts
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
11 changes: 9 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,6 @@ __metadata:
growl: "npm:1.10.5"
husky: "npm:8.0.3"
is-ci: "npm:3.0.1"
json5: "npm:2.2.3"
jsonc-eslint-parser: "npm:2.1.0"
lint-staged: "npm:13.1.0"
mri: "npm:1.2.0"
Expand All @@ -1370,6 +1369,7 @@ __metadata:
semver: "npm:7.3.8"
serve: "npm:14.2.0"
strip-bom: "npm:5.0.0"
strip-json-comments: "npm:5.0.0"
tempfile: "npm:4.0.0"
trash-cli: "npm:5.0.0"
ts-dedent: "npm:2.2.0"
Expand Down Expand Up @@ -5842,7 +5842,7 @@ __metadata:
languageName: node
linkType: hard

"json5@npm:2.2.3, json5@npm:^2.2.1":
"json5@npm:^2.2.1":
version: 2.2.3
resolution: "json5@npm:2.2.3"
bin:
Expand Down Expand Up @@ -8275,6 +8275,13 @@ __metadata:
languageName: node
linkType: hard

"strip-json-comments@npm:5.0.0":
version: 5.0.0
resolution: "strip-json-comments@npm:5.0.0"
checksum: 3987fe7ac03f1f912375b63bfa010c54cfcf93b464eead789bd9deb3911bcf8fdaa6d3335e20915a7debbe76754b8bba18c13568e7c27946c21bbda117567666
languageName: node
linkType: hard

"strip-json-comments@npm:^3.1.0, strip-json-comments@npm:^3.1.1":
version: 3.1.1
resolution: "strip-json-comments@npm:3.1.1"
Expand Down

0 comments on commit 8eea639

Please sign in to comment.