Skip to content
This repository has been archived by the owner on Aug 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3114 from bbc/format-duration-utility
Browse files Browse the repository at this point in the history
Add formatDuration utility function to Timestamp-container package
  • Loading branch information
itimirichard authored Feb 12, 2020
2 parents 39c660c + 8b8e4e5 commit f96f948
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- prettier-ignore -->
| Version | Description |
|---------|-------------|
| 2.7.3 | [PR#3114](https://github.com/bbc/psammead/pull/3114) Added formatDuration function to timestampUtilities |
| 2.7.2 | [PR#3085](https://github.com/bbc/psammead/pull/3085) Rename timestampUtilities with index |
| 2.7.1 | [PR#3082](https://github.com/bbc/psammead/pull/3082) Talos - Bump Dependencies - @bbc/psammead-locales |
| 2.7.0 | [PR#3078](https://github.com/bbc/psammead/pull/3078) Export timestamp utilities |
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bbc/psammead-timestamp-container",
"version": "2.7.2",
"version": "2.7.3",
"main": "dist/index.js",
"module": "esm/index.js",
"sideEffects": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ moment.relativeTimeThreshold('M', 12);

const defaultFormat = 'LL, LT z';

export const formatDuration = (durationValue, format = 'mm:ss') => {
const durationInMilliseconds = moment
.duration(durationValue)
.asMilliseconds();
return moment.utc(durationInMilliseconds).format(format);
};

// if the date is invalid return false - https://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript#answer-1353711
export const isValidDateTime = dateTime => {
// eslint-disable-next-line no-restricted-globals
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import moment from 'moment-timezone';
import { unixTimestampToMoment, formatUnixTimestamp, isValidDateTime } from '.';
import {
unixTimestampToMoment,
formatUnixTimestamp,
isValidDateTime,
formatDuration,
} from '.';
import timestampGenerator from '../helpers/testHelpers';

const timezone = 'Europe/London';
const timestamp = 1539969006000; // 19 October 2018
const locale = 'en-gb';

describe('Timestamp utility functions', () => {
describe('formatDuration', () => {
it('should return duration in expected format', () => {
const durationInISO8601Format = 'PT30M'; // 30:00
expect(formatDuration(durationInISO8601Format)).toEqual('30:00');
});
it('should return duration in expected format when a format is passed in', () => {
const durationInISO8601Format = 'PT30M'; // 30:00
expect(formatDuration(durationInISO8601Format, 'mm,ss')).toEqual('30,00');
});
});

describe('isValidDateTime', () => {
it('should return true if timestamp is valid', () => {
expect(isValidDateTime(timestamp)).toEqual(true);
Expand Down

0 comments on commit f96f948

Please sign in to comment.