Skip to content

Commit

Permalink
bs locale typescript migration (date-fns#2882)
Browse files Browse the repository at this point in the history
  • Loading branch information
fturmel authored Dec 28, 2021
1 parent 2d4231d commit 0c1dae2
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
var formatDistanceLocale = {
import type { FormatDistanceFn, FormatDistanceLocale } from '../../../types'

type FormatDistanceTokenValue =
| string
| {
one: {
standalone: string
withPrepositionAgo: string
withPrepositionIn: string
}
dual: string
other: string
}

const formatDistanceLocale: FormatDistanceLocale<FormatDistanceTokenValue> = {
lessThanXSeconds: {
one: {
standalone: 'manje od 1 sekunde',
Expand Down Expand Up @@ -152,35 +166,34 @@ var formatDistanceLocale = {
},
}

export default function formatDistance(token, count, options) {
options = options || {}
const formatDistance: FormatDistanceFn = (token, count, options) => {
let result

var result

if (typeof formatDistanceLocale[token] === 'string') {
result = formatDistanceLocale[token]
const tokenValue = formatDistanceLocale[token]
if (typeof tokenValue === 'string') {
result = tokenValue
} else if (count === 1) {
if (options.addSuffix) {
if (options.comparison > 0) {
result = formatDistanceLocale[token].one.withPrepositionIn
if (options?.addSuffix) {
if (options.comparison && options.comparison > 0) {
result = tokenValue.one.withPrepositionIn
} else {
result = formatDistanceLocale[token].one.withPrepositionAgo
result = tokenValue.one.withPrepositionAgo
}
} else {
result = formatDistanceLocale[token].one.standalone
result = tokenValue.one.standalone
}
} else if (
count % 10 > 1 &&
count % 10 < 5 && // if last digit is between 2 and 4
String(count).substr(-2, 1) !== '1' // unless the 2nd to last digit is "1"
) {
result = formatDistanceLocale[token].dual.replace('{{count}}', count)
result = tokenValue.dual.replace('{{count}}', String(count))
} else {
result = formatDistanceLocale[token].other.replace('{{count}}', count)
result = tokenValue.other.replace('{{count}}', String(count))
}

if (options.addSuffix) {
if (options.comparison > 0) {
if (options?.addSuffix) {
if (options.comparison && options.comparison > 0) {
return 'za ' + result
} else {
return 'prije ' + result
Expand All @@ -189,3 +202,5 @@ export default function formatDistance(token, count, options) {

return result
}

export default formatDistance
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
import type { FormatLong } from '../../../types'
import buildFormatLongFn from '../../../_lib/buildFormatLongFn/index'

var dateFormats = {
const dateFormats = {
full: 'EEEE, d. MMMM yyyy.',
long: 'd. MMMM yyyy.',
medium: 'd. MMM yy.',
short: 'dd. MM. yy.',
}

var timeFormats = {
const timeFormats = {
full: 'HH:mm:ss (zzzz)',
long: 'HH:mm:ss z',
medium: 'HH:mm:ss',
short: 'HH:mm',
}

var dateTimeFormats = {
const dateTimeFormats = {
full: "{{date}} 'u' {{time}}",
long: "{{date}} 'u' {{time}}",
medium: '{{date}} {{time}}',
short: '{{date}} {{time}}',
}

var formatLong = {
const formatLong: FormatLong = {
date: buildFormatLongFn({
formats: dateFormats,
defaultWidth: 'full',
}),

time: buildFormatLongFn({
formats: timeFormats,
defaultWidth: 'full',
}),

dateTime: buildFormatLongFn({
formats: dateTimeFormats,
defaultWidth: 'full',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var formatRelativeLocale = {
lastWeek: function (date) {
var day = date.getUTCDay()
import type { FormatRelativeFn } from '../../../types'

switch (day) {
const formatRelativeLocale = {
lastWeek: (date: Date) => {
switch (date.getUTCDay()) {
case 0:
return "'prošle nedjelje u' p"
case 3:
Expand All @@ -16,10 +16,8 @@ var formatRelativeLocale = {
yesterday: "'juče u' p",
today: "'danas u' p",
tomorrow: "'sutra u' p",
nextWeek: function (date) {
var day = date.getUTCDay()

switch (day) {
nextWeek: (date: Date) => {
switch (date.getUTCDay()) {
case 0:
return "'sljedeće nedjelje u' p"
case 3:
Expand All @@ -33,12 +31,14 @@ var formatRelativeLocale = {
other: 'P',
}

export default function formatRelative(token, date, _baseDate, _options) {
var format = formatRelativeLocale[token]
const formatRelative: FormatRelativeFn = (token, date, _baseDate, _options) => {
const format = formatRelativeLocale[token]

if (typeof format === 'function') {
return format(date)
}

return format
}

export default formatRelative
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import type { Quarter } from '../../../../types'
import type { Localize, LocalizeFn } from '../../../types'
import buildLocalizeFn from '../../../_lib/buildLocalizeFn/index'

function ordinalNumber(dirtyNumber) {
var number = Number(dirtyNumber)
return String(number).concat('.')
const eraValues = {
narrow: ['pr.n.e.', 'AD'] as const,
abbreviated: ['pr. Hr.', 'po. Hr.'] as const,
wide: ['Prije Hrista', 'Poslije Hrista'] as const,
}

var eraValues = {
narrow: ['pr.n.e.', 'AD'],
abbreviated: ['pr. Hr.', 'po. Hr.'],
wide: ['Prije Hrista', 'Poslije Hrista'],
const quarterValues = {
narrow: ['1.', '2.', '3.', '4.'] as const,
abbreviated: ['1. kv.', '2. kv.', '3. kv.', '4. kv.'] as const,
wide: ['1. kvartal', '2. kvartal', '3. kvartal', '4. kvartal'] as const,
}

var monthValues = {
const monthValues = {
narrow: [
'1.',
'2.',
Expand All @@ -25,7 +28,7 @@ var monthValues = {
'10.',
'11.',
'12.',
],
] as const,
abbreviated: [
'jan',
'feb',
Expand All @@ -39,7 +42,7 @@ var monthValues = {
'okt',
'nov',
'dec',
],
] as const,
wide: [
'januar',
'februar',
Expand All @@ -53,10 +56,10 @@ var monthValues = {
'oktobar',
'novembar',
'decembar',
],
] as const,
}

var formattingMonthValues = {
const formattingMonthValues = {
narrow: [
'1.',
'2.',
Expand All @@ -70,7 +73,7 @@ var formattingMonthValues = {
'10.',
'11.',
'12.',
],
] as const,
abbreviated: [
'jan',
'feb',
Expand All @@ -84,7 +87,7 @@ var formattingMonthValues = {
'okt',
'nov',
'dec',
],
] as const,
wide: [
'januar',
'februar',
Expand All @@ -98,19 +101,13 @@ var formattingMonthValues = {
'oktobar',
'novembar',
'decembar',
],
] as const,
}

var quarterValues = {
narrow: ['1.', '2.', '3.', '4.'],
abbreviated: ['1. kv.', '2. kv.', '3. kv.', '4. kv.'],
wide: ['1. kvartal', '2. kvartal', '3. kvartal', '4. kvartal'],
}

var dayValues = {
narrow: ['N', 'P', 'U', 'S', 'Č', 'P', 'S'],
short: ['ned', 'pon', 'uto', 'sre', 'čet', 'pet', 'sub'],
abbreviated: ['ned', 'pon', 'uto', 'sre', 'čet', 'pet', 'sub'],
const dayValues = {
narrow: ['N', 'P', 'U', 'S', 'Č', 'P', 'S'] as const,
short: ['ned', 'pon', 'uto', 'sre', 'čet', 'pet', 'sub'] as const,
abbreviated: ['ned', 'pon', 'uto', 'sre', 'čet', 'pet', 'sub'] as const,
wide: [
'nedjelja',
'ponedjeljak',
Expand All @@ -119,10 +116,10 @@ var dayValues = {
'četvrtak',
'petak',
'subota',
],
] as const,
}

var formattingDayPeriodValues = {
const dayPeriodValues = {
narrow: {
am: 'AM',
pm: 'PM',
Expand Down Expand Up @@ -155,7 +152,7 @@ var formattingDayPeriodValues = {
},
}

var dayPeriodValues = {
const formattingDayPeriodValues = {
narrow: {
am: 'AM',
pm: 'PM',
Expand Down Expand Up @@ -188,29 +185,40 @@ var dayPeriodValues = {
},
}

var localize = {
ordinalNumber: ordinalNumber,
const ordinalNumber: LocalizeFn<number, undefined> = (
dirtyNumber,
_options
) => {
const number = Number(dirtyNumber)
return String(number) + '.'
}

const localize: Localize = {
ordinalNumber,

era: buildLocalizeFn({
values: eraValues,
defaultWidth: 'wide',
}),

quarter: buildLocalizeFn({
values: quarterValues,
defaultWidth: 'wide',
argumentCallback: function (quarter) {
return Number(quarter) - 1
},
argumentCallback: (quarter) => (quarter - 1) as Quarter,
}),

month: buildLocalizeFn({
values: monthValues,
defaultWidth: 'wide',
formattingValues: formattingMonthValues,
defaultFormattingWidth: 'wide',
}),

day: buildLocalizeFn({
values: dayValues,
defaultWidth: 'wide',
}),

dayPeriod: buildLocalizeFn({
values: dayPeriodValues,
defaultWidth: 'wide',
Expand Down
Loading

0 comments on commit 0c1dae2

Please sign in to comment.