Factor out 'toCompactNotation'
This commit is contained in:
parent
b859283f82
commit
df4d142370
2 changed files with 14 additions and 10 deletions
|
@ -2,6 +2,7 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import 'scss/component/_view_count.scss';
|
import 'scss/component/_view_count.scss';
|
||||||
import * as PAGES from 'constants/pages';
|
import * as PAGES from 'constants/pages';
|
||||||
|
import { toCompactNotation } from 'util/string';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
uri: string,
|
uri: string,
|
||||||
|
@ -14,16 +15,7 @@ type Props = {
|
||||||
|
|
||||||
export default function FileViewCountInline(props: Props) {
|
export default function FileViewCountInline(props: Props) {
|
||||||
const { isLivestream, claim, viewCount, lang } = props;
|
const { isLivestream, claim, viewCount, lang } = props;
|
||||||
let formattedViewCount;
|
const formattedViewCount = toCompactNotation(viewCount, lang);
|
||||||
|
|
||||||
try {
|
|
||||||
formattedViewCount = Number(viewCount).toLocaleString(lang || 'en', {
|
|
||||||
compactDisplay: 'short',
|
|
||||||
notation: 'compact',
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
formattedViewCount = Number(viewCount).toLocaleString();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Limit the view-count visibility to specific pages for now. We'll eventually
|
// Limit the view-count visibility to specific pages for now. We'll eventually
|
||||||
// show it everywhere, so this band-aid would be the easiest to clean up
|
// show it everywhere, so this band-aid would be the easiest to clean up
|
||||||
|
|
|
@ -3,3 +3,15 @@
|
||||||
export function toCapitalCase(string: string) {
|
export function toCapitalCase(string: string) {
|
||||||
return string.charAt(0).toUpperCase() + string.slice(1);
|
return string.charAt(0).toUpperCase() + string.slice(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function toCompactNotation(number: string | number, lang: ?string) {
|
||||||
|
try {
|
||||||
|
return Number(number).toLocaleString(lang || 'en', {
|
||||||
|
compactDisplay: 'short',
|
||||||
|
notation: 'compact',
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
// Not all browsers support the addition options.
|
||||||
|
return Number(number).toLocaleString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue