-
-
Notifications
You must be signed in to change notification settings - Fork 48
Add TypeScript definition #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a9f2804
Add Typescript Definition
DenisCarriere c8ae085
Update based on comments
DenisCarriere 302aa2a
Update based on comments
DenisCarriere f3b32c0
Add dot & ignore xo
DenisCarriere 2bbbe34
Remove {void} & fix indentation
DenisCarriere 129e058
Fix example
DenisCarriere 017faad
Change module.export => module.exports
DenisCarriere a3f21f9
Fix import
DenisCarriere d3dad7b
Update @example & include generic T=any
DenisCarriere 26c0380
Expand Function details
DenisCarriere 2584c36
Export optional types
DenisCarriere 8b792d6
Update index.d.ts
sindresorhus bbe8b24
Update changes @sindresorhus
DenisCarriere 534fd39
Merge branch 'master' of github.com:DenisCarriere/load-json-file
DenisCarriere d2bd1d7
Add extra return after @example
DenisCarriere c95f14f
Add index.test-d.ts
DenisCarriere e198adb
Update package.json
sindresorhus 9c96144
Update package.json
DenisCarriere 2a11d5d
Change any to unknown
DenisCarriere 3ad168d
Update package.json
sindresorhus a67eb2d
Update index.test-d.ts
sindresorhus 1dfeeef
Update index.test-d.ts
sindresorhus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,44 @@ | ||
export type Reviver = (key: string, value: any) => any; | ||
export type BeforeParse = (data: string) => string; | ||
|
||
export interface Options { | ||
/** | ||
* Applies a function to the JSON string before parsing. | ||
*/ | ||
beforeParse?: BeforeParse; | ||
/** | ||
* Prescribes how the value originally produced by parsing is transformed, before being returned. | ||
* See the [`JSON.parse` docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter) for more. | ||
*/ | ||
reviver?: Reviver; | ||
} | ||
|
||
/** | ||
* Read and parse a JSON file. | ||
* | ||
* Strips UTF-8 BOM, uses graceful-fs, and throws more helpful JSON errors. | ||
* | ||
* @example | ||
* | ||
* import * as loadJsonFile from 'load-json-file'; | ||
* | ||
* const json = loadJsonFile.sync('foo.json'); | ||
* //=> {foo: true} | ||
*/ | ||
export function sync<T = any>(filePath: string, options?: Options): T; | ||
|
||
/** | ||
* Read and parse a JSON file. | ||
* | ||
* Strips UTF-8 BOM, uses graceful-fs, and throws more helpful JSON errors. | ||
* | ||
* @example | ||
* | ||
* import loadJsonFile from 'load-json-file'; | ||
* | ||
* (async () => { | ||
* const json = await loadJsonFile('foo.json'); | ||
* //=> {foo: true} | ||
* })(); | ||
*/ | ||
export default function loadJsonFile<T = any>(filePath: string, options?: Options): Promise<T>; | ||
This file contains hidden or 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 hidden or 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 @@ | ||
import {expectType} from 'tsd-check'; | ||
import loadJsonFile, {sync, Reviver, BeforeParse} from '.'; | ||
|
||
(async () => { | ||
// To-Do => Expand Tests | ||
// https://github.com/sindresorhus/write-json-file/blob/master/index.test-d.ts | ||
expectType<Reviver>(() => 1); | ||
expectType<Reviver>((a: string) => a.length); | ||
expectType<Reviver>((a: string, b: string) => a.length - b.length); | ||
|
||
expectType<BeforeParse>((data) => data); | ||
|
||
expectType<void>(await loadJsonFile('unicorn.json')); | ||
|
||
expectType<void>(sync('unicorn.json')); | ||
})(); |
This file contains hidden or 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 |
---|---|---|
|
@@ -13,10 +13,11 @@ | |
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
"test": "xo && ava && tsd-check" | ||
}, | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"read", | ||
|
@@ -35,6 +36,12 @@ | |
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"tsd-check": "^0.1.0", | ||
|
||
"xo": "*" | ||
}, | ||
"xo": { | ||
"ignores": [ | ||
"index.d.ts" | ||
] | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use
unknown
instead ofany
so users are forced to explicitly coerce it. Thoughts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
unknown
works as the defaultVery cool advanced type from Typescript 3.x