test: 💍 add flatObj and getIn tests

This commit is contained in:
Christian Kaisermann 2020-01-15 13:47:02 -03:00
parent 270aefa199
commit e02427de3b
7 changed files with 55 additions and 10660 deletions

2189
example/package-lock.json generated

File diff suppressed because it is too large Load Diff

8466
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -11,6 +11,7 @@ import { walk } from 'estree-walker'
import { Ast } from 'svelte/types/compiler/interfaces'
import { parse } from 'svelte/compiler'
import { getIn } from './includes/getIn'
import { deepSet } from './includes/deepSet'
import { getObjFromExpression } from './includes/getObjFromExpression'
import { Message } from './types'
@ -20,9 +21,6 @@ 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
@ -175,7 +173,7 @@ export function extractMessages(
} else {
if (
overwrite === false &&
typeof delve(accumulator, message.meta.id) !== 'undefined'
typeof getIn(accumulator, message.meta.id) !== 'undefined'
) {
return
}

View File

@ -0,0 +1,8 @@
export const getIn = (o: Record<string, any>, id: string) => {
return id.split('.').reduce((acc, path) => {
if (typeof acc !== 'object') {
return acc
}
return acc[path]
}, o)
}

View File

@ -21,7 +21,7 @@ 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
// we want plain objects and arrays
if (typeof obj[key] === 'object') {
Object.assign(flatted, flatObj(obj[key], `${flatKey}.`))
} else {

17
test/cli/includes.test.ts Normal file
View File

@ -0,0 +1,17 @@
import { getIn } from '../../src/cli/includes/getIn'
describe('deep object handling', () => {
test('gets a deep property', () => {
const obj = {
a: { b: { c: { d: ['foo', 'bar'] } } },
}
expect(getIn(obj, 'a.b.c.d.1')).toBe('bar')
})
test('returns undefined for if some property not found', () => {
const obj = {
a: { b: 1 },
}
expect(getIn(obj, 'c.b')).toBe(undefined)
})
})

View File

@ -4,6 +4,7 @@ import {
title,
upper,
lower,
flatObj,
} from '../../../src/client/includes/utils'
describe('getting client locale', () => {
@ -129,3 +130,29 @@ describe('string utilities', () => {
expect(lower('UPPERCASE STRING')).toMatch('uppercase string')
})
})
describe('deep object handling', () => {
test('flattens a deep object', () => {
const obj = {
a: { b: { c: { d: 'foo' } } },
e: { f: 'bar' },
}
expect(flatObj(obj)).toMatchObject({
'a.b.c.d': 'foo',
'e.f': 'bar',
})
})
test('flattens a deep object with array values', () => {
const obj = {
a: { b: { c: { d: ['foo', 'bar'] } } },
e: { f: ['foo', 'bar'] },
}
expect(flatObj(obj)).toMatchObject({
'a.b.c.d.0': 'foo',
'a.b.c.d.1': 'bar',
'e.f.0': 'foo',
'e.f.1': 'bar',
})
})
})