Skip to content

Commit

Permalink
refactor: 💡 remove deepmerge and dlv dependencies
Browse files Browse the repository at this point in the history
No need for them anymore. We now flat the dicitonary partials on
`addMessages`
  • Loading branch information
kaisermann committed Jan 15, 2020
1 parent e889a41 commit 270aefa
Show file tree
Hide file tree
Showing 8 changed files with 5,957 additions and 19 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion src/cli/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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

Expand Down
15 changes: 15 additions & 0 deletions src/client/includes/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 7 additions & 10 deletions src/client/stores/dictionary.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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'

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() {
Expand All @@ -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
}
Expand All @@ -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
})
}
Expand Down
6 changes: 4 additions & 2 deletions src/client/types/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/client/types/modules.d.ts

This file was deleted.

6 changes: 4 additions & 2 deletions test/client/stores/dictionary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
})
})
Expand Down
Loading

0 comments on commit 270aefa

Please sign in to comment.