Skip to content

Instantly share code, notes, and snippets.

@horiuchie
Last active July 31, 2019 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horiuchie/e717775c1645c9df6a53e40b45654258 to your computer and use it in GitHub Desktop.
Save horiuchie/e717775c1645c9df6a53e40b45654258 to your computer and use it in GitHub Desktop.
mustBeArray
/*
e.g.
mustBeArray([123, 456, 789]) => [123, 456, 789]
mustBeArray(123) => [123]
*/
const mustBeArray = R.unless(R.is(Array), R.of);
// array type expected
function doSomething (numbers) {
// ...
console.log(numbers);
// ...
}
doSomething(mustBeArray(123));
// => [123]
doSomething(mustBeArray([123, 456]));
// => [123, 456]
function saveUsers (users) {
// ...
console.log(users);
// ...
}
Promise.resolve({ name: 'haru', age: 1 }).then(mustBeArray).then(saveUsers);
// => [{ name: 'haru', age: 1 }]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment