Skip to content

Commit

Permalink
[chore] update date-fns
Browse files Browse the repository at this point in the history
  • Loading branch information
vicmeow authored and rexxars committed Oct 6, 2020
1 parent 48c7769 commit 0743242
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 28 deletions.
3 changes: 2 additions & 1 deletion packages/@sanity/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"react-props-stream": "^1.0.0",
"rxjs": "^6.5.3",
"semver-compare": "^1.0.0",
"shallow-equals": "^1.0.0"
"shallow-equals": "^1.0.0",
"date-fns": "^2.16.1"
},
"devDependencies": {
"jest": "^26.4.2",
Expand Down
22 changes: 11 additions & 11 deletions packages/@sanity/base/src/time/useTimeAgo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,36 @@ export function useTimeAgo(time: Date | string, {minimal}: TimeAgoOpts = {}): st
return resolved.timestamp
}

// eslint-disable-next-line complexity
function formatRelativeTime(date: Date | string, opts: TimeAgoOpts = {}): TimeSpec {
const now = Date.now()
const parsedDate = date instanceof Date ? date : new Date(date)

const diffMonths = differenceInMonths(now, date)
const diffYears = differenceInYears(now, date)
const diffMonths = differenceInMonths(now, parsedDate)
const diffYears = differenceInYears(now, parsedDate)

if (diffMonths || diffYears) {
if (opts.minimal && diffYears === 0) {
// same year
return {
timestamp: format(date, 'MMM D'),
timestamp: format(parsedDate, 'MMM d'),
refreshInterval: +Infinity
}
}

if (opts.minimal) {
return {
timestamp: format(date, 'MMM D, YYYY'),
timestamp: format(parsedDate, 'MMM d, yyyy'),
refreshInterval: +Infinity
}
}

return {
timestamp: format(date, 'MMM D, YYYY, hh:mm A'),
timestamp: format(parsedDate, 'MMM d, yyyy, hh:mm a'),
refreshInterval: +Infinity
}
}

const diffWeeks = differenceInWeeks(now, date)
const diffWeeks = differenceInWeeks(now, parsedDate)
if (diffWeeks) {
if (opts.minimal) {
return {timestamp: `${diffWeeks}w`, refreshInterval: ONE_HOUR}
Expand All @@ -83,7 +83,7 @@ function formatRelativeTime(date: Date | string, opts: TimeAgoOpts = {}): TimeSp
return {timestamp: `${diffWeeks} weeks ago`, refreshInterval: ONE_HOUR}
}

const diffDays = differenceInDays(now, date)
const diffDays = differenceInDays(now, parsedDate)
if (diffDays) {
if (opts.minimal) {
return {
Expand All @@ -98,7 +98,7 @@ function formatRelativeTime(date: Date | string, opts: TimeAgoOpts = {}): TimeSp
}
}

const diffHours = differenceInHours(now, date)
const diffHours = differenceInHours(now, parsedDate)
if (diffHours) {
if (opts.minimal) {
return {timestamp: `${diffHours}h`, refreshInterval: ONE_MINUTE}
Expand All @@ -107,7 +107,7 @@ function formatRelativeTime(date: Date | string, opts: TimeAgoOpts = {}): TimeSp
return {timestamp: `${diffHours} hours ago`, refreshInterval: ONE_MINUTE}
}

const diffMins = differenceInMinutes(now, date)
const diffMins = differenceInMinutes(now, parsedDate)
if (diffMins) {
if (opts.minimal) {
return {timestamp: `${diffMins}m`, refreshInterval: TWENTY_SECONDS}
Expand All @@ -116,7 +116,7 @@ function formatRelativeTime(date: Date | string, opts: TimeAgoOpts = {}): TimeSp
return {timestamp: `${diffMins} minutes ago`, refreshInterval: TWENTY_SECONDS}
}

const diffSeconds = differenceInSeconds(now, date)
const diffSeconds = differenceInSeconds(now, parsedDate)
if (diffSeconds > 10) {
if (opts.minimal) {
return {timestamp: `${diffSeconds}s`, refreshInterval: FIVE_SECONDS}
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"chroma-js": "^2.0.4",
"circular-at": "^1.0.3",
"classnames": "^2.2.5",
"date-fns": "^1.29.0",
"date-fns": "^2.16.1",
"dom-scroll-into-view": "^1.2.1",
"element-resize-detector": "^1.1.14",
"exif-component": "^1.0.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/@sanity/components/src/previews/CardPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ export default class CardPreview extends React.PureComponent<CardPreviewProps> {
<div
ref={this.setDateElement}
className={styles.date}
title={formatDate(date, 'ddd, MMM Do, YYYY hh:mm A')}
title={formatDate(date, 'ddd, MMM do, yyyy hh:mm a')}
>
{emWidth <= 10 && formatDate(date, 'DD.MM.YY')}
{emWidth > 10 && emWidth <= 15 && formatDate(date, 'DD.MM.YY hh:mm A')}
{emWidth > 15 && formatDate(date, 'ddd, MMM Do, YYYY hh:mm A')}
{emWidth > 10 && emWidth <= 15 && formatDate(date, 'dd.MM.yy hh:mm a')}
{emWidth > 15 && formatDate(date, 'ddd, MMM do, yyyy hh:mm a')}
</div>
)}
<div className={styles.heading}>
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/desk-tool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@sanity/uuid": "1.150.1",
"boundless-arrow-key-navigation": "^1.1.0",
"classnames": "^2.2.5",
"date-fns": "^1.29.0",
"date-fns": "^2.16.1",
"diff": "^3.2.0",
"element-resize-detector": "^1.1.14",
"hashlru": "^2.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/field/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@sanity/diff": "1.150.7",
"@sanity/react-hooks": "1.150.8",
"@sanity/types": "1.150.7",
"date-fns": "^1.29.0",
"date-fns": "^2.16.1",
"sanity-diff-patch": "^1.0.9"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ export const DatetimePreview: PreviewComponent<string> = function DatetimePrevie

function formatDate(value: string, schemaType: StringSchemaType): string {
const {options, name} = schemaType
const date = new Date(value)

let dateFormat = 'YYYY-MM-DD'
let dateFormat = 'yyyy-MM-dd'
if (options && options.dateFormat) {
dateFormat = options.dateFormat
}

if (name === 'date') {
// If the type is date only
return format(value, dateFormat)
return format(date, dateFormat)
}

// If the type is datetime
Expand All @@ -30,5 +31,5 @@ function formatDate(value: string, schemaType: StringSchemaType): string {
dateFormat += ' HH:mm'
}

return format(value, dateFormat)
return format(date, dateFormat)
}
2 changes: 1 addition & 1 deletion packages/@sanity/form-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@sanity/uuid": "1.150.1",
"attr-accept": "^1.1.0",
"classnames": "^2.2.5",
"date-fns": "^1.29.0",
"date-fns": "^2.16.1",
"debug": "^3.1.0",
"diff-match-patch": "^1.0.4",
"exif-component": "^1.0.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isValidDate from 'date-fns/is_valid'
import isValidDate from 'date-fns/isValid'
import parseDate from 'date-fns/parse'

const TRUTHY_STRINGS = ['yes', 'true', '1']
Expand Down Expand Up @@ -38,7 +38,7 @@ export default {
!FALSEY_STRINGS.includes(value.toLowerCase())
},
richDate: {
test: val => isValidDate(parseDate(val)),
test: val => isValidDate(val),
convert: value => {
return {
_type: 'richDate',
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/validation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"dependencies": {
"clone-deep": "^2.0.1",
"date-fns": "^1.29.0",
"date-fns": "^2.16.1",
"es6-error": "^4.0.2",
"type-of-is": "^3.5.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const genericValidator = require('./genericValidator')
const isoDate = /^(?:[-+]\d{2})?(?:\d{4}(?!\d{2}\b))(?:(-?)(?:(?:0[1-9]|1[0-2])(?:\1(?:[12]\d|0[1-9]|3[01]))?|W(?:[0-4]\d|5[0-2])(?:-?[1-7])?|(?:00[1-9]|0[1-9]\d|[12]\d{2}|3(?:[0-5]\d|6[1-6])))(?![T]$|[T][\d]+Z$)(?:[T\s](?:(?:(?:[01]\d|2[0-3])(?:(:?)[0-5]\d)?|24\:?00)(?:[.,]\d+(?!:))?)(?:\2[0-5]\d(?:[.,]\d+)?)?(?:[Z]|(?:[+-])(?:[01]\d|2[0-3])(?::?[0-5]\d)?)?)?)?$/

const getFormattedDate = (type = '', value, options) => {
let format = 'YYYY-MM-DD'
let format = 'yyyy-MM-dd'
if (options && options.dateFormat) {
format = options.dateFormat
}
Expand Down
2 changes: 1 addition & 1 deletion packages/example-studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@sanity/storybook": "1.150.1",
"@sanity/vision": "1.150.6",
"bio-pv": "^1.8.1",
"date-fns": "^1.29.0",
"date-fns": "^2.16.1",
"fetch": "^1.1.0",
"get-video-id": "^2.1.4",
"humanize-list": "^1.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/test-studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@turf/helpers": "^6.0.1",
"@turf/points-within-polygon": "^5.1.5",
"bio-pv": "^1.8.1",
"date-fns": "^1.29.0",
"date-fns": "^2.16.1",
"fetch": "^1.1.0",
"get-video-id": "^2.1.4",
"humanize-list": "^1.0.1",
Expand Down

0 comments on commit 0743242

Please sign in to comment.