@shapediver/sdk.sdtf-core
Version:
Base implementation for all sdTF TypeScript SDKs
41 lines (35 loc) • 1.23 kB
text/typescript
import { SdtfError, SdtfErrorType } from '../SdtfError';
/** Type guard for all error types of the sdTF package. */
export function isSdtfError(e: any): e is SdtfError {
return (
e instanceof Error &&
'errorType' in e &&
Object.values(SdtfErrorType).includes(e.errorType as any)
);
}
/**
* Function to indicate unreachable paths to the TypeScript compiler.
* @throws {@link SdtfError}
*/
export function sdAssertUnreachable(_: never): never {
throw new SdtfError('Reached unreachable block.');
}
/** Creates a new type for a string enum that enables to iterate through its keys */
export function enumKeys<O extends object, K extends keyof O = keyof O>(o: O): K[] {
return Object.keys(o).filter((k) => Number.isNaN(+k)) as K[];
}
/** Returns all values of the given enum */
export function enumValues(o: object): (string | number)[] {
return enumKeys(o).map((k) => o[k]);
}
/**
* Tries to deep copy the given value.
* Returns the original value if the used Node.js or Browser version does not support `structuredClone`.
*/
export function tryDeepCopy<T>(original: T): T {
try {
return structuredClone(original);
} catch (e) {
return original;
}
}