
Security News
/Research
Wallet-Draining npm Package Impersonates Nodemailer to Hijack Crypto Transactions
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
react-intl-formatted-duration
Advanced tools
react-intl is an amazing library providing React components and API to localize your application, however it lacks a Duration
component.
If you want to show the time it takes to do something like 1 minute
or 5 minutes
or even a simple timer 0:30
you're out of luck because the ECMA committee hasn't specified the DurationFormat yet.
This component provides a very simple abstraction that works on React (DOM), React Native and any other target environment to format simple durations.
npm i --save react-intl-formatted-duration
// Using React DOM
import React from 'react';
import FormattedDuration from 'react-intl-formatted-duration';
import styled from 'styled-components';
const Text = styled.span``;
export default MyComponent() {
return <FormattedDuration value={60} textComponent={Text} />
// will render `1 minute`
}
It's not necessary to use styled components, you can use any component you like available on your target environment
// Using React Native
import React from 'react';
import FormattedDuration from 'react-intl-formatted-duration';
import { Text } from 'react-native';
export default MyComponent() {
return <FormattedDuration value={60} textComponent={Text} />
// will render `1 minute`
}
If you want to style numbers differently from text you can pass a valueComponent
<FormattedDuration value={90} textComponent={Text} valueComponent={Value} />
// renders
<Value>1</Value> <Text>minute</Text> <Value>30</Value> <Text>seconds</Text>
Having different components is useful not only for styling. Some languages use different numbering systems. For example Japanese has full-width numbers, so you might want to render 10分
instead of 10分
, to do so you can use:
import React from 'react';
import { FormattedNumber } from 'react-intl';
import FormattedDuration from 'react-intl-formatted-duration';
/*
You'll also need to select Japanese locale and configure the IntlProvider to use
`ja-JP-u-nu-fullwide`
Somewhere in you application
import { IntlProvider } from 'react-intl';
<IntlProvider
locale="ja-JP-u-nu-fullwide"
/>
*/
export default MyComponent() {
return <FormattedDuration value={600} textComponent={Text} valueComponent={FormattedNumber} />
// will render `10分`
}
import FormattedDuration, { TIMER_FORMAT } from 'react-intl-formatted-duration';
export default MyComponent() {
return <FormattedDuration value={60} textComponent={Text} format={TIMER_FORMAT} />
// will render `1:00`
}
react-intl-formatted-duration
expects the following keys inside your translation file
react-intl-formatted-duration.longFormatting
the default format that generates something like 1 minute 30 seconds
. It uses the values {minutes}
and {seconds}
. For example you can change it to {minutes} and {seconds}
.react-intl-formatted-duration.duration
the format used by the minutes
and seconds
variables described above. It uses the values {value}
and {unit}
. The default is {value} {unit}
where value
is a number and {unit}
is the textual unit like minute(s)
or second(s)
.react-intl-formatted-duration.timerFormatting
format for TIMER_FORMAT
, defaults to {minutes}:{seconds}
where both values are numbers padded to have a minimum length of 2 charactersreact-intl-formatted-duration.minutesPlural
string for plural minutesreact-intl-formatted-duration.minutesSingular
string for singular minutesreact-intl-formatted-duration.secondsPlural
string for plural secondsreact-intl-formatted-duration.secondsSingular
string for singular secondsIf you're using the extract-intl
script from react-boilerplate you can import react-intl-formatted-duration/messages
to automatically generate the keys in your translation files.
FAQs
React intl component to express time duration
The npm package react-intl-formatted-duration receives a total of 8,254 weekly downloads. As such, react-intl-formatted-duration popularity was classified as popular.
We found that react-intl-formatted-duration 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
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.