Skip to content

Convenient Tools

This encapsulation method is mounted globally, making it ready to use out of the box through global injection.

Global Injection

typescript
import { globalRegister } from '@hy-app/ui';
import { createSSRApp } from 'vue';

export function createApp() {
    const app = createSSRApp(App);
    app.use(globalRegister);
    return { app };
}

Function List

$hy.addUnit(value, unit?) => string

Add a unit to a number, globally accessible for convenient use.

Parameters

Parameter NameTypeRequiredDefaultDescription
valuestring | numberYes-The value to add the unit to
unitstringNopxThe unit name to add

Return Value

TypeDescription
stringThe string with the added unit

Example

html
<template>
    <view :style="{ height: $hy.addUnit(10) }"></view>
</template>

$hy.random(min, max) => number

Generate a random integer within a specified range, globally accessible for convenient use.

Parameters

Parameter NameTypeRequiredDefaultDescription
minstring | numberYes-Minimum value
maxstring | numberYes-Maximum value

Return Value

TypeDescription
numberRandom number

Example

html
<template>
    <view>{{$hy.random(1, 10)}}</view>
</template>

$hy.formatTime(timestamp, fmt?) => string

Format a timestamp into a specified format, globally accessible for convenient use.

Parameters

Parameter NameTypeRequiredDefaultDescription
timestampstring | numberYes-Timestamp
fmtstringNoyyyy-MM-ddTime format

Return Value

TypeDescription
stringThe formatted time string

Example

html
<template>
    <view>{{$hy.formatTime(1702051200000)}}</view>
</template>

$hy.bytesToSize(bytes) => string

Convert byte size to a readable unit, globally accessible for convenient use.

Parameters

Parameter NameTypeRequiredDefaultDescription
bytesnumberYes-Byte size

Return Value

TypeDescription
stringThe converted readable unit string

Example

html
<template>
    <view>{{$hy.bytesToSize(10000)}}</view>
</template>