From 9b1d5d5ba9f1ff3745380a4abeef1dda85f3b546 Mon Sep 17 00:00:00 2001 From: leaftail1880 <110915645+leaftaul1880@users.noreply.github.com> Date: Mon, 5 Aug 2024 17:10:20 +0300 Subject: [PATCH] chore: move text utilities --- src/lib/text.test.ts | 13 +------------ src/lib/text.ts | 17 +---------------- src/lib/utils/ms.ts | 2 +- src/lib/utils/ngettext.test.ts | 16 ++++++++++++++++ src/lib/utils/ngettext.ts | 15 +++++++++++++++ src/modules/commands/tp.ts | 2 +- src/modules/world-edit/lib/world-edit.ts | 3 ++- src/modules/world-edit/tools/smooth.ts | 2 +- 8 files changed, 38 insertions(+), 32 deletions(-) create mode 100644 src/lib/utils/ngettext.test.ts create mode 100644 src/lib/utils/ngettext.ts diff --git a/src/lib/text.test.ts b/src/lib/text.test.ts index 7efa695c..53257e61 100644 --- a/src/lib/text.test.ts +++ b/src/lib/text.test.ts @@ -7,7 +7,7 @@ import { Player } from '@minecraft/server' import { beforeEach, describe, expect, expectTypeOf, it } from 'vitest' import { setRole } from './roles' import './text' -import { ngettext, t, textTable } from './text' +import { t, textTable } from './text' let player: Player beforeEach(() => { @@ -80,17 +80,6 @@ describe('text', () => { expect(t.error) }) - it('should stringify num with plurals', () => { - const n = 10 - - const text = ngettext(n, ['блок', 'блока', 'блоков']) - expect(`§7Было сломано §6${n} §7${text}`).toMatchInlineSnapshot(`"§7Было сломано §610 §7блоков"`) - - expect(t.num`Было сломано ${n} ${['блок', 'блока', 'блоков']}`).toMatchInlineSnapshot( - `"§7Было сломано §610§7 блоков§7"`, - ) - }) - it('should work with time', () => { expect(t.time`Прошло ${0}`).toMatchInlineSnapshot(`"§7Прошло §f0 §7миллисекунд§7"`) expect(t.time`Прошло ${3000}`).toMatchInlineSnapshot(`"§7Прошло §f3 §7секунды§7"`) diff --git a/src/lib/text.ts b/src/lib/text.ts index a4ccd1a1..5660ece8 100644 --- a/src/lib/text.ts +++ b/src/lib/text.ts @@ -4,6 +4,7 @@ import { ROLES, getRole } from './roles' import { separateNumberWithDots } from './util' import { stringify } from './utils/inspect' import { ms } from './utils/ms' +import { Plurals, ngettext } from './utils/ngettext' export type Text = string export type MaybeRawText = string | RawText @@ -180,19 +181,3 @@ export function textUnitColorize(unit: unknown, options: ColorizingOptions = {}) return unit ? '§fДа' : '§cНет' } } - -export type Plurals = [one: string, two: string, five: string] - -/** - * Gets plural form based on provided number - * - * @param n - Number - * @param forms - Plurals forms in format `1 секунда 2 секунды 5 секунд` - * @returns Plural form. Currently only Russian supported - */ -export function ngettext(n: number, [one = 'секунда', few = 'секунды', more = 'секунд']: Plurals) { - if (!Number.isInteger(n)) return more - return [one, few, more][ - n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2 - ] -} diff --git a/src/lib/utils/ms.ts b/src/lib/utils/ms.ts index 784a32a8..63ae0c47 100644 --- a/src/lib/utils/ms.ts +++ b/src/lib/utils/ms.ts @@ -1,4 +1,4 @@ -import { Plurals, ngettext } from '../text' +import { Plurals, ngettext } from './ngettext' type Time = 'year' | 'month' | 'day' | 'hour' | 'min' | 'sec' | 'ms' diff --git a/src/lib/utils/ngettext.test.ts b/src/lib/utils/ngettext.test.ts new file mode 100644 index 00000000..9250b0bf --- /dev/null +++ b/src/lib/utils/ngettext.test.ts @@ -0,0 +1,16 @@ +import { t } from 'lib/text' +import { describe, expect, it } from 'vitest' +import { ngettext } from './ngettext' + +describe('ngettext', () => { + it('should stringify num with plurals', () => { + const n = 10 + + const text = ngettext(n, ['блок', 'блока', 'блоков']) + expect(`§7Было сломано §6${n} §7${text}`).toMatchInlineSnapshot(`"§7Было сломано §610 §7блоков"`) + + expect(t.num`Было сломано ${n} ${['блок', 'блока', 'блоков']}`).toMatchInlineSnapshot( + `"§7Было сломано §610§7 блоков§7"`, + ) + }) +}) diff --git a/src/lib/utils/ngettext.ts b/src/lib/utils/ngettext.ts new file mode 100644 index 00000000..084745b3 --- /dev/null +++ b/src/lib/utils/ngettext.ts @@ -0,0 +1,15 @@ +export type Plurals = [one: string, two: string, five: string] +/** + * Gets plural form based on provided number + * + * @param n - Number + * @param forms - Plurals forms in format `1 секунда 2 секунды 5 секунд` + * @returns Plural form. Currently only Russian supported + */ + +export function ngettext(n: number, [one = 'секунда', few = 'секунды', more = 'секунд']: Plurals) { + if (!Number.isInteger(n)) return more + return [one, few, more][ + n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2 + ] +} diff --git a/src/modules/commands/tp.ts b/src/modules/commands/tp.ts index e097c7f2..db3c33e9 100644 --- a/src/modules/commands/tp.ts +++ b/src/modules/commands/tp.ts @@ -2,7 +2,7 @@ import { Player, system, world } from '@minecraft/server' import { ActionForm, Vector } from 'lib' import { isNotPlaying } from 'lib/game-utils' import { getFullname } from 'lib/get-fullname' -import { ngettext } from 'lib/text' +import { ngettext } from 'lib/utils/ngettext' import { PlaceWithSafeArea } from 'modules/places/lib/place-with-safearea' import { Spawn } from 'modules/places/spawn' import { StoneQuarry } from 'modules/places/stone-quarry/stone-quarry' diff --git a/src/modules/world-edit/lib/world-edit.ts b/src/modules/world-edit/lib/world-edit.ts index e8c47cb1..ae45a418 100644 --- a/src/modules/world-edit/lib/world-edit.ts +++ b/src/modules/world-edit/lib/world-edit.ts @@ -11,8 +11,9 @@ import { import { Vector, getRole, prompt } from 'lib' import { Sounds } from 'lib/assets/config' import { table } from 'lib/database/abstract' -import { ngettext, t } from 'lib/text' +import { t } from 'lib/text' import { stringify } from 'lib/utils/inspect' +import { ngettext } from 'lib/utils/ngettext' import { WeakPlayerMap } from 'lib/weak-player-storage' import { stringifyReplaceTargets, toPermutation, toReplaceTarget } from 'modules/world-edit/menu' import { WE_CONFIG, spawnParticlesInArea } from '../config' diff --git a/src/modules/world-edit/tools/smooth.ts b/src/modules/world-edit/tools/smooth.ts index 53bbd19e..e562e7e2 100644 --- a/src/modules/world-edit/tools/smooth.ts +++ b/src/modules/world-edit/tools/smooth.ts @@ -3,7 +3,7 @@ import { Block, BlockPermutation, Player, system, world } from '@minecraft/serve import { MinecraftBlockTypes } from '@minecraft/vanilla-data' import { is, ModalForm, util, Vector } from 'lib' import { CustomItems } from 'lib/assets/config' -import { ngettext } from 'lib/text' +import { ngettext } from 'lib/utils/ngettext' import { WorldEdit } from 'modules/world-edit/lib/world-edit' import { BlocksSetRef, getAllBlocksSets, SHARED_POSTFIX } from 'modules/world-edit/utils/blocks-set' import { BaseBrushTool } from '../lib/base-brush-tool'