Skip to content

Commit

Permalink
refactor: replace lodash with radashi
Browse files Browse the repository at this point in the history
Closes #692
  • Loading branch information
Harjot1Singh committed Nov 5, 2024
1 parent f7fa202 commit 11c3692
Show file tree
Hide file tree
Showing 24 changed files with 44 additions and 71 deletions.
2 changes: 0 additions & 2 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"gurmukhi-utils": "^3.2.1",
"helmet": "^7.1.0",
"import-fresh": "^3.3.0",
"lodash-es": "^4.17.21",
"memoizee": "^0.4.15",
"node-fetch": "^3.3.2",
"open": "^8.4.0",
Expand All @@ -44,7 +43,6 @@
"@types/compression": "^1.7.2",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.12.12",
"@types/pacote": "^11.1.8",
"@types/primus": "^7.3.6",
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/features/content/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import http from 'node:http'

import { Language, RecommendedSources, Writer } from '@presenter/contract'
import { first, last } from '@presenter/swiss-knife'
import express from 'express'
import { first, last } from 'radashi'
import { describe, expect, it } from 'vitest'

import { fetchApi } from '~/../test/utils/fetch'
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/features/content/state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Content, Line, ServerEventParameters } from '@presenter/contract'
import { getLogger, mutableValue, readOnly, subscribable } from '@presenter/node'
import { first, last } from '@presenter/swiss-knife'
import { first, last } from 'radashi'

import { getBaniLines, getShabad, getShabadByOrderId } from '~/services/database'

Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/features/history/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Line, ViewedLines } from '@presenter/contract'
import { HISTORY_FILE, mutableValue, readOnly, subscribable } from '@presenter/node'
import * as CSV from 'csv-string'
import { createWriteStream, WriteStream } from 'fs'
import { omit } from 'lodash-es'
import { omit } from 'radashi'

// Fields to store in the history CSV file
const CSV_FIELDS = [
Expand Down
8 changes: 4 additions & 4 deletions apps/backend/src/features/settings/state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ManyClientSettings, Settings } from '@presenter/contract'
import { mutableValue, readOnly, subscribable } from '@presenter/node'
import { omit, omitBy } from 'lodash-es'
import { omit, pick } from 'radashi'

import { GlobalSettings } from '~/services/global-settings'

