Skip to content

Commit

Permalink
refactor: remove ramda
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko committed Jan 16, 2025
1 parent 7540adb commit 4b4fb8f
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 130 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"@types/jest": "^29.4.0",
"@types/mock-fs": "^4.13.1",
"@types/node": "20.14.8",
"@types/ramda": "^0.27.23",
"babel-jest": "^29.7.0",
"chalk": "^4.1.0",
"cross-env": "^7.0.2",
Expand Down
1 change: 0 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
"pkg-up": "^3.1.0",
"pofile": "^1.1.4",
"pseudolocale": "^2.0.0",
"ramda": "^0.27.1",
"source-map": "^0.8.0-beta.0"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/api/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ describe("order", () => {
}),
}

const orderedCatalogs = order("messageId")(catalog)
const orderedCatalogs = order("messageId", catalog)

// Test that the message content is the same as before
expect(orderedCatalogs).toMatchSnapshot()
Expand Down Expand Up @@ -560,7 +560,7 @@ describe("order", () => {
}),
}

const orderedCatalogs = order("origin")(catalog)
const orderedCatalogs = order("origin", catalog)

// Test that the message content is the same as before
expect(orderedCatalogs).toMatchSnapshot()
Expand Down Expand Up @@ -596,7 +596,7 @@ describe("order", () => {
}),
}

const orderedCatalogs = order("message")(catalog)
const orderedCatalogs = order("message", catalog)

