Skip to content

Latest commit

 

History

History
148 lines (115 loc) · 3.14 KB

File metadata and controls

148 lines (115 loc) · 3.14 KB

Maybe

Some/None/From

const maybe = Maybe.some('apple');

console.log(maybe.hasValue); // true
console.log(maybe.hasNoValue); // false
console.log(maybe.getValueOrDefault('banana')); // 'apple'
console.log(maybe.getValueOrThrow()); // 'apple'
const maybe = Maybe.none();

console.log(maybe.hasValue); // false
console.log(maybe.hasNoValue); // true
console.log(maybe.getValueOrDefault('banana')); // 'banana'
console.log(maybe.getValueOrThrow()); // throws Error 'No value'
const maybe = Maybe.from(undefined);

console.log(maybe.hasValue); // false
console.log(maybe.hasNoValue); // true
console.log(maybe.getValueOrDefault('banana')); // 'banana'
console.log(maybe.getValueOrThrow()); // throws Error 'No value'

TryFirst

const maybe = Maybe.tryFirst(['apple', 'banana']);

console.log(maybe.getValueOrThrow()); // 'apple'
const maybe = Maybe.tryFirst(['apple', 'banana'], (fruit) => fruit.length > 6);

console.log(maybe.getValueOrThrow()); // throws Error 'No value'

TryLast

const maybe = Maybe.tryLast(
  ['apple', 'banana', 'mango'],
  (fruit) => fruit.length === 5
);

console.log(maybe.getValueOrThrow()); // 'mango'
const maybe = Maybe.tryLast(
  ['apple', 'banana', 'mango'],
  (fruit) => fruit === 'pear'
);

console.log(maybe.getValueOrThrow()); // throws Error 'No value'

Map

const maybe = Maybe.some({ type: 'fruit', name: 'apple' })
  .map(({ type }) => ({ type, name: 'banana' }))
  .map((food) => food.name)
  .map((name) => name.length);

console.log(maybe.getValueOrThrow()); // 6
type Food = {
  type: string;
  name: string;
};

const maybe = Maybe.none<Food>()
  .map(({ type }) => ({ type, name: 'banana' }))
  .map((food) => food.name)
  .map((name) => name.length);

console.log(maybe.getValueOrThrow()); // throws Error 'No value'

Match

const maybe = Maybe.some({ type: 'fruit', name: 'apple' })
  .map(({ type }) => ({ type, name: 'banana' }))
  .map((food) => food.name)
  .map((name) => name.length)
  .match({
    some: (number) => console.log(number),
    none: () => console.log('None!'),
  }); // 6
type Food = {
  type: string;
  name: string;
};

const maybe = Maybe.none<Food>()
  .map(({ type }) => ({ type, name: 'banana' }))
  .map((food) => food.name)
  .map((name) => name.length)
  .match({
    some: (number) => console.log(number),
    none: () => console.log('None!'),
  }); // None!

Pipe

// custom-operators.ts
import { logger, LogLevel } from 'logger';

export function log<TValue>(
  messageCreator: FunctionOfTtoK<TValue, string>,
  logLevel: LogLevel = 'debug'
): MaybeOpFn<TValue, TValue> {
  return (maybe) => {
    if (maybe.hasValue) {
      logger.log(messageCreator(maybe.getValueOrThrow()), logLevel);
    } else {
      logger.error('No value found!');
    }

    return maybe;
  };
}

// app.ts
import { log } from './custom-operators.ts';

const maybe = Maybe.some('apple')
  .pipe(log((f) => `My fruit is ${f}`, 'information'))
  .map((f) => `${f} and banana`)
  .pipe(log((f) => `Now I have ${f}`));