Skip to content

Commit

Permalink
fix: improve types of function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer committed Oct 17, 2022
1 parent d86dbfe commit de86dad
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 95 deletions.
80 changes: 9 additions & 71 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,80 +13,18 @@
* limitations under the License.
*/

/* eslint-disable @typescript-eslint/unbound-method */

import type { Currency } from './types';
import {
formatNumberFactory,
formatNumberToPartsFactory,
resolveNumberFormatFactory,
getNumberOptions,
getCurrencyOptions,
} from './lib/number-format';

export {
format,
formatNumber,
formatCurrency,
formatToParts,
formatNumberToParts,
formatCurrencyToParts,
resolveFormat,
resolveNumberFormat,
resolveCurrencyFormat,
isNumberFormatSupported,
isNumberFormatToPartsSupported,
isIntlSupported,
} from './lib/number-format';
export { CURRENCIES } from './data/currencies';

type NumberArgs = [Intl.NumberFormatOptions?];
type CurrencyArgs = [Currency?, Intl.NumberFormatOptions?];

/**
* Formats a number according to the locale with support for various
* [styles, units](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_style_and_unit),
* and [notations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_notation).
*/
export const formatNumber = formatNumberFactory<NumberArgs>(getNumberOptions);

/**
* @deprecated Use {@link formatNumber} instead.
*/
export const format = formatNumber;

/**
* Formats a number according to the locale in the country's official currency
* with support for various [notations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_notation).
*/
export const formatCurrency =
formatNumberFactory<CurrencyArgs>(getCurrencyOptions);

/**
* Formats a number according to the locale with support for various
* [styles, units](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_style_and_unit),
* and [notations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_notation).
*/
export const formatNumberToParts =
formatNumberToPartsFactory<NumberArgs>(getNumberOptions);

/**
* @deprecated Use {@link formatNumberToParts} instead.
*/
export const formatToParts = formatNumberToParts;

/**
* Formats a number according to the locale in the country's official currency
* with support for various [notations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_notation).
*/
export const formatCurrencyToParts =
formatNumberToPartsFactory<CurrencyArgs>(getCurrencyOptions);

/**
* Resolves the locale and collation options that are used to format a number.
*/
export const resolveNumberFormat =
resolveNumberFormatFactory<NumberArgs>(getNumberOptions);

/**
* @deprecated Use {@link resolveNumberFormat} instead.
*/
export const resolveFormat = resolveNumberFormat;

/**
* Resolves the locale and collation options that are used to format a number
* in the country's official currency.
*/
export const resolveCurrencyFormat =
resolveNumberFormatFactory<CurrencyArgs>(getCurrencyOptions);
135 changes: 111 additions & 24 deletions src/lib/number-format/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
* limitations under the License.
*/

import type { Locale, NumberFormat, NumericOptions } from '../../types';
import type {
Currency,
Locale,
NumberFormat,
NumericOptions,
} from '../../types';
import { findIndex } from '../find-index';

