Skip to content

Commit

Permalink
fix: replace lodash.merge with just-extend (#697)
Browse files Browse the repository at this point in the history
Co-authored-by: Ivan Hofer <ivan.hofer@outlook.com>
  • Loading branch information
osdiab and ivanhofer committed Jul 20, 2023
1 parent e40d345 commit c961aaa
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

# Version 5

## 5.25.1 (2023-07-20)

Fix:
- replace `lodash.merge` with `just-extend"`

## 5.25.0 (2023-07-18)

Feat:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ Your locale translation files can be any kind of JavaScript object. So you can m
export default en_US
```

> If you are using nested translations, you should use the provided `extendDictionary` function that uses [`lodash/merge`](https://lodash.com/docs/4.17.15#merge) under the hood.
> If you are using nested translations, you should use the provided `extendDictionary` function that uses [`just-extend`](https://github.com/angus-c/just#just-extend) under the hood.
> ```ts
> import { extendDictionary } from '../i18n-utils'
> import en from '../en' // import translations from 'en' locale
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ const en_US = extendDictionary(en, {

export default en_US
```
> uses [`lodash/merge`](https://lodash.com/docs/4.17.15#merge) under the hood
> uses [`just-extend`](https://github.com/angus-c/just#just-extend) under the hood
6 changes: 3 additions & 3 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"scripts": {
"dev": "tsx esbuild.ts --watch",
"build": "tsx esbuild.ts && tsc -p tsconfig.json --emitDeclarationOnly",
"test": "tsc --noEmit",
"test": "uvu -r @esbuild-kit/cjs-loader src && tsc --noEmit",
"test:watch": "watchlist src -- pnpm test"
},
"devDependencies": {
"@types/lodash.merge": "^4.6.7",
"esbuild": "^0.18.13",
"lodash.merge": "^4.6.2",
"just-extend": "^6.2.0",
"tsx": "^3.12.7",
"typescript": "^5.1.6",
"uvu": "^0.5.6",
"watchlist": "^0.3.1"
},
"type": "module"
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/extendDictionary.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import merge from 'lodash.merge'
import extend from 'just-extend'
import type { BaseTranslation } from '../../runtime/src/core.mjs'

type DeepPartial<T> = T extends BaseTranslation
Expand All @@ -21,6 +21,6 @@ export const initExtendDictionary =
base: Base,
part: DeepPartial<ToGenericString<Translation>>,
): Translation =>
merge({}, base, part) as Translation
extend({}, base, part) as Translation

export const extendDictionary = initExtendDictionary()
119 changes: 119 additions & 0 deletions packages/utils/src/extendDictionary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import type { BaseTranslation } from 'packages/runtime/src/core.mjs'
import { suite } from 'uvu'
import * as assert from 'uvu/assert'
import { extendDictionary } from './index.mjs'

const test = suite('utils')

const translation = {
simple: 'Hello',
nested: { value: 'Hello nested' },
}

test('does no mutation', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const base = {} as any
extendDictionary(base, { hey: 'there' })
assert.not(base.hey)
})

test('empty base', () => {
const extended = extendDictionary({}, { hey: 'there' })
assert.equal(extended, {
hey: 'there',
})
})

test('empty part', () => {
const extended = extendDictionary({ hey: 'there' }, {})
assert.equal(extended, {
hey: 'there',
})
})

test('simple extend', () => {
const extended = extendDictionary(translation, { simple: 'Hello extended' })
assert.equal(extended, {
simple: 'Hello extended',
nested: {
value: 'Hello nested',
},
})
})

test('nested extend', () => {
const extended = extendDictionary(translation, { nested: { value: 'Hello nested extended' } })
assert.equal(extended, {
simple: 'Hello',
nested: {
value: 'Hello nested extended',
},
})
})

test('nested extend with simple', () => {
const extended = extendDictionary(translation, {
simple: 'Hello extended',
nested: { value: 'Hello nested extended' },
})
assert.equal(extended, {
simple: 'Hello extended',
nested: {
value: 'Hello nested extended',
},
})
})

test('add prop', () => {
const extended = extendDictionary<BaseTranslation, BaseTranslation>(translation, {
add: 'test',
})

assert.equal(extended, {
simple: 'Hello',
add: 'test',
nested: {
value: 'Hello nested',
},
})
})

test('add nested prop', () => {
const extended = extendDictionary<BaseTranslation, BaseTranslation>(translation, {
add: {
nested: 'test',
},
})

assert.equal(extended, {
simple: 'Hello',
add: {
nested: 'test',
},
nested: {
value: 'Hello nested',
},
})
})

test('array', () => {
const extended = extendDictionary<BaseTranslation, BaseTranslation>(
{},
{
add: ['hey'],
},
)
assert.equal(extended, {
add: ['hey'],
})

const extended2 = extendDictionary<BaseTranslation, BaseTranslation>(extended, {
add: ['ho'],
})

assert.equal(extended2, {
add: ['ho'],
})
})

test.run()
2 changes: 1 addition & 1 deletion packages/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// this file gets auto-generated
export const version = '5.24.4'
export const version = '5.25.0'
26 changes: 10 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c961aaa

Please sign in to comment.