refactor: 💡 remove deepmerge and dlv dependencies

No need for them anymore. We now flat the dicitonary partials on
`addMessages`
This commit is contained in:
Christian Kaisermann 2020-01-15 11:50:08 -03:00
parent e889a41e63
commit 270aefa199
8 changed files with 5957 additions and 19 deletions

View File

@ -79,8 +79,6 @@
},
"dependencies": {
"commander": "^4.0.1",
"deepmerge": "^4.2.2",
"dlv": "^1.1.3",
"estree-walker": "^0.9.0",
"fast-memoize": "^2.5.1",
"intl-messageformat": "^7.5.2",

View File

@ -7,7 +7,6 @@ import {
Identifier,
Literal,
} from 'estree'
import delve from 'dlv'
import { walk } from 'estree-walker'
import { Ast } from 'svelte/types/compiler/interfaces'
import { parse } from 'svelte/compiler'
@ -21,6 +20,9 @@ const DEFINE_MESSAGES_METHOD_NAME = 'defineMessages'
const FORMAT_METHOD_NAMES = new Set(['format', '_', 't'])
const IGNORED_UTILITIES = new Set(['number', 'date', 'time'])
const delve = (o: Record<string, any>, id: string) =>
id.split('.').reduce((acc, path) => acc[path], o)
function isFormatCall(node: Node, imports: Set<string>) {
if (node.type !== 'CallExpression') return false

View File

@ -16,6 +16,21 @@ export function lower(str: string) {
return str.toLocaleLowerCase()
}
// 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 plainobjects
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)

View File

@ -1,8 +1,7 @@
import delve from 'dlv'
import merge from 'deepmerge'
import { writable, derived } from 'svelte/store'
import { Dictionary } from '../types/index'
import { LocaleDictionary, DeepDictionary, Dictionary } from '../types/index'
import { flatObj } from '../includes/utils'
import { getFallbackOf } from './locale'
@ -10,7 +9,7 @@ let dictionary: Dictionary
const $dictionary = writable<Dictionary>({})
export function getLocaleDictionary(locale: string) {
return (dictionary[locale] as Dictionary) || null
return (dictionary[locale] as LocaleDictionary) || null
}
export function getDictionary() {
@ -27,8 +26,6 @@ export function getMessageFromDictionary(locale: string, id: string) {
if (id in localeDictionary) {
return localeDictionary[id]
}
const message = delve(localeDictionary, id)
if (message) return message
}
return null
}
@ -38,11 +35,11 @@ export function getClosestAvailableLocale(locale: string): string | null {
return getClosestAvailableLocale(getFallbackOf(locale))
}
export function addMessages(locale: string, ...partials: Dictionary[]) {
export function addMessages(locale: string, ...partials: DeepDictionary[]) {
const flattedPartials = partials.map(partial => flatObj(partial))
$dictionary.update(d => {
dictionary[locale] = merge.all<Dictionary>(
[getLocaleDictionary(locale) || {}].concat(partials)
)
d[locale] = Object.assign(d[locale] || {}, ...flattedPartials)
return d
})
}

View File

@ -1,8 +1,10 @@
import { Formats } from 'intl-messageformat'
export interface Dictionary {
[key: string]: string | string[] | Dictionary | Dictionary[]
export interface DeepDictionary {
[key: string]: DeepDictionary | string | string[]
}
export type LocaleDictionary = Record<string, string>
export type Dictionary = Record<string, LocaleDictionary>
export interface MessageObject {
id?: string

View File

@ -1,2 +0,0 @@
declare module 'dlv'
declare module 'nano-memoize'

View File

@ -40,12 +40,14 @@ test('merges the existing dictionaries with new ones', () => {
en: {
field_1: 'name',
field_2: 'lastname',
deep: { prop1: 'foo', prop2: 'foo' },
'deep.prop1': 'foo',
'deep.prop2': 'foo',
},
pt: {
field_1: 'nome',
field_2: 'sobrenome',
deep: { prop1: 'foo', prop2: 'foo' },
'deep.prop1': 'foo',
'deep.prop2': 'foo',
},
})
})

5924
yarn.lock Normal file

File diff suppressed because it is too large Load Diff