added is_none and is_some

This commit is contained in:
Maccesch 2023-07-16 14:00:12 +01:00
parent 6897c3d1f1
commit 67bdde2960
10 changed files with 90 additions and 0 deletions

View file

@ -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 🛠

View file

@ -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)

View file

@ -0,0 +1,3 @@
# is_none
<!-- cmdrun python3 ../extract_doc_comment.py is_none -->

View file

@ -0,0 +1,3 @@
# is_some
<!-- cmdrun python3 ../extract_doc_comment.py is_some -->

View file

@ -2,6 +2,8 @@
resolver = "2"
members = [
"is_none",
"is_some",
"on_click_outside",
"use_abs",
"use_active_element",

27
src/is_none.rs Normal file
View file

@ -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<Option<T>, T: 'static> -> bool
|value| value.is_none()
);

27
src/is_some.rs Normal file
View file

@ -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<Option<T>, T: 'static> -> bool
|value| value.is_some()
);

View file

@ -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::*;

View file

@ -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::*;

View file

@ -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<V $(, $( $type_param ),* )? >(cx: Scope, value: V) -> Signal<$return_type>
where
V: Into<MaybeSignal<$inner_signal_type $(< $( $inner_type_param ),+ >)?>> $(, $( $type_param $( : $first_bound $(+ $rest_bound)* )? ),+ )?
{
let value = value.into();
Signal::derive(cx, move || value.with($($body)+))
}
};
}
pub(crate) use use_derive_signal;