From 6981e324091f810f1ed88760a9f76f1537b81b9e Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 30 Jun 2024 20:15:48 +0300 Subject: [PATCH] Remove usage of `assert.invariant` This reduces dependencies when building for the browser target, since the assert module is a Node.js dependency. --- src/helpers.ts | 11 +++++++++++ src/serialize.ts | 3 +-- src/unserialize.ts | 3 +-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index 1a7be12..cd18580 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -30,3 +30,14 @@ export function getClass(prototype: Record) { PhpClass.prototype = prototype return PhpClass } + +/** + * Ensures that the given {@link value} is truthy, throws an {@link Error} otherwise. + * @param value the value to check to be truthy. + * @param message the message of the {@link Error} if the value is falsy. + */ +export function invariant(value: any, message?: string) { + if (!value) { + throw new Error(message) + } +} diff --git a/src/serialize.ts b/src/serialize.ts index 3acc170..50eab3f 100644 --- a/src/serialize.ts +++ b/src/serialize.ts @@ -1,5 +1,4 @@ -import invariant from 'assert' -import { isInteger, getByteLength } from './helpers' +import { isInteger, getByteLength, invariant } from './helpers' function getClassNamespace(item: any, scope: Record) { return ( diff --git a/src/unserialize.ts b/src/unserialize.ts index ca7025d..1a1dc1b 100644 --- a/src/unserialize.ts +++ b/src/unserialize.ts @@ -1,7 +1,6 @@ -import invariant from 'assert' // eslint-disable-next-line import/no-cycle import Parser from './parser' -import { isInteger, getClass, getIncompleteClass, __PHP_Incomplete_Class } from './helpers' +import { isInteger, getClass, getIncompleteClass, __PHP_Incomplete_Class, invariant } from './helpers' export type Options = { strict: boolean