• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict";
2/*
3 * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16Object.defineProperty(exports, "__esModule", { value: true });
17exports.getDistancePx = exports.isFiniteNumber = exports.parseNumber = exports.modulo = exports.clamp = exports.lerp = void 0;
18const compat_1 = require("#koalaui/compat");
19/**
20 * Computes the linear interpolation between `source` and `target` based on `weight`.
21 *
22 * @param weight - interpolation factor in the range [0..1]
23 * @param source - a value corresponding to weight 0
24 * @param target - a value corresponding to weight 1
25 * @returns interpolated value
26 */
27function lerp(weight, source, target) {
28    return source * (1.0 - weight) + target * weight;
29}
30exports.lerp = lerp;
31/**
32 * Clamps a {@link value} within the specified range.
33 *
34 * @param value - a value to clamp
35 * @param min - the lower boundary of the range
36 * @param max - the upper boundary of the range
37 * @returns `min` if `value` is less than `min`,
38 *          `max` if `value` is greater than `max`,
39 *          `value` otherwise
40 */
41function clamp(value, min, max) {
42    return value <= min ? min : value >= max ? max : value;
43}
44exports.clamp = clamp;
45/**
46 * Calculates the difference between the argument and
47 * the largest (closest to positive infinity) integer value
48 * that is less than or equal to the argument.
49 *
50 * @param value a floating-point value to process
51 * @returns a floor modulus of the given value in the range [0..1)
52 */
53function modulo(value) {
54    // The casts below are needed since floor returns double in ArkTS
55    const modulo = value - Math.floor(value);
56    return (modulo < 1.0) ? modulo : 0.0;
57}
58exports.modulo = modulo;
59/**
60 * @param str a string to parse
61 * @param name a name for error message
62 * @param verify whether to verify parsing validity
63 * @returns a floating-point number
64 * @throws Error if `str` cannot be parsed
65 */
66function parseNumber(str, name = "number", verify = false) {
67    if (str != "") { // do not parse empty string to 0
68        // ArkTS does not support NaN, isNaN, parseFloat
69        const value = (0, compat_1.asFloat64)(str);
70        if (verify) {
71            const reverseStr = (0, compat_1.asString)(value);
72            if (reverseStr !== undefined && (reverseStr === null || reverseStr === void 0 ? void 0 : reverseStr.length) == str.length && reverseStr == str) {
73                return value;
74            }
75        }
76        else {
77            return value;
78        }
79    }
80    throw new Error(`cannot parse ${name}: "${str}"`);
81}
82exports.parseNumber = parseNumber;
83/**
84 * An ArkTS-compliant replacement for {@link isFinite}.
85 */
86function isFiniteNumber(number) {
87    // With Node.js:
88    // isFiniteNumber(Number.NEGATIVE_INFINITY) == false
89    // isFiniteNumber(Number.POSITIVE_INFINITY) == false
90    // isFiniteNumber(NaN) == false
91    return number >= Number.MIN_SAFE_INTEGER && number <= Number.MAX_SAFE_INTEGER;
92}
93exports.isFiniteNumber = isFiniteNumber;
94function getDistancePx(startX, startY, endX, endY) {
95    const cathetA = Math.abs(endX - startX);
96    const cathetB = Math.abs(endY - startY);
97    return Math.sqrt(cathetA * cathetA + cathetB * cathetB);
98}
99exports.getDistancePx = getDistancePx;
100//# sourceMappingURL=math.js.map