Skip to content

Commit 1a5441d

Browse files
refactor: remove ramda
1 parent 7540adb commit 1a5441d

File tree

12 files changed

+142
-123
lines changed

12 files changed

+142
-123
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"@types/jest": "^29.4.0",
4949
"@types/mock-fs": "^4.13.1",
5050
"@types/node": "20.14.8",
51-
"@types/ramda": "^0.27.23",
5251
"babel-jest": "^29.7.0",
5352
"chalk": "^4.1.0",
5453
"cross-env": "^7.0.2",

packages/cli/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
"pkg-up": "^3.1.0",
8181
"pofile": "^1.1.4",
8282
"pseudolocale": "^2.0.0",
83-
"ramda": "^0.27.1",
8483
"source-map": "^0.8.0-beta.0"
8584
},
8685
"devDependencies": {

packages/cli/src/api/catalog.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ describe("order", () => {
528528
}),
529529
}
530530

531-
const orderedCatalogs = order("messageId")(catalog)
531+
const orderedCatalogs = order("messageId", catalog)
532532

533533
// Test that the message content is the same as before
534534
expect(orderedCatalogs).toMatchSnapshot()
@@ -560,7 +560,7 @@ describe("order", () => {
560560
}),
561561
}
562562

563-
const orderedCatalogs = order("origin")(catalog)
563+
const orderedCatalogs = order("origin", catalog)
564564

565565
// Test that the message content is the same as before
566566
expect(orderedCatalogs).toMatchSnapshot()
@@ -596,7 +596,7 @@ describe("order", () => {
596596
}),
597597
}
598598

599-
const orderedCatalogs = order("message")(catalog)
599+
const orderedCatalogs = order("message", catalog)
600600

