Utils

Last Updated on : 2024-06-05 03:14:35download

RatioUtils

RatioUtils provides a quick way to adapt to the screen aspect ratio.

Property name Description
hRatio Horizontal ratio of the screen, which is the width ratio of the current screen to ip6 screen.
vRatio Vertical ratio of the screen, which is the height ratio of the current screen to ip6 screen.
width, viewWidth Width of the current screen.
height Height of the current screen.
ratio Diagonal line ratio, which is the ratio of diagonal line length of the current screen to diagonal line length of ip6 screen.
convertX Use hRatio to convert the length.
convertY Use vRatio to convert the length.
convert Use ratio to convert the length.
isIos Whether it is an iOS system.
isIphoneX,iPhoneX Whether it belongs to the iPhoneX series (including iPhone XS and iPhone XS Max).
import { Utils } from "tuya-panel-kit"; const { convertX } = Utils.RatioUtils; // The width of a component on the design draft is 13 width = convertX(13); // The final width of the component on the current device

ColorUtils

ColorUtils is a tool for color conversion.

  • HSV to RGB conversion
/** * @param {Number} h - Hue, ranging from 0° to 360° * @param {Number} s - Saturation, ranging from 0% to 100% * @param {Number} v - Value, ranging from 0% to 100% * returns {Object} RGB color value */ hsvToRgb(0, 1, 1); // {r: 255, g: 0, b: 0}
  • RGB to HSV conversion
/** * @param {Number} r - red value, ranging from 0 to 255 * @param {Number} g - green value, ranging from 0 to 255 * @param {Number} b - blue value, ranging from 0 to 255 * returns {Object} hsv value */ rgbToHsv(255, 0, 0); // {h: 0, s: 1, v: 1} rgbToHsv(128, 1, 0); // {h: 0, s: 1, v: 0.5019607843137255}
  • HSL to RGB conversion
/** * @param {Number} h - Hue, ranging from 0° to 360° * @param {Number} s - Saturation, ranging from 0% to 100% * @param {Number} v - Lightness, ranging from 0% to 100% * returns {Object} RGB color value */ hslToRgb(0, 1, 0.5); // {r: 255, g: 0, b: 0} hslToRgb(120, 1, 0.75); // {r: 128, g: 255, b: 128} hslToRgb(240, 1, 0.25); // {r: 0, g: 0, b: 128}
  • 2.4 RGB to HSL conversion
/** * @param {Number} rr - red value, ranging from 0 to 255 * @param {Number} gg - green value, ranging from 0 to 255 * @param {Number} bb - blue value, ranging from 0 to 255 * returns {Object} HSL color value */ rgbToHsl(255, 0, 0); // {h: 0, s: 1, l: 0.5} rgbToHsl(128, 255, 128); // {h: 120, s: 1, l: 0.7509803921568627} rgbToHsl(0, 0, 128); // {h: 240, s: 1, l: 0.25098039215686274}

NumberUtils

Number related tools.

  • toFixedString
/** * @param {Number} num * @param {Number} Length of the return string * returns {String} */ toFixedString(111, 5); // '00111'
  • toFilledString
/** * @param {Number} num * @param {Number} Length of the return string * returns {String} */ toFixedString(111, 5); // '00111'
  • getBitValue
/** * @param {Number} num * @param {Number} idx idx is reverse, starting from 0 * returns {Number} */ // 17 = 10001(2) getBitValue(17, 0); // 1 getBitValue(17, 1); // 0 getBitValue(17, 4); // 1
  • setBitValueWithOne
/** * @param {Number} idx idx is reverse, starting from 0 * returns {Number} */ // 17 = 10001(2) setBitValueWithOne(17, 0); // 17, 10001 setBitValueWithOne(17, 1); // 19, 10010 setBitValueWithOne(17, 5); // 49, 110001
  • setBitValueWithZero
/** * @param {Number} idx idx is reverse, starting from 0 * returns {Number} */ // 17 = 10001(2) setBitValueWithZero(17, 0); // 16, 10000 setBitValueWithZero(17, 1); // 17, 10001 setBitValueWithZero(17, 4); // 1, 00001
  • bytesToHexString
/** * @param {Array} byte, byte is a number array with 8-bit integers. * returns {String} */ bytesToHexString([1, 2]); // '0102' bytesToHexString([23, 2]); // '1702'
  • numToByteNumbers
/** * @param {Number} The converted string, and each character is hexadecimal * returns {String} */ numToHexString(111); // '6f' numToHexString(15); // '0f'
  • numToHexString
/** * @param {Number} number * returns {String} */ numToByteNumbers(111); // [0, 111] numToHexString(1040001); // [15, 222, 129]
  • highLowToInt
/** * @param {Number} A high 8-bit number * @param {Number} A low 8-bit number * returns {Number} */ highLowToInt(11, 22); // 2838 highLowToInt(22, 11); // 5643
  • intToHighLow
/** * @param {Number} number * returns {Number} An array of numbers. The first is the high 8-bit number, and the second is the low 8-bit number */ highLowToInt(2838); // [11, 22] highLowToInt(5643); // [22, 11]
  • inMaxMin
/** * @param {Number} value * @param {Number} value * @param {Number} value * returns {Number} Returns the middle value of the three numbers */ inMaxMin(2838, 1, 233); // 233 inMaxMin(1, 2, 0); // 1
  • scaleNumber
