https://github.com/zhangchiqing/maybell
Failed attempt to make Maybe pattern with optional type as data structure :'( See https://github.com/zhangchiqing/maybe instead
https://github.com/zhangchiqing/maybell
Last synced: 18 days ago
JSON representation
Failed attempt to make Maybe pattern with optional type as data structure :'( See https://github.com/zhangchiqing/maybe instead
- Host: GitHub
- URL: https://github.com/zhangchiqing/maybell
- Owner: zhangchiqing
- License: mit
- Created: 2016-04-06T16:08:11.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-04-08T04:34:14.000Z (about 9 years ago)
- Last Synced: 2025-05-03T18:19:15.360Z (about 2 months ago)
- Language: JavaScript
- Homepage:
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
maybell is a lightweight library that provides functions to fight against `undefined`.
## Installation
```
$ npm install maybell
```## API
### fmap
```
fmap :: (a -> b) -> Maybe a -> Maybe b
```Takes a function, applies the function to the input value, returns the result if the input value is not undefined, or returns undefined if the value is undefined.
Example:
```
fmap(function(x) {
return x + 1;
})(undefined);
//=> undefinedfmap(function(x) {
return x + 1;
})(2);
//=> 3
````Maybe a` here means a value that could be either undefined or any other type. It could be `undefined` or `"foo"`, in which case Type "a" is String. Or it could be `undefined` or `1`, then Type "a" is Number. But it can't be `undefined` or `"foo"` or `1`.
`Maybe b` means its value could be `undefined` or any other type which doesn't have to be the same type as Type "a".
Other use case:
```
function getUserName(user) {
return user.name;
}getUserName();
//=> !TypeError: Cannot read property 'name' of undefined// Workaround
function getUserName(user) {
if(!user) {
return ;
}return user.name;
}getUserName();
//=> undefined// Better
var getUserNameFromMaybeUser = fmap(getUserName);getUserNameFromMaybeUser();
//=> undefinedgetUserNameFromMaybeUser({ name: 'Leo' });
//=> 'Leo'
```### empty
```
empty :: * -> Maybe *
```returns 'undefined'
```
empty()
//=> undefined
```### isNothing
```
isNothing :: Maybe a -> Boolean
```Checks if the input is `undefined` or `null`
```
isNothing(undefined);
//=> trueisNothing(null);
//=> trueisNothing(0)
//=> falseisNothing(false)
//=> false
```### isJust
```
isJust :: Maybe a -> Boolean
```Checks if the input is not either `undefined` or `null`
```
isJust(undefined);
//=> falseisJust(null);
//=> falseisJust(0);
//=> trueisJust(false);
//=> true
```### lift
```
lift :: (a -> b -> ... -> n -> x) -> Maybe a -> Maybe b -> ... -> Maybe n -> Maybe x
```"lifts" a function and applies the input values to the function if none of them is 'undefined', otherwise returns 'undefined'.
```
var sumValue = function(a, b) {
return a.value + b.value;
};lift(sumValue)({ value: 1 }, { value: 2 });
//=> 3;lift(sumValue)(undefined, { value: 2 });
//=> undefinedlift(sumValue)();
//=> undefined
```### sequence
```
sequence :: Array Maybe a -> Maybe Array a
```Takes a list of maybe value and returns the list if none of them is 'undefined'. Otherwise returns 'undefined' if any of list item is 'undefined'.
```
sequence([undefined, 1, 2, 3]);
//=> undefinedsequence([1, 2, 3, 4]);
//=> [1, 2, 3, 4]sequence([])
//=> []
```### traverse
```
traverse :: (a -> Maybe b) -> Array a -> Maybe Array b
```Maps map a function, which takes a value and returns a maybe value, over a list of value, and use sequence to transform the list of maybe value into a list of value.
```
function gt3(x) {
if (x > 3) {
return x;
} else {
return undefined;
}
}traverse(gt3)([5, 6, 7]);
//=> [5, 6, 7]traverse(gt3)([0, 1, 2]);
//=> undefinedtraverse(gt3)([5, 6, 1]);
//=> undefinedtraverse(gt3)([]);
//=> []
```