mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00:00
refactor(editor): remove assert functions (#10629)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
export * from './assert.js';
|
||||
export * from './crypto.js';
|
||||
export * from './disposable.js';
|
||||
export * from './function.js';
|
||||
export * from './is-equal.js';
|
||||
export * from './iterable.js';
|
||||
export * from './logger.js';
|
||||
export * from './signal-watcher.js';
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
// https://stackoverflow.com/questions/31538010/test-if-a-variable-is-a-primitive-rather-than-an-object
|
||||
import { ErrorCode } from '../exceptions/code.js';
|
||||
import { BlockSuiteError } from '../exceptions/index.js';
|
||||
|
||||
export function isPrimitive(
|
||||
a: unknown
|
||||
): a is null | undefined | boolean | number | string {
|
||||
@@ -10,16 +7,6 @@ export function isPrimitive(
|
||||
|
||||
export function assertType<T>(_: unknown): asserts _ is T {}
|
||||
|
||||
export function assertNotExists<T>(
|
||||
val: T | null | undefined,
|
||||
message = 'val exists',
|
||||
errorCode = ErrorCode.ValueNotExists
|
||||
): asserts val is null | undefined {
|
||||
if (val !== null && val !== undefined) {
|
||||
throw new BlockSuiteError(errorCode, message);
|
||||
}
|
||||
}
|
||||
|
||||
export type Equals<X, Y> =
|
||||
///
|
||||
(<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2
|
||||
@@ -65,26 +52,3 @@ export function isEqual<T extends Allowed, U extends T>(
|
||||
}
|
||||
return true as Equals<T, U>;
|
||||
}
|
||||
export function assertEquals<T extends Allowed, U extends T>(
|
||||
val: T,
|
||||
expected: U,
|
||||
message = 'val is not same as expected',
|
||||
errorCode = ErrorCode.ValueNotEqual
|
||||
): asserts val is U {
|
||||
if (!isEqual(val, expected)) {
|
||||
throw new BlockSuiteError(errorCode, message);
|
||||
}
|
||||
}
|
||||
|
||||
type Class<T> = new (...args: any[]) => T;
|
||||
|
||||
export function assertInstanceOf<T>(
|
||||
val: unknown,
|
||||
expected: Class<T>,
|
||||
message = 'val is not instance of expected',
|
||||
errorCode = ErrorCode.ValueNotInstanceOf
|
||||
): asserts val is T {
|
||||
if (!(val instanceof expected)) {
|
||||
throw new BlockSuiteError(errorCode, message);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
import { assertInstanceOf } from '@blocksuite/global/utils';
|
||||
import { effect } from '@preact/signals-core';
|
||||
import * as Y from 'yjs';
|
||||
|
||||
@@ -47,7 +46,9 @@ export class RangeService<TextAttributes extends BaseTextAttributes> {
|
||||
return null;
|
||||
}
|
||||
const textNode = text.childNodes[1];
|
||||
assertInstanceOf(textNode, Text);
|
||||
if (!(textNode instanceof Text)) {
|
||||
return null;
|
||||
}
|
||||
range.setStart(textNode, 0);
|
||||
range.setEnd(textNode, textNode.textContent?.length ?? 0);
|
||||
const inlineRange = this.toInlineRange(range);
|
||||
|
||||
Reference in New Issue
Block a user