Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
d2l-intl
Advanced tools
Readme
This library consists of four sets of APIs (each described in detail below) to format and parse numbers, dates and times in JavaScript.
Why not just use the standard ECMAScript Internationalization API (ECMA-402) and related polyfills? Firstly, the standard doesn't include any parsing functionality. Additionally, Brightspace supports fine-grained locale customization at the organization and user levels -- a level of configuration that simply isn't present in the standard. This library does attempt to follow the standard API syntax and naming conventions when possible.
Install from NPM:
npm install d2l-intl
In NPM, require it normally:
const d2lIntl = require('d2l-intl');
In ES6, use an import
statement:
import d2lIntl from 'd2l-intl';
If you're using Polymer to write a web component or application, use d2l-localize-behavior instead, as it wraps d2l-intl
and exposes its functionality.
Each of the APIs have a locales
argument, which must be a string language tag (or array of language tags) matching a locale supported by Brightspace. A list of all supported locales can be found in the locale-data
directory.
For example, to create a number formatter using the French Canadian (fr-CA
) locale:
var formatter = new d2lIntl.NumberFormat('fr-CA');
If the provided locale isn't supported (e.g. fr-BE
), the base language (fr
) will be used.
If an array of language tags is provided, the resolved locale will be the first supported locale.
All locale data can be overridden by providing a locale
option. Only the settings you specify will be overridden. For example, to use the tr-TR
locale, but override the decimal symbol (which for Turkish is a comma):
var options = {
locale: {
number: {
symbols: {
decimal: '.'
}
}
}
};
new d2lIntl.NumberFormat('tr-TR', options).format(3.14); // -> 3.14
The full set of overridable locale data can be found by inspecting one of the JSON files in the locale-data
directory.
All locale data can be retrieved by providing a 'locale' option. The locale data can be overridden.
var localeData = d2lIntl.LocaleProvider('fr');
Integer and decimal numbers can be formatted in the user's locale using the NumberFormat
class. It intentionally mirrors the ECMA-402 Intl.NumberFormat class.
Syntax:
var formatter = new d2lIntl.NumberFormat(locales[, options]);
Options:
minimumFractionDigits
and 3.Example 1: formatting as an integer (rounded to 0 decimal places)
var formatter = new d2lIntl.NumberFormat('en', {
maximumFractionDigits: 0
});
console.log(formatter.format(89.72)); // -> 90
Example 2: formatting as a percentage (rounded to 2 decimal places, but always showing at least 2 decimals)
var formatter = new d2lIntl.NumberFormat('en', {
style: 'percent',
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
console.log(formatter.format(0.333)); // -> 33.30%
The NumberParse
object can be used to parse an integer or decimal number written in the user's locale.
Syntax:
var parser = new d2lIntl.NumberParse(locales[, options]);
Options:
Example:
var parser = new d2lIntl.NumberParse('fr-CA');
console.log(parser.parse('-8 942,39')); // -> -8942.39
Dates and times can be formatted in the user's locale using the DateTimeFormat
class. It behaves similar to the ECMA-402 Intl.DateTimeFormat class.
Syntax:
var formatter = new d2lIntl.DateTimeFormat(locales[, options]);
Options:
All the date and time formatting methods take a JavaScript Date object as input.
To format a date and time, use the format
method:
var formatter = new d2lIntl.DateTimeFormat('sv-SE');
var time = formatter.format(
new Date(2015, 8, 23, 14, 5)
); // -> 2015-09-23 14:05
To format a time only (without the date portion), use the formatTime
method:
var formatter = new d2lIntl.DateTimeFormat('ko');
var time = formatter.formatTime(
new Date(2015, 8, 23, 14, 5)
); // -> 오후 14:05
To format a date only (without the time portion), use the formatDate
method:
var formatter = new d2lIntl.DateTimeFormat('es-MX', {
format: 'full'
});
console.log(
formatter.formatDate(new Date(2015, 8, 23))
); // -> miércoles 23 de septiembre de 2015
The DateTimeParse
object can be used to parse a date or time written in the user's locale.
Syntax:
var parser = new d2lIntl.DateTimeParse(locales[, options]);
Options:
Both the parseDate
and parseTime
methods take a string input and return a JavaScript Date object.
To parse a time, use the parseTime
method:
var parser = new d2lIntl.DateTimeParse('fr-CA');
var time = parser.parseTime('14 h 05');
console.log(
time.getHours(), // -> 14
time.getMinutes() // -> 5
);
To parse a date, use the parseDate
method:
var parser = new d2lIntl.DateTimeParse('fr-CA');
var date = parser.parseDate('2015-09-23');
console.log(
date.getFullYear(), // -> 2015
date.getMonth(), // -> 8 (months are 0-11)
date.getDate() // -> 23
);
The FileSizeFormat
object can be used to format a file size appropriately for the user's locale.
Syntax:
var formatFS = new d2lIntl.FileSizeFormat(locale[, options]);
To format a file size, call the format
method:
var formatFS = new d2lIntl.FileSizeFormat('en-US');
var fileSize = formatFS.format(100);
console.log(fileSize) // -> 100 Bytes
Contributions are welcome, please submit a pull request!
This repository is configured with EditorConfig rules and contributions should make use of them.
FAQs
D2L internationalization APIs for number, date and time formatting and parsing.
The npm package d2l-intl receives a total of 1,421 weekly downloads. As such, d2l-intl popularity was classified as popular.
We found that d2l-intl demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.