-
-
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 2 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,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,54 @@ | ||
type Replacer = (key: string, value: any) => void; | ||
|
||
interface Options { | ||
indent?: string | number | null; | ||
detectIndent?: boolean; | ||
sortKeys?: boolean | ((a: string, b: string) => number); | ||
replacer?: Replacer | Array<number | string> | null; | ||
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. This should not be |
||
mode?: number; | ||
} | ||
|
||
/** | ||
* Stringify and write JSON to a file atomically | ||
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 end in a dot. |
||
* | ||
* Creates directories for you as needed. | ||
* | ||
* @param {string} filepath Filepath | ||
* @param {any} data Data | ||
* @param {object} [options] Optional parameters | ||
* @param {string|number|null} [options.indent='\t'] Indentation as a string or number of spaces. Pass in null for no formatting. | ||
* @param {boolean} [options.detectIndent=false] Detect indentation automatically if the file exists. | ||
* @param {boolean|function} [options.sortKeys=false] Sort the keys recursively. Optionally pass in a compare function. | ||
* @param {function|Array<number|string>|null} [options.replacer] Passed into JSON.stringify. | ||
* @param {number} [options.mode=0o666] Mode used when writing the 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. From the style guide:
Since it's inferred from the type definition already and just results in duplication. 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. 👍 |
||
* @returns {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. |
||
* @example | ||
* const writeJsonFile = require('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. Use |
||
* | ||
* writeJsonFile.sync('foo.json', {foo: true}); | ||
* console.log('done'); | ||
*/ | ||
export function sync(filepath: string, data: any, 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.
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. Consider the fact that even in TypeScripts "official" JSON.stringify(value: any, replacer?: (key: string, value: any) => any, ...): string; That means even the TypeScript team couldn't come with a reliable, sound way to describe JSON. 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. There's always the 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. @brandon93s was hoping to prevent passing function foo(a: object | number | string): void { }
// ts won't complain i.e no red squiggly lines
foo(undefined)
// no complains here either
foo(null) EDIT I was wrong, I forgot about 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. Yeah, let's make it strict. Can be a type called 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. |
||
|
||
/** | ||
* Stringify and write JSON to a file atomically | ||
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 end in a dot. |
||
* | ||
* Creates directories for you as needed. | ||
* | ||
* @param {string} filepath Filepath | ||
* @param {any} data Data | ||
* @param {object} [options] Optional parameters | ||
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. No need to have a description for these 3 parameters as the name is clear enough. 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. Fair enough, dropping those params can be extracted from the Types |
||
* @param {string|number|null} [options.indent='\t'] Indentation as a string or number of spaces. Pass in null for no formatting. | ||
* @param {boolean} [options.detectIndent=false] Detect indentation automatically if the file exists. | ||
* @param {boolean|function} [options.sortKeys=false] Sort the keys recursively. Optionally pass in a compare function. | ||
* @param {function|Array<number|string>|null} [options.replacer] Passed into JSON.stringify. | ||
* @param {number} [options.mode=0o666] Mode used when writing the file. | ||
* @returns {Promise<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. This is moot as it has no description and therefore is better documented by the type definition itself. 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. 👍 yea it's pretty pointless, it is defined by the Types |
||
* @example | ||
* const writeJsonFile = require('write-json-file'); | ||
* | ||
* writeJsonFile('foo.json', {foo: true}).then(() => { | ||
* console.log('done'); | ||
* }); | ||
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. Use this instead: (async () => {
await writeJsonFile('foo.json', {foo: true});
console.log('done');
})(); 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 like this example as well |
||
*/ | ||
export default function writeJsonFile(filepath: string, data: any, options?: Options): Promise<void>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ const main = (fp, data, opts) => { | |
}; | ||
|
||
const mainSync = (fp, data, opts) => { | ||
let indent = opts.indent; | ||
let {indent} = opts; | ||
|
||
if (opts.detectIndent) { | ||
try { | ||
|
@@ -67,6 +67,12 @@ module.exports = (fp, data, opts) => { | |
.then(() => init(main, fp, data, opts)); | ||
}; | ||
|
||
// Support for Typescript default export | ||
module.exports.default = (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.sync = (fp, data, opts) => { | ||
makeDir.sync(path.dirname(fp), {fs}); | ||
init(mainSync, fp, data, opts); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,14 +10,16 @@ | |
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"types": "index.d.ts", | ||
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. This is not needed. From the style guide:
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, didn't know this wasn't required, good to know. |
||
"keywords": [ | ||
"write", | ||
"json", | ||
|
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