// Jest snapshot order the keys automatically, so test that the key order explicitly
expect(Object.keys(orderedCatalogs)).toMatchInlineSnapshot(`
Expand Down
64 changes: 32 additions & 32 deletions packages/cli/src/api/catalog.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from "fs"
import path from "path"
import * as R from "ramda"
import { globSync } from "glob"
import normalize from "normalize-path"

Expand All @@ -23,12 +22,7 @@ import {
replacePlaceholders,
writeFile,
} from "./utils"
import {
AllCatalogsType,
CatalogType,
ExtractedCatalogType,
ExtractedMessageType,
} from "./types"
import { AllCatalogsType, CatalogType, ExtractedCatalogType } from "./types"

const LOCALE = "{locale}"
const LOCALE_SUFFIX_RE = /\{locale\}.*$/
Expand Down Expand Up @@ -95,16 +89,17 @@ export class Catalog {
})

// Map over all locales and post-process each catalog
const cleanAndSort = R.map(
R.pipe(
// Clean obsolete messages
(options.clean ? cleanObsolete : R.identity) as any,
// Sort messages
order(options.orderBy)
)
) as unknown as (catalog: AllCatalogsType) => AllCatalogsType
const sortedCatalogs = Object.fromEntries(
Object.entries(catalogs).map(([locale, catalog]) => {
if (options.clean) {
catalog = cleanObsolete(catalog)
}

const sortedCatalogs = cleanAndSort(catalogs)
catalog = order(options.orderBy, catalog)

return [locale, catalog]
})
) as AllCatalogsType

const locales = options.locale ? options.locale : this.locales
await Promise.all(
Expand All @@ -119,7 +114,7 @@ export class Catalog {
): Promise<CatalogType | false> {
const catalog = await this.collect({ files: options.files })
if (!catalog) return false
const sorted = order<CatalogType>(options.orderBy)(catalog as CatalogType)
const sorted = order(options.orderBy, catalog as CatalogType)

await this.writeTemplate(sorted)
return sorted
Expand Down Expand Up @@ -169,14 +164,17 @@ export class Catalog {
nextCatalog: ExtractedCatalogType,
options: MergeOptions
) {
return R.mapObjIndexed((prevCatalog, locale) => {
return mergeCatalog(
prevCatalog,
nextCatalog,
this.config.sourceLocale === locale,
options
)
}, prevCatalogs)
return Object.fromEntries(
Object.entries(prevCatalogs).map(([locale, prevCatalog]) => [
locale,
mergeCatalog(
prevCatalog,
nextCatalog,
this.config.sourceLocale === locale,
options
),
])
)
}

async getTranslations(locale: string, options: GetTranslationsOptions) {
Expand Down Expand Up @@ -287,20 +285,22 @@ function getTemplatePath(ext: string, path: string) {
return path.replace(LOCALE_SUFFIX_RE, "messages" + ext)
}

export const cleanObsolete = R.filter(
(message: ExtractedMessageType) => !message.obsolete
)
export function cleanObsolete<T extends ExtractedCatalogType>(messages: T): T {
return Object.fromEntries(
Object.entries(messages).filter(([, message]) => !message.obsolete)
) as T
}

export function order<T extends ExtractedCatalogType>(
by: OrderBy
): (catalog: T) => T {
by: OrderBy,
catalog: T
): T {
return {
messageId: orderByMessageId,
message: orderByMessage,
origin: orderByOrigin,
}[by]
}[by](catalog)
}

/**
* Object keys are in the same order as they were created
* https://stackoverflow.com/a/31102605/1535540
Expand Down
78 changes: 40 additions & 38 deletions packages/cli/src/api/catalog/mergeCatalog.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as R from "ramda"
import type { MergeOptions } from "../catalog"
import { CatalogType, ExtractedCatalogType, MessageType } from "../types"
import { CatalogType, ExtractedCatalogType } from "../types"

export function mergeCatalog(
prevCatalog: CatalogType,
Expand All @@ -9,49 +8,52 @@ export function mergeCatalog(
options: MergeOptions
): CatalogType {
const nextKeys = Object.keys(nextCatalog)
const prevKeys = Object.keys(prevCatalog || {})

const prevKeys = R.keys(prevCatalog).map(String)

const newKeys = R.difference(nextKeys, prevKeys)
const mergeKeys = R.intersection(nextKeys, prevKeys)
const obsoleteKeys = R.difference(prevKeys, nextKeys)
const newKeys = nextKeys.filter((key) => !prevKeys.includes(key))
const mergeKeys = nextKeys.filter((key) => prevKeys.includes(key))
const obsoleteKeys = prevKeys.filter((key) => !nextKeys.includes(key))

// Initialize new catalog with new keys
const newMessages = R.mapObjIndexed(
(message: MessageType, key) => ({
translation: forSourceLocale ? message.message || key : "",
...message,
}),
R.pick(newKeys, nextCatalog)
const newMessages: CatalogType = Object.fromEntries(
newKeys.map((key) => [
key,
{
translation: forSourceLocale ? nextCatalog[key].message || key : "",
...nextCatalog[key],
},
])
)

// Merge translations from previous catalog
const mergedMessages = mergeKeys.map((key) => {
const updateFromDefaults =
forSourceLocale &&
(prevCatalog[key].translation === prevCatalog[key].message ||
options.overwrite)

const translation = updateFromDefaults
? nextCatalog[key].message || key
: prevCatalog[key].translation

return {
[key]: {
translation,
...R.omit(["obsolete, translation"], nextCatalog[key]),
},
}
})
const mergedMessages = Object.fromEntries(
mergeKeys.map((key) => {
const updateFromDefaults =
forSourceLocale &&
(prevCatalog[key].translation === prevCatalog[key].message ||
options.overwrite)

const translation = updateFromDefaults
? nextCatalog[key].message || key
: prevCatalog[key].translation

const { obsolete, ...rest } = nextCatalog[key]

return [key, { ...rest, translation }]
})
)

// Mark all remaining translations as obsolete
// Only if *options.files* is not provided
const obsoleteMessages = obsoleteKeys.map((key) => ({
[key]: {
...prevCatalog[key],
...(!options.files && { obsolete: true }),
},
}))

return R.mergeAll([newMessages, ...mergedMessages, ...obsoleteMessages])
const obsoleteMessages = Object.fromEntries(
obsoleteKeys.map((key) => [
key,
{
...prevCatalog[key],
...(options.files ? {} : { obsolete: true }),
},
])
)

return { ...newMessages, ...mergedMessages, ...obsoleteMessages }
}
11 changes: 3 additions & 8 deletions packages/cli/src/api/pseudoLocalize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import R from "ramda"
import pseudolocale from "pseudolocale"

const delimiter = "%&&&%"
Expand Down Expand Up @@ -45,18 +44,14 @@ function addDelimitersVariables(message: string) {
})
}

const addDelimiters = R.compose(
addDelimitersVariables,
addDelimitersMacro,
addDelimitersHTMLTags
)

function removeDelimiters(message: string) {
return message.replace(new RegExp(delimiter, "g"), "")
}

export default function (message: string) {
message = addDelimiters(message)
message = addDelimitersHTMLTags(message)
message = addDelimitersMacro(message)
message = addDelimitersVariables(message)
message = pseudolocale(message, {
delimiter,
prepend: "",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/extract-experimental/writeCatalogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function cleanAndSort(catalog: CatalogType, clean: boolean, orderBy: OrderBy) {
catalog = cleanObsolete(catalog)
}

return order(orderBy)(catalog) as CatalogType
return order(orderBy, catalog) as CatalogType
}

export async function writeCatalogs(
Expand Down
3 changes: 1 addition & 2 deletions packages/format-json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
"dist/"
],
"dependencies": {
"@lingui/conf": "5.1.2",
"ramda": "^0.28.0"
"@lingui/conf": "5.1.2"
},
"devDependencies": {
"tsd": "^0.28.0",
Expand Down
60 changes: 40 additions & 20 deletions packages/format-json/src/json.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import * as R from "ramda"

import {
CatalogFormatter,
CatalogType,
ExtractedMessageType,
MessageType,
} from "@lingui/conf"

export type JsonFormatterOptions = {
Expand Down Expand Up @@ -43,27 +40,50 @@ type NoOriginsCatalogType = {

type MinimalCatalogType = Record<string, string>

const serializeMinimal = R.map(
(message: MessageType) => message.translation || ""
) as unknown as (catalog: CatalogType) => MinimalCatalogType
const serializeMinimal = (catalog: CatalogType): MinimalCatalogType => {
const result: MinimalCatalogType = {}
for (const key in catalog) {
result[key] = catalog[key].translation || ""
}
return result
}

const deserializeMinimal = R.map((translation: string) => ({
translation,
obsolete: false,
message: null,
origin: [],
})) as unknown as (minimalCatalog: MinimalCatalogType) => CatalogType
const deserializeMinimal = (
minimalCatalog: MinimalCatalogType
): CatalogType => {
const result: CatalogType = {}
for (const key in minimalCatalog) {
result[key] = {
translation: minimalCatalog[key],
obsolete: false,
message: null,
origin: [],
}
}
return result
}

const removeOrigins = R.map(({ origin, ...message }) => message) as unknown as (
catalog: CatalogType
) => NoOriginsCatalogType
const removeOrigins = (catalog: CatalogType): NoOriginsCatalogType => {
const result: NoOriginsCatalogType = {}
for (const key in catalog) {
const { origin, ...message } = catalog[key]
result[key] = message
}
return result
}

const removeLineNumbers = R.map((message: ExtractedMessageType) => {
if (message.origin) {
message.origin = message.origin.map(([file]) => [file])
const removeLineNumbers = (
catalog: ExtractedMessageType
): NoOriginsCatalogType => {
const result: NoOriginsCatalogType = {}
for (const key in catalog) {
result[key] = {
...catalog[key],
origin: catalog[key].origin?.map(([file]) => [file]),
}
}
return message
}) as unknown as (catalog: ExtractedMessageType) => NoOriginsCatalogType
return result
}

export function formatter(
options: JsonFormatterOptions = {}
Expand Down
3 changes: 1 addition & 2 deletions packages/remote-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@
],
"dependencies": {
"@lingui/core": "4.0.0",
"@lingui/message-utils": "4.0.0",
"ramda": "^0.27.1"
"@lingui/message-utils": "4.0.0"
},
"devDependencies": {
"unbuild": "2.0.0"
Expand Down
Loading

0 comments on commit 4b4fb8f

Please sign in to comment.