Skip to content

Commit

Permalink
Improve Chinese locale
Browse files Browse the repository at this point in the history
* Change date format to meet the national standard (GB/T 7408-2005)
* Improve `ordinalNumber` function behavior
* Add prefix in `formatRelative` depending on if it's a current week or not
  • Loading branch information
cubicwork committed Mar 13, 2020
1 parent 2282d1b commit 487a511
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 135 deletions.
26 changes: 13 additions & 13 deletions scripts/build/localeSnapshots/renderFormatDistance/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ If now is January 1st, 2000, 00:00.
| Date | Result | \`includeSeconds: true\` | \`addSuffix: true\` |
|-|-|-|-|
${dates
.map(date => {
const dateString = date.toISOString()
const result = formatDistance(date, baseDate, { locale })
const resultIncludeSeconds = formatDistance(date, baseDate, {
locale,
includeSeconds: true
.map(date => {
const dateString = date.toISOString()
const result = formatDistance(date, baseDate, { locale })
const resultIncludeSeconds = formatDistance(date, baseDate, {
locale,
includeSeconds: true
})
const resultAddSuffix = formatDistance(date, baseDate, {
locale,
addSuffix: true
})
return `| ${dateString} | ${result} | ${resultIncludeSeconds} | ${resultAddSuffix} |`
})
const resultAddSuffix = formatDistance(date, baseDate, {
locale,
addSuffix: true
})
return `| ${dateString} | ${result} | ${resultIncludeSeconds} | ${resultAddSuffix} |`
})
.join('\n')}`
.join('\n')}`
}
26 changes: 13 additions & 13 deletions scripts/build/localeSnapshots/renderFormatDistanceStrict/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ If now is January 1st, 2000, 00:00.
| Date | Result | \`addSuffix: true\` | With forced unit (i.e. \`hour\`)
|-|-|-|-|
${dates
.map(date => {
const dateString = date.toISOString()
const result = formatDistanceStrict(date, baseDate, { locale })
const resultAddSuffix = formatDistanceStrict(date, baseDate, {
locale,
addSuffix: true
.map(date => {
const dateString = date.toISOString()
const result = formatDistanceStrict(date, baseDate, { locale })
const resultAddSuffix = formatDistanceStrict(date, baseDate, {
locale,
addSuffix: true
})
const resultForcedUnit = formatDistanceStrict(date, baseDate, {
locale,
unit: 'hour'
})
return `| ${dateString} | ${result} | ${resultAddSuffix} | ${resultForcedUnit} |`
})
const resultForcedUnit = formatDistanceStrict(date, baseDate, {
locale,
unit: 'hour'
})
return `| ${dateString} | ${result} | ${resultAddSuffix} | ${resultForcedUnit} |`
})
.join('\n')}`
.join('\n')}`
}
88 changes: 44 additions & 44 deletions scripts/build/localeSnapshots/renderFormatParse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,50 @@ export default function renderFormatParse(locale) {
| Title | Token string | Date | \`format\` result | \`parse\` result |
|-|-|-|-|-|
${formatParseTokens
.map(({ title, tokens, dates, options = {}, skipParse }) => {
return tokens
.map((token, tokenIndex) => {
return dates
.map((date, dateIndex) => {
const dateString = toDate(date).toISOString()
const formatResult = format(
date,
token,
Object.assign({ locale }, options)
)
let parsedDate
try {
parsedDate =
!skipParse &&
parse(
formatResult,
token,
date,
Object.assign({ locale }, options)
)
} catch (_err) {
parsedDate = 'Errored'
}
.map(({ title, tokens, dates, options = {}, skipParse }) => {
return tokens
.map((token, tokenIndex) => {
return dates
.map((date, dateIndex) => {
const dateString = toDate(date).toISOString()
const formatResult = format(
date,
token,
Object.assign({ locale }, options)
)
let parsedDate
try {
parsedDate =
!skipParse &&
parse(
formatResult,
token,
date,
Object.assign({ locale }, options)
)
} catch (_err) {
parsedDate = 'Errored'
}
const parseResult = skipParse
? 'NA'
: parsedDate === 'Errored'
? parsedDate
: isValid(parsedDate)
? parsedDate.toISOString()
: 'Invalid Date'
const parseResult = skipParse
? 'NA'
: parsedDate === 'Errored'
? parsedDate
: isValid(parsedDate)
? parsedDate.toISOString()
: 'Invalid Date'
if (dateIndex === 0 && tokenIndex === 0) {
return `| ${title} | ${token} | ${dateString} | ${formatResult} | ${parseResult} |`
} else if (dateIndex === 0) {
return `| | ${token} | ${dateString} | ${formatResult} | ${parseResult} |`
} else {
return `| | | ${dateString} | ${formatResult} | ${parseResult} |`
}
})
.join('\n')
})
.join('\n')
})
.join('\n')}`
if (dateIndex === 0 && tokenIndex === 0) {
return `| ${title} | ${token} | ${dateString} | ${formatResult} | ${parseResult} |`
} else if (dateIndex === 0) {
return `| | ${token} | ${dateString} | ${formatResult} | ${parseResult} |`
} else {
return `| | | ${dateString} | ${formatResult} | ${parseResult} |`
}
})
.join('\n')
})
.join('\n')
})
.join('\n')}`
}
22 changes: 11 additions & 11 deletions scripts/build/localeSnapshots/renderFormatRelative/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ If now is January 1st, 2000, 00:00.
| Date | Result |
|-|-|
${relativeDates
.map(date => {
const dateString = date.toISOString()
let result
try {
result = formatRelative(date, baseDate, { locale })
} catch (_err) {
result = 'Errored'
}
return `| ${dateString} | ${result} |`
})
.join('\n')}`
.map(date => {
const dateString = date.toISOString()
let result
try {
result = formatRelative(date, baseDate, { locale })
} catch (_err) {
result = 'Errored'
}
return `| ${dateString} | ${result} |`
})
.join('\n')}`
}
9 changes: 5 additions & 4 deletions src/_lib/getUTCWeekYear/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ export default function getUTCWeekYear(dirtyDate, dirtyOptions) {

var options = dirtyOptions || {}
var locale = options.locale
var localeFirstWeekContainsDate = locale &&
locale.options &&
locale.options.firstWeekContainsDate
var localeFirstWeekContainsDate =
locale && locale.options && locale.options.firstWeekContainsDate
var defaultFirstWeekContainsDate =
localeFirstWeekContainsDate == null
? 1
Expand All @@ -27,7 +26,9 @@ export default function getUTCWeekYear(dirtyDate, dirtyOptions) {

// Test if weekStartsOn is between 1 and 7 _and_ is not NaN
if (!(firstWeekContainsDate >= 1 && firstWeekContainsDate <= 7)) {
throw new RangeError('firstWeekContainsDate must be between 1 and 7 inclusively')
throw new RangeError(
'firstWeekContainsDate must be between 1 and 7 inclusively'
)
}

var firstWeekOfNextYear = new Date(0)
Expand Down
5 changes: 2 additions & 3 deletions src/_lib/startOfUTCWeekYear/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ export default function startOfUTCWeekYear(dirtyDate, dirtyOptions) {

var options = dirtyOptions || {}
var locale = options.locale
var localeFirstWeekContainsDate = locale &&
locale.options &&
locale.options.firstWeekContainsDate
var localeFirstWeekContainsDate =
locale && locale.options && locale.options.firstWeekContainsDate
var defaultFirstWeekContainsDate =
localeFirstWeekContainsDate == null
? 1
Expand Down
22 changes: 11 additions & 11 deletions src/locale/ar-MA/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import match from './_lib/match/index.js'
* @author Achraf Rrami [@rramiachraf]{@link https://github.com/rramiachraf}
*/
var locale = {
code: 'ar-MA',
formatDistance: formatDistance,
formatLong: formatLong,
formatRelative: formatRelative,
localize: localize,
match: match,
options: {
// Monday is 1
weekStartsOn: 1,
firstWeekContainsDate: 1
}
code: 'ar-MA',
formatDistance: formatDistance,
formatLong: formatLong,
formatRelative: formatRelative,
localize: localize,
match: match,
options: {
// Monday is 1
weekStartsOn: 1,
firstWeekContainsDate: 1
}
}

export default locale
2 changes: 1 addition & 1 deletion src/locale/hr/_lib/formatLong/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ var formatLong = {
})
}

export default formatLong
export default formatLong
34 changes: 30 additions & 4 deletions src/locale/hr/_lib/match/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,34 @@ var matchMonthPatterns = {
}
var parseMonthPatterns = {
narrow: [/(10|11|12|[123456789])/i],
abbreviated: [/^sij/i, /^velj/i, /^(ožu|ozu)/i, /^tra/i, /^svi/i, /^lip/i, /^srp/i, /^kol/i, /^ruj/i, /^lis/i, /^stu/i, /^pro/i],
wide: [/^sij/i, /^velj/i, /^(ožu|ozu)/i, /^tra/i, /^svi/i, /^lip/i, /^srp/i, /^kol/i, /^ruj/i, /^lis/i, /^stu/i, /^pro/i]
abbreviated: [
/^sij/i,
/^velj/i,
/^(ožu|ozu)/i,
/^tra/i,
/^svi/i,
/^lip/i,
/^srp/i,
/^kol/i,
/^ruj/i,
/^lis/i,
/^stu/i,
/^pro/i
],
wide: [
/^sij/i,
/^velj/i,
/^(ožu|ozu)/i,
/^tra/i,
/^svi/i,
/^lip/i,
/^srp/i,
/^kol/i,
/^ruj/i,
/^lis/i,
/^stu/i,
/^pro/i
]
}

var matchDayPatterns = {
Expand Down Expand Up @@ -64,7 +90,7 @@ var match = {
ordinalNumber: buildMatchPatternFn({
matchPattern: matchOrdinalNumberPattern,
parsePattern: parseOrdinalNumberPattern,
valueCallback: function (value) {
valueCallback: function(value) {
return parseInt(value, 10)
}
}),
Expand All @@ -79,7 +105,7 @@ var match = {
defaultMatchWidth: 'wide',
parsePatterns: parseQuarterPatterns,
defaultParseWidth: 'any',
valueCallback: function (index) {
valueCallback: function(index) {
return index + 1
}
}),
Expand Down
25 changes: 21 additions & 4 deletions src/locale/zh-CN/_lib/formatRelative/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import isSameUTCWeek from '../../../../_lib/isSameUTCWeek/index.js'

function checkWeek(_date, _baseDate, _options, baseFormat) {
if (isSameUTCWeek(_date, _baseDate, _options)) {
return baseFormat // in same week
} else if (_date.getTime() > _baseDate.getTime()) {
return "'下个'" + baseFormat // in next week
}
return "'上个'" + baseFormat // in last week
}

var formatRelativeLocale = {
lastWeek: "'上个' eeee p",
lastWeek: checkWeek, // days before yesterday, maybe in this week or last week
yesterday: "'昨天' p",
today: "'今天' p",
tomorrow: "'明天' p",
nextWeek: "'下个' eeee p",
other: 'P'
nextWeek: checkWeek, // days after tomorrow, maybe in this week or next week
other: 'PP p'
}

export default function formatRelative(token, _date, _baseDate, _options) {
return formatRelativeLocale[token]
var format = formatRelativeLocale[token]

if (typeof format === 'function') {
return format(_date, _baseDate, _options, 'eeee p')
}

return format
}
18 changes: 9 additions & 9 deletions src/locale/zh-CN/snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,12 @@ If now is January 1st, 2000, 00:00.

If now is January 1st, 2000, 00:00.

| Date | Result |
| ------------------------ | ---------------------- |
| 2000-01-10T00:00:00.000Z | 00-01-10 |
| 2000-01-05T00:00:00.000Z | 下个 星期三 上午 12:00 |
| 2000-01-02T00:00:00.000Z | 明天 上午 12:00 |
| 2000-01-01T00:00:00.000Z | 今天 上午 12:00 |
| 1999-12-31T00:00:00.000Z | 昨天 上午 12:00 |
| 1999-12-27T00:00:00.000Z | 上个 星期一 上午 12:00 |
| 1999-12-21T00:00:00.000Z | 99-12-21 |
| Date | Result |
| ------------------------ | --------------------- |
| 2000-01-10T00:00:00.000Z | 2000-01-10 上午 12:00 |
| 2000-01-05T00:00:00.000Z | 下个星期三 上午 12:00 |
| 2000-01-02T00:00:00.000Z | 明天 上午 12:00 |
| 2000-01-01T00:00:00.000Z | 今天 上午 12:00 |
| 1999-12-31T00:00:00.000Z | 昨天 上午 12:00 |
| 1999-12-27T00:00:00.000Z | 星期一 上午 12:00 |
| 1999-12-21T00:00:00.000Z | 1999-12-21 上午 12:00 |
14 changes: 8 additions & 6 deletions src/max/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import assert from 'power-assert'
import max from '.'

describe('max', function() {
function isInvalidDate (dirtyDate) {
function isInvalidDate(dirtyDate) {
return dirtyDate instanceof Date && isNaN(dirtyDate)
}

Expand Down Expand Up @@ -70,11 +70,13 @@ describe('max', function() {
})

it('converts iterable objects into Array', function() {
// $ExpectedMistake
var result = max(new Set([
new Date(1989, 6 /* Jul */, 10),
new Date(1987, 1 /* Feb */, 11),
]))
var result = max(
// $ExpectedMistake
new Set([
new Date(1989, 6 /* Jul */, 10),
new Date(1987, 1 /* Feb */, 11)
])
)
assert.deepEqual(result, new Date(1989, 6 /* Jul */, 10))
})

Expand Down

0 comments on commit 487a511

Please sign in to comment.