From 31900353b6ea3e4570c15d1c1585685b4413d6d9 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 9 Oct 2020 21:46:45 +0300 Subject: feat: toggle between compact and full date format --- src/components/DateString/compactDateString.ts | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/components/DateString/compactDateString.ts (limited to 'src/components/DateString/compactDateString.ts') diff --git a/src/components/DateString/compactDateString.ts b/src/components/DateString/compactDateString.ts new file mode 100644 index 0000000..dffd912 --- /dev/null +++ b/src/components/DateString/compactDateString.ts @@ -0,0 +1,34 @@ +const metrics = [ + { name: 'minute', ratio: 60 }, + { name: 'hour', ratio: 60 }, + { name: 'day', ratio: 24 }, + { name: 'week', ratio: 7 }, + { name: 'month', ratio: 4 }, + { name: 'year', ratio: 12 } +]; + +const PRECISION = 0.75; + +const resolve = (value: number, metricIndex = 0): string => { + // Recursively divide value until it's ready to be presented as a string + const metric = metrics[metricIndex]; + const nextMetric = metrics[metricIndex + 1]; + const newValue = value / metric.ratio; + + if (newValue < nextMetric.ratio * PRECISION) { + const rounded = Math.round(newValue); + const isPlural = rounded > 1; + const count = isPlural ? rounded : 'a'; + const ending = isPlural ? 's' : ''; + return `${count} ${metric.name}${ending} ago`; + } + return resolve(newValue, metricIndex + 1); +}; + +const compactDateString = (date: Date): string => { + const now = new Date(); + const diff = (now.valueOf() - date.valueOf()) / 1000; + return resolve(diff); +}; + +export default compactDateString; -- cgit v1.2.3 From 4c73ebe644e7b781fee57bb295f8819bf6568628 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 9 Oct 2020 21:48:38 +0300 Subject: feat: display 'just now' if was just posted --- src/components/DateString/compactDateString.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'src/components/DateString/compactDateString.ts') diff --git a/src/components/DateString/compactDateString.ts b/src/components/DateString/compactDateString.ts index dffd912..f067b1a 100644 --- a/src/components/DateString/compactDateString.ts +++ b/src/components/DateString/compactDateString.ts @@ -28,6 +28,7 @@ const resolve = (value: number, metricIndex = 0): string => { const compactDateString = (date: Date): string => { const now = new Date(); const diff = (now.valueOf() - date.valueOf()) / 1000; + if (diff < 60) return 'just now'; return resolve(diff); }; -- cgit v1.2.3 From 5ee4f42b577449a58469fd3c7892455d097a6c79 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 10 Oct 2020 12:01:01 +0300 Subject: feat: increase diff rounding precision --- src/components/DateString/compactDateString.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/components/DateString/compactDateString.ts') diff --git a/src/components/DateString/compactDateString.ts b/src/components/DateString/compactDateString.ts index f067b1a..8bff4b7 100644 --- a/src/components/DateString/compactDateString.ts +++ b/src/components/DateString/compactDateString.ts @@ -7,7 +7,7 @@ const metrics = [ { name: 'year', ratio: 12 } ]; -const PRECISION = 0.75; +const PRECISION = 0.85; const resolve = (value: number, metricIndex = 0): string => { // Recursively divide value until it's ready to be presented as a string -- cgit v1.2.3