1-liners
Version:
Useful oneliners and shorthand functions
20 lines (19 loc) • 541 B
JavaScript
/**
* @module 1-liners/ifThen
*
* @description
*
* Creates a function which calls `then` if the `predicate` is true
* and returns `undefined` if the `predicate` is false.
*
* @example
*
* const ifThen = require('1-liners/ifThen');
*
* const words = ifThen((str) => typeof str === 'string', (str) => str.split(' '));
*
* words('Hello ES2015'); // => ['Hello', 'ES2015']
* words(['Hello', 'ES2015']); // => undefined
*
*/
export default (predicate, then) => (...args) => predicate(...args) ? then(...args) : undefined;