util.uts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // import {isNumber} from '@/uni_modules/lime-shared/isNumber'
  2. // import {isString} from '@/uni_modules/lime-shared/isString'
  3. // import {isNumeric} from '@/uni_modules/lime-shared/isNumeric'
  4. // #ifdef APP-ANDROID
  5. import BigDecimal from 'java.math.BigDecimal'
  6. // #endif
  7. export function isNumber(value: any|null):boolean{
  8. // #ifdef APP-ANDROID
  9. return ['Byte', 'UByte','Short','UShort','Int','UInt','Long','ULong','Float','Double','number'].includes(typeof value)
  10. // #endif
  11. // #ifdef APP-IOS
  12. return ['Int8', 'UInt8','Int16','UInt16','Int32','UInt32','Int64','UInt64','Int','UInt','Float','Float16','Float32','Float64','Double', 'number'].includes(typeof value)
  13. // #endif
  14. // #ifndef APP-ANDROID || APP-IOS
  15. return typeof value == 'number' && !isNaN(value);
  16. // #endif
  17. }
  18. export function isString(value: any|null):boolean{
  19. return typeof value == 'string';
  20. }
  21. export function isNumeric(value: any|null):boolean{
  22. if(isNumber(value)) {
  23. return true
  24. } else if(isString(value)) {
  25. // const regex = "-?\\d+(\\.\\d+)?".toRegex()
  26. const regex = new RegExp("^(-)?\\d+(\\.\\d+)?$")
  27. return regex.test(value as string) //regex.matches(value as string)
  28. }
  29. return false
  30. }
  31. export function toBoolean(value: any|null):boolean{
  32. // #ifdef APP
  33. // 根据输入值的类型,返回相应的布尔值
  34. if(isNumber(value)){
  35. return (value as number) != 0;
  36. }
  37. if(isString(value)){
  38. return `${value}`.length > 0;
  39. }
  40. if(typeof value == 'boolean'){
  41. return value as boolean;
  42. }
  43. return value != null
  44. // #endif
  45. // #ifndef APP
  46. return Boolean(value)
  47. // #endif
  48. }
  49. /**
  50. * Check to see if string passed in is a percentage
  51. * 检查传入的字符串是否为百分比
  52. * @hidden
  53. */
  54. export function isPercentage(n : any) : boolean {
  55. return isString(n) && (n as string).indexOf('%') != -1;
  56. }
  57. /**
  58. * Need to handle 1.0 as 100%, since once it is a number, there is no difference between it and 1
  59. * 需要处理 1.0 为 100%,因为一旦它是数字,它与 1 没有区别
  60. * <http://stackoverflow.com/questions/7422072/javascript-how-to-detect-number-as-a-decimal-including-1-0>
  61. * @hidden
  62. */
  63. export function isOnePointZero(n : any) : boolean {
  64. return isString(n) && (n as string).indexOf('.') != -1 && parseFloat(n as string) == 1;
  65. }
  66. /**
  67. * Take input from [0, n] and return it as [0, 1]
  68. * 将输入值从 [0, n] 转换为 [0, 1]
  69. * @hidden
  70. */
  71. function bound01(n: string, max: number): number
  72. function bound01(n: number, max: number): number
  73. // #ifndef APP
  74. function bound01(n : any, max : number) : number
  75. // #endif
  76. function bound01(n : any, max : number) : number {
  77. if(!(isNumber(n) || isString(n))){
  78. return 1
  79. }
  80. if (isOnePointZero(n)) {
  81. n = '100%';
  82. }
  83. const isPercent = isPercentage(n);
  84. n = (isNumber(n) ? n : parseFloat(n as string)) as number
  85. n = max == 360 ? n : Math.min(max, Math.max(0, n));
  86. // Automatically convert percentage into number
  87. // 自动将百分比转换为数字
  88. if (isPercent) {
  89. n = parseInt(`${Math.min(n, 100) * max}`, 10) / 100;
  90. }
  91. // Handle floating point rounding errors
  92. // 处理浮点数舍入误差
  93. if ( Math.abs(n - max) < 0.000001) {
  94. return 1;
  95. }
  96. // Convert into [0, 1] range if it isn't already
  97. // 如果它还不是,将其转换为 [0, 1] 范围
  98. if (max == 360) {
  99. // If n is a hue given in degrees,
  100. // wrap around out-of-range values into [0, 360] range
  101. // then convert into [0, 1].
  102. // 如果 n 是以度为单位的色调,
  103. // 将超出范围的值环绕到 [0, 360] 范围内
  104. // 然后将其转换为 [0, 1]。
  105. n = (n < 0 ? (n % max) + max : n % max) / max // parseFloat(`${max}`);
  106. } else {
  107. // If n not a hue given in degrees
  108. // Convert into [0, 1] range if it isn't already.
  109. // 如果 n 不是以度为单位的色调
  110. // 如果它还不是,将其转换为 [0, 1] 范围。
  111. n = (n % max) / max //parseFloat(`${max}`);
  112. }
  113. return n;
  114. }
  115. export {bound01}
  116. /**
  117. * Force a number between 0 and 1
  118. * 在 0 和 1 之间强制一个数字
  119. * @hidden
  120. */
  121. export function clamp01(val : number) : number {
  122. return Math.min(1, Math.max(0, val));
  123. }
  124. /**
  125. * Return a valid alpha value [0,1] with all invalid values being set to 1
  126. * 返回一个有效的 alpha 值 [0,1],将所有无效值设置为 1
  127. * @hidden
  128. */
  129. function boundAlpha(a: number):number
  130. function boundAlpha(a: string):number
  131. // #ifndef APP
  132. function boundAlpha(a: any|null) : number
  133. // #endif
  134. function boundAlpha(a: any|null) : number {
  135. let n = a == null ? 1 : (isString(a) ? parseFloat(a as string) : a as number)
  136. if (isNaN(n) || n < 0 || n > 1) {
  137. n = 1;
  138. }
  139. return n;
  140. }
  141. export {
  142. boundAlpha
  143. }
  144. /**
  145. * Replace a decimal with it's percentage value
  146. * 用百分比值替换小数
  147. * number | string
  148. * @hidden
  149. */
  150. function convertToPercentage(n:number):number
  151. function convertToPercentage(n:string):string
  152. // #ifndef APP
  153. function convertToPercentage(n:any): any
  154. // #endif
  155. function convertToPercentage(n:any): any{
  156. // #ifdef APP-ANDROID
  157. n = isNumeric(n) ? parseFloat(typeof n == 'string' ? n as string : BigDecimal.valueOf((n as number).toDouble()).toPlainString()) : n// as number
  158. // #endif
  159. // #ifndef APP-ANDROID
  160. n = isNumeric(n) ? parseFloat(`${n}`) : n// as number
  161. // #endif
  162. if(isNumber(n) && (n as number) <= 1){
  163. return `${n * 100}%`.replace('.0%','%');
  164. }
  165. return n;
  166. }
  167. export {convertToPercentage}
  168. /**
  169. * Force a hex value to have 2 characters
  170. * 强制使十六进制值具有 2 个字符
  171. * @hidden
  172. */
  173. export function pad2(c : string) : string {
  174. //c.padStart(2, '0');//
  175. return c.length == 1 ? '0' + c : `${c}`;
  176. }