Initial commit of akmon project
This commit is contained in:
32
uni_modules/lime-dayuts/common/constant.ts
Normal file
32
uni_modules/lime-dayuts/common/constant.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
// @ts-nocheck
|
||||
export const SECONDS_A_MINUTE = 60
|
||||
export const SECONDS_A_HOUR = SECONDS_A_MINUTE * 60
|
||||
export const SECONDS_A_DAY = SECONDS_A_HOUR * 24
|
||||
export const SECONDS_A_WEEK = SECONDS_A_DAY * 7
|
||||
|
||||
export const MILLISECONDS_A_SECOND = 1e3
|
||||
export const MILLISECONDS_A_MINUTE = SECONDS_A_MINUTE * MILLISECONDS_A_SECOND
|
||||
export const MILLISECONDS_A_HOUR = SECONDS_A_HOUR * MILLISECONDS_A_SECOND
|
||||
export const MILLISECONDS_A_DAY = SECONDS_A_DAY * MILLISECONDS_A_SECOND
|
||||
export const MILLISECONDS_A_WEEK = SECONDS_A_WEEK * MILLISECONDS_A_SECOND
|
||||
|
||||
// English locales
|
||||
export const MS = 'millisecond'
|
||||
export const S = 'second'
|
||||
export const MIN = 'minute'
|
||||
export const H = 'hour'
|
||||
export const D = 'day'
|
||||
export const W = 'week'
|
||||
export const M = 'month'
|
||||
export const Q = 'quarter'
|
||||
export const Y = 'year'
|
||||
export const DATE = 'date'
|
||||
|
||||
export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'
|
||||
|
||||
export const INVALID_DATE_STRING = 'Invalid Date'
|
||||
|
||||
// regex
|
||||
export const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/
|
||||
export const REGEX_FORMAT = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g
|
||||
// export const REGEX_FORMAT = /(?:[^\]]+)|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g;
|
||||
194
uni_modules/lime-dayuts/common/dates.ts
Normal file
194
uni_modules/lime-dayuts/common/dates.ts
Normal file
@@ -0,0 +1,194 @@
|
||||
// @ts-nocheck
|
||||
import {dayuts, Dayuts} from "./index"
|
||||
// import {DayutsConfig} from '../../types/date.uts'
|
||||
import { DayutsConfig } from '../utssdk/interface'
|
||||
export class Dates {
|
||||
|
||||
/**
|
||||
* Dayuts实例
|
||||
*/
|
||||
newDate() : Dayuts;
|
||||
newDate(date : string) : Dayuts;
|
||||
newDate(date : any[]) : Dayuts;
|
||||
newDate(date : number) : Dayuts;
|
||||
newDate(date : UTSJSONObject) : Dayuts;
|
||||
newDate(date : Date) : Dayuts;
|
||||
newDate(date : Dayuts) : Dayuts;
|
||||
// #ifndef APP-ANDROID || APP-IOS || APP-HARMONY
|
||||
newDate(date : any | null, format : string) : Dayuts;
|
||||
newDate(date : any | null, format : string | null, locale : string | null) : Dayuts;
|
||||
// #endif
|
||||
newDate(date : any | null = null, format : string | null = null, locale : string | null = null) : Dayuts {
|
||||
if (date != null && date instanceof Dayuts) return date.clone()
|
||||
return new Dayuts({
|
||||
date,
|
||||
format,
|
||||
locale
|
||||
} as DayutsConfig)
|
||||
}
|
||||
|
||||
/**
|
||||
* 万能转日期对象
|
||||
* @param date 时间
|
||||
*/
|
||||
toDate(date : string) : Date {
|
||||
date = date.split('周')[0].trim().split('星期')[0].trim().split('礼拜')[0].trim()
|
||||
|
||||
if (date == '') {
|
||||
return new Date()
|
||||
}
|
||||
|
||||
if (date.length >= 10 && /^\d+$/.test(date)) {
|
||||
// 时间戳
|
||||
let timestamp = parseInt(date)
|
||||
|
||||
// 若为unix秒时间戳,则转为毫秒时间戳
|
||||
if (/^\d{10}$/.test(timestamp.toString())) {
|
||||
return new Date(timestamp * 1000)
|
||||
} else {
|
||||
return new Date(timestamp)
|
||||
}
|
||||
} else {
|
||||
if (!date.includes('T')) {
|
||||
// 容错
|
||||
date = date.replace(/\//g, '-').
|
||||
replace(/年/g, '-').
|
||||
replace(/月/g, '-').
|
||||
replace(/日/g, '').
|
||||
replace(/时/g, ':').
|
||||
replace(/分/g, ':').
|
||||
replace(/秒/g, '').
|
||||
replace(/^-+|-+$/g, '').
|
||||
trim()
|
||||
|
||||
// 补全
|
||||
if (date.length == 4) {
|
||||
date += '-01-01 00:00:00'
|
||||
} else if (date.length == 7) {
|
||||
date += '-01 00:00:00'
|
||||
} else if (date.length == 8) {
|
||||
date = `${this.today()} ${date}`
|
||||
} else if (date.length == 10) {
|
||||
date += ' 00:00:00'
|
||||
} else if (date.length == 13) {
|
||||
date += ':00:00'
|
||||
} else if (date.length == 16) {
|
||||
date += ':00'
|
||||
}
|
||||
|
||||
let d = date.split(/[^0-9]/)
|
||||
|
||||
try{
|
||||
return new Date(parseInt(d[0]), parseInt(d[1]) - 1, parseInt(d[2]), parseInt(d[3]), parseInt(d[4]), parseInt(d[5]))
|
||||
}catch(e){
|
||||
console.error(`[ux-date]解析失败:${date}`)
|
||||
return new Date()
|
||||
}
|
||||
|
||||
} else {
|
||||
return new Date(date)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 万能格式化日期
|
||||
* @param date 时间
|
||||
* @param format 格式化规则 支持yyyy-MM-dd|yyyy-MM-dd HH:mm:ss|yyyy/MM/dd|yyyy/MM/dd HH:mm:ss|yyyy年MM月dd日等组合 默认yyyy-mm-dd
|
||||
*/
|
||||
fmtDate(date : string, format : string) : string {
|
||||
if(format == '') {
|
||||
format = 'yyyy-MM-dd'
|
||||
}
|
||||
|
||||
date = date.split('周')[0].trim().split('星期')[0].trim().split('礼拜')[0].trim()
|
||||
|
||||
let d = this.toDate(date)
|
||||
|
||||
let timeSource = new Map<string, string>()
|
||||
timeSource.set('y', d.getFullYear().toString())
|
||||
timeSource.set('M', (d.getMonth() + 1).toString().padStart(2, '0'))
|
||||
timeSource.set('d', d.getDate().toString().padStart(2, '0'))
|
||||
timeSource.set('H', d.getHours().toString().padStart(2, '0'))
|
||||
timeSource.set('m', d.getMinutes().toString().padStart(2, '0'))
|
||||
timeSource.set('s', d.getSeconds().toString().padStart(2, '0'))
|
||||
|
||||
let result = format.split('周')[0].trim().split('星期')[0].trim().split('礼拜')[0].trim()
|
||||
timeSource.forEach((v : string, key : string) => {
|
||||
const rets = new RegExp(`${key}+`).exec(result) ?? [] as RegExp[]
|
||||
if (rets.length > 0) {
|
||||
result = result.replace(rets[0].toString(), v)
|
||||
}
|
||||
})
|
||||
|
||||
let fmtWeek = format.indexOf('周') != -1 || format.indexOf('星期') != -1 || format.indexOf('礼拜') != -1
|
||||
if(fmtWeek) {
|
||||
result += ` ${this.weekName(this.toDate(result).getDay(), format)}`
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* 现在 yyyy-MM-dd HH:mm:ss
|
||||
*/
|
||||
now() : string {
|
||||
let date = new Date()
|
||||
|
||||
let year = date.getFullYear()
|
||||
let month = `${date.getMonth() + 1}`.padStart(2, '0')
|
||||
let day = `${date.getDate()}`.padStart(2, '0')
|
||||
let hour = `${date.getHours()}`.padStart(2, '0')
|
||||
let minute = `${date.getMinutes()}`.padStart(2, '0')
|
||||
let second = `${date.getSeconds()}`.padStart(2, '0')
|
||||
|
||||
return `${year}-${month}-${day} ${hour}:${minute}:${second}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 今天
|
||||
* @param n n为负则代表取前n天,为正则代表取后n天,0则为今天
|
||||
*/
|
||||
today(n : number = 0) : string {
|
||||
let date = new Date()
|
||||
date.setDate(date.getDate() + n)
|
||||
|
||||
let year = date.getFullYear()
|
||||
let month = `${date.getMonth() + 1}`.padStart(2, '0')
|
||||
let day = `${date.getDate()}`.padStart(2, '0')
|
||||
|
||||
return `${year}-${month}-${day}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 本周所有日期
|
||||
*/
|
||||
weeks() : string[] {
|
||||
const fDate = new Date()
|
||||
const eDate = new Date()
|
||||
fDate.setDate(fDate.getDate() - fDate.getDay() + 1)
|
||||
eDate.setDate(eDate.getDate() - eDate.getDay() + 7)
|
||||
|
||||
const result : string[] = []
|
||||
|
||||
for (let d = fDate; d.getTime() <= eDate.getTime(); d.setDate(d.getDate() + 1)) {
|
||||
result.push(d.toISOString().slice(0, 10))
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 周几
|
||||
*/
|
||||
weekName(day: number, format: string) : string {
|
||||
if(format.indexOf('星期') != -1) {
|
||||
return ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'][day]
|
||||
} else if(format.indexOf('礼拜') != -1 ) {
|
||||
return ['礼拜天', '礼拜一', '礼拜二', '礼拜三', '礼拜四', '礼拜五', '礼拜六'][day]
|
||||
} else {
|
||||
return ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][day]
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
1103
uni_modules/lime-dayuts/common/index.ts
Normal file
1103
uni_modules/lime-dayuts/common/index.ts
Normal file
File diff suppressed because it is too large
Load Diff
57
uni_modules/lime-dayuts/common/locale/en/index.ts
Normal file
57
uni_modules/lime-dayuts/common/locale/en/index.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
// @ts-nocheck
|
||||
import type { DayutsLocale, DayutsRelativeTime} from '../../../utssdk/interface'
|
||||
/**
|
||||
* 英语本地化对象。
|
||||
*/
|
||||
export default {
|
||||
name: 'en',
|
||||
/**
|
||||
* 星期名称数组。
|
||||
*/
|
||||
weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
|
||||
/**
|
||||
* 月份名称数组。
|
||||
*/
|
||||
months: [
|
||||
'January',
|
||||
'February',
|
||||
'March',
|
||||
'April',
|
||||
'May',
|
||||
'June',
|
||||
'July',
|
||||
'August',
|
||||
'September',
|
||||
'October',
|
||||
'November',
|
||||
'December',
|
||||
],
|
||||
relativeTime: {
|
||||
future: 'in %s',
|
||||
past: '%s ago',
|
||||
s: 'a few seconds',
|
||||
m: 'a minute',
|
||||
mm: '%d minutes',
|
||||
h: 'an hour',
|
||||
hh: '%d hours',
|
||||
d: 'a day',
|
||||
dd: '%d days',
|
||||
M: 'a month',
|
||||
MM: '%d months',
|
||||
y: 'a year',
|
||||
yy: '%d years'
|
||||
} as DayutsRelativeTime,
|
||||
/**
|
||||
* 序数函数,用于将数字转换为带有序数后缀的字符串。
|
||||
*
|
||||
* @param {number} n - 要转换的数字。
|
||||
* @returns {string} 带有序数后缀的字符串。
|
||||
*/
|
||||
ordinal: (n : number, _ : string) : string => {
|
||||
const s = ['th', 'st', 'nd', 'rd']
|
||||
const v = n % 100
|
||||
const i = (v - 20) % 10
|
||||
const k = i < s.length ? i : v < s.length ? v : 0
|
||||
return `[${n}${(s[k])}]`
|
||||
},
|
||||
} as DayutsLocale
|
||||
75
uni_modules/lime-dayuts/common/locale/zh-cn/index.ts
Normal file
75
uni_modules/lime-dayuts/common/locale/zh-cn/index.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
// @ts-nocheck
|
||||
import type { DayutsLocale, DayutsRelativeTime, DayutsFormats } from '../../../utssdk/interface'
|
||||
|
||||
const locale : DayutsLocale = {
|
||||
name: 'zh-cn',
|
||||
weekdays: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
|
||||
weekdaysShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
|
||||
weekdaysMin: ['日', '一', '二', '三', '四', '五', '六'],
|
||||
months: [
|
||||
'一月', '二月', '三月', '四月', '五月', '六月',
|
||||
'七月', '八月', '九月', '十月', '十一月', '十二月'
|
||||
],
|
||||
monthsShort: [
|
||||
'1月', '2月', '3月', '4月', '5月', '6月',
|
||||
'7月', '8月', '9月', '10月', '11月', '12月'
|
||||
],
|
||||
ordinal: (number:number, period:string):string => {
|
||||
// switch (period) {
|
||||
// case 'W':
|
||||
// return `${number}周`;
|
||||
// default:
|
||||
// return `${number}日`;
|
||||
// }
|
||||
if(period == 'W'){
|
||||
return `${number}周`;
|
||||
}
|
||||
return `${number}日`
|
||||
},
|
||||
weekStart: 1,
|
||||
yearStart: 4,
|
||||
formats: {
|
||||
LT: 'HH:mm',
|
||||
LTS: 'HH:mm:ss',
|
||||
L: 'YYYY/MM/DD',
|
||||
LL: 'YYYY年M月D日',
|
||||
LLL: 'YYYY年M月D日Ah点mm分',
|
||||
LLLL: 'YYYY年M月D日ddddAh点mm分',
|
||||
l: 'YYYY/M/D',
|
||||
ll: 'YYYY年M月D日',
|
||||
lll: 'YYYY年M月D日 HH:mm',
|
||||
llll: 'YYYY年M月D日dddd HH:mm'
|
||||
} as DayutsFormats,
|
||||
relativeTime: {
|
||||
future: '%s内',
|
||||
past: '%s前',
|
||||
s: '几秒',
|
||||
m: '1 分钟',
|
||||
mm: '%d 分钟',
|
||||
h: '1 小时',
|
||||
hh: '%d 小时',
|
||||
d: '1 天',
|
||||
dd: '%d 天',
|
||||
M: '1 个月',
|
||||
MM: '%d 个月',
|
||||
y: '1 年',
|
||||
yy: '%d 年'
|
||||
} as DayutsRelativeTime,
|
||||
meridiem: (hour:number, minute:number, _ : boolean):string => {
|
||||
const hm = (hour * 100) + minute;
|
||||
if (hm < 600) {
|
||||
return '凌晨';
|
||||
} else if (hm < 900) {
|
||||
return '早上';
|
||||
} else if (hm < 1100) {
|
||||
return '上午';
|
||||
} else if (hm < 1300) {
|
||||
return '中午';
|
||||
} else if (hm < 1800) {
|
||||
return '下午';
|
||||
}
|
||||
return '晚上';
|
||||
}
|
||||
};
|
||||
|
||||
export default locale;
|
||||
104
uni_modules/lime-dayuts/common/test.ts
Normal file
104
uni_modules/lime-dayuts/common/test.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
// @ts-nocheck
|
||||
import { dayuts } from "./index"
|
||||
// import { Dates } from "./dates"
|
||||
import { dayutsIntl } from './use'
|
||||
console.log('dayuts:::test::12')
|
||||
// console.log('format1', dayuts().format())
|
||||
console.log('string number', dayuts(`${Date.now()}`))
|
||||
console.log('format1', dayuts('2018-04-04T16:00:00.000Z'))
|
||||
console.log('format1', dayuts('2018 三月 15', 'YYYY MMMM DD', 'zh-cn'))
|
||||
console.log('format2', dayuts([2010, 1, 14, 15, 25, 50, 125]))
|
||||
console.log('format1', dayuts('1970-00-00', 'YYYY-MM-DD'))
|
||||
console.log('format1', dayuts(1318781876406))
|
||||
console.log('format1', dayuts(new Date(2018, 8, 18)))
|
||||
console.log('format1', dayuts([2010, 1, 14, 15, 25, 50, 125]))
|
||||
console.log('millisecond2', dayuts().millisecond())
|
||||
console.log('millisecond2', dayuts().millisecond(1))
|
||||
console.log('second2', dayuts().second())
|
||||
console.log('second2', dayuts().second(1))
|
||||
console.log('minute2', dayuts().minute())
|
||||
console.log('minute2', dayuts().minute(1))
|
||||
console.log('hour2', dayuts().hour())
|
||||
console.log('hour2', dayuts().hour(12))
|
||||
console.log('date2', dayuts().date())
|
||||
console.log('date2', dayuts().date(1))
|
||||
console.log('dayOfYear', dayuts().dayOfYear())
|
||||
console.log('dayOfYear', dayuts().dayOfYear(1))
|
||||
console.log('month', dayuts().month())
|
||||
console.log('month', dayuts().month(1))
|
||||
console.log('year', dayuts().year())
|
||||
console.log('year', dayuts().year(2000))
|
||||
console.log(dayuts().get('year'))
|
||||
console.log(dayuts().get('month')) // start 0
|
||||
console.log(dayuts().get('date'))
|
||||
console.log(dayuts().get('hour'))
|
||||
console.log(dayuts().get('minute'))
|
||||
console.log(dayuts().get('second'))
|
||||
console.log(dayuts().get('millisecond'))
|
||||
console.log('add', dayuts().add(7, 'day'))
|
||||
// console.log('add1', new Dates().newDate().add(7, 'day').format('YYYY-MM-DD'))
|
||||
console.log('subtract', dayuts().subtract(7, 'year'))
|
||||
console.log('startOf', dayuts().startOf('year'))
|
||||
console.log('endOf', dayuts().endOf('month'))
|
||||
console.log('format', dayuts().format())
|
||||
console.log('format', dayuts('2019-01-25').format('[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]'))
|
||||
console.log('format', dayuts('2019-01-25').format('DD/MM/YYYY'))
|
||||
console.log('fromNow:1', dayuts('1999-01-01').fromNow())
|
||||
console.log('fromNow:1', dayuts('1999-01-01').fromNow(true))
|
||||
console.log('fromNow:1', dayuts('2024-03-29').fromNow(true))
|
||||
console.log('from1', dayuts('1999-01-01').from(dayuts()))
|
||||
console.log('from1', dayuts('1999-01-01').from(dayuts(), true))
|
||||
console.log('toNow1', dayuts('1999-01-01').toNow(true))
|
||||
console.log('toNow1', dayuts('1999-01-01').to(dayuts()))
|
||||
console.log('diff1', dayuts('2019-01-25').diff(dayuts('2018-06-05')))
|
||||
console.log('diff1', dayuts('2019-01-25').diff(dayuts('2018-06-05'), 'month'))
|
||||
console.log('diff1', dayuts('2019-01-25').diff(dayuts('2018-06-05'), 'month', true))
|
||||
console.log('valueOf', dayuts('2019-01-25').valueOf())
|
||||
console.log('unix', dayuts('2019-01-25').unix())
|
||||
console.log('daysInMonth', dayuts('2019-01-25').daysInMonth())
|
||||
console.log('toDate', dayuts().toDate())
|
||||
console.log('toArray', dayuts().toArray())
|
||||
console.log('toJSON', dayuts().toJSON())
|
||||
console.log('toObject', dayuts().toObject())
|
||||
console.log('toString', dayuts().toString())
|
||||
console.log('isBefore', dayuts().isBefore(dayuts('2011-01-01')))
|
||||
console.log('isBefore', dayuts().isBefore('2011-01-01', 'month'))
|
||||
console.log('isSame', dayuts().isSame(dayuts('2011-01-01')))
|
||||
console.log('isSame', dayuts().isSame('2011-01-01', 'year'))
|
||||
console.log('isAfter', dayuts().isAfter(dayuts('2011-01-01')))
|
||||
console.log('isAfter', dayuts().isAfter('2011-01-01', 'month'))
|
||||
console.log('isSameOrBefore', dayuts().isSameOrBefore(dayuts('2011-01-01')))
|
||||
console.log('isSameOrBefore', dayuts().isSameOrBefore('2011-01-01', 'month'))
|
||||
console.log('isSameOrBefore', dayuts().isSameOrAfter(dayuts('2011-01-01')))
|
||||
console.log('isSameOrBefore', dayuts().isSameOrAfter('2011-01-01', 'month'))
|
||||
console.log('isBetween', dayuts('2010-10-20').isBetween('2010-10-19', dayuts('2010-10-25')))
|
||||
console.log('isBetween', dayuts().isBetween('2010-10-19', '2010-10-25', 'month'))
|
||||
console.log('isBetween', dayuts('2016-10-30').isBetween('2016-01-01', '2016-10-30', 'day', '[)'))
|
||||
console.log('isLeapYear', dayuts().isLeapYear())
|
||||
console.log('isToday', dayuts().isToday())
|
||||
|
||||
// const a = dayuts('1999-01-01')
|
||||
// const b = dayuts()
|
||||
// console.log('fromNow:1',a.diff(b, 'year', true))
|
||||
// console.log('a', a)
|
||||
// console.log('b', b)
|
||||
// console.log('fromNow:2',a.diff(b, 'year', true))
|
||||
|
||||
dayutsIntl.locale = 'zh-cn'
|
||||
console.log('new::1', dayuts().$L)
|
||||
|
||||
// setTimeout(()=>{
|
||||
// console.log('new::23')
|
||||
// },100)
|
||||
|
||||
|
||||
export const test = (n : number) : number => {
|
||||
switch (n) {
|
||||
case 1:
|
||||
return 1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
console.log('test::::', test(1))
|
||||
63
uni_modules/lime-dayuts/common/use.ts
Normal file
63
uni_modules/lime-dayuts/common/use.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
// @ts-nocheck
|
||||
import {DayutsLocale} from '../utssdk/interface'
|
||||
import en from './locale/en'
|
||||
import zhCn from './locale/zh-cn'
|
||||
|
||||
// #ifndef UNI-APP-X
|
||||
|
||||
// #ifdef VUE2
|
||||
import {reactive} from '@vue/composition-api'
|
||||
// #endif
|
||||
|
||||
// #ifdef VUE3
|
||||
import {reactive} from 'vue'
|
||||
// #endif
|
||||
|
||||
// #endif
|
||||
|
||||
|
||||
const localesMap = new Map<string, DayutsLocale>()
|
||||
//定义一个大写的State类型
|
||||
type LocaleState = {
|
||||
lang: string;
|
||||
locales: Map<string, DayutsLocale>
|
||||
}
|
||||
export let localeState = reactive({
|
||||
lang: 'en',
|
||||
locales: localesMap
|
||||
} as LocaleState)
|
||||
localeState.locales.set('en', en)
|
||||
localeState.locales.set('zh-cn', zhCn)
|
||||
|
||||
class DayutsIntl {
|
||||
constructor(){}
|
||||
use(locale:DayutsLocale):DayutsIntl{
|
||||
localeState.locales.set(locale.name, locale)
|
||||
return this
|
||||
}
|
||||
set locale(locale: string){
|
||||
if(localeState.locales.has(locale)){
|
||||
localeState.lang = locale
|
||||
} else {
|
||||
let list:string[] = []
|
||||
localeState.locales.forEach(function(_:any,key:string){
|
||||
list.push(key)
|
||||
})
|
||||
console.warn(`未知语言: "${locale}". 请使用以下已知语言之一:${list.join(', ')}`);
|
||||
}
|
||||
}
|
||||
get locale(): string{
|
||||
return localeState.lang
|
||||
}
|
||||
set(name: string, locale:DayutsLocale){
|
||||
localeState.locales.set(name, locale)
|
||||
}
|
||||
has(name: string):boolean{
|
||||
return localeState.locales.has(name)
|
||||
}
|
||||
}
|
||||
export const dayutsIntl = new DayutsIntl()
|
||||
|
||||
|
||||
// const dyauts = use(xx).use(xx).use(xx).use(xx)
|
||||
// dyauts.locale ='zh'
|
||||
106
uni_modules/lime-dayuts/common/utils.ts
Normal file
106
uni_modules/lime-dayuts/common/utils.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
// @ts-nocheck
|
||||
import { Dayuts } from './index'
|
||||
import { M, Y, W, D, DATE, H, MIN, S, MS, Q } from './constant'
|
||||
import {DayutsUnit} from '../utssdk/interface'
|
||||
/**
|
||||
* 用指定字符串填充目标字符串的开头,以达到指定的总长度。
|
||||
*
|
||||
* @param {string} string - 需要填充的目标字符串。
|
||||
* @param {number} length - 填充后的总长度。
|
||||
* @param {string} pad - 用于填充的字符串。
|
||||
* @returns {string} 填充后的字符串。
|
||||
*/
|
||||
function padStart(string : string, length : number, pad : string) : string {
|
||||
const str = string//`${string}`
|
||||
if (str.length >= length) return str
|
||||
return str.padStart(length, pad) //`${Array((length + 1) - string.length).join(pad)}${string}`
|
||||
}
|
||||
export {
|
||||
padStart
|
||||
}
|
||||
function padZoneStr(instance : Dayuts) : string {
|
||||
const negMinutes = -instance.utcOffset()
|
||||
const minutes = Math.abs(negMinutes)
|
||||
const hourOffset = Math.floor(minutes / 60)
|
||||
const minuteOffset = minutes % 60
|
||||
return `${negMinutes <= 0 ? '+' : '-'}${padStart(hourOffset.toString(), 2, '0')}:${padStart(minuteOffset.toString(), 2, '0')}`
|
||||
}
|
||||
export {
|
||||
padZoneStr
|
||||
}
|
||||
|
||||
// export function isNull(s): boolean{
|
||||
// return s == null
|
||||
// }
|
||||
|
||||
export function isNumber(value : any | null) : boolean {
|
||||
// #ifdef APP-ANDROID
|
||||
return ['Byte', 'UByte', 'Short', 'UShort', 'Int', 'UInt', 'Long', 'ULong', 'Float', 'Double', 'number'].includes(typeof value)
|
||||
// #endif
|
||||
// #ifdef APP-IOS
|
||||
return ['Int8', 'UInt8', 'Int16', 'UInt16', 'Int32', 'UInt32', 'Int64', 'UInt64', 'Int', 'UInt', 'Float', 'Float16', 'Float32', 'Float64', 'Double', 'number'].includes(typeof value)
|
||||
// #endif
|
||||
// #ifndef APP-ANDROID || APP-IOS
|
||||
return typeof value === 'number' && !isNaN(value);
|
||||
// #endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 将给定的时间单位转换为标准格式。
|
||||
*
|
||||
* @param {string} u - 要转换的时间单位。
|
||||
* @returns {string} 返回转换后的时间单位。
|
||||
*/
|
||||
export function prettyUnit(u : string) : DayutsUnit {
|
||||
const special = new Map<string, string>([
|
||||
['M', M],
|
||||
['y', Y],
|
||||
['w', W],
|
||||
['d', D],
|
||||
['D', DATE],
|
||||
['h', H],
|
||||
['m', MIN],
|
||||
['s', S],
|
||||
['ms', MS],
|
||||
['Q', Q]
|
||||
])
|
||||
return (special.get(u) ?? `${u}`.toLowerCase().replace(/s$/, '')) as DayutsUnit
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 计算两个日期之间的月份差值
|
||||
* @param {Dayjs} a - 第一个日期
|
||||
* @param {Dayjs} b - 第二个日期
|
||||
* @returns {number} 返回两个日期之间的月份差值
|
||||
*/
|
||||
export function monthDiff(a : Dayuts, b : Dayuts) : number {
|
||||
// 该函数来自 moment.js,以保持相同的结果
|
||||
if (a.date() < b.date()) return -monthDiff(b, a)
|
||||
const wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month())
|
||||
const anchor = a.clone().add(wholeMonthDiff, M).valueOf()
|
||||
const c = b.valueOf() - anchor < 0
|
||||
const anchor2 = a.clone().add(wholeMonthDiff + (c ? -1 : 1), M).valueOf()
|
||||
// return +(-(wholeMonthDiff + ((b.valueOf() - anchor) / (c ? (anchor - anchor2) :
|
||||
// (anchor2 - anchor)))) || 0)
|
||||
|
||||
const decimalMonthDiff = (b.valueOf() - anchor) / (c ? (anchor - anchor2) : (anchor2 - anchor));
|
||||
const result = wholeMonthDiff + decimalMonthDiff;
|
||||
const negatedResult = -result;
|
||||
const absResult = +negatedResult;
|
||||
const finalResult = !isNaN(absResult) ? absResult : 0;
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回向下取整的绝对值
|
||||
* @param {number} n - 输入的数字
|
||||
* @returns {number} 返回向下取整的绝对值
|
||||
*/
|
||||
export function absFloor(n : number):number {
|
||||
// return (n < 0 ? Math.ceil(n) || 0 : Math.floor(n))
|
||||
return (n < 0 ? Math.max(Math.ceil(n), 0) : Math.floor(n))
|
||||
}
|
||||
Reference in New Issue
Block a user