From 50345a2befad2f6fdb10febc3f48f931023c3e9c Mon Sep 17 00:00:00 2001 From: Maccesch Date: Mon, 2 Oct 2023 21:05:20 +0100 Subject: [PATCH] added use_sorted --- .idea/leptos-use.iml | 1 + CHANGELOG.md | 6 + Cargo.toml | 2 +- docs/book/src/SUMMARY.md | 5 +- docs/book/src/iterable/use_sorted.md | 3 + examples/Cargo.toml | 1 + examples/use_sorted/Cargo.toml | 16 + examples/use_sorted/README.md | 23 ++ examples/use_sorted/Trunk.toml | 2 + examples/use_sorted/index.html | 7 + examples/use_sorted/input.css | 3 + examples/use_sorted/rust-toolchain.toml | 2 + examples/use_sorted/src/main.rs | 44 +++ examples/use_sorted/style/output.css | 289 ++++++++++++++++++ examples/use_sorted/tailwind.config.js | 15 + .../server/cert.crt | 24 ++ .../server/cert.key | 28 ++ src/lib.rs | 2 + src/use_sorted.rs | 125 ++++++++ 19 files changed, 596 insertions(+), 2 deletions(-) create mode 100644 docs/book/src/iterable/use_sorted.md create mode 100644 examples/use_sorted/Cargo.toml create mode 100644 examples/use_sorted/README.md create mode 100644 examples/use_sorted/Trunk.toml create mode 100644 examples/use_sorted/index.html create mode 100644 examples/use_sorted/input.css create mode 100644 examples/use_sorted/rust-toolchain.toml create mode 100644 examples/use_sorted/src/main.rs create mode 100644 examples/use_sorted/style/output.css create mode 100644 examples/use_sorted/tailwind.config.js create mode 100644 examples/use_webtransport_with_server/server/cert.crt create mode 100644 examples/use_webtransport_with_server/server/cert.key create mode 100644 src/use_sorted.rs diff --git a/.idea/leptos-use.iml b/.idea/leptos-use.iml index ad25f2d..df1b62f 100644 --- a/.idea/leptos-use.iml +++ b/.idea/leptos-use.iml @@ -56,6 +56,7 @@ + diff --git a/CHANGELOG.md b/CHANGELOG.md index c49cdcf..65d75ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.7.1] - 2023-10-02 + +### New Function 🚀 + +- `use_sorted` + ## [0.7.0] - 2023-09-30 ### New Functions 🚀 diff --git a/Cargo.toml b/Cargo.toml index 23ca4bb..2747c15 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "leptos-use" -version = "0.7.0" +version = "0.7.1" edition = "2021" authors = ["Marc-Stefan Cassola"] categories = ["gui", "web-programming"] diff --git a/docs/book/src/SUMMARY.md b/docs/book/src/SUMMARY.md index cd0e45c..dbb23c8 100644 --- a/docs/book/src/SUMMARY.md +++ b/docs/book/src/SUMMARY.md @@ -72,8 +72,11 @@ - [signal_debounced](reactivity/signal_debounced.md) - [signal_throttled](reactivity/signal_throttled.md) -# Utilities +# Iterable +- [use_sorted](iterable/use_sorted.md) + +# Utilities - [is_err](utilities/is_err.md) - [is_none](utilities/is_none.md) - [is_ok](utilities/is_ok.md) diff --git a/docs/book/src/iterable/use_sorted.md b/docs/book/src/iterable/use_sorted.md new file mode 100644 index 0000000..ad6b740 --- /dev/null +++ b/docs/book/src/iterable/use_sorted.md @@ -0,0 +1,3 @@ +# use_sorted + + diff --git a/examples/Cargo.toml b/examples/Cargo.toml index f7c5a5d..930d976 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -35,6 +35,7 @@ members = [ "use_resize_observer", "use_round", "use_scroll", + "use_sorted", "use_storage", "use_throttle_fn", "use_timestamp", diff --git a/examples/use_sorted/Cargo.toml b/examples/use_sorted/Cargo.toml new file mode 100644 index 0000000..6b5a699 --- /dev/null +++ b/examples/use_sorted/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "use_sorted" +version = "0.1.0" +edition = "2021" + +[dependencies] +leptos = { version = "0.5", features = ["nightly", "csr"] } +console_error_panic_hook = "0.1" +console_log = "1" +log = "0.4" +leptos-use = { path = "../..", features = ["docs"] } +web-sys = "0.3" + +[dev-dependencies] +wasm-bindgen = "0.2" +wasm-bindgen-test = "0.3.0" diff --git a/examples/use_sorted/README.md b/examples/use_sorted/README.md new file mode 100644 index 0000000..25443d9 --- /dev/null +++ b/examples/use_sorted/README.md @@ -0,0 +1,23 @@ +A simple example for `use_sorted`. + +If you don't have it installed already, install [Trunk](https://trunkrs.dev/) and [Tailwind](https://tailwindcss.com/docs/installation) +as well as the nightly toolchain for Rust and the wasm32-unknown-unknown target: + +```bash +cargo install trunk +npm install -D tailwindcss @tailwindcss/forms +rustup toolchain install nightly +rustup target add wasm32-unknown-unknown +``` + +Then, open two terminals. In the first one, run: + +``` +npx tailwindcss -i ./input.css -o ./style/output.css --watch +``` + +In the second one, run: + +```bash +trunk serve --open +``` \ No newline at end of file diff --git a/examples/use_sorted/Trunk.toml b/examples/use_sorted/Trunk.toml new file mode 100644 index 0000000..3e4be08 --- /dev/null +++ b/examples/use_sorted/Trunk.toml @@ -0,0 +1,2 @@ +[build] +public_url = "/demo/" \ No newline at end of file diff --git a/examples/use_sorted/index.html b/examples/use_sorted/index.html new file mode 100644 index 0000000..ae249a6 --- /dev/null +++ b/examples/use_sorted/index.html @@ -0,0 +1,7 @@ + + + + + + + diff --git a/examples/use_sorted/input.css b/examples/use_sorted/input.css new file mode 100644 index 0000000..bd6213e --- /dev/null +++ b/examples/use_sorted/input.css @@ -0,0 +1,3 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; \ No newline at end of file diff --git a/examples/use_sorted/rust-toolchain.toml b/examples/use_sorted/rust-toolchain.toml new file mode 100644 index 0000000..271800c --- /dev/null +++ b/examples/use_sorted/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly" \ No newline at end of file diff --git a/examples/use_sorted/src/main.rs b/examples/use_sorted/src/main.rs new file mode 100644 index 0000000..4ffb11d --- /dev/null +++ b/examples/use_sorted/src/main.rs @@ -0,0 +1,44 @@ +use leptos::*; +use leptos_use::docs::demo_or_body; +use leptos_use::use_sorted; + +fn string_list(list: &[i32]) -> String { + list.into_iter() + .map(i32::to_string) + .collect::>() + .join(",") +} + +#[component] +fn Demo() -> impl IntoView { + let (list, set_list) = create_signal::>(vec![4, 2, 67, 34, 76, 22, 2, 4, 65, 23]); + + let sorted: Signal> = use_sorted(list); + + let on_input = move |evt| { + set_list.update(|list| { + *list = event_target_value(&evt) + .split(",") + .map(|n| n.parse::().unwrap_or(0)) + .collect::>() + }); + }; + + let input_text = move || string_list(&list()); + let sorted_text = move || string_list(&sorted()); + + view! { +
Input:
+ +

