Format.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. export const getKeyValueListFromJSON = (
  67. paramJSON: string
  68. ): { key: string; value: string }[] => {
  69. try {
  70. const obj = JSON.parse(paramJSON);
  71. const pairs: { key: string; value: string }[] = Object.entries(obj).map(
  72. ([key, value]) => ({
  73. key,
  74. value: value as string,
  75. })
  76. );
  77. return pairs;
  78. } catch (err) {
  79. throw err;
  80. }
  81. };
  82. // BinarySubstructure includes Superstructure and Substructure
  83. export const checkIsBinarySubstructure = (metricLabel: string): boolean => {
  84. return metricLabel === 'Superstructure' || metricLabel === 'Substructure';
  85. };
  86. export const getCreateFieldType = (config: Field): CreateFieldType => {
  87. if (config.is_primary_key) {
  88. return 'primaryKey';
  89. }
  90. if (config.isDefault) {
  91. return 'defaultVector';
  92. }
  93. const vectorTypes = [DataTypeEnum.BinaryVector, DataTypeEnum.FloatVector];
  94. if (vectorTypes.includes(config.data_type)) {
  95. return 'vector';
  96. }
  97. return 'number';
  98. };