Add svelte, sveltify the code and add some tests

This commit is contained in:
Christian Kaisermann 2018-07-26 01:40:19 -03:00
parent f362bea98f
commit 6584c1375b
6 changed files with 102 additions and 22 deletions

6
package-lock.json generated
View File

@ -9782,6 +9782,12 @@
"integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=",
"dev": true
},
"svelte": {
"version": "2.9.7",
"resolved": "https://registry.npmjs.org/svelte/-/svelte-2.9.7.tgz",
"integrity": "sha512-+OwyqaGgy25vKBmZOR7usUE4bK1sHnxvv+Q+8IXFZsanvR+acTjIL+GRdRS8XRvoaxfxM47DpwWk1wo4RV0ucg==",
"dev": true
},
"symbol-observable": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/symbol-observable/-/symbol-observable-1.2.0.tgz",

View File

@ -99,6 +99,7 @@
"rollup-plugin-sourcemaps": "^0.4.2",
"rollup-plugin-typescript2": "^0.11.1",
"semantic-release": "^15.0.0",
"svelte": "^2.9.7",
"ts-jest": "^22.0.0",
"ts-node": "^6.0.0",
"tslint": "^5.8.0",

View File

@ -11,3 +11,20 @@ export interface Sveltei18n {
upper: TranslationGetter
lower: TranslationGetter
}
export interface SvelteEventListener {
cancel(): void
}
export interface SvelteStore {
on: (event: string, callback: Function) => SvelteEventListener
set: (newState: Object) => void
fire: (event: string, value: any) => void
[prop: string]: any
}
export interface Locale {
[id: string | number]: string
}
export interface Locales {
[locale: string]: Locale
}

View File

@ -1,18 +1,19 @@
import { InterpolationObj, Sveltei18n } from './interfaces'
import { InterpolationObj, Sveltei18n, SvelteStore, Locale, Locales } from './interfaces'
import { capitalize, titlelize, upper, lower, getNestedProp } from './utils'
export default function() {
const _ = <Sveltei18n>function(str: string, values: InterpolationObj) {
return str
}
export default function(store: SvelteStore, locales: Locales) {
store.locale = (locale: string) => store.fire('locale', locale)
store.on('locale', function(locale: string) {
const localeDict: Locale = locales[locale]
const _ = <Sveltei18n>function(id, values) {
return getNestedProp(localeDict, id) || id
}
Object.assign(_, {
capitalize: (id: string, values: InterpolationObj) =>
_(id, values).replace(/(^|\s)\S/, l => l.toUpperCase()),
titlelize: (id: string, values: InterpolationObj) =>
_(id, values).replace(/(^|\s)\S/g, l => l.toUpperCase()),
upper: (id: string, values: InterpolationObj) => _(id, values).toLocaleUpperCase(),
lower: (id: string, values: InterpolationObj) => _(id, values).toLocaleLowerCase()
_.capitalize = (id, values) => capitalize(_(id, values))
_.titlelize = (id, values) => titlelize(_(id, values))
_.upper = (id, values) => upper(_(id, values))
_.lower = (id, values) => lower(_(id, values))
store.set({ locale, _ })
})
return _
}

18
src/utils.ts Normal file
View File

@ -0,0 +1,18 @@
export const capitalize = (str: string) => str.replace(/(^|\s)\S/, l => l.toUpperCase())
export const titlelize = (str: string) => str.replace(/(^|\s)\S/g, l => l.toUpperCase())
export const upper = (str: string) => str.toLocaleUpperCase()
export const lower = (str: string) => str.toLocaleLowerCase()
export const getNestedProp = (obj: { [prop: string]: any }, path: string) => {
try {
return path
.replace('[', '.')
.replace(']', '')
.split('.')
.reduce(function(o, property) {
return o[property]
}, obj)
} catch (err) {
return undefined
}
}

View File

@ -1,14 +1,51 @@
import DummyClass from '../src/svelte-i18n'
import i18n from '../src/svelte-i18n'
import { Store } from 'svelte/store.umd'
import { capitalize, titlelize, upper, lower, getNestedProp } from '../src/utils'
const store = new Store()
const locales = {
'pt-br': {
test: 'teste',
phrase: 'Adoro banana',
phrases: ['Frase 1', 'Frase 2']
},
po: {
test: 'prøve',
phrase: 'Jeg elsker banan',
phrases: ['sætning 1', 'sætning 2']
}
}
i18n(store, locales)
/**
* Dummy test
*/
describe('Dummy test', () => {
it('works if true is truthy', () => {
expect(true).toBeTruthy()
})
it('DummyClass is instantiable', () => {
expect(new DummyClass()).toBeInstanceOf(DummyClass)
describe('utils', () => {
it('works', () => {
expect(getNestedProp(store['pt-br'], 'phrases[3]')).toBe(undefined)
})
})
describe('Dummy test', () => {
it('works if true is truthy', () => {
expect(store.get().locale).toBeFalsy()
expect(store.get()._).toBeFalsy()
store.fire('locale', 'en')
expect(store.get().locale).toBe('en')
store.locale('pt-br')
expect(store.get().locale).toBe('pt-br')
expect(store.get()._).toBeInstanceOf(Function)
expect(store.get()._('non-existent')).toBe('non-existent')
expect(store.get()._('test')).toBe(locales['pt-br'].test)
store.fire('locale', 'po')
expect(store.get()._('test')).not.toBe(locales['pt-br'].test)
expect(store.get()._('test')).toBe(locales.po.test)
expect(store.get()._('phrases[1]')).toBe(locales.po.phrases[1])
expect(store.get()._('phrases[2]')).toBe('phrases[2]')
expect(store.get()._.capitalize('phrase')).toBe(capitalize(locales.po.phrase))
expect(store.get()._.titlelize('phrase')).toBe(titlelize(locales.po.phrase))
expect(store.get()._.upper('phrase')).toBe(upper(locales.po.phrase))
expect(store.get()._.lower('phrase')).toBe(lower(locales.po.phrase))
})
})