Skip to content

Commit

Permalink
feat: implement fallback. close #19
Browse files Browse the repository at this point in the history
  • Loading branch information
customcommander committed Oct 9, 2021
1 parent 269b1f5 commit 3bce5d3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/fallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @license MIT
* @copyright (c) 2021 Julien Gonzalez <hello@spinjs.com>
*/

const {curry} = require('./functions');

/**
* @namespace
* @alias ROOT
*/
module.exports = {
/**
* Returns `x` if and only if `y` is nil.
* Otherwise returns `y`.
*
* @example
* ```javascript
* const your_name = fallback('john doe');
*
* your_name('tom');
* //=> 'tom'
*
* your_name(null);
* //=> 'john doe'
* ```
*
* @public
* @param {?} x Any
* @param {?} y Any
* @return {?}
*/
fallback: curry((x, y) => y == null ? x : y)
};
12 changes: 12 additions & 0 deletions test/fallback.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const test = require('tape');
const {fallback: sut} = require('..');

test('fallback', t => {
const your_name = sut('john doe');
t.equal('tom', your_name('tom'));
t.equal(0, your_name(0));
t.equal(false, your_name(false));
t.equal('john doe', your_name(null));
t.equal('john doe', your_name(undefined));
t.end();
});

0 comments on commit 3bce5d3

Please sign in to comment.