import {
Expand All @@ -22,28 +27,54 @@ import {
isNumberFormatToPartsSupported,
getNumberFormat,
} from './intl';
import { getNumberOptions } from './numbers';
import { getCurrencyOptions } from './currencies';

export { getNumberOptions } from './numbers';
export { getCurrencyOptions } from './currencies';
export {
isIntlSupported,
isNumberFormatSupported,
isNumberFormatToPartsSupported,
};

type Args = Array<unknown>;
type GetOptions = (
locales: Locale | Locale[],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...args: any[]
) => NumericOptions;

type GetOptions<T extends Args> = (
/**
* Formats a number according to the locale with support for various
* [styles, units](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_style_and_unit),
* and [notations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_notation).
*/
export const formatNumber = formatNumberFactory(getNumberOptions) as (
value: number,
locales?: Locale | Locale[],
...args: T
) => NumericOptions;
options?: Intl.NumberFormatOptions,
) => string;

/**
* @deprecated Use {@link formatNumber} instead.
*/
export const format = formatNumber;

/**
* Formats a number according to the locale in the country's official currency
* with support for various [notations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_notation).
*/
export const formatCurrency = formatNumberFactory(getCurrencyOptions) as (
value: number,
locales?: Locale | Locale[],
currency?: Currency,
options?: Intl.NumberFormatOptions,
) => string;

export function formatNumberFactory<T extends Args>(
getOptions: GetOptions<T>,
): (value: number, locales?: Locale | Locale[], ...args: T) => string {
function formatNumberFactory<T extends GetOptions>(
getOptions: T,
): (value: number, ...args: Parameters<T>) => string {
if (!isNumberFormatSupported) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return (value, _locales, ..._args): string => `${value}`;
return (value, _locales, ..._args): string => value.toLocaleString();
}

return (value, locales, ...args): string => {
Expand All @@ -53,17 +84,44 @@ export function formatNumberFactory<T extends Args>(
};
}

export function formatNumberToPartsFactory<T extends Args>(
getOptions: GetOptions<T>,
): (
/**
* Formats a number according to the locale with support for various
* [styles, units](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_style_and_unit),
* and [notations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_notation).
*/
export const formatNumberToParts = formatNumberToPartsFactory(
getNumberOptions,
) as (
value: number,
locales?: Locale | Locale[],
...args: T
) => Intl.NumberFormatPart[] {
options?: Intl.NumberFormatOptions,
) => Intl.NumberFormatPart[];

/**
* @deprecated Use {@link formatNumberToParts} instead.
*/
export const formatToParts = formatNumberToParts;

/**
* Formats a number according to the locale in the country's official currency
* with support for various [notations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_notation).
*/
export const formatCurrencyToParts = formatNumberToPartsFactory(
getCurrencyOptions,
) as (
value: number,
locales?: Locale | Locale[],
currency?: Currency,
options?: Intl.NumberFormatOptions,
) => Intl.NumberFormatPart[];

function formatNumberToPartsFactory<T extends GetOptions>(
getOptions: T,
): (value: number, ...args: Parameters<T>) => Intl.NumberFormatPart[] {
if (!isNumberFormatToPartsSupported) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return (value, _locales, ..._args): Intl.NumberFormatPart[] => [
{ type: 'integer', value: value.toString() },
{ type: 'integer', value: value.toLocaleString() },
];
}

Expand All @@ -74,15 +132,37 @@ export function formatNumberToPartsFactory<T extends Args>(
};
}

const TEST_VALUE = 1001001001.11111;
/**
* Resolves the locale and collation options that are used to format a number.
*/
export const resolveNumberFormat = resolveNumberFormatFactory(
getNumberOptions,
) as (
locales?: Locale | Locale[],
options?: Intl.NumberFormatOptions,
) => NumberFormat | null;

function getPart(parts: Intl.NumberFormatPart[], name: string): string {
return parts.find((part) => part.type === name)?.value as string;
}
/**
* @deprecated Use {@link resolveNumberFormat} instead.
*/
export const resolveFormat = resolveNumberFormat;

/**
* Resolves the locale and collation options that are used to format a number
* in the country's official currency.
*/
export const resolveCurrencyFormat = resolveNumberFormatFactory(
getCurrencyOptions,
) as (
locales?: Locale | Locale[],
options?: Intl.NumberFormatOptions,
) => NumberFormat | null;

export function resolveNumberFormatFactory<T extends Args>(
getOptions: GetOptions<T>,
): (locales?: Locale | Locale[], ...args: T) => null | NumberFormat {
const TEST_VALUE = 1001001001.11111;

function resolveNumberFormatFactory<T extends GetOptions>(
getOptions: T,
): (...args: Parameters<T>) => NumberFormat | null {
if (!isNumberFormatToPartsSupported) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return (_locales, ..._args) => null;
Expand Down Expand Up @@ -116,3 +196,10 @@ export function resolveNumberFormatFactory<T extends Args>(
return { ...resolvedOptions, groupDelimiter, decimalDelimiter };
};
}

function getPart(
parts: Intl.NumberFormatPart[],
name: Intl.NumberFormatPart['type'],
): string | undefined {
return parts.find((part) => part.type === name)?.value;
}

0 comments on commit de86dad

Please sign in to comment.