Skip to content

Commit

Permalink
Update code style and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Jul 10, 2023
1 parent 8901510 commit 84c9b11
Show file tree
Hide file tree
Showing 32 changed files with 551 additions and 431 deletions.
2 changes: 1 addition & 1 deletion browser/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { equal } from 'uvu/assert'
import { test } from 'uvu'
import { equal } from 'uvu/assert'

import { browser } from '../index.js'

Expand Down
2 changes: 1 addition & 1 deletion browser/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { browser } from '../index.js'

function test(locale: 'ru' | 'en'): void {
function test(locale: 'en' | 'ru'): void {
console.log(locale)
}

Expand Down
8 changes: 4 additions & 4 deletions count/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type {
TranslationJSON,
TranslationFunction,
TranslationFunctionAlternatives
TranslationFunctionAlternatives,
TranslationJSON
} from '../create-i18n/index.js'

export type CountInput = {
one?: TranslationJSON
few?: TranslationJSON
many: TranslationJSON
one?: TranslationJSON
}

interface Count {
Expand All @@ -33,7 +33,7 @@ interface Count {
* @param input Pluralization variants.
* @return Transform for translation.
*/
<Parameters extends Record<string, string | number>>(
<Parameters extends Record<string, number | string>>(
input: TranslationFunction<[Parameters], string>
): TranslationFunctionAlternatives<Parameters>
<Input extends CountInput>(input: Input): TranslationFunction<
Expand Down
2 changes: 1 addition & 1 deletion count/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transform, strings } from '../transforms/index.js'
import { strings, transform } from '../transforms/index.js'

export const count = transform((locale, translation, num) => {
let form = new Intl.PluralRules(locale).select(num)
Expand Down
25 changes: 12 additions & 13 deletions count/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { ComponentsJSON } from '../index.js'

import { equal } from 'uvu/assert'
import { atom } from 'nanostores'
import { test } from 'uvu'
import { equal } from 'uvu/assert'

import { createI18n, count } from '../index.js'
import { count, createI18n } from '../index.js'
import type { ComponentsJSON } from '../index.js'

let resolveGet: (translations: ComponentsJSON) => void = () => {}

Expand All @@ -23,26 +22,26 @@ let i18n = createI18n(locale, { get })

test('uses pluralization rules', async () => {
let messages = i18n('templates', {
robots: count({
one: '{count} robot',
many: '{count} robots'
}),
onlyMany: count({
many: 'many'
}),
robots: count({
many: '{count} robots',
one: '{count} robot'
})
})

messages.subscribe(() => {})

await getResponse({
templates: {
robots: {
one: '{count} робот',
few: '{count} робота',
many: '{count} роботов'
},
onlyMany: {
many: 'много'
},
robots: {
few: '{count} робота',
many: '{count} роботов',
one: '{count} робот'
}
}
})
Expand Down
10 changes: 5 additions & 5 deletions count/types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { AssertTrue as Assert, IsExact } from 'conditional-type-checks'
import type { TranslationFunction } from '../create-i18n'

import { count, params } from '..'
import type { TranslationFunction } from '../create-i18n'

const f1 = count({
one: 'One page',
many: '{count} pages'
many: '{count} pages',
one: 'One page'
})
const f2 = count(
params<{ category: number }>({
one: 'One page in {category}',
many: '{count} pages in {category}'
many: '{count} pages in {category}',
one: 'One page in {category}'
})
)
const f21 = f2({ category: 12 })
Expand Down
5 changes: 3 additions & 2 deletions create-i18n/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ReadableAtom } from 'nanostores'

import type { LocaleStore } from '../locale-from/index.js'
import type { Processor } from '../processor/index.js'

Expand All @@ -14,7 +15,7 @@ export interface ComponentsJSON {

export interface TranslationFunction<
Arguments extends any[] = any[],
Output = TranslationJSON | Translation
Output = Translation | TranslationJSON
> {
(...args: Arguments): Output
}
Expand All @@ -40,8 +41,8 @@ export type Messages<Body extends Translations = Translations> =
ReadableAtom<Body>

export interface I18n<Locale extends string = string> {
loading: ReadableAtom<boolean>
cache: Record<Locale, Translations>
loading: ReadableAtom<boolean>

<Body extends Translations>(
componentName: string,
Expand Down
37 changes: 18 additions & 19 deletions create-i18n/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { StoreValue } from 'nanostores'
import type { ComponentsJSON } from '../index.js'

import { atom, STORE_UNMOUNT_DELAY } from 'nanostores'
import { restoreAll, spyOn } from 'nanospy'
import { equal, match } from 'uvu/assert'
import { delay } from 'nanodelay'
import { restoreAll, spyOn } from 'nanospy'
import { atom, STORE_UNMOUNT_DELAY } from 'nanostores'
import type { StoreValue } from 'nanostores'
import { test } from 'uvu'
import { equal, match } from 'uvu/assert'

import { createI18n, params, count } from '../index.js'
import { count, createI18n, params } from '../index.js'
import type { ComponentsJSON } from '../index.js'

let getCalls: string[] = []
let resolveGet: (translations: ComponentsJSON) => void = () => {}
Expand Down Expand Up @@ -49,7 +48,7 @@ test('is loaded from the start', () => {
})

test('loads locale', async () => {
let locale = atom<'en' | 'ru' | 'fr'>('ru')
let locale = atom<'en' | 'fr' | 'ru'>('ru')
let i18n = createI18n(locale, { get })

equal(i18n.loading.get(), false)
Expand Down Expand Up @@ -92,7 +91,7 @@ test('loads locale', async () => {
})

test('is ready for locale change in the middle of request', async () => {
let locale = atom<'en' | 'ru' | 'fr'>('en')
let locale = atom<'en' | 'fr' | 'ru'>('en')
let i18n = createI18n(locale, { get })
let messages = i18n('component', { title: 'Title' })
let events: string[] = []
Expand All @@ -108,7 +107,7 @@ test('is ready for locale change in the middle of request', async () => {
})

test('is ready for wrong response order', async () => {
let locale = atom<'en' | 'ru' | 'fr'>('en')
let locale = atom<'en' | 'fr' | 'ru'>('en')
let i18n = createI18n(locale, { get })
let messages = i18n('component', { title: 'Title' })
let events: string[] = []
Expand Down Expand Up @@ -160,7 +159,7 @@ test('mixes translations with base', async () => {
let i18n = createI18n(locale, { get })
equal(i18n.loading.get(), false)

let messages = i18n('component', { title: 'Title', other: 'Other' })
let messages = i18n('component', { other: 'Other', title: 'Title' })
let events: string[] = []
messages.subscribe(t => {
events.push(t.other)
Expand All @@ -184,8 +183,8 @@ test('applies transforms', async () => {
let messages = i18n('component', {
pages: params<{ category: number }>(
count({
one: 'One page in {category}',
many: '{count} pages in {category}'
many: '{count} pages in {category}',
one: 'One page in {category}'
})
)
})
Expand All @@ -202,9 +201,9 @@ test('applies transforms', async () => {
await getResponse({
component: {
pages: {
one: '{count} страница в {category}',
few: '{count} страницы в {category}',
many: '{count} страниц в {category}'
many: '{count} страниц в {category}',
one: '{count} страница в {category}'
}
}
})
Expand All @@ -217,8 +216,8 @@ test('supports reverse transform', () => {
let messages = i18n('component', {
reverse: count(
params<{ category: number }>({
one: 'One page in {category}',
many: '{count} pages in {category}'
many: '{count} pages in {category}',
one: 'One page in {category}'
})
)
})
Expand Down Expand Up @@ -248,10 +247,10 @@ test('tracks double definition', () => {
test('cache is used on first use', async () => {
let locale = atom('ru')
let i18n = createI18n(locale, {
get,
cache: {
ru: { games: { title: 'Игры' } }
}
},
get
})
let gamesWithCache = i18n('games', { title: 'Games' })
equal(gamesWithCache.value?.title, 'Игры')
Expand Down
23 changes: 11 additions & 12 deletions create-i18n/partial.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { ComponentsJSON } from '../index.js'

import { atom, STORE_UNMOUNT_DELAY } from 'nanostores'
import { equal } from 'uvu/assert'
import { delay } from 'nanodelay'
import { atom, STORE_UNMOUNT_DELAY } from 'nanostores'
import { test } from 'uvu'
import { equal } from 'uvu/assert'

import { createI18n } from '../index.js'
import type { ComponentsJSON } from '../index.js'

let getCalls: object[] = []
let resolveGet: (translations: ComponentsJSON[]) => void = () => {}
Expand Down Expand Up @@ -34,7 +33,7 @@ test.after.each(() => {
getCalls = []
})

let locale = atom<'en' | 'ru' | 'fr' | 'de'>('ru')
let locale = atom<'de' | 'en' | 'fr' | 'ru'>('ru')
let i18n = createI18n(locale, { get })
let events: string[] = []

Expand Down Expand Up @@ -67,9 +66,9 @@ test("after mount shouldn't load translations with same prefix", async () => {
test('loads translations partial', async () => {
await getResponse([
{
'main/post': { title: 'Публикация' },
'main/comment': { title: 'Комментарий' },
'main/heading': { title: 'Заголовок' },
'main/comment': { title: 'Комментарий' }
'main/post': { title: 'Публикация' }
}
])
equal(i18n.loading.get(), false)
Expand Down Expand Up @@ -207,19 +206,19 @@ test('if get returns array, it transforms to object', async () => {

await getResponse([
{
'main/post': { title: 'Publikation' },
'main/comment': { title: 'Kommentar' },
'main/heading': { title: 'Titel' },
'main/comment': { title: 'Kommentar' }
'main/post': { title: 'Publikation' }
},
{
'chat/message': { title: 'Nachricht' }
}
])
equal(i18n.cache.de, {
'main/post': { title: 'Publikation' },
'main/heading': { title: 'Titel' },
'chat/message': { title: 'Nachricht' },
'main/comment': { title: 'Kommentar' },
'chat/message': { title: 'Nachricht' }
'main/heading': { title: 'Titel' },
'main/post': { title: 'Publikation' }
})
equal(i18n.loading.get(), false)
equal(events, [
Expand Down
18 changes: 9 additions & 9 deletions create-i18n/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { localeFrom, browser, createI18n, params, count } from '../index.js'
import { browser, count, createI18n, localeFrom, params } from '../index.js'

let locale = localeFrom(
browser({
Expand Down Expand Up @@ -33,17 +33,17 @@ let i18n2 = createI18n(locale, {
})

let messages2 = i18n2('post', {
title: params('Title: {name}'),
posts: count({
one: '1 post',
many: '{count} posts'
}),
pages: params<{ category: number }>(
count({
one: 'One page in {category}',
many: '{count} pages in {category}'
many: '{count} pages in {category}',
one: 'One page in {category}'
})
)
),
posts: count({
many: '{count} posts',
one: '1 post'
}),
title: params('Title: {name}')
})
let t = messages2.get()
testString(t.title({ name: 'Post' }))
Expand Down
2 changes: 1 addition & 1 deletion demo/footer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { locale, localeSetting, i18n } from './i18n.js'
import { i18n, locale, localeSetting } from './i18n.js'

let currentLocale = document.querySelector('strong')
let changeLocale = document.querySelector('select')
Expand Down
6 changes: 3 additions & 3 deletions demo/i18n.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { persistentAtom } from '@nanostores/persistent'

import { createI18n, localeFrom, browser, formatter } from '../index.js'
import { browser, createI18n, formatter, localeFrom } from '../index.js'

export let localeSetting = persistentAtom('locale')

Expand All @@ -16,8 +16,8 @@ export let i18n = createI18n(locale, {
setTimeout(() => {
resolve({
page: {
title: 'Демо интернационализации',
desc: 'Сегодня {date}'
desc: 'Сегодня {date}',
title: 'Демо интернационализации'
}
})
}, 1000)
Expand Down
2 changes: 1 addition & 1 deletion demo/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import './page.js'
import './footer.js'
import './page.js'
10 changes: 5 additions & 5 deletions demo/page.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { i18n, format } from './i18n.js'
import { params } from '../index.js'
import { format, i18n } from './i18n.js'

let messages = i18n('page', {
title: 'I18n demo',
desc: params('Today is {date}')
desc: params('Today is {date}'),
title: 'I18n demo'
})

let title = document.querySelector('h1')
Expand All @@ -14,9 +14,9 @@ messages.subscribe(t => {
title.innerText = t.title
desc.innerText = t.desc({
date: time(new Date(), {
year: 'numeric',
day: 'numeric',
month: 'long',
day: 'numeric'
year: 'numeric'
})
})
})
1 change: 1 addition & 0 deletions formatter/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ReadableAtom } from 'nanostores'

import type { LocaleStore } from '../locale-from/index.js'

export interface Formatter {
Expand Down
Loading

0 comments on commit 84c9b11

Please sign in to comment.