forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber.ts
More file actions
25 lines (22 loc) · 873 Bytes
/
Copy pathnumber.ts
File metadata and controls
25 lines (22 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* Copyright (c) 2022 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
/**
* @since 0.6.0
* @returns [true, intValue] if str is an integer, or [false, str]
*/
export function tryParseInteger(str: string): [true, number] | [false, string] {
const num: number = Number.parseInt(str)
const isInteger: boolean = !isNaN(num) && num.toString().length === str.length
return isInteger ? [true, num] : [false, str]
}
/**
* Generate random integer between {@param lowerInclusive} and {@param upperExclusive}
*/
export function randomIntBetween(lowerInclusive: number, upperExclusive: number): number {
return Math.floor(Math.random() * (upperExclusive - lowerInclusive)) + lowerInclusive
}
export const clamp = (v: number, min: number, max: number): number => Math.min(max, Math.max(min, v))