Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd852d4
commit 502b885
Showing
14 changed files
with
1,430 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
function buildLocalizeTokenFn(schema) { | ||
return function(count, options) { | ||
if (count === 1) { | ||
if (options.addSuffix) { | ||
return schema.one[0].replace('{{time}}', schema.one[2]) | ||
} else { | ||
return schema.one[0].replace('{{time}}', schema.one[1]) | ||
} | ||
} else { | ||
var rem = count % 10 === 1 && count % 100 !== 11 | ||
if (options.addSuffix) { | ||
return schema.other[0] | ||
.replace('{{time}}', rem ? schema.other[3] : schema.other[4]) | ||
.replace('{{count}}', count) | ||
} else { | ||
return schema.other[0] | ||
.replace('{{time}}', rem ? schema.other[1] : schema.other[2]) | ||
.replace('{{count}}', count) | ||
} | ||
} | ||
} | ||
} | ||
|
||
var formatDistanceLocale = { | ||
lessThanXSeconds: buildLocalizeTokenFn({ | ||
one: ['mazāk par {{time}}', 'sekundi', 'sekundi'], | ||
other: [ | ||
'mazāk nekā {{count}} {{time}}', | ||
'sekunde', | ||
'sekundes', | ||
'sekundes', | ||
'sekundēm' | ||
] | ||
}), | ||
|
||
xSeconds: buildLocalizeTokenFn({ | ||
one: ['1 {{time}}', 'sekunde', 'sekundes'], | ||
other: ['{{count}} {{time}}', 'sekunde', 'sekundes', 'sekundes', 'sekundēm'] | ||
}), | ||
|
||
halfAMinute: function(count, options) { | ||
if (options.addSuffix) { | ||
return 'pusminūtes' | ||
} else { | ||
return 'pusminūte' | ||
} | ||
}, | ||
|
||
lessThanXMinutes: buildLocalizeTokenFn({ | ||
one: ['mazāk par {{time}}', 'minūti', 'minūti'], | ||
other: [ | ||
'mazāk nekā {{count}} {{time}}', | ||
'minūte', | ||
'minūtes', | ||
'minūtes', | ||
'minūtēm' | ||
] | ||
}), | ||
|
||
xMinutes: buildLocalizeTokenFn({ | ||
one: ['1 {{time}}', 'minūte', 'minūtes'], | ||
other: ['{{count}} {{time}}', 'minūte', 'minūtes', 'minūtes', 'minūtēm'] | ||
}), | ||
|
||
aboutXHours: buildLocalizeTokenFn({ | ||
one: ['apmēram 1 {{time}}', 'stunda', 'stundas'], | ||
other: [ | ||
'apmēram {{count}} {{time}}', | ||
'stunda', | ||
'stundas', | ||
'stundas', | ||
'stundām' | ||
] | ||
}), | ||
|
||
xHours: buildLocalizeTokenFn({ | ||
one: ['1 {{time}}', 'stunda', 'stundas'], | ||
other: ['{{count}} {{time}}', 'stunda', 'stundas', 'stundas', 'stundām'] | ||
}), | ||
|
||
xDays: buildLocalizeTokenFn({ | ||
one: ['1 {{time}}', 'diena', 'dienas'], | ||
other: ['{{count}} {{time}}', 'diena', 'dienas', 'dienas', 'dienām'] | ||
}), | ||
|
||
aboutXMonths: buildLocalizeTokenFn({ | ||
one: ['apmēram 1 {{time}}', 'mēnesis', 'mēneša'], | ||
other: [ | ||
'apmēram {{count}} {{time}}', | ||
'mēnesis', | ||
'mēneši', | ||
'mēneša', | ||
'mēnešiem' | ||
] | ||
}), | ||
|
||
xMonths: buildLocalizeTokenFn({ | ||
one: ['1 {{time}}', 'mēnesis', 'mēneša'], | ||
other: ['{{count}} {{time}}', 'mēnesis', 'mēneši', 'mēneša', 'mēnešiem'] | ||
}), | ||
|
||
aboutXYears: buildLocalizeTokenFn({ | ||
one: ['apmēram 1 {{time}}', 'gads', 'gada'], | ||
other: ['apmēram {{count}} {{time}}', 'gads', 'gadi', 'gada', 'gadiem'] | ||
}), | ||
|
||
xYears: buildLocalizeTokenFn({ | ||
one: ['1 {{time}}', 'gads', 'gada'], | ||
other: ['{{count}} {{time}}', 'gads', 'gadi', 'gada', 'gadiem'] | ||
}), | ||
|
||
overXYears: buildLocalizeTokenFn({ | ||
one: ['ilgāk par 1 {{time}}', 'gadu', 'gadu'], | ||
other: ['vairāk nekā {{count}} {{time}}', 'gads', 'gadi', 'gada', 'gadiem'] | ||
}), | ||
|
||
almostXYears: buildLocalizeTokenFn({ | ||
one: ['gandrīz 1 {{time}}', 'gads', 'gada'], | ||
other: ['vairāk nekā {{count}} {{time}}', 'gads', 'gadi', 'gada', 'gadiem'] | ||
}) | ||
} | ||
|
||
export default function formatDistance(token, count, options) { | ||
options = options || {} | ||
var result = formatDistanceLocale[token](count, options) | ||
|
||
if (options.addSuffix) { | ||
if (options.comparison > 0) { | ||
return 'pēc ' + result | ||
} else { | ||
return 'pirms ' + result | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
// @flow | ||
/* eslint-env mocha */ | ||
|
||
import assert from 'power-assert' | ||
import formatDistance from '.' | ||
|
||
describe('lv locale > formatDistance', function() { | ||
describe('lessThanXSeconds', function() { | ||
context('when the count equals 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('lessThanXSeconds', 1) === 'mazāk par sekundi') | ||
}) | ||
}) | ||
|
||
context('when the count is more than 1', function() { | ||
it('returns a proper string', function() { | ||
assert( | ||
formatDistance('lessThanXSeconds', 2) === 'mazāk nekā 2 sekundes' | ||
) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('xSeconds', function() { | ||
context('when the count equals 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('xSeconds', 1) === '1 sekunde') | ||
}) | ||
}) | ||
|
||
context('when the count is more than 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('xSeconds', 2) === '2 sekundes') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('halfAMinute', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('halfAMinute') === 'pusminūte') | ||
}) | ||
|
||
it('ignores the second argument', function() { | ||
assert(formatDistance('halfAMinute', 123) === 'pusminūte') | ||
}) | ||
}) | ||
|
||
describe('lessThanXMinutes', function() { | ||
context('when the count equals 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('lessThanXMinutes', 1) === 'mazāk par minūti') | ||
}) | ||
}) | ||
|
||
context('when the count is more than 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('lessThanXMinutes', 2) === 'mazāk nekā 2 minūtes') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('xMinutes', function() { | ||
context('when the count equals 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('xMinutes', 1) === '1 minūte') | ||
}) | ||
}) | ||
|
||
context('when the count is more than 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('xMinutes', 2) === '2 minūtes') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('aboutXHours', function() { | ||
context('when the count equals 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('aboutXHours', 1) === 'apmēram 1 stunda') | ||
}) | ||
}) | ||
|
||
context('when the count is more than 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('aboutXHours', 2) === 'apmēram 2 stundas') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('xHours', function() { | ||
context('when the count equals 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('xHours', 1) === '1 stunda') | ||
}) | ||
}) | ||
|
||
context('when the count is more than 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('xHours', 2) === '2 stundas') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('xDays', function() { | ||
context('when the count equals 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('xDays', 1) === '1 diena') | ||
}) | ||
}) | ||
|
||
context('when the count is more than 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('xDays', 2) === '2 dienas') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('aboutXMonths', function() { | ||
context('when the count equals 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('aboutXMonths', 1) === 'apmēram 1 mēnesis') | ||
}) | ||
}) | ||
|
||
context('when the count is more than 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('aboutXMonths', 2) === 'apmēram 2 mēneši') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('xMonths', function() { | ||
context('when the count equals 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('xMonths', 1) === '1 mēnesis') | ||
}) | ||
}) | ||
|
||
context('when the count is more than 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('xMonths', 2) === '2 mēneši') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('aboutXYears', function() { | ||
context('when the count equals 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('aboutXYears', 1) === 'apmēram 1 gads') | ||
}) | ||
}) | ||
|
||
context('when the count is more than 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('aboutXYears', 2) === 'apmēram 2 gadi') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('xYears', function() { | ||
context('when the count equals 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('xYears', 1) === '1 gads') | ||
}) | ||
}) | ||
|
||
context('when the count is more than 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('xYears', 2) === '2 gadi') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('overXYears', function() { | ||
context('when the count equals 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('overXYears', 1) === 'ilgāk par 1 gadu') | ||
}) | ||
}) | ||
|
||
context('when the count is more than 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('overXYears', 2) === 'vairāk nekā 2 gadi') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('almostXYears', function() { | ||
context('when the count equals 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('almostXYears', 1) === 'gandrīz 1 gads') | ||
}) | ||
}) | ||
|
||
context('when the count is more than 1', function() { | ||
it('returns a proper string', function() { | ||
assert(formatDistance('almostXYears', 2) === 'vairāk nekā 2 gadi') | ||
}) | ||
}) | ||
}) | ||
|
||
context('with a past suffix', function() { | ||
it('adds `pirms` to a string', function() { | ||
var result = formatDistance('aboutXYears', 1, { | ||
addSuffix: true, | ||
comparison: -1 | ||
}) | ||
assert(result === 'pirms apmēram 1 gada') | ||
}) | ||
}) | ||
|
||
context('with a future suffix', function() { | ||
it('adds `pēc` to a string', function() { | ||
var result = formatDistance('halfAMinute', null, { | ||
addSuffix: true, | ||
comparison: 1 | ||
}) | ||
assert(result === 'pēc pusminūtes') | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.