Expand All @@ -12,9 +12,9 @@ const createSettingsState = ( { globalSettings }: SettingsStateOptions ) => {
const manyClientSettings = subscribable<ManyClientSettings>( mutableValue( {} ) )
const publicSettings = subscribable( mutableValue<ManyClientSettings>( {} ) )

const omitPrivateClients = ( allSettings: ManyClientSettings ) => omitBy(
const omitPrivateClients = ( allSettings: ManyClientSettings ) => pick(
allSettings,
( _, id ) => manyClientSettings.get()[ id ]?.security.private
( _, id ) => !manyClientSettings.get()[ id ]?.security.private
)

const setSettings = ( id: string, { local, global, clients }: Partial<Settings> ) => {
Expand All @@ -31,7 +31,7 @@ const createSettingsState = ( { globalSettings }: SettingsStateOptions ) => {
}

const getClientSettings = ( id: string ): Settings => {
const clients = omit( publicSettings.get(), id )
const clients = omit( publicSettings.get(), [ id ] )
const local = manyClientSettings.get()[ id ]
const global = globalSettings.get()

Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/features/zoom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { mutableCounter, mutableValue } from '@presenter/node'
import { LANGUAGES, TRANSLATION_ORDER, TRANSLITERATION_ORDER } from '@shabados/frontend/src/lib/data'
import { customiseLine, getTranslations, getTransliterators } from '@shabados/frontend/src/lib/line'
import { stripVishraams, toUnicode } from 'gurmukhi-utils'
import { mapValues } from 'lodash-es'
import { mapValues } from 'radashi'
import Url from 'url-parse'

import settings from '~/features/settings'
import fetch from '~/helpers/fetch'
import { getSources, GetSourcesResult } from '~/services/database'
import settings from '~/features/settings'

const prepareSecondary = ( data, sorter ) => Object
.entries( data )
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/services/database.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Bani, Language, Shabad, Source, TranslationSource } from '@presenter/contract'
import { Banis, CommonBuilder, Languages, Lines, LinesBuilder, Model, Shabads, Sources, UnwrapModel, Writers } from '@shabados/database'
import gurmukhiUtils from 'gurmukhi-utils'
import { groupBy, last } from 'lodash-es'
import { group, last } from 'radashi'
import type { AsyncReturnType } from 'type-fest'

import { MAX_RESULTS } from '~/helpers/consts'
Expand Down Expand Up @@ -71,7 +71,7 @@ const groupTranslationSources = <Result>(
...acc,
[ id ]: {
...source,
translationSources: groupBy( translationSources, ( { languageId } ) => languageId ),
translationSources: group( translationSources, ( { languageId } ) => languageId ),
},
} )

Expand Down
2 changes: 1 addition & 1 deletion apps/electron/src/window.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as remote from '@electron/remote/main'
// eslint-disable-next-line import/no-extraneous-dependencies
import { app, BrowserWindow, Menu, screen } from 'electron'
import { omit } from 'lodash'
import { omit } from 'radashi'

import { isDev, PORT } from '../../../app/helpers/consts'

Expand Down
2 changes: 0 additions & 2 deletions apps/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"gurmukhi-utils": "^3.2.2",
"is-mobile": "^3.1.1",
"jotai": "^2.10.1",
"lodash": "^4.17.21",
"memoizee": "^0.4.17",
"notistack": "^3.0.1",
"partysocket": "^1.0.2",
Expand All @@ -60,7 +59,6 @@
"@testing-library/jest-dom": "^6.4.5",
"@testing-library/react": "^15.0.7",
"@testing-library/user-event": "^14.5.2",
"@types/lodash": "^4.17.7",
"@types/memoizee": "^0.4.11",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/src/app/overlay/index.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createLazyFileRoute } from '@tanstack/react-router'
import classNames from 'classnames'
import { mapValues } from 'lodash'
import { mapValues } from 'radashi'
import { useContext } from 'react'

import { SettingsContext } from '~/helpers/contexts'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './index.css'

import classNames from 'classnames'
import { mapValues } from 'lodash'
import { mapValues } from 'radashi'

import { LANGUAGES } from '~/helpers/data'
import { customiseLine, getTransliterators } from '~/helpers/line'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import ListItem from '@mui/material/ListItem'
import { createFileRoute } from '@tanstack/react-router'
import classNames from 'classnames'
import { stripVishraams } from 'gurmukhi-utils'
import { invert } from 'lodash'
import { invert } from 'radashi'
import { useCallback, useContext, useEffect, useMemo, useState } from 'react'

import GlobalHotKeys from '~/components/GlobalHotKeys'
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/src/app/settings/client/hotkeys/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { Button, Grid, List, ListItem, Tooltip, Typography } from '@mui/material'
import { createFileRoute } from '@tanstack/react-router'
import classNames from 'classnames'
import { groupBy } from 'lodash'
import { group } from 'radashi'
import { useState } from 'react'

import keyMap from '~/helpers/keyMap'
Expand Down Expand Up @@ -93,7 +93,7 @@ const Hotkeys = ( { keys, shortcuts, device }: HotkeysProps ) => {

<List className="hotkeys">
{Object
.entries( groupBy( shortcuts, ( { group } ) => group ) )
.entries( group( shortcuts, ( { group } ) => group ) )
.map( ( [ groupName, hotkeys ] ) => (
<ListItem key={groupName} className="group">

Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/src/components/CopyHotkeys.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { stripVishraams, toUnicode } from 'gurmukhi-utils'
import { mapValues } from 'lodash'
import { mapValues } from 'radashi'
import { ReactNode, useContext } from 'react'

import { RecommendedSourcesContext, SettingsContext, WritersContext } from '~/helpers/contexts'
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/src/components/GlobalHotKeys.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mapValues } from 'lodash'
import { mapValues } from 'radashi'
import { ReactNode } from 'react'
import { configure, GlobalHotKeys as HotKeys } from 'react-hotkeys/es'
import KeyEventManager from 'react-hotkeys/es/lib/KeyEventManager'
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/src/components/NavigatorHotkeys.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { noop } from 'lodash'
import { noop } from 'radashi'
import { useCallback, useContext, useEffect } from 'react'

import { getJumpLines } from '~/helpers/auto-jump'
Expand Down
9 changes: 4 additions & 5 deletions apps/frontend/src/helpers/auto-jump.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ClientEventParameters } from '@presenter/contract'
import { find, findIndex, findLastIndex, invert } from 'lodash'
import memoize from 'memoizee'
import { invert } from 'radashi'

import { BANIS } from './data'

Expand Down Expand Up @@ -64,17 +64,16 @@ export const getBaniNextJumpLine = ( { content, lineId } ) => {
const currentLine = lines[ currentLineIndex ]

// Get next jump line by searching for it from the current line's index
const nextJumpLineFinder = () => find(
lines,
const nextJumpLineFinder = () => lines.find(
( { id } ) => !!jumpLines[ id ],
Math.min( currentLineIndex + 1, lines.length - 1 ),
) || {}

// Returns a line from the current line's index, based on a regex
const regexFinder = ( regex: RegExp, forward = true, offset = currentLineIndex ) => {
const findFn = forward ? findIndex : findLastIndex
const findFn = forward ? lines.indexOf : lines.lastIndexOf

return findFn( lines, ( { gurmukhi } ) => regex.test( gurmukhi ), offset )
return findFn( ( { gurmukhi } ) => regex.test( gurmukhi ), offset )
}

// Gets the next line after the last pauri
Expand Down
10 changes: 3 additions & 7 deletions apps/frontend/src/helpers/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*! Shared with backend. To be refactored into shared location.
*/
import { toEnglish, toHindi, toShahmukhi, toUnicode } from 'gurmukhi-utils'
import { invert } from 'lodash'
import { invert } from 'radashi'

// Bani IDs
export const BANIS = {
Expand All @@ -21,9 +21,7 @@ export const LANGUAGES = {
// Languages by keyed by IDs
export const LANGUAGE_NAMES = invert( LANGUAGES )

export type Transliterators = {
[language: string]: ( ascii: string ) => string,
}
export type Transliterators = Record<string, ( ascii: string ) => string>

// The transliterator functions for each language, presuming ascii input
export const TRANSLITERATORS = Object.entries( {
Expand All @@ -35,9 +33,7 @@ export const TRANSLITERATORS = Object.entries( {
[ language ]: ( ascii: string ) => fn( toUnicode( ascii ) ),
} ), {} as Transliterators )

export type Translations = {
[language: string]: number,
}
export type Translations = Record<string, number>

// The order of translations
export const TRANSLATION_ORDER = [
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/src/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import deepmerge from 'deepmerge'
import { debounce } from 'lodash'
import { debounce } from 'radashi'
import { findDOMNode } from 'react-dom'
import scrollIntoView from 'scroll-into-view'

Expand All @@ -17,7 +17,7 @@ export const scrollIntoCenter = ( ref: any, options?: __ScrollIntoView.Settings
{ time: 200, ...options }
)

export const debounceHotKey = ( fn: () => void ) => debounce( fn, 300, { leading: true } )
export const debounceHotKey = ( fn: () => void ) => debounce( { leading: true, delay: 300 }, fn )

export const mapPlatformKey = ( key: string ) => ( isMac ? key.replace( 'ctrl', 'cmd' ) : key )

Expand Down
35 changes: 11 additions & 24 deletions package-lock.json

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"devDependencies": {
"@presenter/tsconfig": "*",
"@shabados/eslint-config": "^0.14.0",
"@types/lodash-es": "^4.17.12",
"type-fest": "^4.18.2",
"typescript": "^5.5.4",
"undici": "^6.18.0",
Expand All @@ -26,7 +25,7 @@
"dependencies": {
"@presenter/contract": "*",
"deepmerge": "^4.3.1",
"lodash-es": "^4.17.21",
"make-error": "^1.3.6"
"make-error": "^1.3.6",
"radashi": "^12.2.0"
}
}
4 changes: 2 additions & 2 deletions packages/node/src/mutable-value.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventEmitter } from 'eventemitter3'
import { debounce } from 'lodash-es'
import { debounce } from 'radashi'

export type ReadOnlyMutable<T> = { get: () => T }
export type WriteOnlyMutable<T> = { set: ( value: T ) => void }
Expand Down Expand Up @@ -33,7 +33,7 @@ export const subscribable = <T>( { get, set }: Mutable<T> ) => {

const onChange = ( fn: ( value: T ) => void ) => emitter.on( 'change', fn )

const emit = debounce( () => emitter.emit( 'change', get() ) )
const emit = debounce( { delay: 0 }, () => emitter.emit( 'change', get() ) )

const setWithEmit = ( value: T ) => {
set( value )
Expand Down
Loading

0 comments on commit 11c3692

Please sign in to comment.