1.0.2 • Published 3 years ago
optional-neo v1.0.2
optional-neo
This tiny library allows you to work with object in functional programming style and forget about null checking. More or less it is very similar to Java's implementation of Optional functionality with several useful unique utilities.
Since it's written in Typescript, you will always able to have correct type of particular object.
Why do you probably need this lib
- You like functional programming style and do not want to write
ifin order to check object's nullability - You have come to JS/TS world from other ones, where you used to write the code with similar constructions (for example, like in Java)
- You want to have additional set of useful utilities, what other similar libraries does not provide you
- You want to use typed library for optional objects
Installation
npm install optional-neoInterface's methods
map(mapping_function)- map value of this Optional viamapping_functionand return new one. New value would be result of mappingchain(mapping_function)- similar tomap, but heremapping_functionreturns Optional valuefilter(filter_function)- return this Optional iffilter_functionfor current value returnstrue, otherwise empty OptionalgetOrElse/getOrNull/getOrUndefined- return this value in case of non-empty Optional, otherwisedefaulValue/null/undefinedisPresent()/isEmpty()- returntruein case ofnon-empty/emptyOptional, otherwisefalseifPresent(function)/ifAbsent(function)- invokefunctionin case ofnon-empty/emptyOptionalisEqual(another_optional, comparison_function?)- compare this andanother_optionalOptional. Ifcomparison_functionis defined - use it, otherwise use===backUp(another_optional)- return this Optional if it is non-empty, otherwiseanother_optionalOptional
Utility functions and constants
empty- constant for Empty Optionalsome(value)- create a new Optional with non-null valuefromNullable(possibly_nullable_value)- create a new Optional with non-null value or return Empty OptionalcleanOptional(possibly_undefined_value)- sometime already created Optional could have undefined value (it's JS world, dude :-) ). In this casecleanOptionalfunction will 'clean' it - returns itself if value is notnullorundefined, otherwise returns Empty OptionalemptyFunction- function, which return Empty Optionalgroup- group several Optional objects into single oneinstanceOfOptional(object)- checks, does provided object Optional or not
Examples
- For React components
import * as React from 'react'
import {fromNullable} from 'optional-neo'
type LabelProps = {
value?: string;
};
export const Label: React.FC<LabelProps> => ({value}) =>
fromNullable(value)
.map(val => (<span>{val}</span>))
.getOrNull();- Grouping several Optionals together into single one and map it somehow:
import {group, some} from 'optional-neo';
const firstDateOpt = some(new Date(2023, 1, 1));
const secondDateOpt = some(new Date(2022, 12, 31));
const isDayBefore = group({
firstDate: firstDateOpt,
secondDate: secondDateOpt,
})
.map((firstDate: Date, secondDate: Date) => firstDate.getTime() < secondDate.getTime())
.getOrElse(false);- Use back up value in case of empty Optional
import {fromNullable, Optional, some} from 'optional-neo';
// Since it is incorrect Date, it will be parsed to NaN. We just filter it out and convert to empty Optional
const resolvedMillis: Optional<number> = fromNullable(Date.parse('date')).filter(millis => !isNaN(millis));
const nowMillis: Optional<number> = some(Date.UTC());
console.log(resolvedMillis.backUp(nowMillis).getOrNull()); // for example, 1679157633434- Use chain for inner Optional values
import {} from 'optional-neo';
type TestType = {
name: Optional<string>;
}
const test: Optional<TestType> = some({name: some('Dima')});
console.log(
test
.chain(t => t.name) // return name, Optional<string>
.getOrNull() // return value of 'name' Optional
); // DimaLicense
MIT licensed