
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
@formatjs/intl-utils
Advanced tools
@formatjs/intl-utils is a utility library that provides various internationalization (i18n) utilities. It is part of the FormatJS suite and is designed to help with formatting dates, numbers, and messages in a way that is locale-aware.
Date Formatting
This feature allows you to format dates according to the specified locale and options. The example formats the current date in the 'en-US' locale.
const { formatDate } = require('@formatjs/intl-utils');
const date = new Date();
const formattedDate = formatDate(date, { year: 'numeric', month: 'long', day: 'numeric' }, 'en-US');
console.log(formattedDate); // Output: 'October 5, 2023'
Number Formatting
This feature allows you to format numbers according to the specified locale and options. The example formats a number as a currency in the 'en-US' locale.
const { formatNumber } = require('@formatjs/intl-utils');
const number = 1234567.89;
const formattedNumber = formatNumber(number, { style: 'currency', currency: 'USD' }, 'en-US');
console.log(formattedNumber); // Output: '$1,234,567.89'
Message Formatting
This feature allows you to format messages with placeholders according to the specified locale and values. The example formats a message by replacing the placeholder with a value in the 'en-US' locale.
const { formatMessage } = require('@formatjs/intl-utils');
const message = 'Hello, {name}!';
const values = { name: 'John' };
const formattedMessage = formatMessage(message, values, 'en-US');
console.log(formattedMessage); // Output: 'Hello, John!'
The 'intl' package provides the ECMAScript Internationalization API as a polyfill. It offers similar functionalities for date, number, and message formatting but is more focused on providing a standard API that is part of the ECMAScript specification.
The 'i18next' package is a powerful internationalization framework for JavaScript. It provides extensive support for translation, formatting, and localization. Compared to @formatjs/intl-utils, it offers a more comprehensive solution for managing translations and localization in applications.
The 'moment' package is a popular library for parsing, validating, manipulating, and formatting dates. While it is not specifically focused on internationalization, it provides robust date formatting capabilities that can be used in conjunction with locale settings.
Provide i18n utilities.
This function determines the best fit
unit based on a specific set of customizable thresholds.
function selectUnit(
from: Date | number,
to: Date | number = Date.now(),
thresholds = DEFAULT_THRESHOLDS
): {value: number; unit: Unit};
where thresholds
has the shape of:
interface Threshold {
second: number;
minute: number;
hour: number;
day: number;
}
month
& year
are based on calendar, thus not customizable.
Example:
import {selectUnit} from '@formatjs/intl-utils';
selectUnit(Date.now() - 1000); // { value: -1, unit: 'second' }
selectUnit(Date.now() - 44000); // { value: -44, unit: 'second' }
selectUnit(Date.now() - 50000); // { value: 1, unit: 'minute' }
selectUnit
is meant to be a stepping stone from the old IntlRelativeFormat
to the officially spec-ed Intl.RelativeTimeFormat
. Therefore we don't recommend using this for an extended period of time because of ambiguous editorial issues such as:
From 2019/01/01 -> 2018/11/01 can technically be last year
, 2 months ago
or a quarter ago
.
From 2019/01/02 6am to 2019/01/01 11pm can also be 7 hours ago
or yesterday
. Timezone further complicates the issue.
The examples above have not even tackled the differences in non-Gregorian calendars. There is an issue opened upstream in the spec that potentially introduces a best fit
algorithm. Therefore, we recommend that you implement your own version of selectUnit
that matches your editorial expectation. This will be removed in future releases.
FAQs
Smartly determine best unit for relative time format
The npm package @formatjs/intl-utils receives a total of 176,876 weekly downloads. As such, @formatjs/intl-utils popularity was classified as popular.
We found that @formatjs/intl-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.