Skip to content

Others

import {...} from '@kaiverse/k/utils'

Primitives

classNames

Merge classname(s).

let condition1 = true,
condition2 = true
classNames('flex', condition1 && 'flex-col gap-4', condition2 ? 'p-4' : 'p-2') // 'flex flex-col gap-4 p-4'
condition1 = false
classNames('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]) // 5
clamp(16, [10, 15]) // 15
clamp(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) => 0
digitalRoot(1) => 1
digitalRoot(86) => 5
digitalRoot(999) => 9
digitalRoot(-999) => 9
digitalRoot(987654321987654321n) => 9
digitalRoot(-987654321987654321n) => 9
digitalRoot(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 a array.sort instead.
  • symbol is not supported.
  • Sort descending? Use sortMixedArrAsc(...).reverse().

Type definition

function sortMixedArrAsc(inputArr: SortableMixedArr, mutate?: boolean): SortableMixedArr
ParameterTypeDefaultDescription
inputArrSortableMixedArrRequireda mixed typed array.
mutatebooleanfalseShould 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
ParameterTypeDefaultDescription
objTRequiredThe object to remove properties from.
keys(keyof T)[]RequiredKey list to remove from the object.