Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

tablesort.js 1.9 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  1. // Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. // tablesort.filesize.min.js
  3. !(function () {
  4. const filesizeRegex = /^(\d+(\.\d+)?) ?((K|M|G|T|P|E|Z|Y|B$)i?B?)$/i;
  5. function r(t) {
  6. t = t.match(filesizeRegex);
  7. if (!t) {
  8. return 0;
  9. }
  10. const value = parseFloat(t[1].replace(/[^\-?0-9.]/g, ""));
  11. const unit = t[3].toLowerCase();
  12. const base = unit[1] === "i" ? 1024 : 1e3;
  13. const powers = { k: 2, m: 3, g: 4, t: 5, p: 6, e: 7, z: 8, y: 9 };
  14. return value * (powers[unit[0]] ? Math.pow(base, powers[unit[0]]) : base);
  15. }
  16. Tablesort.extend(
  17. "filesize",
  18. (t) => filesizeRegex.test(t),
  19. (t, e) => {
  20. t = r(t);
  21. e = r(e);
  22. return (isNaN(e) ? 0 : e) - (isNaN(t) ? 0 : t);
  23. },
  24. );
  25. })();
  26. // tablesort.dotsep.min.js
  27. Tablesort.extend(
  28. "dotsep",
  29. (t) => /^(\d+\.)+\d+$/.test(t),
  30. (t, r) => {
  31. t = t.split(".");
  32. r = r.split(".");
  33. for (let i = 0, s = t.length; i < s; i++) {
  34. const e = parseInt(t[i], 10);
  35. const n = parseInt(r[i], 10);
  36. if (e !== n) {
  37. return n < e ? -1 : 1;
  38. }
  39. }
  40. return 0;
  41. },
  42. );
  43. // tablesort.number.min.js
  44. (function () {
  45. const cleanNumber = (i) =>
  46. i
  47. .split("±")[0]
  48. .trim()
  49. .replace(/[^\-?0-9.]/g, "");
  50. const compareNumber = (a, b) => (parseFloat(a) || 0) - (parseFloat(b) || 0);
  51. Tablesort.extend(
  52. "number",
  53. (item) =>
  54. item.match(/^[-+]?[£\x24Û¢´€]?\d+\s*([,\.]\d{0,2})/) || // Prefixed currency
  55. item.match(/^[-+]?\d+\s*([,\.]\d{0,2})?[£\x24Û¢´€]/) || // Suffixed currency
  56. item.match(/^[-+]?(\d)*-?([,\.]){0,1}-?(\d)+([E,e][\-+][\d]+)?%?$/), // Number
  57. (a, b) => compareNumber(cleanNumber(b), cleanNumber(a)),
  58. );
  59. })();
  60. // subscribe
  61. document$.subscribe(() => {
  62. document.querySelectorAll("article table:not([class])").forEach((table) => {
  63. new Tablesort(table);
  64. });
  65. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...