Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
checks-and-balances
Advanced tools
A set of type checks, guards, and predicates for simpler, safer, and easier to read code.
A set of type checks, guards, and predicates for simpler, safer, and easier to read code.
Check your code paths to balance expressibility vs complexity
Simpler, safer, and easier to read code.
Type guards are built from type checks.
const isBlue(value: any): value is 'blue' = value === 'blue'
if (isBlue(color)) throw new Error('should be blue')
Type guards allow us to check values at runtime - to protect code paths from unwanted values. These are very useful with typescript, as typescript will warn you if you dont protect certain code paths from unconstrained inputs and respect the type checking that type guards conduct.
Type guards allow us to
The goal of this library is to define a reusable set of type checks that will add value in the most cases - without adding bloat.
For more information about typescripts type predicates and type guards, see this section in the typescript docs on "narrowing"
npm install --save checks-and-balances
isPresent
The type predicate of isPresent
any informs typescript that if a value passes this type check, the value is not null
or undefined
:
This is most useful for filtering, to inform typescript that we have removed all null
or undefined
values from an array. For example:
import { isPresent } from 'checks-and-balances';
// you have an array that contains strings or nulls
const stringsOrNulls = ['success:1', 'success:2', null, 'success:3', null]; // type = `(string | null)[]`
// now you want to get rid of all the nulls and only think about the strings: use `isPresent`
const strings = stringsOrNulls.filter(isPresent); // type = string[]
// the type predicate on the `isPresent` function informs typescript that all of the nulls and undefineds have been removed
strings.map((string) => string.toUpperCase()); // now you can operate on the strings without typescript complaining!
isOfEnum
This library exposes a function that lets you create type check functions for any enum. For example:
import { createIsOfEnum } from 'checks-and-balances';
// you have an enum
enum Planet {
...
VENUS = 'VENUS',
EARTH = 'EARTH',
MARS = 'MARS',
...
}
// define a type check for your enum
const isPlanet = createIsOfEnum(Planet);
// use your new type check for a type guard
if (!isPlanet(potentialPlanet)) throw new Error('is not a planet');
The following type checks are supported. Please see their definition and tests for more details
isPresent
isOfEnum
hasUuid
hasId
FAQs
A set of type checks, guards, and predicates for simpler, safer, and easier to read code.
The npm package checks-and-balances receives a total of 1 weekly downloads. As such, checks-and-balances popularity was classified as not popular.
We found that checks-and-balances demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.