You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.ts 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * @Author: 赵伟
  3. * @Date: 2024-03-25 13:52:54
  4. * @Description: 工具类
  5. */
  6. import G6 from '@antv/g6';
  7. // 生成 8 位随机数
  8. export function s8() {
  9. return (((1 + Math.random()) * 0x100000000) | 0).toString(16).substring(1);
  10. }
  11. export function getNameByCode(list: any[], code: any) {
  12. let name = '';
  13. list.forEach((item) => {
  14. if (item.dictValue === code) name = item.dictLabel;
  15. });
  16. return name;
  17. }
  18. // 解析 json 字符串
  19. export function parseJsonText(text?: string | null): any | null {
  20. if (!text) {
  21. return null;
  22. }
  23. try {
  24. return JSON.parse(text);
  25. } catch (error) {
  26. return null;
  27. }
  28. }
  29. // 判断是否为对象
  30. function isPlainObject(value: any) {
  31. if (value === null || typeof value !== 'object') return false;
  32. let proto = Object.getPrototypeOf(value);
  33. while (proto !== null) {
  34. if (proto.constructor && proto.constructor !== Object) return false;
  35. proto = Object.getPrototypeOf(proto);
  36. }
  37. return true;
  38. }
  39. // underscore to camelCase
  40. export function underscoreToCamelCase(obj: Record<string, any>) {
  41. if (!isPlainObject(obj)) {
  42. return obj;
  43. }
  44. const newObj: Record<string, any> = {};
  45. for (const key in obj) {
  46. if (obj.hasOwnProperty(key)) {
  47. const newKey = key.replace(/([-_][a-z])/gi, function ($1) {
  48. return $1.toUpperCase().replace('[-_]', '').replace('_', '');
  49. });
  50. let value = obj[key];
  51. if (Array.isArray(value)) {
  52. value = value.map((item) => underscoreToCamelCase(item));
  53. } else if (isPlainObject(value)) {
  54. value = underscoreToCamelCase(value);
  55. }
  56. newObj[newKey] = value;
  57. }
  58. }
  59. return newObj;
  60. }
  61. // camelCase to underscore
  62. export function camelCaseToUnderscore(obj: Record<string, any>) {
  63. if (!isPlainObject(obj)) {
  64. return obj;
  65. }
  66. const newObj: Record<string, any> = {};
  67. for (const key in obj) {
  68. if (obj.hasOwnProperty(key)) {
  69. const newKey = key.replace(/([A-Z])/g, '_$1').toLowerCase();
  70. let value = obj[key];
  71. if (Array.isArray(value)) {
  72. value = value.map((item) => camelCaseToUnderscore(item));
  73. } else if (isPlainObject(value)) {
  74. value = camelCaseToUnderscore(value);
  75. }
  76. newObj[newKey] = value;
  77. }
  78. }
  79. return newObj;
  80. }
  81. // null to undefined
  82. export function nullToUndefined(obj: Record<string, any>) {
  83. if (!isPlainObject(obj)) {
  84. return obj;
  85. }
  86. const newObj: Record<string, any> = {};
  87. for (const key in obj) {
  88. if (obj.hasOwnProperty(key)) {
  89. const value = obj[key];
  90. if (value === null) {
  91. newObj[key] = undefined;
  92. } else if (Array.isArray(value)) {
  93. newObj[key] = value.map((item) => nullToUndefined(item));
  94. } else if (isPlainObject(value)) {
  95. newObj[key] = nullToUndefined(value);
  96. } else {
  97. newObj[key] = value;
  98. }
  99. }
  100. }
  101. return newObj;
  102. }
  103. // undefined to null
  104. export function undefinedToNull(obj: Record<string, any>) {
  105. if (!isPlainObject(obj)) {
  106. return obj;
  107. }
  108. const newObj: Record<string, any> = {};
  109. for (const key in obj) {
  110. if (obj.hasOwnProperty(key)) {
  111. const value = obj[key];
  112. if (value === undefined) {
  113. newObj[key] = null;
  114. } else if (Array.isArray(value)) {
  115. newObj[key] = value.map((item) => undefinedToNull(item));
  116. } else if (isPlainObject(value)) {
  117. newObj[key] = undefinedToNull(value);
  118. } else {
  119. newObj[key] = value;
  120. }
  121. }
  122. }
  123. return newObj;
  124. }
  125. /**
  126. * Changes the property names of an object based on a mapping provided.
  127. *
  128. * @param obj - The object whose property names need to be changed.
  129. * @param mapping - The mapping of old property names to new property names.
  130. * @return The object with the changed property names.
  131. */
  132. export function changePropertyName(obj: Record<string, any>, mapping: Record<string, string>) {
  133. if (!isPlainObject(obj)) {
  134. return obj;
  135. }
  136. const newObj: Record<string, any> = {};
  137. for (const key in obj) {
  138. if (obj.hasOwnProperty(key)) {
  139. let value = obj[key];
  140. const newKey = mapping.hasOwnProperty(key) ? mapping[key] : key;
  141. if (Array.isArray(value)) {
  142. value = value.map((item) => changePropertyName(item, mapping));
  143. } else if (isPlainObject(value)) {
  144. value = changePropertyName(value, mapping);
  145. }
  146. newObj[newKey] = value;
  147. }
  148. }
  149. return newObj;
  150. }
  151. /**
  152. * 计算显示的字符串
  153. * @param tr 要裁剪的字符串
  154. * @param maxWidth 最大宽度
  155. * @param fontSize 字体大小
  156. * @return 处理后的字符串
  157. */
  158. export const fittingString = (str: string, maxWidth: number, fontSize: number) => {
  159. if (!str) {
  160. return '';
  161. }
  162. const ellipsis = '...';
  163. const ellipsisLength = G6.Util.getTextSize(ellipsis, fontSize)[0];
  164. let currentWidth = 0;
  165. let res = str;
  166. const pattern = new RegExp('[\u4E00-\u9FA5]+'); // distinguish the Chinese charactors and letters
  167. str.split('').forEach((letter, i) => {
  168. if (currentWidth > maxWidth - ellipsisLength) return;
  169. if (pattern.test(letter)) {
  170. // Chinese charactors
  171. currentWidth += fontSize;
  172. } else {
  173. // get the width of single letter according to the fontSize
  174. currentWidth += G6.Util.getLetterWidth(letter, fontSize);
  175. }
  176. if (currentWidth > maxWidth - ellipsisLength) {
  177. res = `${str.substring(0, i)}${ellipsis}`;
  178. }
  179. });
  180. return res;
  181. };