mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-10-05 18:44:46 +02:00
refactor: 💡 split single utils file into multiple files
This commit is contained in:
parent
f50748b206
commit
f3b88f90ab
14
src/client/includes/flatObj.ts
Normal file
14
src/client/includes/flatObj.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// could use a reduce, but a simple for-in has less footprint
|
||||||
|
export const flatObj = (obj: Record<string, any>, prefix = '') => {
|
||||||
|
const flatted: Record<string, string> = {}
|
||||||
|
for (const key in obj) {
|
||||||
|
const flatKey = prefix + key
|
||||||
|
// we want plain objects and arrays
|
||||||
|
if (typeof obj[key] === 'object') {
|
||||||
|
Object.assign(flatted, flatObj(obj[key], `${flatKey}.`))
|
||||||
|
} else {
|
||||||
|
flatted[flatKey] = obj[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return flatted
|
||||||
|
}
|
59
src/client/includes/getClientLocale.ts
Normal file
59
src/client/includes/getClientLocale.ts
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
import { GetClientLocaleOptions } from '../types'
|
||||||
|
|
||||||
|
const getFromQueryString = (queryString: string, key: string) => {
|
||||||
|
const keyVal = queryString.split('&').find(i => i.indexOf(`${key}=`) === 0)
|
||||||
|
|
||||||
|
if (keyVal) {
|
||||||
|
return keyVal.split('=').pop()
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
const getFirstMatch = (base: string, pattern: RegExp) => {
|
||||||
|
const match = pattern.exec(base)
|
||||||
|
// istanbul ignore if
|
||||||
|
if (!match) return null
|
||||||
|
// istanbul ignore else
|
||||||
|
return match[1] || null
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getClientLocale = ({
|
||||||
|
navigator,
|
||||||
|
hash,
|
||||||
|
search,
|
||||||
|
pathname,
|
||||||
|
hostname,
|
||||||
|
}: GetClientLocaleOptions) => {
|
||||||
|
let locale
|
||||||
|
|
||||||
|
// istanbul ignore next
|
||||||
|
if (typeof window === 'undefined') return null
|
||||||
|
|
||||||
|
if (hostname) {
|
||||||
|
locale = getFirstMatch(window.location.hostname, hostname)
|
||||||
|
if (locale) return locale
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pathname) {
|
||||||
|
locale = getFirstMatch(window.location.pathname, pathname)
|
||||||
|
if (locale) return locale
|
||||||
|
}
|
||||||
|
|
||||||
|
if (navigator) {
|
||||||
|
// istanbul ignore else
|
||||||
|
locale = window.navigator.language || window.navigator.languages[0]
|
||||||
|
if (locale) return locale
|
||||||
|
}
|
||||||
|
|
||||||
|
if (search) {
|
||||||
|
locale = getFromQueryString(window.location.search.substr(1), search)
|
||||||
|
if (locale) return locale
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hash) {
|
||||||
|
locale = getFromQueryString(window.location.hash.substr(1), hash)
|
||||||
|
if (locale) return locale
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
import { getClientLocale } from './includes/utils'
|
import { getClientLocale } from './includes/getClientLocale'
|
||||||
import { ConfigureOptions } from './types'
|
import { ConfigureOptions } from './types'
|
||||||
import { $locale } from './stores/locale'
|
import { $locale } from './stores/locale'
|
||||||
|
|
||||||
|
@ -1,20 +1,5 @@
|
|||||||
import { GetClientLocaleOptions } from '../types'
|
import { GetClientLocaleOptions } from '../types'
|
||||||
|
|
||||||
// could use a reduce, but a simple for-in has less footprint
|
|
||||||
export const flatObj = (obj: Record<string, any>, prefix = '') => {
|
|
||||||
const flatted: Record<string, string> = {}
|
|
||||||
for (const key in obj) {
|
|
||||||
const flatKey = prefix + key
|
|
||||||
// we want plain objects and arrays
|
|
||||||
if (typeof obj[key] === 'object') {
|
|
||||||
Object.assign(flatted, flatObj(obj[key], `${flatKey}.`))
|
|
||||||
} else {
|
|
||||||
flatted[flatKey] = obj[key]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return flatted
|
|
||||||
}
|
|
||||||
|
|
||||||
const getFromQueryString = (queryString: string, key: string) => {
|
const getFromQueryString = (queryString: string, key: string) => {
|
||||||
const keyVal = queryString.split('&').find(i => i.indexOf(`${key}=`) === 0)
|
const keyVal = queryString.split('&').find(i => i.indexOf(`${key}=`) === 0)
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { writable, derived } from 'svelte/store'
|
import { writable, derived } from 'svelte/store'
|
||||||
|
|
||||||
import { LocaleDictionary, DeepDictionary, Dictionary } from '../types'
|
import { LocaleDictionary, DeepDictionary, Dictionary } from '../types/index'
|
||||||
import { flatObj } from '../includes/utils'
|
import { flatObj } from '../includes/flatObj'
|
||||||
|
|
||||||
import { getFallbackOf } from './locale'
|
import { getFallbackOf } from './locale'
|
||||||
|
|
||||||
|
@ -1,11 +1,5 @@
|
|||||||
import {
|
import { getClientLocale } from '../../../src/client/includes/getClientLocale'
|
||||||
getClientLocale,
|
import { flatObj } from '../../../src/client/includes/flatObj'
|
||||||
capital,
|
|
||||||
title,
|
|
||||||
upper,
|
|
||||||
lower,
|
|
||||||
flatObj,
|
|
||||||
} from '../../../src/runtime/includes/utils'
|
|
||||||
|
|
||||||
describe('getting client locale', () => {
|
describe('getting client locale', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@ -113,24 +107,6 @@ describe('getting client locale', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('string utilities', () => {
|
|
||||||
test('transforms a string into capital case', () => {
|
|
||||||
expect(capital('lowercase string')).toMatch('Lowercase string')
|
|
||||||
})
|
|
||||||
|
|
||||||
test('transforms a string into title case', () => {
|
|
||||||
expect(title('lowercase string')).toMatch('Lowercase String')
|
|
||||||
})
|
|
||||||
|
|
||||||
test('transforms a string into uppercase', () => {
|
|
||||||
expect(upper('lowercase string')).toMatch('LOWERCASE STRING')
|
|
||||||
})
|
|
||||||
|
|
||||||
test('transforms a string into lowercase', () => {
|
|
||||||
expect(lower('UPPERCASE STRING')).toMatch('uppercase string')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('deep object handling', () => {
|
describe('deep object handling', () => {
|
||||||
test('flattens a deep object', () => {
|
test('flattens a deep object', () => {
|
||||||
const obj = {
|
const obj = {
|
||||||
|
Loading…
Reference in New Issue
Block a user