d3f8bb9da059e99915d47d999c4acfb3ef6cc843ee4a76a39bc59fbb723f0712f2ce8c7984b32e6bf343829e1d44834f9ee54163f1bb959df7920a9dfaa535 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. @license
  3. Rollup.js v3.29.4
  4. Sat, 21 Sep 2024 06:29:06 GMT - commit 2ef77c00ec2635d42697cff2c0567ccc8db34fb4
  5. https://github.com/rollup/rollup
  6. Released under the MIT License.
  7. */
  8. const getLogFilter = filters => {
  9. if (filters.length === 0)
  10. return () => true;
  11. const normalizedFilters = filters.map(filter => filter.split('&').map(subFilter => {
  12. const inverted = subFilter.startsWith('!');
  13. if (inverted)
  14. subFilter = subFilter.slice(1);
  15. const [key, ...value] = subFilter.split(':');
  16. return { inverted, key: key.split('.'), parts: value.join(':').split('*') };
  17. }));
  18. return (log) => {
  19. nextIntersectedFilter: for (const intersectedFilters of normalizedFilters) {
  20. for (const { inverted, key, parts } of intersectedFilters) {
  21. const isFilterSatisfied = testFilter(log, key, parts);
  22. if (inverted ? isFilterSatisfied : !isFilterSatisfied) {
  23. continue nextIntersectedFilter;
  24. }
  25. }
  26. return true;
  27. }
  28. return false;
  29. };
  30. };
  31. const testFilter = (log, key, parts) => {
  32. let rawValue = log;
  33. for (let index = 0; index < key.length; index++) {
  34. if (!rawValue) {
  35. return false;
  36. }
  37. const part = key[index];
  38. if (!(part in rawValue)) {
  39. return false;
  40. }
  41. rawValue = rawValue[part];
  42. }
  43. let value = typeof rawValue === 'object' ? JSON.stringify(rawValue) : String(rawValue);
  44. if (parts.length === 1) {
  45. return value === parts[0];
  46. }
  47. if (!value.startsWith(parts[0])) {
  48. return false;
  49. }
  50. const lastPartIndex = parts.length - 1;
  51. for (let index = 1; index < lastPartIndex; index++) {
  52. const part = parts[index];
  53. const position = value.indexOf(part);
  54. if (position === -1) {
  55. return false;
  56. }
  57. value = value.slice(position + part.length);
  58. }
  59. return value.endsWith(parts[lastPartIndex]);
  60. };
  61. export { getLogFilter };