Skip to content

Commit

Permalink
remove lodash in favour of local functions
Browse files Browse the repository at this point in the history
  • Loading branch information
julianbenegas committed Feb 4, 2025
1 parent 31b0a1b commit 2877428
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 22 deletions.
2 changes: 0 additions & 2 deletions packages/basehub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@
"esbuild-plugin-d.ts": "1.2.3",
"github-slugger": "^2.0.0",
"hast-util-to-jsx-runtime": "^2.3.0",
"lodash.debounce": "^4.0.8",
"lodash.get": "^4.4.2",
"pusher-js": "^8.3.0",
"resolve-pkg": "2.0.0",
"server-only": "^0.0.1",
Expand Down
39 changes: 39 additions & 0 deletions packages/basehub/src/_util/lodash.debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
type DebouncedFunction<T extends (...args: any[]) => any> = {
(...args: Parameters<T>): void;
cancel: () => void;
};

export function debounce<T extends (...args: any[]) => any>(
func: T,
wait: number,
immediate: boolean = false
): DebouncedFunction<T> {
let timeout: NodeJS.Timeout | null = null;

const debounced = function (this: any, ...args: Parameters<T>) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const context = this;

if (timeout) clearTimeout(timeout);

if (immediate && !timeout) {
func.apply(context, args);
}

timeout = setTimeout(() => {
timeout = null;
if (!immediate) {
func.apply(context, args);
}
}, wait);
};

debounced.cancel = function () {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
};

return debounced as DebouncedFunction<T>;
}
35 changes: 35 additions & 0 deletions packages/basehub/src/_util/lodash.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
type Path = string | number | Array<string | number>;

/**
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned.
* @param obj The object to query
* @param path The path of the property to get
* @param defaultValue The value returned for undefined resolved values
* @returns The resolved value
*/
export function get(obj: any, path: Path, defaultValue?: any): any {
// Handle empty/null object
if (obj == null) return defaultValue;

// Convert path to array if it's a string
const segments = Array.isArray(path)
? path
: path
.toString()
.replace(/\[(\w+)\]/g, ".$1")
.split(".");

// Traverse the object
let result = obj;
for (const segment of segments) {
// Skip empty segments
if (!segment) continue;

result = result[segment];

// Return default value if we hit undefined/null
if (result == null) return defaultValue;
}

return result;
}
2 changes: 1 addition & 1 deletion packages/basehub/src/next/toolbar/client-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import s from "./toolbar.module.scss";
import { Tooltip } from "./components/tooltip";
import { DragHandle } from "./components/drag-handle";
import { BranchSwitcher, LatestBranch } from "./components/branch-swticher";
import debounce from "lodash.debounce";
import { debounce } from "../../_util/lodash.debounce";
import { usePathname } from "next/navigation";
import { ResolvedRef } from "../../common-types";

Expand Down
2 changes: 1 addition & 1 deletion packages/basehub/src/next/toolbar/components/tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import debounce from "lodash.debounce";
import { debounce } from "../../../_util/lodash.debounce";
import s from "../toolbar.module.scss";

export type Tooltip = { checkOverflow: () => void };
Expand Down
2 changes: 1 addition & 1 deletion packages/basehub/src/react/search/primitive.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import get from "lodash.get";
import { get } from "../../_util/lodash.get";
import { Slot } from "@radix-ui/react-slot";
import { getHitRecentSearchKey } from "../../search/helpers";
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/basehub/src/search/primitive.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Client } from "typesense";
import type { SearchParams } from "typesense/lib/Typesense/Documents";
import get from "lodash.get";
import { get } from "../_util/lodash.get";
import { getHitKey } from "./helpers";

/* -------------------------------------------------------------------------------------------------
Expand Down
16 changes: 0 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 2877428

Please sign in to comment.