Format.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { BYTE_UNITS } from '../consts/Util';
  2. import {
  3. CreateFieldType,
  4. DataTypeEnum,
  5. Field,
  6. } from '../pages/collections/Types';
  7. /**
  8. * transform large capacity to capacity in b.
  9. * @param capacity like: 10g, 10gb, 10m, 10mb, 10k, 10kb, 10b,
  10. * @return number
  11. */
  12. export const parseByte = (capacity?: string | number): number => {
  13. // if it's number return it
  14. if (!isNaN(Number(capacity))) {
  15. return capacity as number;
  16. }
  17. // capacity is '' or 0 or undefined
  18. if (!capacity) {
  19. return 0;
  20. }
  21. let lowerCapacity = (capacity as string).toLowerCase();
  22. const units = BYTE_UNITS;
  23. const isAlpha = /[a-zA-Z]/;
  24. const lastStr = lowerCapacity.charAt(lowerCapacity.length - 1);
  25. const secLastStr = lowerCapacity.charAt(lowerCapacity.length - 2);
  26. // if last two alpha is string, like: mb gb kb.
  27. // delete last alpha b
  28. if (isAlpha.test(lastStr) && isAlpha.test(secLastStr)) {
  29. lastStr === 'b' &&
  30. (lowerCapacity = lowerCapacity.slice(0, lowerCapacity.length - 1));
  31. }
  32. const suffix = lowerCapacity.charAt(lowerCapacity.length - 1);
  33. const digitsPart = lowerCapacity.slice(0, lowerCapacity.length - 1);
  34. if (units[suffix]) {
  35. return Number(digitsPart) * units[suffix];
  36. }
  37. throw new Error(
  38. 'The specified value for memory ({0}) should specify the units. The postfix should be one of the `b` `k` `m` `g` characters'
  39. );
  40. };
  41. /**
  42. *
  43. * @param search ?name=czz&age=18
  44. * @returns {name:'czz',age:'18'}
  45. */
  46. export const parseLocationSearch = (search: string) => {
  47. const pairs = search.substring(1).split('&');
  48. let obj: any = {};
  49. for (let i in pairs) {
  50. if (pairs[i] === '') continue;
  51. const pair = pairs[i].split('=');
  52. obj[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
  53. }
  54. return obj;
  55. };
  56. export const getEnumKeyByValue = (enumObj: any, enumValue: any) => {
  57. const match = Object.entries(enumObj).find(
  58. ([, value]) => value === enumValue
  59. );
  60. if (match) {
  61. const [key] = match;
  62. return key;
  63. }
  64. return '--';
  65. };
  66. /**
  67. *
  68. * @param obj e.g. {name: 'test'}
  69. * @returns key value pair, e.g. [{key: 'name', value: 'test'}]
  70. */
  71. export const getKeyValuePairFromObj = (
  72. obj: { [key in string]: any }
  73. ): { key: string; value: any }[] => {
  74. const pairs: { key: string; value: string }[] = Object.entries(obj).map(
  75. ([key, value]) => ({
  76. key,
  77. value: value as string,
  78. })
  79. );
  80. return pairs;
  81. };
  82. export const getKeyValueListFromJsonString = (
  83. json: string
  84. ): { key: string; value: string }[] => {
  85. try {
  86. const obj = JSON.parse(json);
  87. const pairs = getKeyValuePairFromObj(obj);
  88. return pairs;
  89. } catch (err) {
  90. throw err;
  91. }
  92. };
  93. // BinarySubstructure includes Superstructure and Substructure
  94. export const checkIsBinarySubstructure = (metricLabel: string): boolean => {
  95. return metricLabel === 'Superstructure' || metricLabel === 'Substructure';
  96. };
  97. export const getCreateFieldType = (config: Field): CreateFieldType => {
  98. if (config.is_primary_key) {
  99. return 'primaryKey';
  100. }
  101. if (config.isDefault) {
  102. return 'defaultVector';
  103. }
  104. const vectorTypes = [DataTypeEnum.BinaryVector, DataTypeEnum.FloatVector];
  105. if (vectorTypes.includes(config.data_type)) {
  106. return 'vector';
  107. }
  108. return 'number';
  109. };
  110. // Trim the address
  111. export const formatAddress = (address: string): string => address.trim();