Skip to content

Commit

Permalink
all tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
kensnyder committed Sep 28, 2024
1 parent f2f6d6e commit b97e20c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 62 deletions.
14 changes: 7 additions & 7 deletions src/LocaleHelper/LocaleHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ export default class LocaleHelper {
this.buildDaynames();
this.buildMeridiems();
}
// if (this.locale === 'bn-IN') {
// console.log('lookups=====>', this);
// }
if (this.locale === 'zh-CN' || this.locale === 'ko-KR') {
console.log('lookups=====>', this);
}
}

/**
Expand Down Expand Up @@ -140,10 +140,10 @@ export default class LocaleHelper {
text += '월';
}
if (dateStyle === 'medium') {
// some languages (including arabic and chinese) don't have a 'medium' size
if (/^ar|zh/i.test(this.locale)) {
return;
}
// // some languages (including arabic and chinese) don't have a 'medium' size
// if (/^ar|zh/i.test(this.locale)) {
// return;
// }
text = text.replace(/\.$/, '');
vars[`${text}\\.?`] = true;
} else {
Expand Down
9 changes: 6 additions & 3 deletions src/data/preprocessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ const periodsInsteadOfColons = [
];

const preprocessors = {
ar: [[/ /g, ' ']], // RegExp contains non-breaking space
zh: [
// in Chinese, "PM" comes before the digits
[/下午([\d:]+)/, '$1下午'],
// in Chinese, am/pm comes before the digits
[/早上\s*([\d:]+)/, '$1am'],
[/上午\s*([\d:]+)/, '$1am'],
[/下午\s*([\d:]+)/, '$1pm'],
// Chinese "time"
// [/\[.+?時間]/, ''],
],
Expand Down Expand Up @@ -49,7 +52,7 @@ const preprocessors = {
[/\s*시\s*/, ':'], // "hour"
[/\s*분\s*/, ':'], // "minute"
[/\s*초\s*/, ''], // "second"
[/오전\s*([\d:]+)/, '$1오전'], // move AM/PM to the end
[/(오전|오후)\s*([\d:]+)/, '$2$1'], // move AM/PM to the end
[/(\d{4})\. (\d{1,2})\. (\d{1,2})\./, '$1-$2-$3'],
],
fi: periodsInsteadOfColons,
Expand Down
24 changes: 12 additions & 12 deletions src/formats/fuzzy/fuzzy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,24 @@ const fuzzy = new Format({
if (typeof handled === 'object') {
Object.assign(result, handled);
workingString = workingString.replace(match[0], '');
if (locale === 'ko-KR' && !fullString.includes('UTC')) {
console.log({
fullString,
workingString,
name: extractor.name,
regex: extractor.regex,
result,
match,
handled,
});
}
// if (locale === 'ar-SA') {
// console.log({
// fullString,
// workingString,
// name: extractor.name,
// regex: extractor.regex,
// result,
// match,
// handled,
// });
// }
hasMatch = true;
}
}
if (!hasMatch) {
return null;
}
if (locale === 'ko-KR') console.log('-----');
// if (locale === 'ar-SA') console.log('-----');
return result;
},
});
Expand Down
58 changes: 18 additions & 40 deletions test-fixtures/are-we-fuzzy-yet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,32 @@ const results = [`Using date ${date.toJSON()}`];
const today = toMDY(date);
console.log({ today });
const dateStyles = ['full', 'long', 'medium'] as const;
const timeStyles = ['long', 'medium', 'short'] as const;
const dayPeriods = ['narrow', 'short', 'long'] as const;
let i = 0;
let found = 0;
for (const locale of localeList) {
// if (/^ar|he/.test(locale)) {
// // skip right-to-left languages
// continue;
// }
if (/^ar/.test(locale)) {
// skip arabic for now
continue;
}
const fmt = new Intl.NumberFormat(locale);
const numberSystem = fmt.resolvedOptions().numberingSystem;
for (const dateStyle of dateStyles) {
testIt(
locale,
numberSystem,
{ dateStyle },
const ymd = /^ar/.test(locale)
? // hijri calendar
{
year: 1441,
month: 3,
day: 6,
}
: // gregorian calendar
{
year: 2020,
month: 1,
day: 31,
}
);
};
const fmt = new Intl.NumberFormat(locale);
const numberSystem = fmt.resolvedOptions().numberingSystem;
for (const dateStyle of dateStyles) {
testIt(locale, numberSystem, { dateStyle }, ymd);
testIt(
locale,
numberSystem,
{ dateStyle, timeStyle: 'long', timeZone: 'UTC' },
{
year: 2020,
month: 1,
day: 31,
...ymd,
hour: 1,
minute: 31,
second: 20,
Expand All @@ -54,9 +46,7 @@ for (const locale of localeList) {
numberSystem,
{ dateStyle, timeStyle: 'medium', timeZone: 'UTC' },
{
year: 2020,
month: 1,
day: 31,
...ymd,
hour: 1,
minute: 31,
}
Expand All @@ -66,9 +56,7 @@ for (const locale of localeList) {
numberSystem,
{ dateStyle, timeStyle: 'short', timeZone: 'UTC' },
{
year: 2020,
month: 1,
day: 31,
...ymd,
hour: 1,
}
);
Expand Down Expand Up @@ -113,7 +101,7 @@ function doesOverlap(
return true;
}

function toMDY(d) {
function toMDY(d: Date | { year: number; month: number; day: number }) {
if (d instanceof Date) {
return [d.getFullYear(), pad(d.getMonth() + 1), pad(d.getDate())].join('-');
}
Expand All @@ -123,18 +111,8 @@ function toMDY(d) {
return [d.year, pad(d.month), pad(d.day)].join('-');
}

function pad(n) {
function pad(n: number) {
return (n > 9 ? '' : '0') + n;
}

console.log(`Parsed ${found}/${i} dates ok`);
//
// function log(locale, dateString) {
// i++;
// const result = parser.attempt(dateString, locale);
// if (result.invalid) {
// // console.log(`${i} [${locale}] ${dateString} - ${result.invalid}`);
// } else {
// found++;
// }
// }
12 changes: 12 additions & 0 deletions test-fixtures/dates.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
[
"Using date 2020-01-31T01:31:20.789Z",
"✅ > arab > ar-SA > الجمعة، ٦ جمادى الآخرة، ١٤٤١ هـ > {\"year\":1441,\"month\":3,\"day\":6}",
"✅ > arab > ar-SA > الجمعة، ٦ جمادى الآخرة، ١٤٤١ هـ، ١:٣١:٢٠ ص UTC > {\"year\":1441,\"month\":3,\"day\":6,\"hour\":1,\"minute\":31,\"second\":20,\"offset\":0}",
"✅ > arab > ar-SA > الجمعة، ٦ جمادى الآخرة، ١٤٤١ هـ، ١:٣١:٢٠ ص > {\"year\":1441,\"month\":3,\"day\":6,\"hour\":1,\"minute\":31,\"second\":20}",
"✅ > arab > ar-SA > الجمعة، ٦ جمادى الآخرة، ١٤٤١ هـ، ١:٣١ ص > {\"year\":1441,\"month\":3,\"day\":6,\"hour\":1,\"minute\":31,\"second\":0}",
"✅ > arab > ar-SA > ٦ جمادى الآخرة، ١٤٤١ هـ > {\"year\":1441,\"month\":3,\"day\":6}",
"✅ > arab > ar-SA > ٦ جمادى الآخرة، ١٤٤١ هـ، ١:٣١:٢٠ ص UTC > {\"year\":1441,\"month\":3,\"day\":6,\"hour\":1,\"minute\":31,\"second\":20,\"offset\":0}",
"✅ > arab > ar-SA > ٦ جمادى الآخرة، ١٤٤١ هـ، ١:٣١:٢٠ ص > {\"year\":1441,\"month\":3,\"day\":6,\"hour\":1,\"minute\":31,\"second\":20}",
"✅ > arab > ar-SA > ٦ جمادى الآخرة، ١٤٤١ هـ، ١:٣١ ص > {\"year\":1441,\"month\":3,\"day\":6,\"hour\":1,\"minute\":31,\"second\":0}",
"✅ > arab > ar-SA > ٦ جما٢، ١٤٤١ هـ > {\"year\":1441,\"month\":3,\"day\":6}",
"✅ > arab > ar-SA > ٦ جما٢، ١٤٤١ هـ، ١:٣١:٢٠ ص UTC > {\"year\":1441,\"month\":3,\"day\":6,\"hour\":1,\"minute\":31,\"second\":20,\"offset\":0}",
"✅ > arab > ar-SA > ٦ جما٢، ١٤٤١ هـ، ١:٣١:٢٠ ص > {\"year\":1441,\"month\":3,\"day\":6,\"hour\":1,\"minute\":31,\"second\":20}",
"✅ > arab > ar-SA > ٦ جما٢، ١٤٤١ هـ، ١:٣١ ص > {\"year\":1441,\"month\":3,\"day\":6,\"hour\":1,\"minute\":31,\"second\":0}",
"✅ > beng > bn-BD > শুক্রবার, ৩১ জানুয়ারী, ২০২০ > {\"year\":2020,\"month\":1,\"day\":31}",
"✅ > beng > bn-BD > শুক্রবার, ৩১ জানুয়ারী, ২০২০ এ ১:৩১:২০ AM UTC > {\"year\":2020,\"month\":1,\"day\":31,\"hour\":1,\"minute\":31,\"second\":20,\"offset\":0}",
"✅ > beng > bn-BD > শুক্রবার, ৩১ জানুয়ারী, ২০২০ এ ১:৩১:২০ AM > {\"year\":2020,\"month\":1,\"day\":31,\"hour\":1,\"minute\":31,\"second\":20}",
Expand Down

0 comments on commit b97e20c

Please sign in to comment.