parse.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { forEach, forInObject, isArray, isObject, isString } from "./comm";
  2. /**
  3. * 解析数据
  4. * @example parse<Response>(res.data)
  5. */
  6. export function parse<T>(data: any): T | null {
  7. // #ifdef APP-ANDROID
  8. // @ts-ignore
  9. return (data as UTSJSONObject).parse<T>();
  10. // #endif
  11. // #ifndef APP-ANDROID
  12. return data as T;
  13. // #endif
  14. }
  15. /**
  16. * 解析JSON对象
  17. * @param data 要解析的数据
  18. * @returns 解析后的JSON对象
  19. */
  20. export function parseObject<T>(data: string): T | null {
  21. // #ifdef APP-ANDROID
  22. return JSON.parseObject<T>(data);
  23. // #endif
  24. // #ifndef APP-ANDROID
  25. return JSON.parse(data) as T;
  26. // #endif
  27. }
  28. /**
  29. * 解析透传样式对象
  30. * @param data 要解析的数据
  31. * @returns 解析后的透传样式对象
  32. * @template T 透传样式对象的类型
  33. */
  34. export function parsePt<T>(data: any): T {
  35. // #ifdef APP-ANDROID
  36. // @ts-ignore
  37. return (data as UTSJSONObject).parse<T>() ?? ({} as T);
  38. // #endif
  39. // #ifndef APP-ANDROID
  40. return data as T;
  41. // #endif
  42. }
  43. /**
  44. * 解析对象为类名字符串
  45. * @param obj 要解析的对象,key为类名,value为布尔值表示是否启用该类名
  46. * @returns 解析后的类名字符串,多个类名以空格分隔
  47. * @example
  48. * parseClass({ 'active': true, 'disabled': false }) // 返回 'active'
  49. * parseClass(['ml-2', 'mr-2']) // 返回 'ml-2 mr-2'
  50. * parseClass([{ 'mr-2': true, 'mt-2': false }]) // 返回 'mr-2'
  51. * parseClass([[true, 'mr-2 pt-2', 'mb-2']]) // 返回 'mr-2 pt-2'
  52. */
  53. export const parseClass = (data: any): string => {
  54. // 存储启用的类名
  55. const names: string[] = [];
  56. // 解析数据
  57. function deep(d: any) {
  58. // 如果obj是数组,则将数组中的每个元素添加到names中
  59. if (isArray(d)) {
  60. forEach(d as any[], (value: any) => {
  61. if (isString(value)) {
  62. // @example 2
  63. names.push(value as string);
  64. } else if (isArray(value)) {
  65. // @example 4
  66. const [a, b] = value as any[];
  67. if (a as boolean) {
  68. names.push(b as string);
  69. } else {
  70. if (value.length > 2) {
  71. names.push(value[2] as string);
  72. }
  73. }
  74. } else if (isObject(value)) {
  75. // @example 3
  76. deep(value);
  77. }
  78. });
  79. }
  80. // 遍历对象的每个属性
  81. if (isObject(d)) {
  82. // @example 1
  83. forInObject(d, (value, key) => {
  84. // 如果属性值为true,则将类名添加到数组中
  85. if (value == true && key != "") {
  86. names.push(key.trim());
  87. }
  88. });
  89. }
  90. }
  91. deep(data);
  92. // 将类名数组用空格连接成字符串返回
  93. return names.join(" ");
  94. };
  95. /**
  96. * 将数值或字符串转换为rpx单位的字符串
  97. * @param val 要转换的值,可以是数字或字符串
  98. * @returns 转换后的rpx单位字符串
  99. * @example
  100. * parseRpx(10) // 返回 '10rpx'
  101. * parseRpx('10rpx') // 返回 '10rpx'
  102. * parseRpx('10px') // 返回 '10px'
  103. */
  104. export const parseRpx = (val: number | string): string => {
  105. if (typeof val == "number") {
  106. return val + "rpx";
  107. }
  108. return val;
  109. };
  110. /**
  111. * 将rpx单位转换为px单位
  112. * @param rpx 要转换的rpx值
  113. * @returns 转换后的px值
  114. * @example
  115. */
  116. export const rpx2px = (rpx: number): number => {
  117. let px: number;
  118. // #ifdef MP
  119. px = rpx / (750 / uni.getWindowInfo().windowWidth);
  120. // #endif
  121. // #ifndef MP
  122. px = uni.rpx2px(rpx);
  123. // #endif
  124. return px;
  125. };
  126. /**
  127. * 将px单位转换为rpx单位
  128. * @param px 要转换的px值
  129. * @returns 转换后的rpx值
  130. * @example
  131. */
  132. export const px2rpx = (px: number): number => {
  133. return px / rpx2px(1);
  134. };
  135. /**
  136. * 解析px单位,支持rpx单位转换
  137. * @param val 要解析的值,可以是数字或字符串
  138. * @returns 解析后的px单位
  139. */
  140. export const getPx = (val: string) => {
  141. const num = parseInt(val);
  142. if (val.includes("rpx")) {
  143. return rpx2px(num);
  144. }
  145. return Math.floor(num);
  146. };