JSPM

  • Created
  • Published
  • Downloads 824
  • Score
    100M100P100Q94060F
  • License MIT

Assertions and typeguards

Package Exports

  • @httpx/assert
  • @httpx/assert/package.json

Readme

@httpx/assert

Assertions and typeguards as primitives

npm changelog codecov bundles node browserslist size maintainability downloads license

Install

$ npm install @httpx/assert
$ yarn add @httpx/assert
$ pnpm add @httpx/assert

Features

Wip

Documentation

👉 Official website, GitHub Readme or generated api doc

Usage

assertNever

import { assertNever } from '@httpx/assert';

type PromiseState = 'resolved' | 'rejected' | 'running'
const state: PromiseState = 'rejected';
switch(state) {
  case 'resolved': return v;
  case 'rejected': return new Error();
  default:
    assertNever(state); // 👈 TS will complain about missing 'running' state
    // ☝️ Will throw a TypeError in js.
}

PS: you can use the assertNeverNoThrow with the same behaviour except that it doesn't throw and return the value instead (no runtime error).

isPlainObject

import { isPainObject, assertPlainObject } from '@httpx/assert';

isPlainObject({cool: true}); // 👈 true
isPlainObject(new Promise()); // 👈 false
assertPlainObject({});

isNumberSafeInt

import { assertNumberSafeInt, isNumberSafeInt } from '@httpx/assert';

isNumberSafeInt(10n); // 👉 false
isNumberSafeInt(BigInt(10)); // 👉 false
isNumberSafeInt(Number.MAX_SAFE_INTEGER); // 👉 true
assertNumberSafeInt(Number.MAX_SAFE_INTEGER + 1); // 👉 throws

isArrayNotEmpty

import { isArrayNotEmpty, assertArrayNotEmpty } from '@httpx/assert';

isArrayNotEmpty([]) // 👉 false
isArrayNotEmpty([0,1]) // 👉 true
assertArrayNotEmpty([]) // 👉 throws

isStrNotEmpty

import { assertStrNotEmpty, isStrNotEmpty } from '@httpx/assert';

isStrNotEmpty(''); // 👉 false
isStrNotEmpty(' '); // 👉 false: trim by default
isStrNotEmpty(' ', { trim: false }); // 👉 true: disbable trim
assertStrNotEmpty(''); // 👉 throws
assertStrNotEmpty('', undefined, { trim: false });

isStrParsableSafeInt

import { assertStrParsableSafeInt, isStrParsableSafeInt } from '@httpx/assert';

isStrParsableSafeInt(2); // 👉 false
isStrParsableSafeInt(`${Number.MAX_SAFE_INTEGER}`); // 👉 true
assertStrParsableSafeInt(`${Number.MAX_SAFE_INTEGER}1`); // 👉 throws

Uuid

isUuid

Supported uuid versions are: 1, 3, 4 and 5.

import { isUuid, isUuidV1, isUuidV3, isUuidV4, isUuidV5 } from "@httpx/assert";
import { assertUuid, assertUuidV1, assertUuidV3, assertUuidV4, assertUuidV5 } from "@httpx/assert";
import { getUuidVersion } from '@httpx/assert';

// Without version
isUuid('90123e1c-7512-523e-bb28-76fab9f2f73d'); // 👉 valid uuid v1, 3, 4 or 5
assertUuid('90123e1c-7512-523e-bb28-76fab9f2f73d');

// With version
isUuid('90123e1c-7512-523e-bb28-76fab9f2f73d', { version: 5 });
assertUuid('90123e1c-7512-523e-bb28-76fab9f2f73d', undefined, { version: 5 });
assertUuidV5('90123e1c-7512-523e-bb28-76fab9f2f73d')
isUuidV4('d9428888-122b-11e1-b85c-61cd3cbb3210'); // 👈 or isUuidV1(''), isUuidV3(''), isUuidV5('');

// Utils
getUuidVersion('90123e1c-7512-523e-bb28-76fab9f2f73d'); // 5

Barcode

isEan13

Supported barcodes is currently limited to Ean13

import { isEan13 } from "@httpx/assert";
import { assertEan13 } from "@httpx/assert";

isEan13('1234567890128'); // 👈 will check digit too
assertEan13('1234567890128');

Bundle size

Code and bundler have been tuned to target a minimal compressed footprint for the browser.

ESM individual imports are tracked by a size-limit configuration.

Scenario Size (compressed)
Import isPlainObject ~ 56b
Import isUuid ~ 175b
Import isEan13 ~ 117b
All typeguards, assertions and helpers ~ 900b

For CJS usage (not recommended) track the size on bundlephobia.

Compatibility

Level CI Description
ES2021 Dist files checked with es-check
Node16
Node18 Ensured on CI
Node20 Ensured on CI
Edge Ensured on CI with @vercel/edge-runtime
Browsers > 95% on 12/2023. Minimums to Chrome 96+, Firefox 90+, Edge 19+, iOS 12+, Safari 12+, Opera 77+
Typescript TS 4.7+ / Dual packaging is ensured with are-the-type-wrong on the CI.

For older browsers: most frontend frameworks can transpile the library (ie: nextjs...)

Contributors

Contributions are warmly appreciated. Have a look to the CONTRIBUTING document.

Sponsors

If my OSS work brightens your day, let's take it to new heights together! Sponsor, coffee, or star – any gesture of support fuels my passion to improve. Thanks for being awesome! 🙏❤️

Special thanks to

Jetbrains logo Jetbrains logo
JetBrains Embie.be

License

MIT © belgattitude and contributors.