Skip to content

Commit 7cc004b

Browse files
jantimonraon0211
andauthored
fix(isBuffer): add browser export condition to avoid 44KB Buffer polyfill (#1671)
* fix[isBuffer]: add browser export condition to avoid 28KB Buffer polyfill * fix: read UMD output path from package.json instead of hardcoding * use globalThis.Buffer instead * rollup * comment --------- Co-authored-by: Sojin Park <raon0211@gmail.com>
1 parent c8fec98 commit 7cc004b

6 files changed

Lines changed: 23 additions & 33 deletions

File tree

src/_internal/globalThis.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// eslint-disable-next-line @typescript-eslint/naming-convention
2+
const globalThis_ =
3+
(typeof globalThis === 'object' && globalThis) ||
4+
(typeof window === 'object' && window) ||
5+
(typeof self === 'object' && self) ||
6+
(typeof global === 'object' && global) ||
7+
(function () {
8+
return this;
9+
})() ||
10+
Function('return this')();
11+
12+
export { globalThis_ as globalThis };

src/compat/object/mergeWith.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { cloneDeep } from './cloneDeep.ts';
22
import { isUnsafeProperty } from '../../_internal/isUnsafeProperty.ts';
33
import { clone } from '../../object/clone.ts';
4+
import { isBuffer } from '../../predicate/isBuffer.ts';
45
import { isPrimitive } from '../../predicate/isPrimitive.ts';
56
import { getSymbols } from '../_internal/getSymbols.ts';
67
import { isArguments } from '../predicate/isArguments.ts';
@@ -9,12 +10,6 @@ import { isObjectLike } from '../predicate/isObjectLike.ts';
910
import { isPlainObject } from '../predicate/isPlainObject.ts';
1011
import { isTypedArray } from '../predicate/isTypedArray.ts';
1112

12-
declare let Buffer:
13-
| {
14-
isBuffer: (a: any) => boolean;
15-
}
16-
| undefined;
17-
1813
type MergeWithCustomizer = (objValue: any, srcValue: any, key: string, object: any, source: any, stack: any) => any;
1914

2015
/**
@@ -288,7 +283,7 @@ function mergeWithDeep(
288283
targetValue = { ...targetValue };
289284
}
290285

291-
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(sourceValue)) {
286+
if (isBuffer(sourceValue)) {
292287
sourceValue = cloneDeep(sourceValue);
293288
}
294289

src/compat/predicate/isEmpty.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import { isArguments } from './isArguments.ts';
22
import { isArrayLike } from './isArrayLike.ts';
33
import { isTypedArray } from './isTypedArray.ts';
4+
import { isBuffer } from '../../predicate/isBuffer.ts';
45
import type { EmptyObjectOf } from '../_internal/EmptyObjectOf.ts';
56
import { isPrototype } from '../_internal/isPrototype.ts';
67

7-
declare let Buffer:
8-
| {
9-
isBuffer: (a: any) => boolean;
10-
}
11-
| undefined;
12-
138
export function isEmpty<T extends { __trapAny: any }>(value?: T): boolean;
149
export function isEmpty(value: string): value is '';
1510
export function isEmpty(value: Map<any, any> | Set<any> | ArrayLike<any> | null | undefined): boolean;
@@ -53,7 +48,7 @@ export function isEmpty(value?: unknown): boolean {
5348
if (
5449
typeof (value as any).splice !== 'function' &&
5550
typeof value !== 'string' &&
56-
(typeof Buffer === 'undefined' || !Buffer.isBuffer(value)) &&
51+
!isBuffer(value) &&
5752
!isTypedArray(value) &&
5853
!isArguments(value)
5954
) {

src/object/cloneDeepWith.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
uint16ArrayTag,
2525
uint32ArrayTag,
2626
} from '../compat/_internal/tags.ts';
27+
import { isBuffer } from '../predicate/isBuffer.ts';
2728
import { isPrimitive } from '../predicate/isPrimitive.ts';
2829
import { isTypedArray } from '../predicate/isTypedArray.ts';
2930

@@ -152,12 +153,8 @@ export function cloneDeepWithImpl<T>(
152153
return result as T;
153154
}
154155

155-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
156-
// @ts-ignore
157-
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(valueToClone)) {
158-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
159-
// @ts-ignore
160-
return valueToClone.subarray() as T;
156+
if (isBuffer(valueToClone)) {
157+
return (valueToClone as any).subarray() as T;
161158
}
162159

163160
if (isTypedArray(valueToClone)) {

src/predicate/isBuffer.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
declare let Buffer:
2-
| {
3-
isBuffer: (a: any) => boolean;
4-
}
5-
| undefined;
1+
import { globalThis } from '../_internal/globalThis';
62

73
/**
84
* Checks if the given value is a Buffer instance.
@@ -25,5 +21,5 @@ declare let Buffer:
2521
export function isBuffer(x: unknown): boolean {
2622
// eslint-disable-next-line
2723
// @ts-ignore
28-
return typeof Buffer !== 'undefined' && Buffer.isBuffer(x);
24+
return typeof globalThis.Buffer !== 'undefined' && globalThis.Buffer.isBuffer(x);
2925
}

src/predicate/isEqualWith.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isBuffer } from './isBuffer.ts';
12
import { isPlainObject } from './isPlainObject.ts';
23
import { getSymbols } from '../compat/_internal/getSymbols.ts';
34
import { getTag } from '../compat/_internal/getTag.ts';
@@ -31,12 +32,6 @@ import {
3132
} from '../compat/_internal/tags.ts';
3233
import { eq } from '../compat/util/eq.ts';
3334

34-
declare let Buffer:
35-
| {
36-
isBuffer: (a: any) => boolean;
37-
}
38-
| undefined;
39-
4035
/**
4136
* Compares two values for equality using a custom comparison function.
4237
*
@@ -255,7 +250,7 @@ function areObjectsEqual(
255250
case float32ArrayTag:
256251
case float64ArrayTag: {
257252
// Buffers are also treated as [object Uint8Array]s.
258-
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(a) !== Buffer.isBuffer(b)) {
253+
if (isBuffer(a) !== isBuffer(b)) {
259254
return false;
260255
}
261256

0 commit comments

Comments
 (0)