diff --git a/README.md b/README.md index f347df4..4c1d75a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# tools +# toolkit My most frequently used tools in programming diff --git a/plural/README.md b/plural/README.md new file mode 100644 index 0000000..fc29dba --- /dev/null +++ b/plural/README.md @@ -0,0 +1,37 @@ +# Функция изменения формы слов в зависимости от числительного + +Часто встречается задача вывода правильного окончания слова с предшествующим ему числом. + +Например: + +- 1 депутат +- 2–4 депутат**а** +- 0, 5-9 или 10 депутат**ов** + +## Примеры исользования + +### Typescript +```typescript +import { plural } from 'plural'; + +const totalOrders = 2; + +const words = ['заказ', 'заказа', 'заказов']; + +const result = `${totalOrders} ${plural(totalOrders, words)}`; + +console.log(result); // 2 заказа +``` + +### PHP +```php +include 'plural.php'; + +$totalOrders = 2; + +$words = ['заказ', 'заказа', 'заказов']; + +$result = "{$totalOrders} {plural($totalOrders, $words)}"; + +echo result; // 2 заказа +``` diff --git a/plural/php/index.php b/plural/php/index.php new file mode 100644 index 0000000..77dafea --- /dev/null +++ b/plural/php/index.php @@ -0,0 +1,9 @@ + 4 && $count % 100 < 20 ? 2 : $cases[min($count % 10, 5)] + ]; +} \ No newline at end of file diff --git a/plural/typescript/index.ts b/plural/typescript/index.ts new file mode 100644 index 0000000..c5c207f --- /dev/null +++ b/plural/typescript/index.ts @@ -0,0 +1,8 @@ +export type WordForms = [string, string, string]; + +export const plural = (count: number, words: WordForms): string => { + const cases = [2, 0, 1, 1, 1, 2]; + return words[ + count % 100 > 4 && count % 100 < 20 ? 2 : cases[Math.min(count % 10, 5)] + ]; + }; \ No newline at end of file