Utilities
@ns/utils contains small helpers used inside the component library. They are also exported from @ns/ui, so your code can use them directly:
ts
import { debounce, isEmpty, addClass } from '@ns/ui'
// or
import { debounce, isEmpty, addClass } from '@ns/utils'
Type Checks
| Function | Description |
|---|---|
isString(val), isNumber(val), isBoolean(val), isFunction(val) | Check primitive types |
isArray(val) | Same as Array.isArray |
isObject(val) | Not null and typeof is object (arrays included) |
isEmpty(val) | true for null, undefined, empty strings, empty arrays and plain objects without properties; Map and Set are checked by size; 0, false, Date and other values return false |
Functions and Strings
| Function | Description |
|---|---|
debounce(fn, delay) | Debounce: when called repeatedly, runs once delay ms after the last call |
throttle(fn, delay) | Throttle: the first call runs immediately; the last call within an interval runs when it ends |
noop() | Empty function |
identity(val) | Returns its argument |
capitalize(str) | Uppercase the first letter: 'button' → 'Button' |
camelize(str) | Kebab-case to camelCase: 'date-picker' → 'datePicker' |
kebabCase(str) | camelCase to kebab-case: 'datePicker' → 'date-picker' |
ts
import { debounce } from '@ns/ui'
const search = debounce((keyword: string) => {
// Sends the request only after typing stops for 300 ms
}, 300)
Functions returned by debounce and throttle have a cancel() method; call it when the component unmounts to drop the pending call. When called as an object method, this is passed to the original function.
DOM
| Function | Description |
|---|---|
isClient | Constant: whether running in a browser |
inBrowser() | Same as above, as a function |
isDocumentVisible() | Whether the page is visible (not in a background tab) |
isWindow(val) | Whether the value is the window object |
addEventListener(el, event, handler, options?), removeEventListener(...) | Typed event binding; event names depend on the target (beforeunload on window, visibilitychange on document) |
getComputedStyle(el) | Get the computed style of an element |
getScrollTop(el), setScrollTop(el, value) | Read and write the scroll position; el can be an element or window |
getOffset(el) | top and left of an element relative to the document |
addClass(el, ...classes), removeClass(el, ...classes), hasClass(el, className), toggleClass(el, className, force?) | Class name helpers |
Types
ts
import type { ComponentSize, ComponentType, MaybeArray, Nullable } from '@ns/ui'
type ComponentSize = 'large' | 'default' | 'small'
type ComponentType = 'primary' | 'success' | 'warning' | 'danger' | 'info' | 'help' | 'default'
type MaybeArray<T> = T | T[]
type MaybeFunction<T> = T | (() => T)
type Nullable<T> = T | null
type Optional<T> = T | undefined