@hedeqiang/dsh-nl2sql 0.12.1 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/xlsx.js CHANGED
@@ -208,6 +208,16 @@ const STYLES_XML = XML_HEAD +
208
208
 
209
209
  const SHEET_NAME_BAD_RE = /[\[\]:*?/\\]/g;
210
210
 
211
+ /** First `max` UTF-16 units of `s` (Excel's sheet-name limit), never splitting a surrogate pair. */
212
+ function utf16Prefix(s, max) {
213
+ let out = '';
214
+ for (const ch of s) {
215
+ if (out.length + ch.length > max) break;
216
+ out += ch;
217
+ }
218
+ return out;
219
+ }
220
+
211
221
  /** Make Excel-safe, unique (case-insensitive), non-empty sheet names. */
212
222
  function sanitizeSheetNames(names) {
213
223
  const used = new Set(['history']); // "History" is reserved by Excel
@@ -215,11 +225,11 @@ function sanitizeSheetNames(names) {
215
225
  let base = stripIllegal(String(raw ?? '')).replace(SHEET_NAME_BAD_RE, '_').replace(/[\r\n\t]/g, ' ').trim();
216
226
  base = base.replace(/^'+|'+$/g, '').trim();
217
227
  if (!base) base = `Sheet${i + 1}`;
218
- base = Array.from(base).slice(0, 31).join('');
228
+ base = utf16Prefix(base, 31);
219
229
  let name = base;
220
230
  for (let n = 2; used.has(name.toLowerCase()); n++) {
221
231
  const suffix = ` (${n})`;
222
- name = Array.from(base).slice(0, 31 - suffix.length).join('').trimEnd() + suffix;
232
+ name = utf16Prefix(base, 31 - suffix.length).trimEnd() + suffix;
223
233
  }
224
234
  used.add(name.toLowerCase());
225
235
  return name;