4.2.1 • Published 4 years ago

@juan-utils/ramda-structures v4.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Ramda Structures

Implementations of common structures using ramda for utilities. No inheritance. Just plain objects Now with inheritance!

  • Maybe
  • Result
  • Try (was the same as Result)

All structures share a common set of functions to be used. In terms of nomenclature, all constructor functions start with "from". All structures have a default "from" that marks the most common use of the structure. They all have a "match" function that is case-insensitive to make matches. Due to the use of objects for matching, Order of cases does not alter the result. Matching has two reserved keys for default cases: "default" and "_"(underscore). "default" precedes over underscore and a matching type precedes over default cases. The function signature notation used is similar to haskell's type notations with a little more information: for object method calls, the type of the object is given using the "~>" as seen in this example:

// "~>" used to define the object's type
// This means map must be called on an array object
// and that the object is an array of a's
// map :: [a] ~> (a -> b) -> [b]
[1,2,3].map(() => 42)

// "=>" type constraints are always before the implicit object
// match :: Cases b => Maybe a ~> b -> c
// This means that b" is of type "Cases" and 
// "match" is a method of a "Maybe" object 

Maybe

Maybe<A> = Just<A> | None 

Maybe should be used when communicating the posibility of a falsy value is expected. The default behavior is that a falsy value maps to None and any other value maps to Just.

constructordescription
Just :: a -> Just aJust constructor
None :: () -> NoneNone constructor
from :: a -> Maybe areturns None on falsy value. Just otherwise
fromFalsy :: a -> Maybe areturns None on falsy value. Just otherwise
fromArray :: a -> Maybe areturns None on empty array. Just otherwise
fromNullish :: a -> Maybe areturns None on null or undefined. Just otherwise
fromEmpty :: a -> Maybe areturns None if the value provided is the empty value for the type. Just otherwise. Uses ramda's isEmpty function
fromPredicate :: (a -> Boolean) -> a -> Maybe areturns Just if the predicate returns a truthy value. None otherwise. The second argument is used to evaluate the predicate
fromTry :: Try a => a -> Maybe areturns Just on a Success. None on a Failure
fromResult :: Result a => a -> Maybe areturns Just on a Ok. None on a Err
isEmpty :: Maybe a -> Booleanreturns true on None. False otherwise
match :: (Cases a, Maybe b) => a -> b -> creturns the result of evaluating the cases with the provided value
equals :: a -> b -> Booleanreturns whether two values are equal. Reference to ramda's equals function

Just and None

these are the methods that Just and None have defined.

methoddescription
match :: Cases a => Maybe b ~> a -> creturns the result of evaluating the cases with the called object
get :: Maybe a ~> () -> areturns the inner value. For None, the inner value is undefined
map :: Maybe a ~> (a -> b) -> Maybe breturns Just of the mapped value if called on a Just. None otherwise
effect :: Maybe a ~> (a -> any) -> Maybe acalls the provided function with the inner value and returns the same object if it is a Just. Does nothing otherwise
chain :: Maybe a ~> (a -> b) -> bCall the received function with the inner value
equals :: Maybe a ~> b -> BooleanTests equality. Two Just are equal if their inner values are equal. Two Nones are always equal
onNone :: Maybe a ~> ( (() -> b) | c) -> a | b | cThis function returns the inner value when the object is a Just. Otherwise either calls the provided function and returns it's result or in the case a value was provided, returns said value
isJust :: Maybe a ~> () -> Booleanreturns true if it is a Just. False otherwise
isNone :: Maybe a ~> () -> Booleanreturns true if it is a None. False otherwise
empty :: Maybe a ~> () -> Nonereturn the empty value for the type. Always None

Result

Result<A,B> = Ok<A> | Err<B>

Result marks the possibility of an Error. The default constructor returns Err when the received value is an Error and Ok otherwise.

constructordescription
Ok :: a -> Ok aOk constructor
Err :: a -> Err aErr constructor
from :: a -> Result areturns Err on Error Object. Ok otherwise
fromError :: a -> Result areturns Err on Error Object. Ok otherwise
fromFalsy :: a -> Result areturns Err on falsy value. Ok otherwise
fromPredicate :: (a -> Boolean) -> a -> Result areturns Ok if the predicate returns a truthy value. Err otherwise. The second argument is used to evaluate the predicate
fromTry :: Try a => a -> Result areturns Ok on a Success. Err on a Failure
fromMaybe :: Maybe a => a -> Result areturns Ok on a Just. Err of undefined on a None
attempt :: (() -> a) -> Result areturns Ok if the functions returns. Err if the function throws
match :: (Cases a, Result b) => a -> b -> creturns the result of evaluating the cases with the provided value
equals :: a -> b -> Booleanreturns whether two values are equal. Reference to ramda's equals function

Ok and Err

methoddescription
match :: Cases a => Result b ~> a -> creturns the result of evaluating the cases with the called object
get :: Result a ~> () -> areturns the inner value.
map :: Result a ~> (a -> b) -> Result breturns Ok of the mapped value if called on a Ok. Does nothing otherwise
effect :: Result a ~> (a -> any) -> Result acalls the provided function with the inner value and returns the same object if it is a Ok. Does nothing otherwise
chain :: Result a ~> (a -> b) -> bCall the received function with the inner value if Ok. Returns the same object otherwise
equals :: Result a ~> b -> BooleanTests equality. Two Results are equal if their inner values are equal.
onErr :: Result a ~> ( (() -> b) | c) -> a | b | cThis function returns the inner value when the object is a Ok. Otherwise either calls the provided function and returns it's result or in the case a value was provided, returns said value
isOk :: Result a ~> () -> Booleanreturns true if it is a Ok. False otherwise
isErr :: Result a ~> () -> Booleanreturns true if it is a Err. False otherwise
4.2.1

4 years ago

4.2.0

4 years ago

4.1.0

4 years ago

4.0.3

4 years ago

4.0.1

4 years ago

4.0.2

4 years ago

4.0.0

4 years ago

3.1.0

4 years ago

3.0.0

4 years ago

2.13.1

4 years ago

2.13.0

4 years ago

2.12.2

4 years ago

2.12.1

4 years ago

2.12.0

4 years ago

2.11.1

4 years ago

2.11.0

4 years ago

2.10.2

4 years ago

2.10.0

4 years ago

2.9.0

4 years ago

2.8.1

4 years ago

2.8.3

4 years ago

2.8.4

4 years ago

2.8.0

4 years ago

2.7.1

4 years ago

2.7.0

4 years ago

2.6.0

4 years ago

2.5.2

4 years ago