mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-02-02 10:54:15 -05:00
added use_min and use_max
This commit is contained in:
parent
b84f84d6dc
commit
e9a29fc89a
12 changed files with 146 additions and 6 deletions
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
#### New Functions
|
#### New Functions
|
||||||
- `use_floor`
|
- `use_floor`
|
||||||
|
- `use_max`
|
||||||
|
- `use_min`
|
||||||
|
|
||||||
#### Other Changes
|
#### Other Changes
|
||||||
- New feature: `math` that has to be activated in order to use the math functions.
|
- New feature: `math` that has to be activated in order to use the math functions.
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "leptos-use"
|
name = "leptos-use"
|
||||||
version = "0.1.4"
|
version = "0.1.5"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Marc-Stefan Cassola"]
|
authors = ["Marc-Stefan Cassola"]
|
||||||
categories = ["gui", "web-programming"]
|
categories = ["gui", "web-programming"]
|
||||||
|
|
|
@ -28,6 +28,8 @@
|
||||||
- [use_supported](utilities/use_supported.md)
|
- [use_supported](utilities/use_supported.md)
|
||||||
- [use_throttle_fn](utilities/use_throttle_fn.md)
|
- [use_throttle_fn](utilities/use_throttle_fn.md)
|
||||||
|
|
||||||
# Math
|
# @Math
|
||||||
|
|
||||||
- [use_floor](math/use_floor.md)
|
- [use_floor](math/use_floor.md)
|
||||||
|
- [use_max](math/use_max.md)
|
||||||
|
- [use_min](math/use_min.md)
|
|
@ -42,12 +42,19 @@ def main():
|
||||||
initial_doc_finished = False
|
initial_doc_finished = False
|
||||||
|
|
||||||
for line in f.readlines():
|
for line in f.readlines():
|
||||||
|
stripped_line = line.strip()
|
||||||
|
|
||||||
if initial_doc_finished:
|
if initial_doc_finished:
|
||||||
process_further_line(line, types, module)
|
process_further_line(line, types, module)
|
||||||
|
|
||||||
elif line.startswith("///"):
|
elif stripped_line.startswith("///") or stripped_line.startswith("#[doc ="):
|
||||||
doc_comment_started = True
|
doc_comment_started = True
|
||||||
line = line.strip().replace("/// ", "").replace("///", "")
|
|
||||||
|
if line.startswith("#[doc"):
|
||||||
|
line = stripped_line.replace("#[doc = \" ", "").replace("#[doc = \"", "")[:-2]
|
||||||
|
else:
|
||||||
|
line = stripped_line.replace("/// ", "").replace("///", "")
|
||||||
|
|
||||||
if "```" in line:
|
if "```" in line:
|
||||||
if not in_code_block:
|
if not in_code_block:
|
||||||
line = line.replace("```", "```rust,ignore")
|
line = line.replace("```", "```rust,ignore")
|
||||||
|
@ -60,6 +67,9 @@ def main():
|
||||||
elif doc_comment_started:
|
elif doc_comment_started:
|
||||||
initial_doc_finished = True
|
initial_doc_finished = True
|
||||||
|
|
||||||
|
if feature is not None:
|
||||||
|
append_feature_paragraph(feature)
|
||||||
|
|
||||||
add_types_paragraph(types)
|
add_types_paragraph(types)
|
||||||
add_source_paragraph(name, module)
|
add_source_paragraph(name, module)
|
||||||
|
|
||||||
|
@ -119,6 +129,11 @@ def process_further_line(line, types, module=None):
|
||||||
append_type(line, "struct", types, module)
|
append_type(line, "struct", types, module)
|
||||||
|
|
||||||
|
|
||||||
|
def append_feature_paragraph(feature):
|
||||||
|
print(f"""## Feature
|
||||||
|
> This function is only available if the crate feature **`{feature}`** is enabled""")
|
||||||
|
|
||||||
|
|
||||||
def append_type(line, ty, types, module=None):
|
def append_type(line, ty, types, module=None):
|
||||||
start_index = len(f"pub {ty} ")
|
start_index = len(f"pub {ty} ")
|
||||||
m = re.search(ident_pattern, line[start_index:])
|
m = re.search(ident_pattern, line[start_index:])
|
||||||
|
|
3
docs/book/src/math/use_max.md
Normal file
3
docs/book/src/math/use_max.md
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# use_max
|
||||||
|
|
||||||
|
<!-- cmdrun python3 ../extract_doc_comment.py math/use_max math -->
|
3
docs/book/src/math/use_min.md
Normal file
3
docs/book/src/math/use_min.md
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# use_min
|
||||||
|
|
||||||
|
<!-- cmdrun python3 ../extract_doc_comment.py math/use_min math -->
|
|
@ -1,3 +1,4 @@
|
||||||
|
#![feature(doc_cfg)]
|
||||||
//! Collection of essential Leptos utilities inspired by SolidJS USE / VueUse
|
//! Collection of essential Leptos utilities inspired by SolidJS USE / VueUse
|
||||||
|
|
||||||
pub mod core;
|
pub mod core;
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
|
mod shared;
|
||||||
mod use_floor;
|
mod use_floor;
|
||||||
|
mod use_max;
|
||||||
|
mod use_min;
|
||||||
|
|
||||||
pub use use_floor::*;
|
pub use use_floor::*;
|
||||||
|
pub use use_max::*;
|
||||||
|
pub use use_min::*;
|
||||||
|
|
42
src/math/shared.rs
Normal file
42
src/math/shared.rs
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
macro_rules! use_partial_cmp {
|
||||||
|
($(#[$outer:meta])*
|
||||||
|
$fn_name:ident,
|
||||||
|
$ord:pat
|
||||||
|
) => {
|
||||||
|
$(#[$outer])*
|
||||||
|
pub fn $fn_name<C, S, N>(cx: Scope, container: S) -> Signal<Option<N>>
|
||||||
|
where
|
||||||
|
S: Into<MaybeSignal<C>>,
|
||||||
|
C: 'static,
|
||||||
|
for<'a> &'a C: IntoIterator<Item = &'a N>,
|
||||||
|
N: PartialOrd + Clone,
|
||||||
|
{
|
||||||
|
let container = container.into();
|
||||||
|
|
||||||
|
create_memo(cx, move |_| {
|
||||||
|
container.with(|container| {
|
||||||
|
if container.into_iter().count() == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
container
|
||||||
|
.into_iter()
|
||||||
|
.fold(None, |acc, e| match acc {
|
||||||
|
Some(acc) => match N::partial_cmp(acc, e) {
|
||||||
|
Some($ord) => Some(e),
|
||||||
|
_ => Some(acc),
|
||||||
|
},
|
||||||
|
None => match N::partial_cmp(e, e) {
|
||||||
|
None => None,
|
||||||
|
_ => Some(e),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.cloned()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) use use_partial_cmp;
|
|
@ -7,7 +7,7 @@ use num::Float;
|
||||||
///
|
///
|
||||||
/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_floor)
|
/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_floor)
|
||||||
///
|
///
|
||||||
/// ### Usage
|
/// ## Usage
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use leptos::*;
|
/// # use leptos::*;
|
||||||
|
@ -17,9 +17,12 @@ use num::Float;
|
||||||
/// # fn Demo(cx: Scope) -> impl IntoView {
|
/// # fn Demo(cx: Scope) -> impl IntoView {
|
||||||
/// let (value, set_value) = create_signal(cx, 45.95);
|
/// let (value, set_value) = create_signal(cx, 45.95);
|
||||||
/// let result: Signal<f64> = use_floor(cx, value); // 45
|
/// let result: Signal<f64> = use_floor(cx, value); // 45
|
||||||
|
/// #
|
||||||
|
/// # assert_eq!(result.get(), 45.0);
|
||||||
/// # view! { cx, }
|
/// # view! { cx, }
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
|
#[doc(cfg(feature = "math"))]
|
||||||
pub fn use_floor<S, N>(cx: Scope, x: S) -> Signal<N>
|
pub fn use_floor<S, N>(cx: Scope, x: S) -> Signal<N>
|
||||||
where
|
where
|
||||||
S: Into<MaybeSignal<N>>,
|
S: Into<MaybeSignal<N>>,
|
||||||
|
|
32
src/math/use_max.rs
Normal file
32
src/math/use_max.rs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
use crate::math::shared::use_partial_cmp;
|
||||||
|
use leptos::*;
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
|
use_partial_cmp!(
|
||||||
|
/// Reactive `max()`.
|
||||||
|
///
|
||||||
|
/// Works with any container that implements `IntoIterator` (`Vec`, `HashSet`, ...)
|
||||||
|
/// with any elements that implement `PartialOrd` and `Clone` (floats, ints, strings, ...).
|
||||||
|
///
|
||||||
|
/// If the container is empty or only contains non comparable values like `NaN`, it returns `None`.
|
||||||
|
/// Otherwise it returns the `Some(<largest value>)` in the container.
|
||||||
|
///
|
||||||
|
/// ## Usage
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use leptos::*;
|
||||||
|
/// # use leptos_use::math::use_max;
|
||||||
|
/// #
|
||||||
|
/// # #[component]
|
||||||
|
/// # fn Demo(cx: Scope) -> impl IntoView {
|
||||||
|
/// let (values, set_values) = create_signal(cx, vec![1.0, 2.0, 3.0, f32::NAN, 4.0, 5.0]);
|
||||||
|
/// let result = use_max::<Vec<f32>, _, _>(cx, values); // Some(5.0)
|
||||||
|
/// #
|
||||||
|
/// # assert_eq!(result.get(), Some(5.0));
|
||||||
|
/// # view! { cx, }
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
|
#[doc(cfg(feature = "math"))]
|
||||||
|
use_max,
|
||||||
|
Ordering::Less
|
||||||
|
);
|
32
src/math/use_min.rs
Normal file
32
src/math/use_min.rs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
use crate::math::shared::use_partial_cmp;
|
||||||
|
use leptos::*;
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
|
use_partial_cmp!(
|
||||||
|
/// Reactive `min()`.
|
||||||
|
///
|
||||||
|
/// Works with any container that implements `IntoIterator` (`Vec`, `HashSet`, ...)
|
||||||
|
/// with any elements that implement `PartialOrd` and `Clone` (floats, ints, strings, ...).
|
||||||
|
///
|
||||||
|
/// If the container is empty or only contains non comparable values like `NaN`, it returns `None`.
|
||||||
|
/// Otherwise it returns the `Some(<smallest value>)` in the container.
|
||||||
|
///
|
||||||
|
/// ## Usage
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use leptos::*;
|
||||||
|
/// # use leptos_use::math::use_min;
|
||||||
|
/// #
|
||||||
|
/// # #[component]
|
||||||
|
/// # fn Demo(cx: Scope) -> impl IntoView {
|
||||||
|
/// let (values, set_values) = create_signal(cx, vec![1.0, 2.0, 3.0, f32::NAN, 4.0, 5.0]);
|
||||||
|
/// let result = use_min::<Vec<f32>, _, _>(cx, values); // Some(1.0)
|
||||||
|
/// #
|
||||||
|
/// # assert_eq!(result.get(), Some(1.0));
|
||||||
|
/// # view! { cx, }
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
|
#[doc(cfg(feature = "math"))]
|
||||||
|
use_min,
|
||||||
|
Ordering::Greater
|
||||||
|
);
|
Loading…
Add table
Reference in a new issue