/** * @param {Number} value A number with 10 times scale * @param {Number} value * returns {Number} */ scaleNumber(2, 10245); // 102.45
  • range
/** * @param {Number} start The initial value * @param {Number} end The end value * @param {Number} value Step length * returns (Array) returns an array from start to end with step length */ range(1, 10); // [1, 2, 3, 4, 5, 6, 7, 8, 9] range(1, 10, 2); // [1, 3, 5, 7, 9]
  • calcPosition
/** * @param {Number} value The original value * @param {Number} min The original minimum range * @param {Number} max The original maximum range * @param {Number} newMin The new minimum range * @param {Number} newMax The new maximum range * returns {Number} Convert the value in the original range to the value in the new range */ calcPosition(50, 0, 100, -100, 0); // -50 calcPosition(255, 0, 255, 0, 100); // 100
  • calcPercent
/** * @param {Number} min * @param {Number} max * @param {Number} value The value * returns (Number) offset The percentage start offset (0–1) */ calcPercent(25, 255, 25); // 0 calcPercent(25, 255, 25, 0.1); // 0.1

TimeUtils

Time related tools.

  • parseSecond
/** * @param {Number} t The number based on seconds * @param {Number} n is a number, which means the length of the string is fixed. The default value is 2 * returns {Array} is an array of strings. Each item is a string of length n */ parseSecond(111); // ['00', '01', '51'] parseSecond(3333333); // ['25', '55', '33']
  • parseHour12
/** * @param {Number} t The number based on seconds * returns {String} */ parseHour12(111); // '01:55 AM' parseHour12(3333333); // '12:01 PM'
  • stringToSecond
/** * @param {String} timeStr Time point * returns {String} The number converted from timeStr */ stringToSecond("11:30"); // 690 stringToSecond("22:11:30"); // 79890
  • dateToTimer
/** * @param {String} dateStr Time point * returns {String} The number converted from dateStr */ stringToSecond("11:30"); // 690 stringToSecond("22:11:30"); // 79890
  • dateFormat
/** * @param {String} month (M), day (d), hour (h), minute (m), second (s), and quarter (q) can use 1–2 placeholders; year (y) can use 1–4 placeholders; and there is only 1 placeholder for milliseconds (S) (1 to 3-digit number). * returns {String} The extension of Date, which converts Date to String in the specified format */ new Date().Format("yyyy-MM-dd hh:mm:ss.S"); // 2006-07-02 08:09:04.423 new Date().Format("yyyy-M-d h:m:s.S"); // 2006-7-2 8:9:4.18
  • timezone
/** * @desc is an extension of Date, which is used to get the current time zone * returns {String} Current time zone */ timezone(); // +08:00

StringUtils

String tools.

  • hexStringToNumber
/** * @param {String} bits, bits is a hexadecimal string * returns {Array} Array of numbers converted from the hexadecimal string */ hexStringToNumber("AD03"); // [173, 3]
  • hexStringToBinString
/** * @param {String} str, str is a string. It contains "0" and "1", and can contain other characters * returns {String} The string converted from str. Each character is hexadecimal */ hexStringToBinString("A7B9"); // 1010011110111001 hexStringToBinString("0709"); // 0000011100001001
  • camelize
/** * @param {String} str, str is a string * returns {String} Convert the midline and underline to camel case */ camelize("abb_aa-cc"); // abbAaCc

CoreUtils

Core tools.

  • toFixed
/** * @param {String} str, str is a string * @param {Number} str, count is number, which returns the length of the string * returns {String} */ toFixed("111", 5); // '00111' toFixed("3456111", 5); // '56111'
  • toFilled
/** * @param {String} str, str is a string * @param {Number} str, count is number, which returns the length of the string * returns {String} */ toFilled("111", 5); // '00111' toFilled("3456111", 5); // '3456111'
  • partition
/** * @param {String} str, str is a string * @param {Number} str, count is number, which returns the length of the string * returns {Array} An array whose item is a substring and its length is a block */ partition("1234567", 3); // ['123', '456', '7']
  • isObject
/** * @desc Determine whether it is object * @param {Any} Any property * returns {Boolean} */
  • isArray
/** * @desc Determine whether it is array * @param {Any} Any property * returns {Boolean} */
  • isDate
/** * @desc Determine whether it is a date * @param {Any} Any property * returns {Boolean} */
  • isRegExp
/** * @desc Determine whether it is regexp * @param {Any} Any property * returns {Boolean} */
  • isBoolean
/** * @desc Determine whether it is boolean * @param {Any} Any property * returns {Boolean} */
  • isNumerical
/** * @desc Determine whether it is number * @param {Any} Any property * returns {Boolean} */

TemperatureUtils

Temperature-related tools, and the value is rounded.

  • f2c
/** * @param {Number} Fahrenheit degree * returns {Number} Fahrenheit to Celsius conversion */ f2c(100); // 38
  • c2f
/** * @param {Number} Celsius degree * returns {Number} Celsius to Fahrenheit conversion */ c2f(100); // 212

JsonUtils

JSON related tools.

  • parseJSON
/** * @param {String} str, str is a string * returns {Object} Parse the JSON string */ parseJSON("{a:1, b:2}"); // {a: 1, b: 2}