Last active
October 4, 2019 15:45
-
-
Save c80609a/fc978835cf396a4b977061669c1f6456 to your computer and use it in GitHub Desktop.
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
import Price from '../price_format' | |
import I18next from 'i18next' | |
// noinspection ES6UnusedImports | |
import i18n, {ru_rule} from "../../../config/i18n" | |
export default class RentPrice { | |
/** Среди цен на аренду (prices) найдёт минимальную цену (с учётом скидки) | |
* и выдаст отформатированную строку с учётом локали. | |
* | |
* @param prices {array} of Objects like {value, currency, uom, season, duration, discount} | |
* @param country {string} 'en', 'ru' (ISO 3166-1 alpha-2) | |
* @returns {string} '$ 4,050 per 3 days' | |
*/ | |
static starts_from(prices, country) { | |
if (!prices.length) return '' | |
const min = prices.reduce((price, data) => RentPrice.calc(data.value, data.discount) < RentPrice.calc(price.value, data.discount) ? data : price); | |
const print_val = Price.format(RentPrice.calc(min.value, min.discount), min.currency.symbol, country) | |
const suffix = RentPrice.suffix(min.uom.index, min.duration, country) | |
return `${print_val} ${suffix}` | |
} | |
/** Создаём суффикс: "в день", "в неделю",... | |
* | |
* @param uom_index {string} day, hour, month, week | |
* @param duration {string} длительность периода для плюрализации суффикса | |
* @param country {string} en, ru (ISO 3166-1 alpha-2) | |
* @returns {string} | |
*/ | |
static suffix(uom_index, duration, country) { | |
let result, d = Number(duration) | |
if (country === 'ru') | |
result = I18next.t(`charter_attributes.charter_${uom_index}_${ru_rule(d)}`, { lng: country, duration: d }) | |
else | |
result = I18next.t(`charter_attributes.charter_${uom_index}`, { lng: country, duration: d, count: d}) | |
if (d > 1) result = result.split('.5').join('½') | |
return result.split('.0').join('') | |
} | |
static calc(val, p) { | |
return +val - (+val * (+p / 100)) | |
} | |
} |
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
import RentPrice from '../rent_price' | |
import { UOM_DAY, UOM_HOUR, UOM_MONTH, UOM_WEEK } from "../../../../config/dictionaries" | |
describe('.suffix [EN]', () => { | |
describe('single', () => { | |
test(UOM_HOUR, () => { | |
expect(RentPrice.suffix(UOM_HOUR, '1.0', 'en')).toBe('per hour') | |
}) | |
test(UOM_DAY, () => { | |
expect(RentPrice.suffix(UOM_DAY, '1.0', 'en')).toBe('per day') | |
}) | |
test(UOM_WEEK, () => { | |
expect(RentPrice.suffix(UOM_WEEK, '1.0', 'en')).toBe('per week') | |
}) | |
test(UOM_MONTH, () => { | |
expect(RentPrice.suffix(UOM_MONTH, '1.0', 'en')).toBe('per month') | |
}) | |
}) | |
describe('multiple', () => { | |
test('2 hours', () => { | |
expect(RentPrice.suffix(UOM_HOUR, '2.0', 'en')).toBe('per 2 hours') | |
}) | |
test('6 hours', () => { | |
expect(RentPrice.suffix(UOM_HOUR, '6.0', 'en')).toBe('per 6 hours') | |
}) | |
test('2 days', () => { | |
expect(RentPrice.suffix(UOM_DAY, '2.0', 'en')).toBe('per 2 days') | |
}) | |
test('5 days', () => { | |
expect(RentPrice.suffix(UOM_DAY, '5.0', 'en')).toBe('per 5 days') | |
}) | |
test('3 weeks', () => { | |
expect(RentPrice.suffix(UOM_WEEK, '3.0', 'en')).toBe('per 3 weeks') | |
}) | |
test('5 weeks', () => { | |
expect(RentPrice.suffix(UOM_WEEK, '5.0', 'en')).toBe('per 5 weeks') | |
}) | |
test('2 months', () => { | |
expect(RentPrice.suffix(UOM_MONTH, '2.0', 'en')).toBe('per 2 months') | |
}) | |
test('5 months', () => { | |
expect(RentPrice.suffix(UOM_MONTH, '5.0', 'en')).toBe('per 5 months') | |
}) | |
}) | |
describe('fract', () => { | |
test('0.5 hours', () => { | |
expect(RentPrice.suffix(UOM_HOUR, '0.5', 'en')).toBe('per 0.5 hours') | |
}) | |
// | |
test('1.3 weeks', () => { | |
expect(RentPrice.suffix(UOM_WEEK, '1.3', 'en')).toBe('per 1.3 weeks') | |
}) | |
// | |
test('2.7 months', () => { | |
expect(RentPrice.suffix(UOM_MONTH, '2.7', 'en')).toBe('per 2.7 months') | |
}) | |
test('2.5 hours', () => { | |
expect(RentPrice.suffix(UOM_HOUR, '2.5', 'en')).toBe('per 2½ hours') | |
}) | |
test('6.5 hours', () => { | |
expect(RentPrice.suffix(UOM_HOUR, '6.5', 'en')).toBe('per 6½ hours') | |
}) | |
test('2.5 days', () => { | |
expect(RentPrice.suffix(UOM_DAY, '2.5', 'en')).toBe('per 2½ days') | |
}) | |
test('5.5 days', () => { | |
expect(RentPrice.suffix(UOM_DAY, '5.5', 'en')).toBe('per 5½ days') | |
}) | |
test('3.5 weeks', () => { | |
expect(RentPrice.suffix(UOM_WEEK, '3.5', 'en')).toBe('per 3½ weeks') | |
}) | |
test('5.5 weeks', () => { | |
expect(RentPrice.suffix(UOM_WEEK, '5.5', 'en')).toBe('per 5½ weeks') | |
}) | |
test('2.5 months', () => { | |
expect(RentPrice.suffix(UOM_MONTH, '2.5', 'en')).toBe('per 2½ months') | |
}) | |
test('5.5 months', () => { | |
expect(RentPrice.suffix(UOM_MONTH, '5.5', 'en')).toBe('per 5½ months') | |
}) | |
}) | |
}) |
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
import RentPrice from '../rent_price' | |
import { UOM_DAY, UOM_HOUR, UOM_MONTH, UOM_WEEK } from "../../../../config/dictionaries" | |
describe('.suffix [RU]', () => { | |
describe('single', () => { | |
test(UOM_HOUR, () => { | |
expect(RentPrice.suffix(UOM_HOUR, '1.0', 'ru')).toBe('в час') | |
}) | |
test(UOM_DAY, () => { | |
expect(RentPrice.suffix(UOM_DAY, '1.0', 'ru')).toBe('в день') | |
}) | |
test(UOM_WEEK, () => { | |
expect(RentPrice.suffix(UOM_WEEK, '1.0', 'ru')).toBe('в неделю') | |
}) | |
test(UOM_MONTH, () => { | |
expect(RentPrice.suffix(UOM_MONTH, '1.0', 'ru')).toBe('в месяц') | |
}) | |
}) | |
describe('multiple', () => { | |
test('2 часа', () => { | |
expect(RentPrice.suffix(UOM_HOUR, '2.0', 'ru')).toBe('за 2 часа') | |
}) | |
test('6 часов', () => { | |
expect(RentPrice.suffix(UOM_HOUR, '6.0', 'ru')).toBe('за 6 часов') | |
}) | |
test('2 дня', () => { | |
expect(RentPrice.suffix(UOM_DAY, '2.0', 'ru')).toBe('за 2 дня') | |
}) | |
test('5 дней', () => { | |
expect(RentPrice.suffix(UOM_DAY, '5.0', 'ru')).toBe('за 5 дней') | |
}) | |
test('3 недели', () => { | |
expect(RentPrice.suffix(UOM_WEEK, '3.0', 'ru')).toBe('за 3 недели') | |
}) | |
test('5 недель', () => { | |
expect(RentPrice.suffix(UOM_WEEK, '5.0', 'ru')).toBe('за 5 недель') | |
}) | |
test('2 месяца', () => { | |
expect(RentPrice.suffix(UOM_MONTH, '2.0', 'ru')).toBe('за 2 месяца') | |
}) | |
test('5 месяцев', () => { | |
expect(RentPrice.suffix(UOM_MONTH, '5.0', 'ru')).toBe('за 5 месяцев') | |
}) | |
}) | |
describe('fract', () => { | |
test('0.5 часа', () => { | |
expect(RentPrice.suffix(UOM_HOUR, '0.5', 'ru')).toBe('за 0.5 часа') | |
}) | |
test('6.6 часов', () => { | |
expect(RentPrice.suffix(UOM_HOUR, '6.6', 'ru')).toBe('за 6.6 часов') | |
}) | |
test('1.3 недели', () => { | |
expect(RentPrice.suffix(UOM_WEEK, '1.3', 'ru')).toBe('за 1.3 недели') | |
}) | |
test('2.7 месяцев', () => { | |
expect(RentPrice.suffix(UOM_MONTH, '2.7', 'ru')).toBe('за 2.7 месяца') | |
}) | |
// | |
test('6.6 месяцев', () => { | |
expect(RentPrice.suffix(UOM_MONTH, '6.6', 'ru')).toBe('за 6.6 месяцев') | |
}) | |
test('2.5 часа', () => { | |
expect(RentPrice.suffix(UOM_HOUR, '2.5', 'ru')).toBe('за 2½ часа') | |
}) | |
test('6.5 часов', () => { | |
expect(RentPrice.suffix(UOM_HOUR, '6.5', 'ru')).toBe('за 6½ часов') | |
}) | |
test('2.5 дня', () => { | |
expect(RentPrice.suffix(UOM_DAY, '2.5', 'ru')).toBe('за 2½ дня') | |
}) | |
test('5.5 дня', () => { | |
expect(RentPrice.suffix(UOM_DAY, '5.5', 'ru')).toBe('за 5½ дней') | |
}) | |
test('3.5 недели', () => { | |
expect(RentPrice.suffix(UOM_WEEK, '3.5', 'ru')).toBe('за 3½ недели') | |
}) | |
test('5.5 недель', () => { | |
expect(RentPrice.suffix(UOM_WEEK, '5.5', 'ru')).toBe('за 5½ недель') | |
}) | |
test('2.5 месяца', () => { | |
expect(RentPrice.suffix(UOM_MONTH, '2.5', 'ru')).toBe('за 2½ месяца') | |
}) | |
test('5.5 месяцев', () => { | |
expect(RentPrice.suffix(UOM_MONTH, '5.5', 'ru')).toBe('за 5½ месяцев') | |
}) | |
}) | |
}) |
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
{ | |
"__desc__": "Указаны цены аренды за все периоды всех сезонов. Минимальная цена - 4500, со скидкой 10% - 4050", | |
"prices": { | |
"list": [ | |
{ | |
"season": { | |
"id": 1, | |
"index": "low" | |
}, | |
"uom": { | |
"id": 2, | |
"index": "day" | |
}, | |
"duration": "3.0", | |
"currency": { | |
"id": 1, | |
"index": "USD", | |
"symbol": "$" | |
}, | |
"value": "4500", | |
"discount": "10.00" | |
}, | |
{ | |
"season": { | |
"id": 2, | |
"index": "high" | |
}, | |
"uom": { | |
"id": 2, | |
"index": "day" | |
}, | |
"duration": "3.0", | |
"currency": { | |
"id": 1, | |
"index": "USD", | |
"symbol": "$" | |
}, | |
"value": "4500", | |
"discount": "3.00" | |
}, | |
{ | |
"season": { | |
"id": 1, | |
"index": "low" | |
}, | |
"uom": { | |
"id": 3, | |
"index": "week" | |
}, | |
"duration": "1.0", | |
"currency": { | |
"id": 1, | |
"index": "USD", | |
"symbol": "$" | |
}, | |
"value": "32000", | |
"discount": "0.00" | |
}, | |
{ | |
"season": { | |
"id": 2, | |
"index": "high" | |
}, | |
"uom": { | |
"id": 3, | |
"index": "week" | |
}, | |
"duration": "1.0", | |
"currency": { | |
"id": 1, | |
"index": "USD", | |
"symbol": "$" | |
}, | |
"value": "36000", | |
"discount": "0.00" | |
}, | |
{ | |
"season": { | |
"id": 1, | |
"index": "low" | |
}, | |
"uom": { | |
"id": 4, | |
"index": "month" | |
}, | |
"duration": "1.0", | |
"currency": { | |
"id": 1, | |
"index": "USD", | |
"symbol": "$" | |
}, | |
"value": "84456", | |
"discount": "0.00" | |
}, | |
{ | |
"season": { | |
"id": 2, | |
"index": "high" | |
}, | |
"uom": { | |
"id": 4, | |
"index": "month" | |
}, | |
"duration": "1.0", | |
"currency": { | |
"id": 1, | |
"index": "USD", | |
"symbol": "$" | |
}, | |
"value": "109800", | |
"discount": "0.00" | |
} | |
], | |
"business": { | |
"index": "rent", | |
"id": 1 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment