Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions types/d3-format/d3-format-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ import * as d3Format from 'd3-format';
// Preparatory Steps
// ----------------------------------------------------------------------

class NumCoercible {
a: number;

constructor(a: number) {
this.a = a;
}

valueOf() {
return this.a;
}
}

const numeric: NumCoercible = new NumCoercible(10);

let num: number;

let formatFn: (n: number) => string;
Expand All @@ -30,6 +44,12 @@ formatFn = d3Format.format('.0%');

formatFn = d3Format.formatPrefix(',.0', 1e-6);

d3Format.format('.0%')(10);
d3Format.format('.0%')(numeric);

d3Format.formatPrefix(',.0', 1e-6)(10);
d3Format.formatPrefix(',.0', 1e-6)(numeric);

// ----------------------------------------------------------------------
// Test Format Specifier
// ----------------------------------------------------------------------
Expand All @@ -43,7 +63,7 @@ const symbol: '$' | '#' | '' = specifier.symbol;
const zero: boolean = specifier.zero;
const width: number | undefined = specifier.width;
const comma: boolean = specifier.comma;
const precision: number = specifier.precision;
const precision: number | undefined = specifier.precision;
const type: 'e' | 'f' | 'g' | 'r' | 's' | '%' | 'p' | 'b' | 'o' | 'd' | 'x' | 'X' | 'c' | '' | 'n' = specifier.type;

const formatString: string = specifier.toString();
Expand All @@ -63,10 +83,10 @@ num = d3Format.precisionRound(0.0005, 3000);
// ----------------------------------------------------------------------

localeDef = {
decimal: ',',
thousands: '.',
grouping: [3],
currency: ['EUR', '']
decimal: ',',
thousands: '.',
grouping: [3],
currency: ['EUR', '']
};

localeDef = {
Expand Down
46 changes: 27 additions & 19 deletions types/d3-format/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Type definitions for D3JS d3-format module 1.2
// Project: https://github.com/d3/d3-format/
// Definitions by: Tom Wanzek <https://github.com/tomwanzek>, Alex Ford <https://github.com/gustavderdrache>, Boris Yankov <https://github.com/borisyankov>
// Definitions by: Tom Wanzek <https://github.com/tomwanzek>
// Alex Ford <https://github.com/gustavderdrache>
// Boris Yankov <https://github.com/borisyankov>
// denisname <https://github.com/denisname>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

// Last module patch version validated against: 1.2.0
Expand All @@ -23,15 +26,15 @@ export interface FormatLocaleDefinition {
*/
grouping: number[];
/**
* The currency prefix and suffix (e.g., ["$", ""])
* The currency prefix and suffix (e.g., ["$", ""]).
*/
currency: [string, string];
/**
* An optional array of ten strings to replace the numerals 0-9.
*/
numerals?: string[];
/**
* An optional symbol to replace the `percent` suffix; the percent suffix (defaults to "%")
* An optional symbol to replace the `percent` suffix; the percent suffix (defaults to "%").
*/
percent?: string;
}
Expand All @@ -44,20 +47,22 @@ export interface FormatLocaleObject {
* Returns a new format function for the given string specifier. The returned function
* takes a number as the only argument, and returns a string representing the formatted number.
*
* @param specifier A Specifier string
* @param specifier A Specifier string.
* @throws Error on invalid format specifier.
*/
format(specifier: string): (n: number) => string;
format(specifier: string): (n: number | { valueOf(): number }) => string;

/**
* Returns a new format function for the given string specifier. The returned function
* takes a number as the only argument, and returns a string representing the formatted number.
* The returned function will convert values to the units of the appropriate SI prefix for the
* specified numeric reference value before formatting in fixed point notation.
*
* @param specifier A Specifier string
* @param specifier A Specifier string.
* @param value The reference value to determine the appropriate SI prefix.
* @throws Error on invalid format specifier.
*/
formatPrefix(specifier: string, value: number): (n: number) => string;
formatPrefix(specifier: string, value: number): (n: number | { valueOf(): number }) => string;
}

/**
Expand All @@ -71,7 +76,7 @@ export interface FormatSpecifier {
*/
fill: string;
/**
* Alignment used for format, as set by choosing one of the following
* Alignment used for format, as set by choosing one of the following:
*
* '>' - Forces the field to be right-aligned within the available space. (Default behavior).
* '<' - Forces the field to be left-aligned within the available space.
Expand All @@ -83,9 +88,9 @@ export interface FormatSpecifier {
* The sign can be:
*
* '-' - nothing for positive and a minus sign for negative. (Default behavior.)
* '+' - a plus sign for positive and a minus sign for negative.
* '+' - a plus sign for positive and a minus sign for negative.
* '(' - nothing for positive and parentheses for negative.
* ' '(space) - a space for positive and a minus sign for negative.
* ' ' (space) - a space for positive and a minus sign for negative.
*
*/
sign: '-' | '+' | '(' | ' ';
Expand All @@ -94,7 +99,7 @@ export interface FormatSpecifier {
*
* '$' - apply currency symbols per the locale definition.
* '#' - for binary, octal, or hexadecimal notation, prefix by 0b, 0o, or 0x, respectively.
* ''(none) - no symbol.
* '' (none) - no symbol. (Default behavior.)
*/
symbol: '$' | '#' | '';
/**
Expand All @@ -116,9 +121,9 @@ export interface FormatSpecifier {
* it defaults to 6 for all types except '' (none), which defaults to 12.
* Precision is ignored for integer formats (types 'b', 'o', 'd', 'x', 'X' and 'c').
*
* See precisionFixed and precisionRound for help picking an appropriate precision
* See precisionFixed and precisionRound for help picking an appropriate precision.
*/
precision: number;
precision: number | undefined;
/**
* The available type values are:
*
Expand All @@ -137,7 +142,7 @@ export interface FormatSpecifier {
* 'c' - converts the integer to the corresponding unicode character before printing.
* '' (none) - like g, but trim insignificant trailing zeros.
*
* The type 'n' is also supported as shorthand for ',g'. For the 'g', 'n' and ''(none) types,
* The type 'n' is also supported as shorthand for ',g'. For the 'g', 'n' and '' (none) types,
* decimal notation is used if the resulting string would have precision or fewer digits; otherwise, exponent notation is used.
*/
type: 'e' | 'f' | 'g' | 'r' | 's' | '%' | 'p' | 'b' | 'o' | 'd' | 'x' | 'X' | 'c' | '' | 'n';
Expand Down Expand Up @@ -173,25 +178,27 @@ export function formatDefaultLocale(defaultLocale: FormatLocaleDefinition): Form
* The general form of a specifier is [[fill]align][sign][symbol][0][width][,][.precision][type].
* For reference, an explanation of the segments of the specifier string, refer to the FormatSpecifier interface properties.
*
* @param specifier A Specifier string
* @param specifier A Specifier string.
* @throws Error on invalid format specifier.
*/
export function format(specifier: string): (n: number) => string;
export function format(specifier: string): (n: number | { valueOf(): number }) => string;

/**
* Returns a new format function for the given string specifier. The returned function
* takes a number as the only argument, and returns a string representing the formatted number.
* The returned function will convert values to the units of the appropriate SI prefix for the
* specified numeric reference value before formatting in fixed point notation.
*
* Uses the current default locale.
* Uses the current default locale.
*
* The general form of a specifier is [[fill]align][sign][symbol][0][width][,][.precision][type].
* For reference, an explanation of the segments of the specifier string, refer to the FormatSpecifier interface properties.
*
* @param specifier A Specifier string
* @param specifier A Specifier string.
* @param value The reference value to determine the appropriate SI prefix.
* @throws Error on invalid format specifier.
*/
export function formatPrefix(specifier: string, value: number): (n: number) => string;
export function formatPrefix(specifier: string, value: number): (n: number | { valueOf(): number }) => string;

/**
* Parses the specified specifier, returning an object with exposed fields that correspond to the
Expand All @@ -201,6 +208,7 @@ export function formatPrefix(specifier: string, value: number): (n: number) => s
* For reference, an explanation of the segments of the specifier string, refer to the FormatSpecifier interface properties.
*
* @param specifier A specifier string.
* @throws Error on invalid format specifier.
*/
export function formatSpecifier(specifier: string): FormatSpecifier;

Expand Down
2 changes: 1 addition & 1 deletion types/d3-format/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
Expand Down