utils.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. export function looksLikeObjectRepr(value: string) {
  2. const a = value[0];
  3. const z = value[value.length - 1];
  4. if (a === '<' && z === '>') {
  5. return true;
  6. }
  7. if (a === '[' && z === ']') {
  8. return true;
  9. }
  10. if (a === '(' && z === ')') {
  11. return true;
  12. }
  13. if (z === ')' && value.match(/^[\w\d._-]+\(/)) {
  14. return true;
  15. }
  16. return false;
  17. }
  18. export function looksLikeMultiLineString(value: string) {
  19. return !!value.match(/[\r\n]/);
  20. }
  21. export function padNumbersInString(string: string) {
  22. return string.replace(/(\d+)/g, (num: string) => {
  23. let isNegative = false;
  24. let realNum = parseInt(num, 10);
  25. if (realNum < 0) {
  26. realNum *= -1;
  27. isNegative = true;
  28. }
  29. let s = '0000000000000' + realNum;
  30. s = s.substr(s.length - (isNegative ? 11 : 12));
  31. if (isNegative) {
  32. s = '-' + s;
  33. }
  34. return s;
  35. });
  36. }
  37. export function naturalCaseInsensitiveSort(a: string, b: string) {
  38. a = padNumbersInString(a).toLowerCase();
  39. b = padNumbersInString(b).toLowerCase();
  40. return a === b ? 0 : a < b ? -1 : 1;
  41. }
  42. export function analyzeStringForRepr(value: string) {
  43. const rv = {
  44. repr: value,
  45. isString: true,
  46. isMultiLine: false,
  47. isStripped: false,
  48. };
  49. // stripped for security reasons
  50. if (value.match(/^['"]?\*{8,}['"]?$/)) {
  51. rv.isStripped = true;
  52. return rv;
  53. }
  54. if (looksLikeObjectRepr(value)) {
  55. rv.isString = false;
  56. return rv;
  57. }
  58. rv.isMultiLine = looksLikeMultiLineString(value);
  59. return rv;
  60. }