CoreUtils

Last Updated on : 2023-10-12 08:00:24download

This topic describes the utility methods related to core settings.

Get a string with the required length

API operation name

toFixed

API description

Extract a substring with the required length that starts from the end of a specified string. If the string length is less than the required one, one or more leading zeros are added to the string to reach the required length.

Request parameter

Name Data type Description Required
str string String Yes
num number The length of the returned substring. Yes

Return parameter

Name Data type Description
result string The returned substring of the required length.

Sample request

import { Utils } from "tuya-panel-kit"; const { toFixed } = Utils.CoreUtils; toFixed(str, num); // Example: toFixed("111", 5); toFixed("3456111", 5);

Sample response

'00111' '56111'

Pad or get a specified string

API operation name

toFilled

API description

If the length of a specified string is less than the required length, pad the string with one or more leading zeros to the required length. If the string length is more than the required one, return the string.

Request parameter

Name Data type Description Required
str string String Yes
num number The length of the returned substring. Yes

Return parameter

Name Data type Description
result string The returned substring of the required length.

Sample request

import { Utils } from "tuya-panel-kit"; const { toFilled } = Utils.CoreUtils; toFilled(str, num); // Example: toFilled("111", 5); toFilled("3456111", 5);

Sample response

'00111' '3456111'

Convert a string to an array

API operation name

partition

API description

Convert a string to an array in which each element is a substring of the specified string. The substring length is specified.

Request parameter

Name Data type Description Required
str string String Yes
num number The length of the returned substring. Yes

Return parameter

Name Data type Description
result str[] The array in which each element is a substring of the specified string. The length of each substring is specified by num. If the actual length of a substring is less than num, the actual length is used.

Sample request

import { Utils } from "tuya-panel-kit"; const { partition } = Utils.CoreUtils; partition(str, num); // Example: partition("1234567", 3);

Sample response

['123', '456', '7']

Other methods

Method name Request parameter type Return parameter type Description
isObject any boolean Indicate whether the specified value is an object.
isArray any boolean Indicate whether the specified value is an array.
isDate any boolean Indicate whether the specified value is a date.
isRegExp any boolean Indicate whether the specified value is a regular expression.
isBoolean any boolean Indicate whether the specified value is a Boolean value.
isNumerical any boolean Indicate whether the specified value is a number.