601601
// Jest snapshot order the keys automatically, so test that the key order explicitly
602602
expect(Object.keys(orderedCatalogs)).toMatchInlineSnapshot(`

packages/cli/src/api/catalog.ts

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fs from "fs"
22
import path from "path"
3-
import * as R from "ramda"
43
import { globSync } from "glob"
54
import normalize from "normalize-path"
65

@@ -23,12 +22,7 @@ import {
2322
replacePlaceholders,
2423
writeFile,
2524
} from "./utils"
26-
import {
27-
AllCatalogsType,
28-
CatalogType,
29-
ExtractedCatalogType,
30-
ExtractedMessageType,
31-
} from "./types"
25+
import { AllCatalogsType, CatalogType, ExtractedCatalogType } from "./types"
3226

3327
const LOCALE = "{locale}"
3428
const LOCALE_SUFFIX_RE = /\{locale\}.*$/
@@ -95,14 +89,20 @@ export class Catalog {
9589
})
9690

9791
// Map over all locales and post-process each catalog
98-
const cleanAndSort = R.map(
99-
R.pipe(
100-
// Clean obsolete messages
101-
(options.clean ? cleanObsolete : R.identity) as any,
102-
// Sort messages
103-
order(options.orderBy)
92+
const cleanAndSort = (catalogs: AllCatalogsType): AllCatalogsType =>
93+
Object.fromEntries(
94+
Object.entries(catalogs).map(([locale, catalog]) => {
95+
let processedCatalog = catalog
96+
97+
if (options.clean) {
98+
processedCatalog = cleanObsolete(processedCatalog)
99+
}
100+
101+
processedCatalog = order(options.orderBy, processedCatalog)
102+
103+
return [locale, processedCatalog]
104+
})
104105
)
105-
) as unknown as (catalog: AllCatalogsType) => AllCatalogsType
106106

107107
const sortedCatalogs = cleanAndSort(catalogs)
108108

@@ -119,7 +119,7 @@ export class Catalog {
119119
): Promise<CatalogType | false> {
120120
const catalog = await this.collect({ files: options.files })
121121
if (!catalog) return false
122-
const sorted = order<CatalogType>(options.orderBy)(catalog as CatalogType)
122+
const sorted = order(options.orderBy, catalog as CatalogType)
123123

124124
await this.writeTemplate(sorted)
125125
return sorted
@@ -169,14 +169,18 @@ export class Catalog {
169169
nextCatalog: ExtractedCatalogType,
170170
options: MergeOptions
171171
) {
172-
return R.mapObjIndexed((prevCatalog, locale) => {
173-
return mergeCatalog(
172+
const result: AllCatalogsType = {}
173+
174+
for (const [locale, prevCatalog] of Object.entries(prevCatalogs)) {
175+
result[locale] = mergeCatalog(
174176
prevCatalog,
175177
nextCatalog,
176178
this.config.sourceLocale === locale,
177179
options
178180
)
179-
}, prevCatalogs)
181+
}
182+
183+
return result
180184
}
181185

182186
async getTranslations(locale: string, options: GetTranslationsOptions) {
@@ -287,20 +291,22 @@ function getTemplatePath(ext: string, path: string) {
287291
return path.replace(LOCALE_SUFFIX_RE, "messages" + ext)
288292
}
289293

290-
export const cleanObsolete = R.filter(
291-
(message: ExtractedMessageType) => !message.obsolete
292-
)
294+
export function cleanObsolete<T extends ExtractedCatalogType>(messages: T): T {
295+
return Object.fromEntries(
296+
Object.entries(messages).filter(([, message]) => !message.obsolete)
297+
) as T
298+
}
293299

294300
export function order<T extends ExtractedCatalogType>(
295-
by: OrderBy
296-
): (catalog: T) => T {
301+
by: OrderBy,
302+
catalog: T
303+
): T {
297304
return {
298305
messageId: orderByMessageId,
299306
message: orderByMessage,
300307
origin: orderByOrigin,
301-
}[by]
308+
}[by](catalog)
302309
}
303-
304310
/**
305311
* Object keys are in the same order as they were created
306312
* https://stackoverflow.com/a/31102605/1535540
Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import * as R from "ramda"
21
import type { MergeOptions } from "../catalog"
3-
import { CatalogType, ExtractedCatalogType, MessageType } from "../types"
2+
import { CatalogType, ExtractedCatalogType } from "../types"
43

54
export function mergeCatalog(
65
prevCatalog: CatalogType,
@@ -9,49 +8,52 @@ export function mergeCatalog(
98
options: MergeOptions
109
): CatalogType {
1110
const nextKeys = Object.keys(nextCatalog)
11+
const prevKeys = Object.keys(prevCatalog || {})
1212

13-
const prevKeys = R.keys(prevCatalog).map(String)
14-
15-
const newKeys = R.difference(nextKeys, prevKeys)
16-
const mergeKeys = R.intersection(nextKeys, prevKeys)
17-
const obsoleteKeys = R.difference(prevKeys, nextKeys)
13+
const newKeys = nextKeys.filter((key) => !prevKeys.includes(key))
14+
const mergeKeys = nextKeys.filter((key) => prevKeys.includes(key))
15+
const obsoleteKeys = prevKeys.filter((key) => !nextKeys.includes(key))
1816

1917
// Initialize new catalog with new keys
20-
const newMessages = R.mapObjIndexed(
21-
(message: MessageType, key) => ({
22-
translation: forSourceLocale ? message.message || key : "",
23-
...message,
24-
}),
25-
R.pick(newKeys, nextCatalog)
18+
const newMessages: CatalogType = Object.fromEntries(
19+
newKeys.map((key) => [
20+
key,
21+
{
22+
translation: forSourceLocale ? nextCatalog[key].message || key : "",
23+
...nextCatalog[key],
24+
},
25+
])
2626
)
2727

2828
// Merge translations from previous catalog
29-
const mergedMessages = mergeKeys.map((key) => {
30-
const updateFromDefaults =
31-
forSourceLocale &&
32-
(prevCatalog[key].translation === prevCatalog[key].message ||
33-
options.overwrite)
34-
35-
const translation = updateFromDefaults
36-
? nextCatalog[key].message || key
37-
: prevCatalog[key].translation
38-
39-
return {
40-
[key]: {
41-
translation,
42-
...R.omit(["obsolete, translation"], nextCatalog[key]),
43-
},
44-
}
45-
})
29+
const mergedMessages = Object.fromEntries(
30+
mergeKeys.map((key) => {
31+
const updateFromDefaults =
32+
forSourceLocale &&
33+
(prevCatalog[key].translation === prevCatalog[key].message ||
34+
options.overwrite)
35+
36+
const translation = updateFromDefaults
37+
? nextCatalog[key].message || key
38+
: prevCatalog[key].translation
39+
40+
const { obsolete, ...rest } = nextCatalog[key]
41+
42+
return [key, { ...rest, translation }]
43+
})
44+
)
4645

4746
// Mark all remaining translations as obsolete
4847
// Only if *options.files* is not provided
49-
const obsoleteMessages = obsoleteKeys.map((key) => ({
50-
[key]: {
51-
...prevCatalog[key],
52-
...(!options.files && { obsolete: true }),
53-
},
54-
}))
55-
56-
return R.mergeAll([newMessages, ...mergedMessages, ...obsoleteMessages])
48+
const obsoleteMessages = Object.fromEntries(
49+
obsoleteKeys.map((key) => [
50+
key,
51+
{
52+
...prevCatalog[key],
53+
...(options.files ? {} : { obsolete: true }),
54+
},
55+
])
56+
)
57+
58+
return { ...newMessages, ...mergedMessages, ...obsoleteMessages }
5759
}

packages/cli/src/api/pseudoLocalize.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import R from "ramda"
21
import pseudolocale from "pseudolocale"
32

43
const delimiter = "%&&&%"
@@ -45,18 +44,14 @@ function addDelimitersVariables(message: string) {
4544
})
4645
}
4746

48-
const addDelimiters = R.compose(
49-
addDelimitersVariables,
50-
addDelimitersMacro,
51-
addDelimitersHTMLTags
52-
)
53-
5447
function removeDelimiters(message: string) {
5548
return message.replace(new RegExp(delimiter, "g"), "")
5649
}
5750

5851
export default function (message: string) {
59-
message = addDelimiters(message)
52+
message = addDelimitersHTMLTags(message)
53+
message = addDelimitersMacro(message)
54+
message = addDelimitersVariables(message)
6055
message = pseudolocale(message, {
6156
delimiter,
6257
prepend: "",

packages/cli/src/extract-experimental/writeCatalogs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function cleanAndSort(catalog: CatalogType, clean: boolean, orderBy: OrderBy) {
3030
catalog = cleanObsolete(catalog)
3131
}
3232

33-
return order(orderBy)(catalog) as CatalogType
33+
return order(orderBy, catalog) as CatalogType
3434
}
3535

3636
export async function writeCatalogs(

packages/format-json/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
"dist/"
4040
],
4141
"dependencies": {
42-
"@lingui/conf": "5.1.2",
43-
"ramda": "^0.28.0"
42+
"@lingui/conf": "5.1.2"
4443
},
4544
"devDependencies": {
4645
"tsd": "^0.28.0",

packages/format-json/src/json.ts

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import * as R from "ramda"
2-
31
import {
42
CatalogFormatter,
53
CatalogType,
64
ExtractedMessageType,
7-
MessageType,
85
} from "@lingui/conf"
96

107
export type JsonFormatterOptions = {
@@ -43,27 +40,50 @@ type NoOriginsCatalogType = {
4340

4441
type MinimalCatalogType = Record<string, string>
4542

46-
const serializeMinimal = R.map(
47-
(message: MessageType) => message.translation || ""
48-
) as unknown as (catalog: CatalogType) => MinimalCatalogType
43+
const serializeMinimal = (catalog: CatalogType): MinimalCatalogType => {
44+
const result: MinimalCatalogType = {}
45+
for (const key in catalog) {
46+
result[key] = catalog[key].translation || ""
47+
}
48+
return result
49+
}
4950

50-
const deserializeMinimal = R.map((translation: string) => ({
51-
translation,
52-
obsolete: false,
53-
message: null,
54-
origin: [],
55-
})) as unknown as (minimalCatalog: MinimalCatalogType) => CatalogType
51+
const deserializeMinimal = (
52+
minimalCatalog: MinimalCatalogType
53+
): CatalogType => {
54+
const result: CatalogType = {}
55+
for (const key in minimalCatalog) {
56+
result[key] = {
57+
translation: minimalCatalog[key],
58+
obsolete: false,
59+
message: null,
60+
origin: [],
61+
}
62+
}
63+
return result
64+
}
5665

57-
const removeOrigins = R.map(({ origin, ...message }) => message) as unknown as (
58-
catalog: CatalogType
59-
) => NoOriginsCatalogType
66+
const removeOrigins = (catalog: CatalogType): NoOriginsCatalogType => {
67+
const result: NoOriginsCatalogType = {}
68+
for (const key in catalog) {
69+
const { origin, ...message } = catalog[key]
70+
result[key] = message
71+
}
72+
return result
73+
}
6074

61-
const removeLineNumbers = R.map((message: ExtractedMessageType) => {
62-
if (message.origin) {
63-
message.origin = message.origin.map(([file]) => [file])
75+
const removeLineNumbers = (
76+
catalog: ExtractedMessageType
77+
): NoOriginsCatalogType => {
78+
const result: NoOriginsCatalogType = {}
79+
for (const key in catalog) {
80+
result[key] = {
81+
...catalog[key],
82+
origin: catalog[key].origin?.map(([file]) => [file]),
83+
}
6484
}
65-
return message
66-
}) as unknown as (catalog: ExtractedMessageType) => NoOriginsCatalogType
85+
return result
86+
}
6787

6888
export function formatter(
6989
options: JsonFormatterOptions = {}

packages/remote-loader/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@
5050
],
5151
"dependencies": {
5252
"@lingui/core": "4.0.0",
53-
"@lingui/message-utils": "4.0.0",
54-
"ramda": "^0.27.1"
53+
"@lingui/message-utils": "4.0.0"
5554
},
5655
"devDependencies": {
5756
"unbuild": "2.0.0"

0 commit comments

Comments
 (0)