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
31 lines (28 loc) · 888 Bytes
/
number.ts
File metadata and controls
31 lines (28 loc) · 888 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
26
27
28
29
30
31
/**
* Copyright (c) 2022 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
/**
* @since 0.6.0
* @returns T/F
*/
export function isInteger(str: string): boolean {
return tryParseInteger(str)[0]
}
/**
* @since 0.6.0
* @returns [true, intValue] if str is an integer, or [false, str]
*/
export function tryParseInteger(str: string): [boolean, number | string] {
const num: number = Number.parseInt(str)
const isInteger: boolean = !isNaN(num) && num.toString().length === str.length
return [isInteger, isInteger ? num : 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
}