Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/_internal/globalThis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// eslint-disable-next-line @typescript-eslint/naming-convention
const globalThis_ =
(typeof globalThis === 'object' && globalThis) ||
(typeof window === 'object' && window) ||
(typeof self === 'object' && self) ||
(typeof global === 'object' && global) ||
(function () {
return this;
})() ||
Function('return this')();

export { globalThis_ as globalThis };
9 changes: 2 additions & 7 deletions src/compat/object/mergeWith.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { cloneDeep } from './cloneDeep.ts';
import { isUnsafeProperty } from '../../_internal/isUnsafeProperty.ts';
import { clone } from '../../object/clone.ts';
import { isBuffer } from '../../predicate/isBuffer.ts';
import { isPrimitive } from '../../predicate/isPrimitive.ts';
import { getSymbols } from '../_internal/getSymbols.ts';
import { isArguments } from '../predicate/isArguments.ts';
Expand All @@ -9,12 +10,6 @@ import { isObjectLike } from '../predicate/isObjectLike.ts';
import { isPlainObject } from '../predicate/isPlainObject.ts';
import { isTypedArray } from '../predicate/isTypedArray.ts';

declare let Buffer:
| {
isBuffer: (a: any) => boolean;
}
| undefined;

type MergeWithCustomizer = (objValue: any, srcValue: any, key: string, object: any, source: any, stack: any) => any;

/**
Expand Down Expand Up @@ -288,7 +283,7 @@ function mergeWithDeep(
targetValue = { ...targetValue };
}

if (typeof Buffer !== 'undefined' && Buffer.isBuffer(sourceValue)) {
if (isBuffer(sourceValue)) {
sourceValue = cloneDeep(sourceValue);
}

Expand Down
9 changes: 2 additions & 7 deletions src/compat/predicate/isEmpty.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { isArguments } from './isArguments.ts';
import { isArrayLike } from './isArrayLike.ts';
import { isTypedArray } from './isTypedArray.ts';
import { isBuffer } from '../../predicate/isBuffer.ts';
import type { EmptyObjectOf } from '../_internal/EmptyObjectOf.ts';
import { isPrototype } from '../_internal/isPrototype.ts';

declare let Buffer:
| {
isBuffer: (a: any) => boolean;
}
| undefined;

export function isEmpty<T extends { __trapAny: any }>(value?: T): boolean;
export function isEmpty(value: string): value is '';
export function isEmpty(value: Map<any, any> | Set<any> | ArrayLike<any> | null | undefined): boolean;
Expand Down Expand Up @@ -53,7 +48,7 @@ export function isEmpty(value?: unknown): boolean {
if (
typeof (value as any).splice !== 'function' &&
typeof value !== 'string' &&
(typeof Buffer === 'undefined' || !Buffer.isBuffer(value)) &&
!isBuffer(value) &&
!isTypedArray(value) &&
!isArguments(value)
) {
Expand Down
9 changes: 3 additions & 6 deletions src/object/cloneDeepWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
uint16ArrayTag,
uint32ArrayTag,
} from '../compat/_internal/tags.ts';
import { isBuffer } from '../predicate/isBuffer.ts';
import { isPrimitive } from '../predicate/isPrimitive.ts';
import { isTypedArray } from '../predicate/isTypedArray.ts';

Expand Down Expand Up @@ -152,12 +153,8 @@ export function cloneDeepWithImpl<T>(
return result as T;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(valueToClone)) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return valueToClone.subarray() as T;
if (isBuffer(valueToClone)) {
return (valueToClone as any).subarray() as T;
}

if (isTypedArray(valueToClone)) {
Expand Down
8 changes: 2 additions & 6 deletions src/predicate/isBuffer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
declare let Buffer:
| {
isBuffer: (a: any) => boolean;
}
| undefined;
import { globalThis } from '../_internal/globalThis';

/**
* Checks if the given value is a Buffer instance.
Expand All @@ -25,5 +21,5 @@ declare let Buffer:
export function isBuffer(x: unknown): boolean {
// eslint-disable-next-line
// @ts-ignore
return typeof Buffer !== 'undefined' && Buffer.isBuffer(x);
return typeof globalThis.Buffer !== 'undefined' && globalThis.Buffer.isBuffer(x);
}
9 changes: 2 additions & 7 deletions src/predicate/isEqualWith.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isBuffer } from './isBuffer.ts';
import { isPlainObject } from './isPlainObject.ts';
import { getSymbols } from '../compat/_internal/getSymbols.ts';
import { getTag } from '../compat/_internal/getTag.ts';
Expand Down Expand Up @@ -31,12 +32,6 @@ import {
} from '../compat/_internal/tags.ts';
import { eq } from '../compat/util/eq.ts';

declare let Buffer:
| {
isBuffer: (a: any) => boolean;
}
| undefined;

/**
* Compares two values for equality using a custom comparison function.
*
Expand Down Expand Up @@ -255,7 +250,7 @@ function areObjectsEqual(
case float32ArrayTag:
case float64ArrayTag: {
// Buffers are also treated as [object Uint8Array]s.
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(a) !== Buffer.isBuffer(b)) {
if (isBuffer(a) !== isBuffer(b)) {
return false;
}

Expand Down