2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-10 02:43:44 +00:00
|
|
|
// This module is browser compatible.
|
2022-03-21 05:57:16 +00:00
|
|
|
|
2024-05-22 05:55:34 +00:00
|
|
|
/**
|
|
|
|
* Compare two values in ascending order using JavaScript's built in comparison
|
|
|
|
* operators.
|
|
|
|
*
|
|
|
|
* @example Comparing numbers
|
|
|
|
* ```ts
|
|
|
|
* import { ascend } from "@std/data-structures";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* assertEquals(ascend(1, 2), -1);
|
|
|
|
* assertEquals(ascend(2, 1), 1);
|
|
|
|
* assertEquals(ascend(1, 1), 0);
|
2024-05-22 05:55:34 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @typeparam T The type of the values being compared.
|
|
|
|
* @param a The left comparison value.
|
|
|
|
* @param b The right comparison value.
|
|
|
|
* @returns -1 if `a` is less than `b`, 0 if `a` is equal to `b`, and 1 if `a` is greater than `b`.
|
|
|
|
*/
|
2023-12-19 00:26:13 +00:00
|
|
|
export function ascend<T>(a: T, b: T): -1 | 0 | 1 {
|
2022-03-21 05:57:16 +00:00
|
|
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2024-05-22 05:55:34 +00:00
|
|
|
/**
|
|
|
|
* Compare two values in descending order using JavaScript's built in comparison
|
|
|
|
* operators.
|
|
|
|
*
|
|
|
|
* @example Comparing numbers
|
|
|
|
* ```ts
|
|
|
|
* import { descend } from "@std/data-structures";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* assertEquals(descend(1, 2), 1);
|
|
|
|
* assertEquals(descend(2, 1), -1);
|
|
|
|
* assertEquals(descend(1, 1), 0);
|
2024-05-22 05:55:34 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @typeparam T The type of the values being compared.
|
|
|
|
* @param a The left comparison value.
|
|
|
|
* @param b The right comparison value.
|
|
|
|
* @returns -1 if `a` is greater than `b`, 0 if `a` is equal to `b`, and 1 if `a` is less than `b`.
|
|
|
|
*/
|
2023-12-19 00:26:13 +00:00
|
|
|
export function descend<T>(a: T, b: T): -1 | 0 | 1 {
|
2022-03-21 05:57:16 +00:00
|
|
|
return a < b ? 1 : a > b ? -1 : 0;
|
|
|
|
}
|