hack.js 613 B

1234567891011
  1. export function removeRedundantSemicolons(data) {
  2. if (Array.isArray(data)) return data.map(item => removeRedundantSemicolons(item));
  3. if (data && typeof data === 'object') return Object.fromEntries(
  4. Object.entries(data).map(([key, value]) => [key, removeRedundantSemicolons(value)]),
  5. );
  6. if (typeof data === 'string') return data
  7. .replace(/^[\s;]+/, '') // 去除开头的分号和空白
  8. .replace(/\s*;+\s*/g, ';') // 分号和两边空白合并为一个分号
  9. .replace(/;{2,}/g, ';'); // 再次合并可能出现的连续分号
  10. return data;
  11. }