選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

common-util.ts 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { IFactory } from '@/interfaces/database/llm';
  2. import isObject from 'lodash/isObject';
  3. import snakeCase from 'lodash/snakeCase';
  4. export const isFormData = (data: unknown): data is FormData => {
  5. return data instanceof FormData;
  6. };
  7. const excludedFields = ['img2txt_id'];
  8. const isExcludedField = (key: string) => {
  9. return excludedFields.includes(key);
  10. };
  11. export const convertTheKeysOfTheObjectToSnake = (data: unknown) => {
  12. if (isObject(data) && !isFormData(data)) {
  13. return Object.keys(data).reduce<Record<string, any>>((pre, cur) => {
  14. const value = (data as Record<string, any>)[cur];
  15. pre[isFormData(value) || isExcludedField(cur) ? cur : snakeCase(cur)] =
  16. value;
  17. return pre;
  18. }, {});
  19. }
  20. return data;
  21. };
  22. export const getSearchValue = (key: string) => {
  23. const params = new URL(document.location as any).searchParams;
  24. return params.get(key);
  25. };
  26. // Formatize numbers, add thousands of separators
  27. export const formatNumberWithThousandsSeparator = (numberStr: string) => {
  28. const formattedNumber = numberStr.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  29. return formattedNumber;
  30. };
  31. const orderFactoryList = [
  32. 'OpenAI',
  33. 'Moonshot',
  34. 'ZHIPU-AI',
  35. 'Ollama',
  36. 'Xinference',
  37. ];
  38. export const sortLLmFactoryListBySpecifiedOrder = (list: IFactory[]) => {
  39. const finalList: IFactory[] = [];
  40. orderFactoryList.forEach((orderItem) => {
  41. const index = list.findIndex((item) => item.name === orderItem);
  42. if (index !== -1) {
  43. finalList.push(list[index]);
  44. }
  45. });
  46. list.forEach((item) => {
  47. if (finalList.every((x) => x.name !== item.name)) {
  48. finalList.push(item);
  49. }
  50. });
  51. return finalList;
  52. };
  53. export const filterOptionsByInput = (
  54. input: string,
  55. option: { label: string; value: string } | undefined,
  56. ) => (option?.label ?? '').toLowerCase().includes(input.toLowerCase());
  57. export const toFixed = (value: unknown, fixed = 2) => {
  58. if (typeof value === 'number') {
  59. return value.toFixed(fixed);
  60. }
  61. return value;
  62. };
  63. export const stringToUint8Array = (str: string) => {
  64. // const byteString = str.replace(/b'|'/g, '');
  65. const byteString = str.slice(2, -1);
  66. const uint8Array = new Uint8Array(byteString.length);
  67. for (let i = 0; i < byteString.length; i++) {
  68. uint8Array[i] = byteString.charCodeAt(i);
  69. }
  70. return uint8Array;
  71. };
  72. export const hexStringToUint8Array = (hex: string) => {
  73. const arr = hex.match(/[\da-f]{2}/gi);
  74. if (Array.isArray(arr)) {
  75. return new Uint8Array(
  76. arr.map(function (h) {
  77. return parseInt(h, 16);
  78. }),
  79. );
  80. }
  81. };
  82. export function hexToArrayBuffer(input: string) {
  83. if (typeof input !== 'string') {
  84. throw new TypeError('Expected input to be a string');
  85. }
  86. if (input.length % 2 !== 0) {
  87. throw new RangeError('Expected string to be an even number of characters');
  88. }
  89. const view = new Uint8Array(input.length / 2);
  90. for (let i = 0; i < input.length; i += 2) {
  91. view[i / 2] = parseInt(input.substring(i, i + 2), 16);
  92. }
  93. return view.buffer;
  94. }