nick-offerman
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

nick-offerman

npm version

You know how you always have optional values, a: A | null | undefined, and you want to make something of them or do something with them. But not if they're nothing, only if they're something. If they're nothing, they should stay nothing, as is right and proper.

Something like a == null ? a : new Something(a) or maybe a == null ? a : doSomething(a). But you don't want to keep typing all that because that's just so much work. And it's not nice.

And you know how if this was Scala, you'd just write something fyne so you could type a |?> Something but it's not, so you can't, so you just can't have nice things.

Well now you can.

Optionalisating functions

import { of } from "nick-offerman";

const oparseInt = of(parseInt);

const gooseEgg = oparseInt(null, 10); // null
const fortyTwo = oparseInt("42", 10); // 42

Optionalisating constructors

import { Of } from "nick-offerman";

class Something {
  constructor (public value: number) {
  }

  public static readonly of = Of(Something);
}

const nada = Something.of(null); // null
const nichts = Something.of(undefined); // undefined
const summat = Something.of(1); // Something
let ambiguous: number | undefined;
const unknown = Something.of(ambiguous); // Something | undefined

Optionalisating types

Also the types; Perhaps to express the being of optional, CoOptional to widen one type with the optionality of another:

type MaybeDate = Perhaps<Date>; // Date | null | undefined
type MaybeString = CoOptional<string, MaybeDate>; // string | null | undefined
type NullyString = CoOptional<string, number | null>; // string | null

Package Sidebar

Install

npm i nick-offerman

Weekly Downloads

22

Version

0.1.4

License

BSD-3-Clause

Unpacked Size

7.44 kB

Total Files

7

Last publish

Collaborators

  • merlinorg