utils.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // @ts-nocheck
  2. import { Dayuts } from './index'
  3. import { M, Y, W, D, DATE, H, MIN, S, MS, Q } from './constant'
  4. import {DayutsUnit} from '../utssdk/interface'
  5. /**
  6. * 用指定字符串填充目标字符串的开头,以达到指定的总长度。
  7. *
  8. * @param {string} string - 需要填充的目标字符串。
  9. * @param {number} length - 填充后的总长度。
  10. * @param {string} pad - 用于填充的字符串。
  11. * @returns {string} 填充后的字符串。
  12. */
  13. function padStart(string : string, length : number, pad : string) : string {
  14. const str = string//`${string}`
  15. if (str.length >= length) return str
  16. return str.padStart(length, pad) //`${Array((length + 1) - string.length).join(pad)}${string}`
  17. }
  18. export {
  19. padStart
  20. }
  21. function padZoneStr(instance : Dayuts) : string {
  22. const negMinutes = -instance.utcOffset()
  23. const minutes = Math.abs(negMinutes)
  24. const hourOffset = Math.floor(minutes / 60)
  25. const minuteOffset = minutes % 60
  26. return `${negMinutes <= 0 ? '+' : '-'}${padStart(hourOffset.toString(), 2, '0')}:${padStart(minuteOffset.toString(), 2, '0')}`
  27. }
  28. export {
  29. padZoneStr
  30. }
  31. // export function isNull(s): boolean{
  32. // return s == null
  33. // }
  34. export function isNumber(value : any | null) : boolean {
  35. // #ifdef APP-ANDROID
  36. return ['Byte', 'UByte', 'Short', 'UShort', 'Int', 'UInt', 'Long', 'ULong', 'Float', 'Double', 'number'].includes(typeof value)
  37. // #endif
  38. // #ifdef APP-IOS
  39. return ['Int8', 'UInt8', 'Int16', 'UInt16', 'Int32', 'UInt32', 'Int64', 'UInt64', 'Int', 'UInt', 'Float', 'Float16', 'Float32', 'Float64', 'Double', 'number'].includes(typeof value)
  40. // #endif
  41. // #ifndef APP-ANDROID || APP-IOS
  42. return typeof value === 'number' && !isNaN(value);
  43. // #endif
  44. }
  45. /**
  46. * 将给定的时间单位转换为标准格式。
  47. *
  48. * @param {string} u - 要转换的时间单位。
  49. * @returns {string} 返回转换后的时间单位。
  50. */
  51. export function prettyUnit(u : string) : DayutsUnit {
  52. const special = new Map<string, string>([
  53. ['M', M],
  54. ['y', Y],
  55. ['w', W],
  56. ['d', D],
  57. ['D', DATE],
  58. ['h', H],
  59. ['m', MIN],
  60. ['s', S],
  61. ['ms', MS],
  62. ['Q', Q]
  63. ])
  64. return (special.get(u) ?? `${u}`.toLowerCase().replace(/s$/, '')) as DayutsUnit
  65. }
  66. /**
  67. * 计算两个日期之间的月份差值
  68. * @param {Dayjs} a - 第一个日期
  69. * @param {Dayjs} b - 第二个日期
  70. * @returns {number} 返回两个日期之间的月份差值
  71. */
  72. export function monthDiff(a : Dayuts, b : Dayuts) : number {
  73. // 该函数来自 moment.js,以保持相同的结果
  74. if (a.date() < b.date()) return -monthDiff(b, a)
  75. const wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month())
  76. const anchor = a.clone().add(wholeMonthDiff, M).valueOf()
  77. const c = b.valueOf() - anchor < 0
  78. const anchor2 = a.clone().add(wholeMonthDiff + (c ? -1 : 1), M).valueOf()
  79. // return +(-(wholeMonthDiff + ((b.valueOf() - anchor) / (c ? (anchor - anchor2) :
  80. // (anchor2 - anchor)))) || 0)
  81. const decimalMonthDiff = (b.valueOf() - anchor) / (c ? (anchor - anchor2) : (anchor2 - anchor));
  82. const result = wholeMonthDiff + decimalMonthDiff;
  83. const negatedResult = -result;
  84. const absResult = +negatedResult;
  85. const finalResult = !isNaN(absResult) ? absResult : 0;
  86. return finalResult;
  87. }
  88. /**
  89. * 返回向下取整的绝对值
  90. * @param {number} n - 输入的数字
  91. * @returns {number} 返回向下取整的绝对值
  92. */
  93. export function absFloor(n : number):number {
  94. // return (n < 0 ? Math.ceil(n) || 0 : Math.floor(n))
  95. return (n < 0 ? Math.max(Math.ceil(n), 0) : Math.floor(n))
  96. }