This commit is contained in:
2025-03-07 20:41:21 +07:00
parent bf7037f4e8
commit 96b814d54a
52 changed files with 6643 additions and 782 deletions

View File

@@ -0,0 +1,10 @@
const getFullTimestamp = (n: number): number => {
const length = String(n).length
let str = ''
while (str.length + length < 13) {
str += '0'
}
return parseInt(`${n}${str}`)
}
export const toDate = (n: number) => new Date(getFullTimestamp(n))
export const toTimestamp = (d: Date) => d.getTime()

View File

@@ -0,0 +1,6 @@
export const getTomorrow = (): Date => {
const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);
return tomorrow;
}

View File

@@ -0,0 +1,27 @@
import type { IEntity } from "../../types/entity.type";
import type { Scheme } from "../../types/scheme.type";
import { getTomorrow } from "../date/getters";
export const getDefaultValues = <T extends IEntity>(scheme: Scheme<T>) => {
const keys = Object.keys(scheme) as (keyof typeof scheme)[]
let obj: any = {}
for (let key of keys) {
const primitive = scheme[key]?.type?.primitive
if (primitive == 'string' || primitive == 'multiple') {
obj[key] = ''
} else if (primitive == 'date') {
obj[key] = getTomorrow()
} else if (primitive == 'boolean') {
obj[key] = false
} else if (primitive == 'number') {
obj[key] = 1
} else if (scheme[key].type?.many) {
obj[key] = []
} else if (scheme[key]?.type?.nested?.values) {
obj[key] = scheme[key].type.nested.values[0]
}
}
return obj as T
}

View File

@@ -0,0 +1,18 @@
export const structView = (item: any, path: any) => {
if (!item) return;
if (!path?.length) return item;
let result = item
let i = 0
let current
while (current != path[path.length - 1] && result) {
current = path[i]
result = result[current]
i++
}
return result
}
export const manyStructsView = (items: any, path: any) => {
if (!Array.isArray(items)) return structView(items, path);
return items.map(item => structView(item, path)).join(", ")
}

View File

@@ -0,0 +1 @@
export const ifNull = (data: any, elseData: any) => data? data : elseData