Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pselect

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pselect

Safely resolve deeply nested nullable values with type-safety

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

pselect

Safely select arbitrarily nested nullable fields (with strong types).

import { select } from "pselect";

const foo = {};

const baz = select(foo, p => p.bar.baz); // No Error!
console.log(baz); // undefined

How?

pselect uses the JavaScript Proxy object and Reflection to safely select the nullable field. If Proxy support doesn't exist (ex: due to an old browser), if gracefully falls back to the less effiecient, but always available try-catch.

Installing

npm install pselect

API

select

select takes in your subject and a selector function and returns the value or undefined.

const select: <TObj, TResult>(
  subject: TObj,
  selector: (obj: TObj) => TResult
) => TResult;

Example:

import { select } from "pselect";

const foo = {};

const baz = select(foo, p => p.bar.baz); // No Error!
console.log(baz); // undefined
cselect

cselect is the curried version of the select function. It lets you write nice functional code that you can pipe and chain. (See example)

const cselect: <TObj, TResult>(
  selector: (subject: TObj) => TResult
) => (subject: TObj) => TResult;

Examples:

import { cselect } from "pselect";

const foo = {};

const getBaz = p => p.bar.baz;

console.log(getBaz(foo)); // undefined
import { cselect } from "pselect";

interface IPerson {
  name: string;
  identification?: {
    passport?: {
      passportNumber: "111";
    };
  };
}

const getPerson = (id: number) => {
  return people.find(...);
}

const passportNumber = pipe(
  getPerson,
  cselect(p => p.identification!.passport!.passportNumber),
  passportNumber => passportNumber || "NO_PASSPORT"
);

const fred: IPerson = {
  name: "fred"
};

passportNumber(fred); // NO_PASSPORT

Prior Art

  • Optional (null-safe) in javascript by jpitchardo
  • facebookincubator/idx

Licence

MIT

FAQs

Package last updated on 27 Jun 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc