import BSL from '../../typings'; import { MINYEAR, newDate } from '../datetime'; /** 简单的判断非空 */ export function isEmpty(value: any): boolean { if (Array.isArray(value)) { return value.length === 0; } else if (value && typeof value === 'object' && Object.keys(value).length === 0) { return true; } else { return value === undefined || value === null || (typeof value === 'string' && value === ''); } } /** url是否是http协议 */ export function isHttp(url: string): boolean { return url.indexOf('http://') >= 0 || url.indexOf('https://') >= 0 || url.indexOf('./') >= 0 || url.indexOf('../') >= 0; } /** 判断是否是除了value或undefined以外的值 */ export function isValue(value: T): value is NonNullable { return value !== null && value !== undefined; } /** 用于强验证类型,避免断言这样的松散验证 */ export function identity(value: T) { return value; } /** 获取Choice Value数据格式的值 */ export function getChocie(data: readonly { value: any; label: string }[], value: BSL.Nullable) { return data.find((item) => item.value === value)?.label; } /** 从身份证中计算出生日期 */ export function calcBirthdayByIdentity(identity: BSL.Nullable) { if (!identity) { return ''; } let birthday = ''; if (identity.length === 15) { birthday = "19" + identity.substring(6, 6 + 6); } else if(identity.length === 18){ birthday = identity.substring(6, 6 + 8); } else { return ""; } return birthday.replace(/(.{4})(.{2})/,"$1-$2-"); } /** 从出生日期中计算出年龄 */ export function calcAgeByBirthday(birthday: BSL.Nullable) { if (birthday) { return Math.floor((Date.now() - newDate(birthday).getTime()) / MINYEAR); } return ''; } /** 从身份证中计算出年龄 */ export function calcAgeByIdentity(identity: BSL.Nullable) { const birthday = calcBirthdayByIdentity(identity); if (birthday) { return Math.floor((Date.now() - newDate(birthday).getTime()) / MINYEAR); } return ''; }