@sumup/intl
Advanced tools
Comparing version 1.0.0-alpha.1 to 1.0.0-alpha.2
@@ -15,4 +15,5 @@ /** | ||
*/ | ||
import { Value, DecimalOptions, CurrencyOptions } from './types'; | ||
export declare function format(value: Value, options?: DecimalOptions): string; | ||
export declare function formatCurrency(value: Value, options?: CurrencyOptions): string; | ||
export declare const format: (value: number, locales?: string | string[] | undefined, args_0?: Intl.NumberFormatOptions | undefined) => string; | ||
export declare const formatCurrency: (value: number, locales?: string | string[] | undefined, args_0?: string | undefined, args_1?: Intl.NumberFormatOptions | undefined) => string; | ||
export declare const formatToParts: (value: number, locales?: string | string[] | undefined, args_0?: Intl.NumberFormatOptions | undefined) => Intl.NumberFormatPart[]; | ||
export declare const formatCurrencyToParts: (value: number, locales?: string | string[] | undefined, args_0?: string | undefined, args_1?: Intl.NumberFormatOptions | undefined) => Intl.NumberFormatPart[]; |
@@ -17,31 +17,9 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const locales_1 = require("./lib/locales"); | ||
const currencies_1 = require("./lib/currencies"); | ||
const intl_1 = require("./lib/intl"); | ||
function baseFormat(value, locales, options) { | ||
const formatFn = intl_1.getFormatFn(locales, options); | ||
return formatFn(value); | ||
} | ||
function format(value, options) { | ||
var _a; | ||
const locales = locales_1.getLocales((_a = options) === null || _a === void 0 ? void 0 : _a.locale); | ||
return baseFormat(value, locales, Object.assign(Object.assign({}, options), { style: 'decimal' })); | ||
} | ||
exports.format = format; | ||
function formatCurrency(value, options) { | ||
var _a, _b; | ||
const locales = locales_1.getLocales((_a = options) === null || _a === void 0 ? void 0 : _a.locale); | ||
const currency = ((_b = options) === null || _b === void 0 ? void 0 : _b.currency) || currencies_1.getCurrency(locales); | ||
if (!currency) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error([ | ||
`No currency found for "${locales.join(', ')}".`, | ||
'Explicitely pass a currency as part of the options', | ||
'or submit a new one on GitHub.', | ||
].join(' ')); | ||
} | ||
return baseFormat(value, locales, Object.assign(Object.assign({}, options), { style: 'decimal' })); | ||
} | ||
return baseFormat(value, locales, Object.assign(Object.assign({}, options), { style: 'currency', currency })); | ||
} | ||
exports.formatCurrency = formatCurrency; | ||
/* eslint-disable @typescript-eslint/unbound-method */ | ||
const base_1 = require("./base"); | ||
const numbers_1 = require("./numbers"); | ||
const currencies_1 = require("./currencies"); | ||
exports.format = base_1.formatFactory(numbers_1.getDecimalOptions); | ||
exports.formatCurrency = base_1.formatFactory(currencies_1.getCurrencyOptions); | ||
exports.formatToParts = base_1.formatToPartsFactory(numbers_1.getDecimalOptions); | ||
exports.formatCurrencyToParts = base_1.formatToPartsFactory(currencies_1.getCurrencyOptions); |
@@ -17,11 +17,25 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* eslint-disable no-irregular-whitespace */ | ||
const _1 = require("."); | ||
describe('Format', () => { | ||
describe('decimals', () => { | ||
it('should format a number with default options', () => { | ||
const actual = _1.format(123456.789); | ||
const expected = '123,456.789'; | ||
expect(actual).toBe(expected); | ||
describe('number', () => { | ||
it('should format for a locale', () => { | ||
const actual = _1.format(123456.789, 'PT'); | ||
expect(actual).toMatchInlineSnapshot(`"123.456,789"`); | ||
}); | ||
it('should format for an array of locales', () => { | ||
const actual = _1.format(123456.789, ['CN', 'PT']); | ||
expect(actual).toMatchInlineSnapshot(`"123.456,789"`); | ||
}); | ||
}); | ||
describe('currency', () => { | ||
it('should format for a locale', () => { | ||
const actual = _1.formatCurrency(123456.789, 'PT'); | ||
expect(actual).toMatchInlineSnapshot(`"€ 123.456,79"`); | ||
}); | ||
it('should format for an array of locales', () => { | ||
const actual = _1.formatCurrency(123456.789, ['CN', 'PT']); | ||
expect(actual).toMatchInlineSnapshot(`"€ 123.456,79"`); | ||
}); | ||
}); | ||
}); |
@@ -15,16 +15,10 @@ /** | ||
*/ | ||
export declare type Value = number; | ||
export declare type Locales = string[]; | ||
export declare type Locale = 'at' | 'be' | 'bg' | 'br' | 'ch' | 'cl' | 'cy' | 'cz' | 'de' | 'dk' | 'ee' | 'es' | 'fi' | 'fr' | 'gb' | 'gr' | 'hr' | 'hu' | 'ie' | 'it' | 'lt' | 'lu' | 'lv' | 'mt' | 'nl' | 'no' | 'pl' | 'pt' | 'ro' | 'ru' | 'se' | 'si' | 'sk' | 'us' | string; | ||
export interface BaseOptions { | ||
locale?: Locale | Locale[]; | ||
localeMatcher?: 'best fit' | 'lookup'; | ||
style?: 'decimal' | 'currency' | 'percent' | 'unit'; | ||
export interface DecimalOptions extends Intl.NumberFormatOptions { | ||
style: 'decimal'; | ||
} | ||
export declare type DecimalOptions = BaseOptions; | ||
export interface CurrencyOptions extends BaseOptions { | ||
currency?: string; | ||
currencyDisplay?: 'symbol' | 'code' | 'name'; | ||
export interface CurrencyOptions extends Intl.NumberFormatOptions { | ||
style: 'currency'; | ||
currency: string; | ||
} | ||
export declare type Options = DecimalOptions | CurrencyOptions; | ||
export declare type FormatFn = (value: Value) => string; |
@@ -15,4 +15,5 @@ /** | ||
*/ | ||
import { Value, DecimalOptions, CurrencyOptions } from './types'; | ||
export declare function format(value: Value, options?: DecimalOptions): string; | ||
export declare function formatCurrency(value: Value, options?: CurrencyOptions): string; | ||
export declare const format: (value: number, locales?: string | string[] | undefined, args_0?: Intl.NumberFormatOptions | undefined) => string; | ||
export declare const formatCurrency: (value: number, locales?: string | string[] | undefined, args_0?: string | undefined, args_1?: Intl.NumberFormatOptions | undefined) => string; | ||
export declare const formatToParts: (value: number, locales?: string | string[] | undefined, args_0?: Intl.NumberFormatOptions | undefined) => Intl.NumberFormatPart[]; | ||
export declare const formatCurrencyToParts: (value: number, locales?: string | string[] | undefined, args_0?: string | undefined, args_1?: Intl.NumberFormatOptions | undefined) => Intl.NumberFormatPart[]; |
@@ -16,43 +16,10 @@ "use strict"; | ||
*/ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var locales_1 = require("./lib/locales"); | ||
var currencies_1 = require("./lib/currencies"); | ||
var intl_1 = require("./lib/intl"); | ||
function baseFormat(value, locales, options) { | ||
var formatFn = intl_1.getFormatFn(locales, options); | ||
return formatFn(value); | ||
} | ||
function format(value, options) { | ||
var _a; | ||
var locales = locales_1.getLocales((_a = options) === null || _a === void 0 ? void 0 : _a.locale); | ||
return baseFormat(value, locales, __assign(__assign({}, options), { style: 'decimal' })); | ||
} | ||
exports.format = format; | ||
function formatCurrency(value, options) { | ||
var _a, _b; | ||
var locales = locales_1.getLocales((_a = options) === null || _a === void 0 ? void 0 : _a.locale); | ||
var currency = ((_b = options) === null || _b === void 0 ? void 0 : _b.currency) || currencies_1.getCurrency(locales); | ||
if (!currency) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw new Error([ | ||
"No currency found for \"" + locales.join(', ') + "\".", | ||
'Explicitely pass a currency as part of the options', | ||
'or submit a new one on GitHub.', | ||
].join(' ')); | ||
} | ||
return baseFormat(value, locales, __assign(__assign({}, options), { style: 'decimal' })); | ||
} | ||
return baseFormat(value, locales, __assign(__assign({}, options), { style: 'currency', currency: currency })); | ||
} | ||
exports.formatCurrency = formatCurrency; | ||
/* eslint-disable @typescript-eslint/unbound-method */ | ||
var base_1 = require("./base"); | ||
var numbers_1 = require("./numbers"); | ||
var currencies_1 = require("./currencies"); | ||
exports.format = base_1.formatFactory(numbers_1.getDecimalOptions); | ||
exports.formatCurrency = base_1.formatFactory(currencies_1.getCurrencyOptions); | ||
exports.formatToParts = base_1.formatToPartsFactory(numbers_1.getDecimalOptions); | ||
exports.formatCurrencyToParts = base_1.formatToPartsFactory(currencies_1.getCurrencyOptions); |
@@ -17,11 +17,25 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* eslint-disable no-irregular-whitespace */ | ||
var _1 = require("."); | ||
describe('Format', function () { | ||
describe('decimals', function () { | ||
it('should format a number with default options', function () { | ||
var actual = _1.format(123456.789); | ||
var expected = '123,456.789'; | ||
expect(actual).toBe(expected); | ||
describe('number', function () { | ||
it('should format for a locale', function () { | ||
var actual = _1.format(123456.789, 'PT'); | ||
expect(actual).toMatchInlineSnapshot("\"123.456,789\""); | ||
}); | ||
it('should format for an array of locales', function () { | ||
var actual = _1.format(123456.789, ['CN', 'PT']); | ||
expect(actual).toMatchInlineSnapshot("\"123.456,789\""); | ||
}); | ||
}); | ||
describe('currency', function () { | ||
it('should format for a locale', function () { | ||
var actual = _1.formatCurrency(123456.789, 'PT'); | ||
expect(actual).toMatchInlineSnapshot("\"\u20AC\u00A0123.456,79\""); | ||
}); | ||
it('should format for an array of locales', function () { | ||
var actual = _1.formatCurrency(123456.789, ['CN', 'PT']); | ||
expect(actual).toMatchInlineSnapshot("\"\u20AC\u00A0123.456,79\""); | ||
}); | ||
}); | ||
}); |
@@ -15,16 +15,10 @@ /** | ||
*/ | ||
export declare type Value = number; | ||
export declare type Locales = string[]; | ||
export declare type Locale = 'at' | 'be' | 'bg' | 'br' | 'ch' | 'cl' | 'cy' | 'cz' | 'de' | 'dk' | 'ee' | 'es' | 'fi' | 'fr' | 'gb' | 'gr' | 'hr' | 'hu' | 'ie' | 'it' | 'lt' | 'lu' | 'lv' | 'mt' | 'nl' | 'no' | 'pl' | 'pt' | 'ro' | 'ru' | 'se' | 'si' | 'sk' | 'us' | string; | ||
export interface BaseOptions { | ||
locale?: Locale | Locale[]; | ||
localeMatcher?: 'best fit' | 'lookup'; | ||
style?: 'decimal' | 'currency' | 'percent' | 'unit'; | ||
export interface DecimalOptions extends Intl.NumberFormatOptions { | ||
style: 'decimal'; | ||
} | ||
export declare type DecimalOptions = BaseOptions; | ||
export interface CurrencyOptions extends BaseOptions { | ||
currency?: string; | ||
currencyDisplay?: 'symbol' | 'code' | 'name'; | ||
export interface CurrencyOptions extends Intl.NumberFormatOptions { | ||
style: 'currency'; | ||
currency: string; | ||
} | ||
export declare type Options = DecimalOptions | CurrencyOptions; | ||
export declare type FormatFn = (value: Value) => string; |
{ | ||
"name": "@sumup/intl", | ||
"version": "1.0.0-alpha.1", | ||
"version": "1.0.0-alpha.2", | ||
"description": "A partial polyfill for the JavaScript Internationalization API, catered to SumUp's needs", | ||
@@ -5,0 +5,0 @@ "repository": "git@github.com:sumup-oss/intl-js.git", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58663
37
980
1