Merge pull request #17940 from arjenvanderende/master

[csv-stringify] Add formatter options for boolean, date and object types
This commit is contained in:
Ryan Cavanaugh
2017-07-11 10:57:40 -07:00
committed by GitHub
3 changed files with 34 additions and 6 deletions

View File

@@ -12,9 +12,22 @@ stringify([["1", "2", "3"], ["4", "5", "6"]], {
// nothing
});
stringify([["1", true, new Date()], ["4", false, new Date()]], {
delimiter: ",",
formatters: {
bool: value => value ? 'yes' : 'no',
date: value => value.toISOString()
}
}, (error: Error, output: string): void => {
// nothing
});
stream = stringify({ delimiter: "," });
stream.write(["1", "2", "3"]);
stream.write(["4", true, new Date()], 'utf8', (err: Error, output: string): void => {
// nothing
});
const transform: NodeJS.ReadWriteStream = stream;

View File

@@ -1,6 +1,7 @@
// Type definitions for csv-stringify 1.0
// Type definitions for csv-stringify 1.4
// Project: https://github.com/wdavidw/node-csv-stringify
// Definitions by: Rogier Schouten <https://github.com/rogierschouten>
// Arjen van der Ende <https://github.com/arjenvanderende>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
@@ -55,15 +56,24 @@ declare namespace stringify {
* special values are 'auto', 'unix', 'mac', 'windows', 'unicode'; defaults to 'auto' (discovered in source or 'unix' if no source is specified).
*/
rowDelimiter?: string;
/**
* Override serialization of boolean, dates and complex objects.
*/
formatters?: FormatterOpts;
}
interface FormatterOpts {
bool?: (value: boolean) => string;
date?: (value: Date) => string;
object?: (value: any) => string;
}
interface Stringifier extends NodeJS.ReadWriteStream {
// Stringifier stream takes array of strings or Object
write(line: string[] | any): boolean;
// Stringifier stream takes array of strings or Object, and optional encoding and callback
write(line: string[] | any, encoding?: string, cb?: (error: Error | undefined, output: string) => void): boolean;
// repeat declarations from NodeJS.WritableStream to avoid compile error
write(buffer: string | Buffer, cb?: () => void): boolean;
write(str: string, encoding?: string, cb?: () => void): boolean;
write(buffer: string | Buffer, cb?: (error: Error | undefined, output: string) => void): boolean;
}
}

View File

@@ -1 +1,6 @@
{ "extends": "dtslint/dt.json" }
{
"extends": "dtslint/dt.json",
"rules": {
"prefer-method-signature": false
}
}