Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug in fn formatRelative: next/last prefix shows depends on the day in same week or not #1664

Merged
merged 7 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}
12 changes: 7 additions & 5 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 @@ -71,10 +71,12 @@ 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(
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
12 changes: 7 additions & 5 deletions src/min/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import assert from 'power-assert'
import min from '.'

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

Expand Down Expand Up @@ -71,10 +71,12 @@ describe('min', function() {

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

Expand Down