raskolnikov
v0.2.0
Published
Existential operations
Downloads
3
Maintainers
Readme
Raskolnikov
"I want to attempt a thing like that and am frightened by these trifles."
Motivation
As many ex-CoffeeScripters (myself included) return to JavaScript, one of more
missed features is the existential ?
operator which allowed for inline
existence chaining. For example:
sparce?.field1?.field2?.function?(params)
While arguably it can lead to bad practices and habits, it was still very convenient.
raskolnikov
is not meant to be a replacement, but does supply some similar functionality.
The JavaScript equivalent of the above using raskolnikov
would be:
Existential.from(sparce)
.exists('field1')
.exists('field2')
.exists('function')
.exists(fn => fn(params))
.val();
Install
$ npm install raskolnikov
Usage
import Existential from 'raskolnikov';
const object = {
hello: [
world(greeting) {
return `${greeting} world`;
}
]
};
Existential.from(object)
.exists('hello')
.exists(0)
.exists('world')
.exists(fn => fn('Hello'))
.val(); // Returns 'Hello world'
Existential.from(object)
.exists('goodbye')
.exists('crime')
.exists(val => `Is there ${val}?`)
.val(); // Returns null
Methods
raskolnikov
can be initialized using:
import Existential from 'raskolnikov';
const existential = new Existential(value);
// or
const existential = Existential.from(value);
The check for existance is done through:
function exists(value) {
return typeof value !== 'undefined' && value === null;
}
exists(key)
Chain of existential type checking. key
can be a string, a number, an array of
string and numbers, or a function. The return value is a new Existential
instance.
String
Accesses the field of the previous object: previous[string]
Number
Accesses the index of the previous value: previous[number]
Array
Accesses the field (if string) or the index (if number) in sequence. The following two are therefore equivalent:
Existential.from(initialValue)
.exists('answer')
.exists(42)
.exists('universe')
// and
Existential.from(initialValue)
.exists(['answer', 42, 'universe'])
Function
Evaluates the function given the previous value. The typical use case is would be check if an object has a certain method then using this to evaluate the function:
function hello(greeting) {
return `${greeting} world`;
}
Existential.from({hello})
.exists('hello')
.exists(fn => hello('Hola'))
.val(); // Return 'Hola world';
However, any function can be used and the available parameter will be the previous value.
Existential.from({hello: 'world'})
.exists('hello')
.exists(val => `hello ${val}`)
.val(); // Return 'hello world';
$(key)
Shorthand for exists(val)
. So the following are equivalent:
```javascript` Existential.from(object) .exists('hello') .exists(0) .exists('world') .exists(fn => fn('Hello'))
Existential.from(object) .$('hello') .$(0) .$(world) .$(fn => fn('Hello'))
// or using array access Existential.from(object) .$(['hello', 0, 'world') .$(fn => fn('Hello'))
## val()
Ends the existential chain and returns the final value. If at any point the
value did not exists, the chain is short-circuited and will return `null`.
## or(fallback)
Like the `val()` method, this will ends the existential chain and returns the
final value if it exists, otherwise return the `fallback`.
## version
Returns the current version of `raskolnikov`.
# License
Copyright © 2015 Nicholas Hwang - [MIT License](LICENSE)