Output: {sorted_text}

+ } +} + +fn main() { + _ = console_log::init_with_level(log::Level::Debug); + console_error_panic_hook::set_once(); + + mount_to(demo_or_body(), || { + view! { } + }) +} diff --git a/examples/use_sorted/style/output.css b/examples/use_sorted/style/output.css new file mode 100644 index 0000000..ab5191f --- /dev/null +++ b/examples/use_sorted/style/output.css @@ -0,0 +1,289 @@ +[type='text'],[type='email'],[type='url'],[type='password'],[type='number'],[type='date'],[type='datetime-local'],[type='month'],[type='search'],[type='tel'],[type='time'],[type='week'],[multiple],textarea,select { + -webkit-appearance: none; + -moz-appearance: none; + appearance: none; + background-color: #fff; + border-color: #6b7280; + border-width: 1px; + border-radius: 0px; + padding-top: 0.5rem; + padding-right: 0.75rem; + padding-bottom: 0.5rem; + padding-left: 0.75rem; + font-size: 1rem; + line-height: 1.5rem; + --tw-shadow: 0 0 #0000; +} + +[type='text']:focus, [type='email']:focus, [type='url']:focus, [type='password']:focus, [type='number']:focus, [type='date']:focus, [type='datetime-local']:focus, [type='month']:focus, [type='search']:focus, [type='tel']:focus, [type='time']:focus, [type='week']:focus, [multiple]:focus, textarea:focus, select:focus { + outline: 2px solid transparent; + outline-offset: 2px; + --tw-ring-inset: var(--tw-empty,/*!*/ /*!*/); + --tw-ring-offset-width: 0px; + --tw-ring-offset-color: #fff; + --tw-ring-color: #2563eb; + --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color); + --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(1px + var(--tw-ring-offset-width)) var(--tw-ring-color); + box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow); + border-color: #2563eb; +} + +input::-moz-placeholder, textarea::-moz-placeholder { + color: #6b7280; + opacity: 1; +} + +input::placeholder,textarea::placeholder { + color: #6b7280; + opacity: 1; +} + +::-webkit-datetime-edit-fields-wrapper { + padding: 0; +} + +::-webkit-date-and-time-value { + min-height: 1.5em; +} + +::-webkit-datetime-edit,::-webkit-datetime-edit-year-field,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute-field,::-webkit-datetime-edit-second-field,::-webkit-datetime-edit-millisecond-field,::-webkit-datetime-edit-meridiem-field { + padding-top: 0; + padding-bottom: 0; +} + +select { + background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e"); + background-position: right 0.5rem center; + background-repeat: no-repeat; + background-size: 1.5em 1.5em; + padding-right: 2.5rem; + -webkit-print-color-adjust: exact; + print-color-adjust: exact; +} + +[multiple] { + background-image: initial; + background-position: initial; + background-repeat: unset; + background-size: initial; + padding-right: 0.75rem; + -webkit-print-color-adjust: unset; + print-color-adjust: unset; +} + +[type='checkbox'],[type='radio'] { + -webkit-appearance: none; + -moz-appearance: none; + appearance: none; + padding: 0; + -webkit-print-color-adjust: exact; + print-color-adjust: exact; + display: inline-block; + vertical-align: middle; + background-origin: border-box; + -webkit-user-select: none; + -moz-user-select: none; + user-select: none; + flex-shrink: 0; + height: 1rem; + width: 1rem; + color: #2563eb; + background-color: #fff; + border-color: #6b7280; + border-width: 1px; + --tw-shadow: 0 0 #0000; +} + +[type='checkbox'] { + border-radius: 0px; +} + +[type='radio'] { + border-radius: 100%; +} + +[type='checkbox']:focus,[type='radio']:focus { + outline: 2px solid transparent; + outline-offset: 2px; + --tw-ring-inset: var(--tw-empty,/*!*/ /*!*/); + --tw-ring-offset-width: 2px; + --tw-ring-offset-color: #fff; + --tw-ring-color: #2563eb; + --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color); + --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(2px + var(--tw-ring-offset-width)) var(--tw-ring-color); + box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow); +} + +[type='checkbox']:checked,[type='radio']:checked { + border-color: transparent; + background-color: currentColor; + background-size: 100% 100%; + background-position: center; + background-repeat: no-repeat; +} + +[type='checkbox']:checked { + background-image: url("data:image/svg+xml,%3csvg viewBox='0 0 16 16' fill='white' xmlns='http://www.w3.org/2000/svg'%3e%3cpath d='M12.207 4.793a1 1 0 010 1.414l-5 5a1 1 0 01-1.414 0l-2-2a1 1 0 011.414-1.414L6.5 9.086l4.293-4.293a1 1 0 011.414 0z'/%3e%3c/svg%3e"); +} + +[type='radio']:checked { + background-image: url("data:image/svg+xml,%3csvg viewBox='0 0 16 16' fill='white' xmlns='http://www.w3.org/2000/svg'%3e%3ccircle cx='8' cy='8' r='3'/%3e%3c/svg%3e"); +} + +[type='checkbox']:checked:hover,[type='checkbox']:checked:focus,[type='radio']:checked:hover,[type='radio']:checked:focus { + border-color: transparent; + background-color: currentColor; +} + +[type='checkbox']:indeterminate { + background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 16 16'%3e%3cpath stroke='white' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M4 8h8'/%3e%3c/svg%3e"); + border-color: transparent; + background-color: currentColor; + background-size: 100% 100%; + background-position: center; + background-repeat: no-repeat; +} + +[type='checkbox']:indeterminate:hover,[type='checkbox']:indeterminate:focus { + border-color: transparent; + background-color: currentColor; +} + +[type='file'] { + background: unset; + border-color: inherit; + border-width: 0; + border-radius: 0; + padding: 0; + font-size: unset; + line-height: inherit; +} + +[type='file']:focus { + outline: 1px solid ButtonText; + outline: 1px auto -webkit-focus-ring-color; +} + +*, ::before, ::after { + --tw-border-spacing-x: 0; + --tw-border-spacing-y: 0; + --tw-translate-x: 0; + --tw-translate-y: 0; + --tw-rotate: 0; + --tw-skew-x: 0; + --tw-skew-y: 0; + --tw-scale-x: 1; + --tw-scale-y: 1; + --tw-pan-x: ; + --tw-pan-y: ; + --tw-pinch-zoom: ; + --tw-scroll-snap-strictness: proximity; + --tw-gradient-from-position: ; + --tw-gradient-via-position: ; + --tw-gradient-to-position: ; + --tw-ordinal: ; + --tw-slashed-zero: ; + --tw-numeric-figure: ; + --tw-numeric-spacing: ; + --tw-numeric-fraction: ; + --tw-ring-inset: ; + --tw-ring-offset-width: 0px; + --tw-ring-offset-color: #fff; + --tw-ring-color: rgb(59 130 246 / 0.5); + --tw-ring-offset-shadow: 0 0 #0000; + --tw-ring-shadow: 0 0 #0000; + --tw-shadow: 0 0 #0000; + --tw-shadow-colored: 0 0 #0000; + --tw-blur: ; + --tw-brightness: ; + --tw-contrast: ; + --tw-grayscale: ; + --tw-hue-rotate: ; + --tw-invert: ; + --tw-saturate: ; + --tw-sepia: ; + --tw-drop-shadow: ; + --tw-backdrop-blur: ; + --tw-backdrop-brightness: ; + --tw-backdrop-contrast: ; + --tw-backdrop-grayscale: ; + --tw-backdrop-hue-rotate: ; + --tw-backdrop-invert: ; + --tw-backdrop-opacity: ; + --tw-backdrop-saturate: ; + --tw-backdrop-sepia: ; +} + +::backdrop { + --tw-border-spacing-x: 0; + --tw-border-spacing-y: 0; + --tw-translate-x: 0; + --tw-translate-y: 0; + --tw-rotate: 0; + --tw-skew-x: 0; + --tw-skew-y: 0; + --tw-scale-x: 1; + --tw-scale-y: 1; + --tw-pan-x: ; + --tw-pan-y: ; + --tw-pinch-zoom: ; + --tw-scroll-snap-strictness: proximity; + --tw-gradient-from-position: ; + --tw-gradient-via-position: ; + --tw-gradient-to-position: ; + --tw-ordinal: ; + --tw-slashed-zero: ; + --tw-numeric-figure: ; + --tw-numeric-spacing: ; + --tw-numeric-fraction: ; + --tw-ring-inset: ; + --tw-ring-offset-width: 0px; + --tw-ring-offset-color: #fff; + --tw-ring-color: rgb(59 130 246 / 0.5); + --tw-ring-offset-shadow: 0 0 #0000; + --tw-ring-shadow: 0 0 #0000; + --tw-shadow: 0 0 #0000; + --tw-shadow-colored: 0 0 #0000; + --tw-blur: ; + --tw-brightness: ; + --tw-contrast: ; + --tw-grayscale: ; + --tw-hue-rotate: ; + --tw-invert: ; + --tw-saturate: ; + --tw-sepia: ; + --tw-drop-shadow: ; + --tw-backdrop-blur: ; + --tw-backdrop-brightness: ; + --tw-backdrop-contrast: ; + --tw-backdrop-grayscale: ; + --tw-backdrop-hue-rotate: ; + --tw-backdrop-invert: ; + --tw-backdrop-opacity: ; + --tw-backdrop-saturate: ; + --tw-backdrop-sepia: ; +} + +.block { + display: block; +} + +.text-\[--brand-color\] { + color: var(--brand-color); +} + +.text-green-600 { + --tw-text-opacity: 1; + color: rgb(22 163 74 / var(--tw-text-opacity)); +} + +.opacity-75 { + opacity: 0.75; +} + +@media (prefers-color-scheme: dark) { + .dark\:text-green-500 { + --tw-text-opacity: 1; + color: rgb(34 197 94 / var(--tw-text-opacity)); + } +} \ No newline at end of file diff --git a/examples/use_sorted/tailwind.config.js b/examples/use_sorted/tailwind.config.js new file mode 100644 index 0000000..bc09f5e --- /dev/null +++ b/examples/use_sorted/tailwind.config.js @@ -0,0 +1,15 @@ +/** @type {import('tailwindcss').Config} */ +module.exports = { + content: { + files: ["*.html", "./src/**/*.rs", "../../src/docs/**/*.rs"], + }, + theme: { + extend: {}, + }, + corePlugins: { + preflight: false, + }, + plugins: [ + require('@tailwindcss/forms'), + ], +} \ No newline at end of file diff --git a/examples/use_webtransport_with_server/server/cert.crt b/examples/use_webtransport_with_server/server/cert.crt new file mode 100644 index 0000000..3d4b2c1 --- /dev/null +++ b/examples/use_webtransport_with_server/server/cert.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isg+MzV +MA0GCSqGSIb3DQEBCwUAA4IBAQCXBIKF8ylDhq80OxAe1EaRZKEwVKoVtoXEQLQy +0voiIx00AjfMjUC16+8rAhDDLL5PWi56zonH/2hCfJyVec/oF059MghzCPAALv1G +3koaU+4Tp9S17zLbmlExQN72LOGYs9hswWdrtqpWPeeLxzoYn+LMxANVzhBMz66y +KwnwP/BFyeVKYIVTOZfH67j3PYCLizHCOoPOh0m9/ub8QIgFzKhYl4tWKRzJBug1 +wGYoMIbBasU9N4UoeeWC4OP6YA9MDmpy5D2xFDM0DKW7qYP/KsQSAgPbKKd0a//S +xKv6DFpBeEhh6+d+MAHBYu+m7nTHeO90PCtVSohtTH7fSk0r +-----END CERTIFICATE----- diff --git a/examples/use_webtransport_with_server/server/cert.key b/examples/use_webtransport_with_server/server/cert.key new file mode 100644 index 0000000..84069cb --- /dev/null +++ b/examples/use_webtransport_with_server/server/cert.key @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC8OPfQam1y5lxO +dhnFJQsxCpTVyyn/niREzsSZ2mx/bV6zXqtGhQ7Fd+/EadsaBZvoXaUAbwaa/tii +WI4CWI+kVOuaq4aEA7/ikwY+wRZVrfn+M8hkSmWiLen2i6gOLcsiqA0UTcGWdbRd +/dOGczDc/2md0Dn7Y2ckMpgoenS09nAamHqqA5/aeqnRRdn1TkCpR2PGHv+sB5zw +0OYDPpNc9INPZNztdvltR9Y9/YiPFm9x222OGV/wLJhVk8ZZJYKVztHTJrJ25uc4 +fS45h1CHDBB0adgD6Fxr56xahPwh6OYPei6nQoS1EUWDugKYPsfi/+veNlvlcC/K +pdPPPHB3AgMBAAECggEADj4FSmjzNTGHJIy9MHS4HxLc5jyERgpSVj6LE9U6Rn4h +H1N3hFOHJZwIsYUNBjAMdw228Yx1JH9KJyaqQDUxUU73sPFvsUeTWnKjk1YK+Zq7 +gueqLySOAjKVNImmwsPmTg4H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azGZLTnj/RaJmJRb1w+ej1 +yNaquFcA6XZ3WLDYrrcvRA== +-----END PRIVATE KEY----- diff --git a/src/lib.rs b/src/lib.rs index 33a82f4..c3384c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,6 +54,7 @@ mod use_preferred_contrast; mod use_preferred_dark; mod use_raf_fn; mod use_scroll; +mod use_sorted; mod use_supported; mod use_throttle_fn; mod use_timestamp; @@ -102,6 +103,7 @@ pub use use_preferred_contrast::*; pub use use_preferred_dark::*; pub use use_raf_fn::*; pub use use_scroll::*; +pub use use_sorted::*; pub use use_supported::*; pub use use_throttle_fn::*; pub use use_timestamp::*; diff --git a/src/use_sorted.rs b/src/use_sorted.rs new file mode 100644 index 0000000..32ff806 --- /dev/null +++ b/src/use_sorted.rs @@ -0,0 +1,125 @@ +use leptos::*; +use std::cmp::Ordering; +use std::ops::DerefMut; + +/// Reactive sort of iterable +/// +/// ## Demo +/// +/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_sorted) +/// +/// ## Usage +/// +/// ``` +/// # use leptos::*; +/// # use leptos_use::use_sorted; +/// # +/// # #[component] +/// # fn Demo() -> impl IntoView { +/// let source = vec![10, 3, 5, 7, 2, 1, 8, 6, 9, 4]; +/// let sorted = use_sorted(source); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +/// # +/// # view! { } +/// # } +/// ``` +/// +/// You can also sort by key or with a compare function. +/// +/// ``` +/// # use leptos::*; +/// # use leptos_use::{use_sorted_by, use_sorted_by_key}; +/// # +/// #[derive(Clone, PartialEq)] +/// pub struct Person { +/// pub name: String, +/// pub age: u16, +/// } +/// +/// # #[component] +/// # fn Demo() -> impl IntoView { +/// let source = vec![ +/// Person { +/// name: "John".to_string(), +/// age: 40, +/// }, +/// Person { +/// name: "Jane".to_string(), +/// age: 20, +/// }, +/// Person { +/// name: "Joe".to_string(), +/// age: 30, +/// }, +/// Person { +/// name: "Jenny".to_string(), +/// age: 22, +/// }, +/// ]; +/// +/// // sort by key +/// let sorted = use_sorted_by_key( +/// source.clone(), +/// |person| person.age, +/// ); +/// +/// // sort with compare function +/// let sorted = use_sorted_by( +/// source, +/// |p1, p2| p1.age.cmp(&p2.age), +/// ); +/// # +/// # view! { } +/// # } +/// ``` +/// +/// Please note that these two ways of sorting are equivalent. +pub fn use_sorted(iterable: S) -> Signal +where + S: Into>, + T: Ord, + I: DerefMut + Clone + PartialEq, +{ + let iterable = iterable.into(); + + create_memo(move |_| { + let mut iterable = iterable.get(); + iterable.sort(); + iterable + }) + .into() +} + +/// Version of [`use_sorted`] with a compare function. +pub fn use_sorted_by(iterable: S, cmp_fn: F) -> Signal +where + S: Into>, + I: DerefMut + Clone + PartialEq, + F: FnMut(&T, &T) -> Ordering + Clone + 'static, +{ + let iterable = iterable.into(); + + create_memo(move |_| { + let mut iterable = iterable.get(); + iterable.sort_by(cmp_fn.clone()); + iterable + }) + .into() +} + +/// Version of [`use_sorted`] by key. +pub fn use_sorted_by_key(iterable: S, key_fn: F) -> Signal +where + S: Into>, + I: DerefMut + Clone + PartialEq, + K: Ord, + F: FnMut(&T) -> K + Clone + 'static, +{ + let iterable = iterable.into(); + + create_memo(move |_| { + let mut iterable = iterable.get(); + iterable.sort_by_key(key_fn.clone()); + iterable + }) + .into() +}