From 67bdde2960d6f978f17a50f2d11d5ae122a3c1a8 Mon Sep 17 00:00:00 2001 From: Maccesch Date: Sun, 16 Jul 2023 14:00:12 +0100 Subject: [PATCH] added is_none and is_some --- CHANGELOG.md | 2 ++ docs/book/src/SUMMARY.md | 2 ++ docs/book/src/utilities/is_none.md | 3 +++ docs/book/src/utilities/is_some.md | 3 +++ examples/Cargo.toml | 2 ++ src/is_none.rs | 27 +++++++++++++++++++++++++++ src/is_some.rs | 27 +++++++++++++++++++++++++++ src/lib.rs | 4 ++++ src/utils/mod.rs | 2 ++ src/utils/use_derive_signal.rs | 18 ++++++++++++++++++ 10 files changed, 90 insertions(+) create mode 100644 docs/book/src/utilities/is_none.md create mode 100644 docs/book/src/utilities/is_some.md create mode 100644 src/is_none.rs create mode 100644 src/is_some.rs create mode 100644 src/utils/use_derive_signal.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a2b149..0fef6d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### New Functions 🚀 +- `is_none` +- `is_some` - `use_raf_fn` ### Braking Changes 🛠 diff --git a/docs/book/src/SUMMARY.md b/docs/book/src/SUMMARY.md index bf3eb46..ef6ee63 100644 --- a/docs/book/src/SUMMARY.md +++ b/docs/book/src/SUMMARY.md @@ -63,6 +63,8 @@ # Utilities +- [is_none](utilities/is_none.md) +- [is_some](utilities/is_some.md) - [use_cycle_list](utilities/use_cycle_list.md) - [use_debounce_fn](utilities/use_debounce_fn.md) - [use_supported](utilities/use_supported.md) diff --git a/docs/book/src/utilities/is_none.md b/docs/book/src/utilities/is_none.md new file mode 100644 index 0000000..9dc8c84 --- /dev/null +++ b/docs/book/src/utilities/is_none.md @@ -0,0 +1,3 @@ +# is_none + + diff --git a/docs/book/src/utilities/is_some.md b/docs/book/src/utilities/is_some.md new file mode 100644 index 0000000..0258188 --- /dev/null +++ b/docs/book/src/utilities/is_some.md @@ -0,0 +1,3 @@ +# is_some + + diff --git a/examples/Cargo.toml b/examples/Cargo.toml index c959fca..8d1f86c 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -2,6 +2,8 @@ resolver = "2" members = [ + "is_none", + "is_some", "on_click_outside", "use_abs", "use_active_element", diff --git a/src/is_none.rs b/src/is_none.rs new file mode 100644 index 0000000..7bd9375 --- /dev/null +++ b/src/is_none.rs @@ -0,0 +1,27 @@ +use crate::utils::use_derive_signal; +use leptos::*; + +use_derive_signal!( + /// Reactive `Option::is_none()`. + /// + /// ## Usage + /// + /// ``` + /// # use leptos::*; + /// # use leptos_use::is_none; + /// # + /// # #[component] + /// # fn Demo(cx: Scope) -> impl IntoView { + /// let (example, set_example) = create_signal( + /// cx, + /// if js_sys::Math::random() < 0.5 { Some("Example") } else { None } + /// ); + /// + /// let is_empty = is_none(cx, example); + /// # + /// # view! { cx, } + /// # } + /// ``` + is_none, T: 'static> -> bool + |value| value.is_none() +); diff --git a/src/is_some.rs b/src/is_some.rs new file mode 100644 index 0000000..d32a424 --- /dev/null +++ b/src/is_some.rs @@ -0,0 +1,27 @@ +use crate::utils::use_derive_signal; +use leptos::*; + +use_derive_signal!( + /// Reactive `Option::is_some()`. + /// + /// ## Usage + /// + /// ``` + /// # use leptos::*; + /// # use leptos_use::is_some; + /// # + /// # #[component] + /// # fn Demo(cx: Scope) -> impl IntoView { + /// let (example, set_example) = create_signal( + /// cx, + /// if js_sys::Math::random() < 0.5 { Some("Example") } else { None } + /// ); + /// + /// let not_empty = is_some(cx, example); + /// # + /// # view! { cx, } + /// # } + /// ``` + is_some, T: 'static> -> bool + |value| value.is_some() +); diff --git a/src/lib.rs b/src/lib.rs index bc22852..b2c6ead 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,8 @@ cfg_if! { if #[cfg(web_sys_unstable_apis)] { }} mod on_click_outside; +mod is_none; +mod is_some; mod use_active_element; mod use_breakpoints; mod use_color_mode; @@ -56,6 +58,8 @@ mod watch_throttled; mod whenever; pub use on_click_outside::*; +pub use is_none::*; +pub use is_some::*; pub use use_active_element::*; pub use use_breakpoints::*; pub use use_color_mode::*; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index d985115..54d4eb7 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -3,9 +3,11 @@ mod filters; mod is; mod js_value_from_to_string; mod pausable; +mod use_derive_signal; pub use clonable_fn::*; pub use filters::*; pub use is::*; pub(crate) use js_value_from_to_string::*; pub use pausable::*; +pub(crate) use use_derive_signal::*; diff --git a/src/utils/use_derive_signal.rs b/src/utils/use_derive_signal.rs new file mode 100644 index 0000000..048726a --- /dev/null +++ b/src/utils/use_derive_signal.rs @@ -0,0 +1,18 @@ +macro_rules! use_derive_signal { + ( + $(#[$outer:meta])* + $name:ident <$inner_signal_type:tt $(< $( $inner_type_param:tt ),+ >)? $(, $( $type_param:tt $( : $first_bound:tt $(+ $rest_bound:tt)* )? ),+ )? > -> $return_type:tt + $($body:tt)+ + ) => { + $(#[$outer])* + pub fn $name(cx: Scope, value: V) -> Signal<$return_type> + where + V: Into)?>> $(, $( $type_param $( : $first_bound $(+ $rest_bound)* )? ),+ )? + { + let value = value.into(); + Signal::derive(cx, move || value.with($($body)+)) + } + }; +} + +pub(crate) use use_derive_signal;