Skip to content

Commit 278e9c0

Browse files
committed
chore: update shared
1 parent 9261313 commit 278e9c0

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

packages/shared/src/fnUtils.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
/**
22
* Возвращает true, если значение равно null или undefined
33
*/
4-
function isNil<T>(value: T): value is T & (null | undefined) {
5-
return value == null;
4+
export function isNullable<T>(value: T): value is T & (null | undefined) {
5+
return !isNonNullable(value);
6+
}
7+
8+
export function isNonNullable<T>(value: T): value is NonNullable<T> {
9+
return value != null;
610
}
711

812
/**
@@ -14,10 +18,10 @@ function clamp<T extends Date | number>(value: T, min: T, max: T): T {
1418
return (value instanceof Date ? new Date(clampedValue) : clampedValue) as T;
1519
}
1620

17-
function noop() {}
21+
export function noop() {}
1822

1923
export const fnUtils = {
2024
clamp,
2125
noop,
22-
isNil,
26+
isNil: isNullable,
2327
};

packages/shared/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export * from './exhaustiveCheck';
1212
export * from './context/PortalContext';
1313
export * from './getComponentBreakpoint';
1414
export * from './warning';
15+
export * from './object';

packages/shared/src/object.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { isNonNullable } from './fnUtils';
2+
3+
export function isObject<T>(value: T): value is T & object {
4+
return isNonNullable(value) && typeof value === 'object';
5+
}
6+
7+
/* eslint-disable @typescript-eslint/no-explicit-any */
8+
// https://stackoverflow.com/a/74608626
9+
type Intersect<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never;
10+
// https://stackoverflow.com/a/52991061
11+
// eslint-disable-next-line @typescript-eslint/ban-types
12+
type OptionalKeys<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? K : never }[keyof T];
13+
14+
export function hasOwnProperty<T extends object, K extends OptionalKeys<T>>(
15+
val: T,
16+
prop: K,
17+
): val is T & { [P in K]-?: T[P] };
18+
export function hasOwnProperty<T extends object, K extends keyof T>(val: T, prop: K): boolean;
19+
export function hasOwnProperty<T extends object, K extends keyof Intersect<T>>(
20+
val: T,
21+
prop: K,
22+
): val is Extract<T, K extends OptionalKeys<T> ? { [P in K]?: any } : { [P in K]: any }>;
23+
export function hasOwnProperty<T extends object>(val: T, prop: PropertyKey) {
24+
return Object.prototype.hasOwnProperty.call(val, prop);
25+
}
26+
/* eslint-enable @typescript-eslint/no-explicit-any */

0 commit comments

Comments
 (0)