reasonml-community / belt

MIT License
49 stars 1 forks source link

more utils #15

Open chenglou opened 6 years ago

chenglou commented 6 years ago

let isTruthy x => switch (Js.Nullable.to_opt x) { | None => false | Some a => Js.to_bool a };

let (|?) a b => switch a { | None => b | Some a => a };

let isDefined x => Js.Nullable.to_opt x !== None;

/ TODO: @chenglou kill this / let isSomeTrue a => switch a { | None => false | Some a => a };

let nullUndefinedBooleanToOptionBool a => switch (Js.Nullable.to_opt a) { | None => None | Some a => Some (Js.to_bool a) };

let optionBoolToNullUndefinedBoolean a => switch a { | None => Js.Nullable.undefined | Some a => Js.Nullable.return (Js.Boolean.to_js_boolean a) };

let optionBoolToBoolean a => switch a { | None => Js.false_ | Some a => Js.Boolean.to_js_boolean a };

let listFilterMap f ls => { let rec listFilterMap f ls accum => switch ls { | [] => accum | [item, ...rest] => switch (f item) { | None => listFilterMap f rest accum | Some item => listFilterMap f rest [item, ...accum] } }; listFilterMap f ls [] };

let arrayCompact l => l |> Js.Array.filter ( fun item => switch item { | None => false | Some _ => true } ) |> Array.map ( fun | None => raise (Invalid_argument "arrayCompact: sorry, something went wrong.") | Some a => a );