Skip to content

Commit

Permalink
chore: move text utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
leaftail1880 committed Aug 5, 2024
1 parent d3eee1e commit 9b1d5d5
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 32 deletions.
13 changes: 1 addition & 12 deletions src/lib/text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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"`)
Expand Down
17 changes: 1 addition & 16 deletions src/lib/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
]
}
2 changes: 1 addition & 1 deletion src/lib/utils/ms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Plurals, ngettext } from '../text'
import { Plurals, ngettext } from './ngettext'

type Time = 'year' | 'month' | 'day' | 'hour' | 'min' | 'sec' | 'ms'

Expand Down
16 changes: 16 additions & 0 deletions src/lib/utils/ngettext.test.ts
Original file line number Diff line number Diff line change
@@ -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"`,
)
})
})
15 changes: 15 additions & 0 deletions src/lib/utils/ngettext.ts
Original file line number Diff line number Diff line change
@@ -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
]
}
2 changes: 1 addition & 1 deletion src/modules/commands/tp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion src/modules/world-edit/lib/world-edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/modules/world-edit/tools/smooth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 9b1d5d5

Please sign in to comment.