Skip to content

Instantly share code, notes, and snippets.

@amankkg
Last active October 22, 2019 13:41
Show Gist options
  • Save amankkg/da83c2e17f63f39f56302ff4220a8d9e to your computer and use it in GitHub Desktop.
Save amankkg/da83c2e17f63f39f56302ff4220a8d9e to your computer and use it in GitHub Desktop.
Unit word form, word ending, unit suffix (for Russian)
// type getUnitForm = ([string, string, string]) => (number) => string
export const getUnitForm = ([nominative, genitive, plural]) => (num) => {
num = Math.abs(num) % 100
if (num >= 11 && num <= 19) return plural
switch (num % 10) {
case (1): return nominative
case (2):
case (3):
case (4): return genitive
default: return plural
}
}
import {getUnitForm} from './get-unit-form'
describe('getUnitForm', () => {
const fn = getUnitForm(['зал', 'зала', 'залов'])
it('should return "зал" for 1, 21, 31, 101', () => {
const expected = 'зал'
expect(fn(1)).toBe(expected)
expect(fn(21)).toBe(expected)
expect(fn(31)).toBe(expected)
expect(fn(101)).toBe(expected)
})
it('should return "зала" for 2, 22, 33, 104', () => {
const expected = 'зала'
expect(fn(2)).toBe(expected)
expect(fn(22)).toBe(expected)
expect(fn(33)).toBe(expected)
expect(fn(104)).toBe(expected)
})
it('should return "залов" for 0, 5, 11, 12, 26, 37, 100, 111, 114', () => {
const expected = 'залов'
expect(fn(0)).toBe(expected)
expect(fn(5)).toBe(expected)
expect(fn(11)).toBe(expected)
expect(fn(12)).toBe(expected)
expect(fn(26)).toBe(expected)
expect(fn(37)).toBe(expected)
expect(fn(100)).toBe(expected)
expect(fn(111)).toBe(expected)
expect(fn(114)).toBe(expected)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment