Skip to content

Commit c961aaa

Browse files
osdiabivanhofer
andauthored
fix: replace lodash.merge with just-extend (#697)
Co-authored-by: Ivan Hofer <ivan.hofer@outlook.com>
1 parent e40d345 commit c961aaa

File tree

8 files changed

+142
-24
lines changed

8 files changed

+142
-24
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
# Version 5
44

5+
## 5.25.1 (2023-07-20)
6+
7+
Fix:
8+
- replace `lodash.merge` with `just-extend"`
9+
510
## 5.25.0 (2023-07-18)
611

712
Feat:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ Your locale translation files can be any kind of JavaScript object. So you can m
490490
export default en_US
491491
```
492492

493-
> 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.
493+
> 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.
494494
> ```ts
495495
> import { extendDictionary } from '../i18n-utils'
496496
> import en from '../en' // import translations from 'en' locale

packages/utils/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ const en_US = extendDictionary(en, {
2828

2929
export default en_US
3030
```
31-
> uses [`lodash/merge`](https://lodash.com/docs/4.17.15#merge) under the hood
31+
> uses [`just-extend`](https://github.com/angus-c/just#just-extend) under the hood

packages/utils/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
"scripts": {
44
"dev": "tsx esbuild.ts --watch",
55
"build": "tsx esbuild.ts && tsc -p tsconfig.json --emitDeclarationOnly",
6-
"test": "tsc --noEmit",
6+
"test": "uvu -r @esbuild-kit/cjs-loader src && tsc --noEmit",
77
"test:watch": "watchlist src -- pnpm test"
88
},
99
"devDependencies": {
10-
"@types/lodash.merge": "^4.6.7",
1110
"esbuild": "^0.18.13",
12-
"lodash.merge": "^4.6.2",
11+
"just-extend": "^6.2.0",
1312
"tsx": "^3.12.7",
1413
"typescript": "^5.1.6",
14+
"uvu": "^0.5.6",
1515
"watchlist": "^0.3.1"
1616
},
1717
"type": "module"

packages/utils/src/extendDictionary.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import merge from 'lodash.merge'
1+
import extend from 'just-extend'
22
import type { BaseTranslation } from '../../runtime/src/core.mjs'
33

44
type DeepPartial<T> = T extends BaseTranslation
@@ -21,6 +21,6 @@ export const initExtendDictionary =
2121
base: Base,
2222
part: DeepPartial<ToGenericString<Translation>>,
2323
): Translation =>
24-
merge({}, base, part) as Translation
24+
extend({}, base, part) as Translation
2525

2626
export const extendDictionary = initExtendDictionary()
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import type { BaseTranslation } from 'packages/runtime/src/core.mjs'
2+
import { suite } from 'uvu'
3+
import * as assert from 'uvu/assert'
4+
import { extendDictionary } from './index.mjs'
5+
6+
const test = suite('utils')
7+
8+
const translation = {
9+
simple: 'Hello',
10+
nested: { value: 'Hello nested' },
11+
}
12+
13+
test('does no mutation', () => {
14+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
15+
const base = {} as any
16+
extendDictionary(base, { hey: 'there' })
17+
assert.not(base.hey)
18+
})
19+
20+
test('empty base', () => {
21+
const extended = extendDictionary({}, { hey: 'there' })
22+
assert.equal(extended, {
23+
hey: 'there',
24+
})
25+
})
26+
27+
test('empty part', () => {
28+
const extended = extendDictionary({ hey: 'there' }, {})
29+
assert.equal(extended, {
30+
hey: 'there',
31+
})
32+
})
33+
34+
test('simple extend', () => {
35+
const extended = extendDictionary(translation, { simple: 'Hello extended' })
36+
assert.equal(extended, {
37+
simple: 'Hello extended',
38+
nested: {
39+
value: 'Hello nested',
40+
},
41+
})
42+
})
43+
44+
test('nested extend', () => {
45+
const extended = extendDictionary(translation, { nested: { value: 'Hello nested extended' } })
46+
assert.equal(extended, {
47+
simple: 'Hello',
48+
nested: {
49+
value: 'Hello nested extended',
50+
},
51+
})
52+
})
53+
54+
test('nested extend with simple', () => {
55+
const extended = extendDictionary(translation, {
56+
simple: 'Hello extended',
57+
nested: { value: 'Hello nested extended' },
58+
})
59+
assert.equal(extended, {
60+
simple: 'Hello extended',
61+
nested: {
62+
value: 'Hello nested extended',
63+
},
64+
})
65+
})
66+
67+
test('add prop', () => {
68+
const extended = extendDictionary<BaseTranslation, BaseTranslation>(translation, {
69+
add: 'test',
70+
})
71+
72+
assert.equal(extended, {
73+
simple: 'Hello',
74+
add: 'test',
75+
nested: {
76+
value: 'Hello nested',
77+
},
78+
})
79+
})
80+
81+
test('add nested prop', () => {
82+
const extended = extendDictionary<BaseTranslation, BaseTranslation>(translation, {
83+
add: {
84+
nested: 'test',
85+
},
86+
})
87+
88+
assert.equal(extended, {
89+
simple: 'Hello',
90+
add: {
91+
nested: 'test',
92+
},
93+
nested: {
94+
value: 'Hello nested',
95+
},
96+
})
97+
})
98+
99+
test('array', () => {
100+
const extended = extendDictionary<BaseTranslation, BaseTranslation>(
101+
{},
102+
{
103+
add: ['hey'],
104+
},
105+
)
106+
assert.equal(extended, {
107+
add: ['hey'],
108+
})
109+
110+
const extended2 = extendDictionary<BaseTranslation, BaseTranslation>(extended, {
111+
add: ['ho'],
112+
})
113+
114+
assert.equal(extended2, {
115+
add: ['ho'],
116+
})
117+
})
118+
119+
test.run()

packages/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// this file gets auto-generated
2-
export const version = '5.24.4'
2+
export const version = '5.25.0'

pnpm-lock.yaml

Lines changed: 10 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)