-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
Add TypeScript definition and require Node.js 6 #17
Changes from 13 commits
d2332fd
b391dd4
ffa8823
3e683e8
5b9a733
2d1dd15
1c9b6e5
5a45486
54aebc1
6a15428
d2c300b
8d85e0c
4df67a1
8062f51
1f282fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
yarn.lock |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
language: node_js | ||
node_js: | ||
- '10' | ||
- '8' | ||
- '6' | ||
- '4' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
export type Replacer = (key: string, value: any) => void; | ||
export type SortKeys = (a: string, b: string) => number; | ||
|
||
export interface Options { | ||
/** | ||
* Indentation as a string or number of spaces. Pass in null for no formatting. | ||
* | ||
* @default '\t' | ||
*/ | ||
indent?: string | number | null; | ||
/** | ||
* Detect indentation automatically if the file exists. | ||
* | ||
* @default false | ||
*/ | ||
detectIndent?: boolean; | ||
/** | ||
* Sort the keys recursively. Optionally pass in a compare function. | ||
* | ||
* @default false | ||
*/ | ||
sortKeys?: SortKeys | boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 sounds good to me |
||
/** | ||
* Passed into JSON.stringify. | ||
*/ | ||
replacer?: Replacer | Array<number | string>; | ||
/** | ||
* Mode used when writing the file. | ||
* | ||
* @default 0o666 | ||
*/ | ||
mode?: number; | ||
} | ||
|
||
/** | ||
* Stringify and write JSON to a file atomically. | ||
* | ||
* Creates directories for you as needed. | ||
* | ||
* @example | ||
* import * as writeJsonFile from 'write-json-file'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 ... to access There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, good point 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess if you use Babel this example is correct, I'll change it to: import writeJsonFile from 'write-json-file'; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most Typescript users kinda got used to adding Most docs are mixed between Babel & Typescript, we can keep as the Babel way There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sindresorhus FYI: I've changed it back to Reason: Since this is a Typescript definition, it makes more sense to reference the example in the "Typescript" way. |
||
* | ||
* writeJsonFile.sync('foo.json', {foo: true}); | ||
* console.log('done'); | ||
*/ | ||
export function sync(filepath: string, data: object | number | string, options?: Options): void; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok! I'll add it |
||
|
||
/** | ||
* Stringify and write JSON to a file atomically. | ||
* | ||
* Creates directories for you as needed. | ||
* | ||
* @example | ||
* import writeJsonFile from 'write-json-file'; | ||
* | ||
* (async () => { | ||
* await writeJsonFile('foo.json', {foo: true}); | ||
* console.log('done'); | ||
* })(); | ||
*/ | ||
export default function writeJsonFile(filepath: string, data: object | number | string, options?: Options): Promise<void>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,11 +44,12 @@ const main = (fp, data, opts) => { | |
}; | ||
|
||
const mainSync = (fp, data, opts) => { | ||
let indent = opts.indent; | ||
let {indent} = opts; | ||
|
||
if (opts.detectIndent) { | ||
try { | ||
const file = fs.readFileSync(fp, 'utf8'); | ||
// eslint-disable-next-line prefer-destructuring | ||
indent = detectIndent(file).indent; | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
|
@@ -62,11 +63,13 @@ const mainSync = (fp, data, opts) => { | |
return writeFileAtomic.sync(fp, `${json}\n`, {mode: opts.mode}); | ||
}; | ||
|
||
module.exports = (fp, data, opts) => { | ||
const writeJsonFile = (fp, data, opts) => { | ||
return makeDir(path.dirname(fp), {fs}) | ||
.then(() => init(main, fp, data, opts)); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of duplicating the code here, assign the method to a variable and then assign that variable to both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 makes sense |
||
|
||
module.exports = writeJsonFile; | ||
module.exports.default = writeJsonFile; | ||
module.exports.sync = (fp, data, opts) => { | ||
makeDir.sync(path.dirname(fp), {fs}); | ||
init(mainSync, fp, data, opts); | ||
|
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.
Is there any way to define default arguments in the types themselves instead of in the docs?
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.
Good question, I don't think it's possible as an interface.
You can do it if the types are created from a
.ts
file