Skip to content

Commit

Permalink
fix hi locale type issues (date-fns#2845)
Browse files Browse the repository at this point in the history
  • Loading branch information
fturmel authored Dec 15, 2021
1 parent 005a808 commit 9fa754c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 100 deletions.
35 changes: 13 additions & 22 deletions src/locale/hi/_lib/formatDistance/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { FormatDistanceLocale, FormatDistanceToken } from '../../../types'
import { FormatDistanceFnOptions } from '../../../types'
import localize from '../localize/index'
import type { FormatDistanceFn, FormatDistanceLocale } from '../../../types'
import { numberToLocale } from '../localize/index'

// Source: https://www.unicode.org/cldr/charts/32/summary/hi.html

Expand Down Expand Up @@ -92,30 +91,20 @@ const formatDistanceLocale: FormatDistanceLocale<FormatDistanceLocaleValue> = {
},
}

export default function formatDistance(
token: FormatDistanceToken,
count: number,
options: FormatDistanceFnOptions
) {
const newOptions = options || {}

const formatDistance: FormatDistanceFn = (token, count, options) => {
let result
if (typeof formatDistanceLocale[token] === 'string') {
result = formatDistanceLocale[token]

const tokenValue = formatDistanceLocale[token]
if (typeof tokenValue === 'string') {
result = tokenValue
} else if (count === 1) {
result = (formatDistanceLocale[token] as FormatDistanceTokanRelativeValue)
.one
result = tokenValue.one
} else {
result = (formatDistanceLocale[
token
] as FormatDistanceTokanRelativeValue).other.replace(
'{{count}}',
localize.numberToLocale(count)
)
result = tokenValue.other.replace('{{count}}', numberToLocale(count))
}

if (newOptions.addSuffix) {
if (newOptions.comparison! > 0) {
if (options?.addSuffix) {
if (options.comparison && options.comparison > 0) {
return result + 'मे '
} else {
return result + ' पहले'
Expand All @@ -124,3 +113,5 @@ export default function formatDistance(

return result
}

export default formatDistance
15 changes: 5 additions & 10 deletions src/locale/hi/_lib/formatRelative/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { FormatRelativeToken } from '../../../types'
import { FormatRelativeFnOptions } from '../../../types'
import type { FormatRelativeFn } from '../../../types'

const formatRelativeLocale = {
lastWeek: "'पिछले' eeee p",
Expand All @@ -10,11 +9,7 @@ const formatRelativeLocale = {
other: 'P',
}

export default function formatRelative(
token: FormatRelativeToken,
_date: Date | number,
_baseDate: Date | number,
_options: FormatRelativeFnOptions
) {
return formatRelativeLocale[token]
}
const formatRelative: FormatRelativeFn = (token, _date, _baseDate, _options) =>
formatRelativeLocale[token]

export default formatRelative
80 changes: 33 additions & 47 deletions src/locale/hi/_lib/localize/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Day, Era, Month, Quarter } from '../../../../types'
import type { LocaleDayPeriod, QuarterIndex } from '../../../types'
import type { Era, Quarter } from '../../../../types'
import type { Localize, LocalizeFn, QuarterIndex } from '../../../types'
import buildLocalizeFn, {
LocalizePeriodValuesMap,
} from '../../../_lib/buildLocalizeFn/index'

export type hiLocaleNumberType =
type hiLocaleNumberType =
| '\u0967'
| '\u0968'
| '\u0969'
Expand All @@ -16,7 +16,7 @@ export type hiLocaleNumberType =
| '\u096F'
| '\u0966'

export type enLocaleNumberType =
type enLocaleNumberType =
| '1'
| '2'
| '3'
Expand All @@ -28,11 +28,13 @@ export type enLocaleNumberType =
| '9'
| '0'

export type enHiLocaleNumberType = {
type enHiLocaleNumberType = {
// eslint-disable-next-line no-unused-vars
[enNumber in enLocaleNumberType]: hiLocaleNumberType
}

export type hiLocaleEnNumberType = {
type hiLocaleEnNumberType = {
// eslint-disable-next-line no-unused-vars
[hiNumber in hiLocaleNumberType]: enLocaleNumberType
}

Expand Down Expand Up @@ -88,7 +90,7 @@ const quarterValues: LocalizePeriodValuesMap<Quarter> = {
// e.g. in Spanish language the weekdays and months should be in the lowercase.
// https://www.unicode.org/cldr/charts/32/summary/hi.html
// CLDR #1617 - #1688
const monthValues: LocalizePeriodValuesMap<Month> = {
const monthValues = {
narrow: [
'ज',
'फ़',
Expand All @@ -102,7 +104,7 @@ const monthValues: LocalizePeriodValuesMap<Month> = {
'अक्टू',
'न',
'दि',
],
] as const,
abbreviated: [
'जन',
'फ़र',
Expand All @@ -116,7 +118,7 @@ const monthValues: LocalizePeriodValuesMap<Month> = {
'अक्टू',
'नव',
'दिस',
],
] as const,
wide: [
'जनवरी',
'फ़रवरी',
Expand All @@ -130,14 +132,14 @@ const monthValues: LocalizePeriodValuesMap<Month> = {
'अक्टूबर',
'नवंबर',
'दिसंबर',
],
] as const,
}

// CLDR #1689 - #1744
const dayValues: LocalizePeriodValuesMap<Day> = {
narrow: ['र', 'सो', 'मं', 'बु', 'गु', 'शु', 'श'],
short: ['र', 'सो', 'मं', 'बु', 'गु', 'शु', 'श'],
abbreviated: ['रवि', 'सोम', 'मंगल', 'बुध', 'गुरु', 'शुक्र', 'शनि'],
const dayValues = {
narrow: ['र', 'सो', 'मं', 'बु', 'गु', 'शु', 'श'] as const,
short: ['र', 'सो', 'मं', 'बु', 'गु', 'शु', 'श'] as const,
abbreviated: ['रवि', 'सोम', 'मंगल', 'बुध', 'गुरु', 'शुक्र', 'शनि'] as const,
wide: [
'रविवार',
'सोमवार',
Expand All @@ -146,10 +148,10 @@ const dayValues: LocalizePeriodValuesMap<Day> = {
'गुरुवार',
'शुक्रवार',
'शनिवार',
],
] as const,
}

const dayPeriodValues: LocalizePeriodValuesMap<LocaleDayPeriod> = {
const dayPeriodValues = {
narrow: {
am: 'पूर्वाह्न',
pm: 'अपराह्न',
Expand Down Expand Up @@ -215,68 +217,52 @@ const formattingDayPeriodValues = {
},
}

function ordinalNumber(dirtyNumber: string) {
var number = localize.localeToNumber(dirtyNumber)
var localeNumber = localize.numberToLocale(number)

var rem10 = number % 10
switch (rem10) {
case 2:
case 3:
case 4:
case 6:
case 1:
case 5:
case 7:
case 8:
case 9:
case 0:
return localeNumber
}
const ordinalNumber: LocalizeFn<number, undefined> = (
dirtyNumber,
_options
) => {
const number = Number(dirtyNumber)
return numberToLocale(number)
}

function localeToNumber(locale: string): number {
export function localeToNumber(locale: string): number {
const enNumber = locale.toString().replace(/[१२३४५६७८९०]/g, function (match) {
return numberValues.number[match as hiLocaleNumberType]
})
return Number(enNumber)
}

function numberToLocale(enNumber: number) {
export function numberToLocale(enNumber: number) {
return enNumber.toString().replace(/\d/g, function (match) {
return numberValues.locale[match as enLocaleNumberType]
})
}

const localize = {
localeToNumber: localeToNumber,
numberToLocale: numberToLocale,
ordinalNumber: ordinalNumber,
const localize: Localize = {
ordinalNumber,

era: buildLocalizeFn<Era, undefined>({
era: buildLocalizeFn({
values: eraValues,
defaultWidth: 'wide',
}),

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

month: buildLocalizeFn<Month, undefined>({
month: buildLocalizeFn({
values: monthValues,
defaultWidth: 'wide',
}),

day: buildLocalizeFn<Day, undefined>({
day: buildLocalizeFn({
values: dayValues,
defaultWidth: 'wide',
}),

dayPeriod: buildLocalizeFn<LocaleDayPeriod, undefined>({
dayPeriod: buildLocalizeFn({
values: dayPeriodValues,
defaultWidth: 'wide',
formattingValues: formattingDayPeriodValues,
Expand Down
52 changes: 33 additions & 19 deletions src/locale/hi/_lib/match/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Day, Era, Month, Quarter } from '../../../../types'
import type { ParsePatterns } from '../../../types'
import type { Quarter } from '../../../../types'
import type { Match } from '../../../types'
import buildMatchFn from '../../../_lib/buildMatchFn/index'
import buildMatchPatternFn from '../../../_lib/buildMatchPatternFn/index'
import localize from '../localize/index'
import { localeToNumber } from '../localize/index'

const matchOrdinalNumberPattern = /^[०१२३४५६७८९]+/i
const parseOrdinalNumberPattern = /^[०१२३४५६७८९]+/i
Expand All @@ -13,17 +13,17 @@ const matchEraPatterns = {
wide: /^(ईसा-पूर्व|ईसवी पूर्व|ईसवी सन|ईसवी)/i,
}

const parseEraPatterns: ParsePatterns<Era, 'any'> = {
any: [/^b/i, /^(a|c)/i],
const parseEraPatterns = {
any: [/^b/i, /^(a|c)/i] as const,
}

const matchQuarterPatterns = {
narrow: /^[1234]/i,
abbreviated: /^ति[1234]/i,
wide: /^[1234](पहली|दूसरी|तीसरी|चौथी)? तिमाही/i,
}
const parseQuarterPatterns: ParsePatterns<Quarter, 'any'> = {
any: [/1/i, /2/i, /3/i, /4/i],
const parseQuarterPatterns = {
any: [/1/i, /2/i, /3/i, /4/i] as const,
}

const matchMonthPatterns = {
Expand All @@ -32,7 +32,7 @@ const matchMonthPatterns = {
abbreviated: /^(जन|फ़र|मार्च|अप्|मई|जून|जुल|अग|सित|अक्तू|नव|दिस)/i,
wide: /^(जनवरी|फ़रवरी|मार्च|अप्रैल|मई|जून|जुलाई|अगस्त|सितंबर|अक्तूबर|नवंबर|दिसंबर)/i,
}
const parseMonthPatterns: ParsePatterns<Month, 'narrow' | 'any'> = {
const parseMonthPatterns = {
narrow: [
/^ज/i,
/^फ़/i,
Expand All @@ -46,7 +46,7 @@ const parseMonthPatterns: ParsePatterns<Month, 'narrow' | 'any'> = {
/^अक्तू/i,
/^न/i,
/^दि/i,
],
] as const,
any: [
/^जन/i,
/^फ़/i,
Expand All @@ -60,7 +60,7 @@ const parseMonthPatterns: ParsePatterns<Month, 'narrow' | 'any'> = {
/^अक्तू/i,
/^नव/i,
/^दिस/i,
],
] as const,
}

const matchDayPatterns = {
Expand All @@ -70,9 +70,25 @@ const matchDayPatterns = {
abbreviated: /^(रवि|सोम|मंगल|बुध|गुरु|शुक्र|शनि)/i,
wide: /^(रविवार|सोमवार|मंगलवार|बुधवार|गुरुवार|शुक्रवार|शनिवार)/i,
}
const parseDayPatterns: ParsePatterns<Day, 'narrow' | 'any'> = {
narrow: [/^रवि/i, /^सोम/i, /^मंगल/i, /^बुध/i, /^गुरु/i, /^शुक्र/i, /^शनि/i],
any: [/^रवि/i, /^सोम/i, /^मंगल/i, /^बुध/i, /^गुरु/i, /^शुक्र/i, /^शनि/i],
const parseDayPatterns = {
narrow: [
/^रवि/i,
/^सोम/i,
/^मंगल/i,
/^बुध/i,
/^गुरु/i,
/^शुक्र/i,
/^शनि/i,
] as const,
any: [
/^रवि/i,
/^सोम/i,
/^मंगल/i,
/^बुध/i,
/^गुरु/i,
/^शुक्र/i,
/^शनि/i,
] as const,
}

const matchDayPeriodPatterns = {
Expand All @@ -92,11 +108,11 @@ const parseDayPeriodPatterns = {
},
}

const match = {
ordinalNumber: buildMatchPatternFn<number>({
const match: Match = {
ordinalNumber: buildMatchPatternFn({
matchPattern: matchOrdinalNumberPattern,
parsePattern: parseOrdinalNumberPattern,
valueCallback: localize.localeToNumber,
valueCallback: localeToNumber,
}),

era: buildMatchFn({
Expand All @@ -111,9 +127,7 @@ const match = {
defaultMatchWidth: 'wide',
parsePatterns: parseQuarterPatterns,
defaultParseWidth: 'any',
valueCallback: function (index: string | number) {
return (Number(index) + 1) as Quarter
},
valueCallback: (index) => (index + 1) as Quarter,
}),

month: buildMatchFn({
Expand Down
1 change: 1 addition & 0 deletions src/locale/hi/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Locale } from '../types'
import formatDistance from './_lib/formatDistance/index'
import formatLong from './_lib/formatLong/index'
import formatRelative from './_lib/formatRelative/index'
Expand Down
4 changes: 2 additions & 2 deletions src/locale/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export interface FormatRelativeFnOptions {

export type FormatRelativeFn = (
token: FormatRelativeToken,
date: Date | number,
baseDate: Date | number,
date: Date,
baseDate: Date,
options?: FormatRelativeFnOptions
) => string

Expand Down

0 comments on commit 9fa754c

Please sign in to comment.