Others
import {...} from '@kaiverse/k/utils'
Primitives
classNames
Merge classname(s).
let condition1 = true, condition2 = trueclassNames('flex', condition1 && 'flex-col gap-4', condition2 ? 'p-4' : 'p-2') // 'flex flex-col gap-4 p-4'
condition1 = falseclassNames('flex', condition1 && 'flex-col gap-4', condition2 ? 'p-4' : 'p-2') // 'flex p-4'
clamp
Clamp a number between a minimum and maximum value.
clamp(5, [10, 15]) // 5clamp(16, [10, 15]) // 15clamp(9, [10, 15]) // 10
exponential
Convert a number to exponential notation.
exponential(12.78915, 2) => "1.28e+1"exponential(75.396, 3) => "7.540e+1"exponential(75.396) => "7.5396e+1"exponential(75.396, null|0) => "8e+1"exponential(0.0075396) => "7.5396e-3"exponential(0.0075396, null|0) => "8e-3"exponential("sthNaN", any) => "NaN"
digitalRoot
Calculate the digital root of a number.
Example: digitalRoot(86) => 5
because 8 + 6 = 14
and 1 + 4 = 5
.
Type definition
function digitalRoot(input: number | bigint): number
digitalRoot(0) => 0digitalRoot(1) => 1digitalRoot(86) => 5digitalRoot(999) => 9digitalRoot(-999) => 9digitalRoot(987654321987654321n) => 9digitalRoot(-987654321987654321n) => 9digitalRoot(86.75) => 5 // floor to 86 before calculation
Array
sortMixedArrAsc
Mixed types array ascending sorting.
- This function supports all Primitive types, it aims to handle MIXED types of array elements sorting.
- Single type array sorting works fine too (eg:
number[]
,string[]
, etc..). But we should just write aarray.sort
instead. symbol
is not supported.- Sort descending? Use
sortMixedArrAsc(...).reverse()
.
Type definition
function sortMixedArrAsc(inputArr: SortableMixedArr, mutate?: boolean): SortableMixedArr
Parameter | Type | Default | Description |
---|---|---|---|
inputArr | SortableMixedArr | Required | a mixed typed array. |
mutate | boolean | false | Should the origin array be mutated? |
const inputArr = [3, undefined, 'rob', null, ' peter ', ['test'], false, 'a', 43, true, 0, null, 100, { a: 'obj' }, 1, -222, empty, false]sortMixedArrAsc(inputArr) // [-222, 0, 1, 3, 43, 100, "a", " peter ", "rob", true, false, false, ["test"], { "a": "obj" }, null, null, undefined, empty]
Object
objRemoveProperties
Remove multiple 1st level properties from an object without mutating the original object.
Type definition
function objRemoveProperties<T extends ObjectAny>(obj: T, keys: (keyof T)[]): T
Parameter | Type | Default | Description |
---|---|---|---|
obj | T | Required | The object to remove properties from. |
keys | (keyof T)[] | Required | Key list to remove from the object. |