124 lines
3.0 KiB
TypeScript
124 lines
3.0 KiB
TypeScript
import _ from "lodash"
|
|
import { Option, Some, None, Result, Ok, Err } from "ts-results-es";
|
|
import { z } from "zod";
|
|
|
|
export function UNUSED(_: unknown): void { }
|
|
|
|
export namespace type {
|
|
|
|
export const Integer = z.number().int()
|
|
export const UInteger = z.number().int().nonnegative()
|
|
|
|
export const UInt8 = z.number().int().nonnegative().lt(Math.pow(2, 8))
|
|
export const UInt16 = z.number().int().nonnegative().lt(Math.pow(2, 16))
|
|
export const UInt32 = z.number().int().nonnegative().lt(Math.pow(2, 32))
|
|
|
|
export const Int8 = z.number().int().lt(Math.pow(2, 7)).gte(-Math.pow(2, 8))
|
|
export const Int16 = z.number().int().lt(Math.pow(2, 15)).gte(-Math.pow(2, 16))
|
|
export const Int32 = z.number().int().lt(Math.pow(2, 31)).gte(-Math.pow(2, 32))
|
|
|
|
export function numberToBytes(num: number, bytesLength: number): Result<Uint8Array, "Not Integer"> {
|
|
// Check Integer
|
|
if (!(Integer.safeParse(num).success && Integer.safeParse(bytesLength).success)) {
|
|
return new Err("Not Integer")
|
|
}
|
|
|
|
var array = new Uint8Array(bytesLength)
|
|
|
|
for (let i = 0; i < bytesLength; i++) {
|
|
array[i] = num & (0xFF << (i << 3))
|
|
}
|
|
|
|
return new Ok(array)
|
|
}
|
|
|
|
|
|
export function numberMatch(
|
|
srcNum: number,
|
|
destNum: number
|
|
): boolean;
|
|
|
|
export function numberMatch<T>(
|
|
srcNum: number,
|
|
destNum: number,
|
|
True: T,
|
|
False: T
|
|
): T;
|
|
|
|
export function numberMatch<T>(
|
|
srcNum: number,
|
|
destNum: number,
|
|
True: T = true as T,
|
|
False: T = false as T
|
|
): T {
|
|
const ret = (srcNum & destNum) === destNum;
|
|
return ret ? True : False;
|
|
}
|
|
|
|
export function numberMatchEnum() {
|
|
|
|
}
|
|
|
|
export function numberSetBit(num: number, loc: number): number {
|
|
return num | (1 << loc)
|
|
}
|
|
|
|
export function numberUnsetBit(num: number, loc: number): number {
|
|
return num | (~1 << loc)
|
|
}
|
|
|
|
export function numberHighBits(num: number) {
|
|
}
|
|
|
|
export function isStringArray(obj: any): obj is Array<string> {
|
|
return z.string().array().safeParse(obj).success
|
|
}
|
|
|
|
export function isNumberArray(obj: any): obj is Array<number> {
|
|
return z.number().array().safeParse(obj).success
|
|
}
|
|
}
|
|
|
|
export namespace fun {
|
|
|
|
export function randomFromArray(array: Array<any>) {
|
|
return array[_.random(0, array.length - 1, false)]
|
|
}
|
|
|
|
export function sqlConditionFromArray(
|
|
columnName: string,
|
|
array: Array<string>,
|
|
type: "AND" | "OR"
|
|
): Option<string> {
|
|
let condition: string = ""
|
|
const len = array.length
|
|
if (len == 0) {
|
|
return None
|
|
}
|
|
for (let i = 0; i < len; i++) {
|
|
condition.concat(`${columnName}=${array[i]}`)
|
|
if (i != len - 1) {
|
|
condition.concat(` ${type} `)
|
|
}
|
|
}
|
|
|
|
return new Some(condition)
|
|
}
|
|
|
|
export function sqlConditionFromString(
|
|
columnName: string,
|
|
str: string,
|
|
type: "AND" | "OR",
|
|
delimiter: string = " "
|
|
): Option<string> {
|
|
if (str.length == 0) {
|
|
return None
|
|
}
|
|
|
|
const array = str.split(delimiter)
|
|
return sqlConditionFromArray(columnName, array, type)
|
|
}
|
|
}
|
|
|
|
|