mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-11-16 18:10:43 +01:00
refactor: 💡 split single utils file into multiple files
This commit is contained in:
parent
0e0a31fc9e
commit
0822841140
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 { $locale } from './stores/locale'
|
||||
|
||||
|
@ -1,20 +1,5 @@
|
||||
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 keyVal = queryString.split('&').find(i => i.indexOf(`${key}=`) === 0)
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { writable, derived } from 'svelte/store'
|
||||
|
||||
import { LocaleDictionary, DeepDictionary, Dictionary } from '../types'
|
||||
import { flatObj } from '../includes/utils'
|
||||
import { LocaleDictionary, DeepDictionary, Dictionary } from '../types/index'
|
||||
import { flatObj } from '../includes/flatObj'
|
||||
|
||||
import { getFallbackOf } from './locale'
|
||||
|
||||
|
@ -1,11 +1,5 @@
|
||||
import {
|
||||
getClientLocale,
|
||||
capital,
|
||||
title,
|
||||
upper,
|
||||
lower,
|
||||
flatObj,
|
||||
} from '../../../src/runtime/includes/utils'
|
||||
import { getClientLocale } from '../../../src/client/includes/getClientLocale'
|
||||
import { flatObj } from '../../../src/client/includes/flatObj'
|
||||
|
||||
describe('getting client locale', () => {
|
||||
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', () => {
|
||||
test('flattens a deep object', () => {
|
||||
const obj = {
|
||||
|
Loading…
Reference in New Issue
Block a user