SR_Enum
const Enum match unMatched = ;//or: import {Enum, match} from "sr-enum";const OriginalEnum ="You""Can""Make""Original""Enum""Class";// [Enum].[Enumerator] is constructor for enumerator.const foo ="Each enumeratior can have a value";// match with object - 1const matchWithObject_1 =10111213;;// match with object - 2//// "$" + [tag name] means Symbol of each tagsconst matchWithObject_2 =OriginalEnum$You : 20OriginalEnum$Can : 21OriginalEnum$Make : 2213;;// match with pattern matcherconst matchWithPatternMatcher =OriginalEnum 30OriginalEnum 31OriginalEnum 32OriginalEnum 33matchdefault 34;;// using pattern macher with assignment//// "unMached" is Symbol which is returned when pattern is not matched.//// This can be used like "if let" in Rust// exapmle:// const a = SomeEnum.Foo()(a);// if (a !== unMatched) {// // process when a is Foo// }const assignment_1 = OriginalEnumfoo;const assignment_2 = OriginalEnumfoo;const assignment_3 = OriginalEnumfoo;;;;// using pattern matcher with iflet usingPatternMatcherWithIf = 0;ifOriginalEnumfoo !== unMatchedusingPatternMatcherWithIf = 1;else ifOriginalEnumfoo !== unMatchedusingPatternMatcherWithIf = 2;else ifOriginalEnumfoo !== unMatchedusingPatternMatcherWithIf = 3;;// using with switch//// ! This is not recommended. It is recommended to use match. !let usingWithSwitch = 0;;
const Maybe maybe = ;const res =maybe// create method chain// map : (a -> b) -> Maybe bmaybe// andThen : (a -> Maybe b) -> Maybe b;;;
// using maybe with promiseconst maybe = ; const res = await ;
How to install
> npm install sr-enum
require / import
You can require all modules as follows.
// Enumconst Enum match maybe option result Maybe Option Result = ;
You can import all modules as follows.
// Enum;
What's this
This packege give you Enum.
Usage
To make new enumeration type
To make new enumeration type, you can use new Enum(...tags)
.
sample
const EnumType = "Foo" "Bar" "Baz" "Qux";
To make new enumerator of an enumeration type
To make new enumerator of an enumeration type, you can use new [Enum].[Enumerator](value)
.
sample
const EnumType = "Foo" "Bar" "Baz" "Qux";const foo = 112358;
match
syntax
match with object
// matching with priority: (1st) tag, (2nd) name, (3rd) default /*tag or name*/ : /*function which is run when enumerator is matched*/ /*tag or name*/ : /*function which is run when enumerator is matched*/ /*tag or name*/ : /*function which is run when enumerator is matched*/ /*tag or name*/ : /*function which is run when enumerator is matched*/ default : /*function which is run when enumerator is unmatched*/ //////////////////////////////////////////////////////////////////////////////// // matcing with priority: order of arguments /*tag or name*/ : /*function which is run when enumerator is matched*/ /*tag or name*/ : /*function which is run when enumerator is matched*/ /*tag or name*/ : /*function which is run when enumerator is matched*/ /*tag or name*/ : /*function which is run when enumerator is matched*/ default : /*function which is run when enumerator is unmatched*/
match with pettern matcher
/*pattern matcher*/ /*function which is run when enumerator is matched*/ /*pattern matcher*/ /*function which is run when enumerator is matched*/ /*pattern matcher*/ /*function which is run when enumerator is matched*/ /*pattern matcher*/ /*function which is run when enumerator is matched*/ matchdefault /*function which is run when enumerator is unmatched*/
Enumerator
mehods and propaties in value
: any
The value each enumerator has.
sample
const EnumType = "Foo" "Bar" "Baz" "Qux";const foo = 112358; ;
name
: string
The tag name each enumerator has.
sample
const EnumType = "Foo" "Bar" "Baz" "Qux";const foo = 112358; ;
tag
: Symbol
The tag symbol each enumerator has.
sample
const EnumType = "Foo" "Bar" "Baz" "Qux";const foo = 112358; ;
index
: number
The index each enumerator has.
sample
const EnumType = "Foo" "Bar" "Baz" "Qux"; ;;;;
is
: function(Symbol) -> boolean
sample
const EnumType = "Foo" "Bar" "Baz" "Qux";const foo = 112358; ;;
to
: function(Enum) -> Enumerator
map Enumerator to other Enumerator by index.
sample
const State = "First" "Second" "Third" "Last";const StateInv = "Last" "Third" "Second" "First"; ;;;;
mapWith
: function( function(Enumerator) -> any ) -> any
map Enumerator to other value with mapper.
wrapWith
: function( function(Enumerator) -> wrapper ) -> wrapper
wrap Enumerator. wrapper should has "get" mehod to unwrap.
buildin enum
Maybe
const Maybe = "Just" "Nothing";
Option
const Option = "Some" "None";
Result
const Option = "Ok" "Err";
functions
$
maybe.$
, option.$
and result.$
can make method chain for each enumerators.
sample with Maybe - 1
const maybe = ;const methodChain = maybe;
sample with Maybe - 2
const maybe = ;const methodChain = /*some maybe value*/ ;
map(function | value)
maybe.$.map
map value when enumerator is Maybe.Just
.
option.$.map
map value when enumerator is Option.Some
.
result.$.map
map value when enumerator is Result.Ok
.
sample with Maybe
// it will return new Maybe.Just(2)maybe;
andThen(function | value)
maybe.$.andThen
chain value when enumerator is Maybe.Just
.
option.$.andThen
chain value when enumerator is Option.Some
.
result.$.andThen
chain value when enumerator is Result.Ok
.
sample with Maybe
// it will return new Maybe.Just(2)maybe;
withDefault(any)
withDefault
return value of enumerator with default value.
sample with Maybe
// it will return 100maybe;
boolean
Convert to Boolean
.
sample with Result.
// it will return falseresult; // it will return trueresult;
promise
Convert to Promise.resolve
or Promise.reject
.
sample with Option
const Option option = ; const a = await option ; const b = await option ; ;;
map, andThen, withDefault, boolean, promise
You can use these as not a menber of $. This is useful when you use sr_enum in context of lodash, promise methodcahin and so on.