Replace momentjs - it is too large of a dependency

zio/stable
Paul Frazee 2022-07-26 10:03:52 -05:00
parent 041bfa22a9
commit efc28b0098
7 changed files with 37 additions and 35 deletions

View File

@ -32,7 +32,6 @@
"lodash.omit": "^4.5.0",
"mobx": "^6.6.1",
"mobx-react-lite": "^3.4.0",
"moment": "^2.29.4",
"react": "17.0.2",
"react-circular-progressbar": "^2.1.0",
"react-dom": "17.0.2",

View File

@ -2,11 +2,11 @@ import React from 'react'
import {observer} from 'mobx-react-lite'
import {Image, StyleSheet, Text, TouchableOpacity, View} from 'react-native'
import {bsky, AdxUri} from '@adxp/mock-api'
import moment from 'moment'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {OnNavigateContent} from '../../routes/types'
import {FeedViewItemModel} from '../../../state/models/feed-view'
import {s} from '../../lib/styles'
import {ago} from '../../lib/strings'
import {AVIS} from '../../lib/assets'
export const FeedItem = observer(function FeedItem({
@ -78,7 +78,7 @@ export const FeedItem = observer(function FeedItem({
@{item.author.name}
</Text>
<Text style={[styles.metaItem, s.f14, s.gray]}>
&middot; {moment(item.indexedAt).fromNow(true)}
&middot; {ago(item.indexedAt)}
</Text>
</View>
<Text style={[styles.postText, s.f15, s['lh15-1.3']]}>

View File

@ -2,12 +2,11 @@ import React from 'react'
import {observer} from 'mobx-react-lite'
import {Image, StyleSheet, Text, TouchableOpacity, View} from 'react-native'
import {bsky, AdxUri} from '@adxp/mock-api'
import moment from 'moment'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {OnNavigateContent} from '../../routes/types'
import {PostThreadViewPostModel} from '../../../state/models/post-thread-view'
import {s} from '../../lib/styles'
import {pluralize} from '../../lib/strings'
import {ago, pluralize} from '../../lib/strings'
import {AVIS} from '../../lib/assets'
function iter<T>(n: number, fn: (_i: number) => T): Array<T> {
@ -97,7 +96,7 @@ export const PostThreadItem = observer(function PostThreadItem({
@{item.author.name}
</Text>
<Text style={[styles.metaItem, s.f14, s.gray]}>
&middot; {moment(item.indexedAt).fromNow(true)}
&middot; {ago(item.indexedAt)}
</Text>
</View>
<Text

View File

@ -1,4 +1,3 @@
import moment from 'moment'
import {library} from '@fortawesome/fontawesome-svg-core'
import {faArrowLeft} from '@fortawesome/free-solid-svg-icons/faArrowLeft'
@ -16,26 +15,6 @@ import {faRetweet} from '@fortawesome/free-solid-svg-icons/faRetweet'
import {faX} from '@fortawesome/free-solid-svg-icons/faX'
export function setup() {
moment.updateLocale('en', {
relativeTime: {
future: 'in %s',
past: '%s ago',
s: 'a few seconds',
ss: '%ds',
m: 'a minute',
mm: '%dm',
h: 'an hour',
hh: '%dh',
d: 'a day',
dd: '%dd',
w: 'a week',
ww: '%dw',
M: 'a month',
MM: '%dmo',
y: 'a year',
yy: '%dy',
},
})
library.add(
faArrowLeft,
faBars,

View File

@ -21,3 +21,35 @@ export function makeRecordUri(
urip.recordKey = recordKey
return urip.toString()
}
const MINUTE = 60
const HOUR = MINUTE * 60
const DAY = HOUR * 24
const MONTH = DAY * 30
const YEAR = DAY * 365
export function ago(date: number | string | Date): string {
let ts: number
if (typeof date === 'string') {
ts = Number(new Date(date))
} else if (date instanceof Date) {
ts = Number(date)
} else {
ts = date
}
const diffSeconds = Math.floor((Date.now() - ts) / 1e3)
if (diffSeconds === 0) {
return 'just now'
} else if (diffSeconds < MINUTE) {
return `${diffSeconds}s`
} else if (diffSeconds < HOUR) {
return `${Math.floor(diffSeconds / MINUTE)}m`
} else if (diffSeconds < DAY) {
return `${Math.floor(diffSeconds / HOUR)}h`
} else if (diffSeconds < MONTH) {
return `${Math.floor(diffSeconds / DAY)}d`
} else if (diffSeconds < YEAR) {
return `${Math.floor(diffSeconds / MONTH)}mo`
} else {
return new Date(ts).toLocaleDateString()
}
}

View File

@ -11,6 +11,4 @@ Paul's todo list
- *
- Linking
- Web linking
- App linking
- Housekeeping
- Remove moment.js -- it's too heavy a dependency
- App linking

View File

@ -9753,11 +9753,6 @@ mobx@^6.6.1:
resolved "https://registry.yarnpkg.com/mobx/-/mobx-6.6.1.tgz#70ee6aa82f25aeb7e7d522bd621207434e509318"
integrity sha512-7su3UZv5JF+ohLr2opabjbUAERfXstMY+wiBtey8yNAPoB8H187RaQXuhFjNkH8aE4iHbDWnhDFZw0+5ic4nGQ==
moment@^2.29.4:
version "2.29.4"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108"
integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==
ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"