From b77ea3fce5067ffbe93084d1461f69fe79f73efa Mon Sep 17 00:00:00 2001 From: Ramil Fatullaev Date: Thu, 12 Sep 2024 17:53:46 +0300 Subject: [PATCH] fixed pluralize --- src/app.js | 4 +++- src/index.js | 15 +++++++-------- src/utils.js | 10 +++++++++- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/app.js b/src/app.js index 4a76567b8..af77179f7 100644 --- a/src/app.js +++ b/src/app.js @@ -1,5 +1,6 @@ import React from 'react'; import './styles.css'; +import { pluralize } from './utils'; /** * Приложение @@ -27,7 +28,8 @@ function App({ store }) { >
{item.code}
- {item.title} {item.count > 0 ? `| Выделяли ${item.count} раз` : ''} + {item.title} + {item.count > 0 ? `| Выделяли ${item.count} ${pluralize(item.count)}` : ''}
diff --git a/src/index.js b/src/index.js index 9e96418fb..5140f9b7b 100644 --- a/src/index.js +++ b/src/index.js @@ -1,18 +1,17 @@ import React from 'react'; import { createRoot } from 'react-dom/client'; -import { getGenerateCode } from './utils.js'; import App from './app.js'; import Store from './store.js'; const store = new Store({ list: [ - { code: getGenerateCode(), title: 'Название элемента', count: 0 }, - { code: getGenerateCode(), title: 'Некий объект', count: 0 }, - { code: getGenerateCode(), title: 'Заголовок', count: 0 }, - { code: getGenerateCode(), title: 'Очень длинное название элемента из семи слов', count: 0 }, - { code: getGenerateCode(), title: 'Запись', count: 0 }, - { code: getGenerateCode(), title: 'Шестая запись', count: 0 }, - { code: getGenerateCode(), title: 'Седьмая запись', count: 0 }, + { code: 1, title: 'Название элемента', count: 0 }, + { code: 2, title: 'Некий объект', count: 0 }, + { code: 3, title: 'Заголовок', count: 0 }, + { code: 4, title: 'Очень длинное название элемента из семи слов', count: 0 }, + { code: 5, title: 'Запись', count: 0 }, + { code: 6, title: 'Шестая запись', count: 0 }, + { code: 7, title: 'Седьмая запись', count: 0 }, ], }); diff --git a/src/utils.js b/src/utils.js index 02c927a33..ee43136b0 100644 --- a/src/utils.js +++ b/src/utils.js @@ -29,10 +29,18 @@ export function createElement(name, props = {}, ...children) { // Генерация уникального кода function generateCode() { - let code = 0; + let code = 7; return function () { return ++code; }; } export const getGenerateCode = generateCode(); + +//Плюрализация +export function pluralize(number) { + const forms = ['раз', 'раза', 'раз']; + if (number % 10 === 1 && number % 100 !== 11) return forms[0]; + else if ([2, 3, 4].includes(number % 10) && ![12, 13, 14].includes(number % 100)) return forms[1]; + else return forms[2]; +}