Skip to content

Instantly share code, notes, and snippets.

@c80609a
Last active October 4, 2019 15:45
Show Gist options
  • Save c80609a/fc978835cf396a4b977061669c1f6456 to your computer and use it in GitHub Desktop.
Save c80609a/fc978835cf396a4b977061669c1f6456 to your computer and use it in GitHub Desktop.
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('&frac12;')
return result.split('.0').join('')
}
static calc(val, p) {
return +val - (+val * (+p / 100))
}
}
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&frac12; hours')
})
test('6.5 hours', () => {
expect(RentPrice.suffix(UOM_HOUR, '6.5', 'en')).toBe('per 6&frac12; hours')
})
test('2.5 days', () => {
expect(RentPrice.suffix(UOM_DAY, '2.5', 'en')).toBe('per 2&frac12; days')
})
test('5.5 days', () => {
expect(RentPrice.suffix(UOM_DAY, '5.5', 'en')).toBe('per 5&frac12; days')
})
test('3.5 weeks', () => {
expect(RentPrice.suffix(UOM_WEEK, '3.5', 'en')).toBe('per 3&frac12; weeks')
})
test('5.5 weeks', () => {
expect(RentPrice.suffix(UOM_WEEK, '5.5', 'en')).toBe('per 5&frac12; weeks')
})
test('2.5 months', () => {
expect(RentPrice.suffix(UOM_MONTH, '2.5', 'en')).toBe('per 2&frac12; months')
})
test('5.5 months', () => {
expect(RentPrice.suffix(UOM_MONTH, '5.5', 'en')).toBe('per 5&frac12; months')
})
})
})
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&frac12; часа')
})
test('6.5 часов', () => {
expect(RentPrice.suffix(UOM_HOUR, '6.5', 'ru')).toBe('за 6&frac12; часов')
})
test('2.5 дня', () => {
expect(RentPrice.suffix(UOM_DAY, '2.5', 'ru')).toBe('за 2&frac12; дня')
})
test('5.5 дня', () => {
expect(RentPrice.suffix(UOM_DAY, '5.5', 'ru')).toBe('за 5&frac12; дней')
})
test('3.5 недели', () => {
expect(RentPrice.suffix(UOM_WEEK, '3.5', 'ru')).toBe('за 3&frac12; недели')
})
test('5.5 недель', () => {
expect(RentPrice.suffix(UOM_WEEK, '5.5', 'ru')).toBe('за 5&frac12; недель')
})
test('2.5 месяца', () => {
expect(RentPrice.suffix(UOM_MONTH, '2.5', 'ru')).toBe('за 2&frac12; месяца')
})
test('5.5 месяцев', () => {
expect(RentPrice.suffix(UOM_MONTH, '5.5', 'ru')).toBe('за 5&frac12; месяцев')
})
})
})
{
"__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