@nyaomaru/divider 2.0.5 → 2.0.6
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/dist/index.cjs +14 -93
- package/dist/index.js +14 -93
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -122,10 +122,7 @@ function isNoneMode(value) {
|
|
|
122
122
|
function normalizeSeparators(separators) {
|
|
123
123
|
return Array.from(new Set(separators)).filter(
|
|
124
124
|
(separator) => !isEmptyString(separator)
|
|
125
|
-
)
|
|
126
|
-
}
|
|
127
|
-
function createCacheKey(normalizedSeparators) {
|
|
128
|
-
return normalizedSeparators.join(CACHE_KEY_SEPARATOR);
|
|
125
|
+
);
|
|
129
126
|
}
|
|
130
127
|
var RegexCache = class {
|
|
131
128
|
cache = /* @__PURE__ */ new Map();
|
|
@@ -141,15 +138,6 @@ var RegexCache = class {
|
|
|
141
138
|
*/
|
|
142
139
|
get(separators) {
|
|
143
140
|
const key = this.createKey(separators);
|
|
144
|
-
return this.getByKey(key);
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Retrieves a cached RegExp for a precomputed cache key.
|
|
148
|
-
*
|
|
149
|
-
* @param key - Cache key string
|
|
150
|
-
* @returns Cached RegExp or null if not found
|
|
151
|
-
*/
|
|
152
|
-
getByKey(key) {
|
|
153
141
|
const regex = this.cache.get(key);
|
|
154
142
|
if (regex) {
|
|
155
143
|
this.cache.delete(key);
|
|
@@ -165,15 +153,6 @@ var RegexCache = class {
|
|
|
165
153
|
*/
|
|
166
154
|
set(separators, regex) {
|
|
167
155
|
const key = this.createKey(separators);
|
|
168
|
-
this.setByKey(key, regex);
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Stores a RegExp in the cache for a precomputed cache key.
|
|
172
|
-
*
|
|
173
|
-
* @param key - Cache key string
|
|
174
|
-
* @param regex - Compiled RegExp to cache
|
|
175
|
-
*/
|
|
176
|
-
setByKey(key, regex) {
|
|
177
156
|
if (this.cache.has(key)) {
|
|
178
157
|
this.cache.delete(key);
|
|
179
158
|
}
|
|
@@ -194,7 +173,7 @@ var RegexCache = class {
|
|
|
194
173
|
*/
|
|
195
174
|
createKey(separators) {
|
|
196
175
|
const normalizedSeparators = normalizeSeparators(separators);
|
|
197
|
-
return
|
|
176
|
+
return normalizedSeparators.join(CACHE_KEY_SEPARATOR);
|
|
198
177
|
}
|
|
199
178
|
/**
|
|
200
179
|
* Gets current cache size for debugging/monitoring.
|
|
@@ -215,16 +194,15 @@ var RegexCache = class {
|
|
|
215
194
|
var regexCache = new RegexCache();
|
|
216
195
|
function getRegex(separators) {
|
|
217
196
|
if (isEmptyArray(separators)) return null;
|
|
197
|
+
const cached = regexCache.get(separators);
|
|
198
|
+
if (cached) return cached;
|
|
218
199
|
const uniqueSeparators = normalizeSeparators(separators);
|
|
219
200
|
if (uniqueSeparators.length === 0) {
|
|
220
201
|
return null;
|
|
221
202
|
}
|
|
222
|
-
const cacheKey = createCacheKey(uniqueSeparators);
|
|
223
|
-
const cached = regexCache.getByKey(cacheKey);
|
|
224
|
-
if (cached) return cached;
|
|
225
203
|
const pattern = uniqueSeparators.map(escapeRegExp).join("|");
|
|
226
204
|
const regex = new RegExp(`(?:${pattern})`, "g");
|
|
227
|
-
regexCache.
|
|
205
|
+
regexCache.set(separators, regex);
|
|
228
206
|
return regex;
|
|
229
207
|
}
|
|
230
208
|
function escapeRegExp(str) {
|
|
@@ -444,41 +422,13 @@ function dividePreserve(input, separator) {
|
|
|
444
422
|
if (isEmptyString(input)) return [""];
|
|
445
423
|
return divider(input, separator, { preserveEmpty: true });
|
|
446
424
|
}
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
for (let index = 0; index < text.length; index++) {
|
|
450
|
-
if (text[index] !== quote) continue;
|
|
451
|
-
if (text[index + 1] === quote) {
|
|
452
|
-
index++;
|
|
453
|
-
continue;
|
|
454
|
-
}
|
|
455
|
-
count++;
|
|
456
|
-
}
|
|
457
|
-
return count;
|
|
458
|
-
};
|
|
459
|
-
var countUnescapedMultiChar = (text, quote) => {
|
|
460
|
-
const escapedPair = quote + quote;
|
|
461
|
-
const quoteLength = quote.length;
|
|
462
|
-
const escapedPairLength = escapedPair.length;
|
|
425
|
+
function countUnescaped(text, quote) {
|
|
426
|
+
const pair = quote + quote;
|
|
463
427
|
let count = 0;
|
|
464
|
-
for (
|
|
465
|
-
|
|
466
|
-
index += escapedPairLength;
|
|
467
|
-
continue;
|
|
468
|
-
}
|
|
469
|
-
if (text.startsWith(quote, index)) {
|
|
470
|
-
count++;
|
|
471
|
-
index += quoteLength;
|
|
472
|
-
continue;
|
|
473
|
-
}
|
|
474
|
-
index++;
|
|
428
|
+
for (const chunk of dividePreserve(text, pair)) {
|
|
429
|
+
count += dividePreserve(chunk, quote).length - 1;
|
|
475
430
|
}
|
|
476
431
|
return count;
|
|
477
|
-
};
|
|
478
|
-
function countUnescaped(text, quote) {
|
|
479
|
-
if (isEmptyString(quote)) return 0;
|
|
480
|
-
if (quote.length === 1) return countUnescapedSingleChar(text, quote);
|
|
481
|
-
return countUnescapedMultiChar(text, quote);
|
|
482
432
|
}
|
|
483
433
|
function stripOuterQuotes(text, quoteChar, { lenient = true } = {}) {
|
|
484
434
|
const escapedPair = quoteChar + quoteChar;
|
|
@@ -530,8 +480,7 @@ var buildQuotedFields = (line, delimiter, quote, trim, lenient) => {
|
|
|
530
480
|
const pieces = dividePreserve(line, delimiter);
|
|
531
481
|
const state = {
|
|
532
482
|
fields: [],
|
|
533
|
-
current: ""
|
|
534
|
-
insideQuotes: false
|
|
483
|
+
current: ""
|
|
535
484
|
};
|
|
536
485
|
for (const piece of pieces) {
|
|
537
486
|
appendPiece(state, piece, delimiter, quote, trim, lenient);
|
|
@@ -541,18 +490,10 @@ var buildQuotedFields = (line, delimiter, quote, trim, lenient) => {
|
|
|
541
490
|
}
|
|
542
491
|
return state.fields;
|
|
543
492
|
};
|
|
544
|
-
var advanceQuoteState = (insideQuotes, segment, quote) => {
|
|
545
|
-
let nextInsideQuotes = insideQuotes;
|
|
546
|
-
for (const char of segment) {
|
|
547
|
-
if (char === quote) nextInsideQuotes = !nextInsideQuotes;
|
|
548
|
-
}
|
|
549
|
-
return nextInsideQuotes;
|
|
550
|
-
};
|
|
551
493
|
var appendPiece = (state, piece, delimiter, quote, trim, lenient) => {
|
|
552
|
-
|
|
553
|
-
state.current
|
|
554
|
-
|
|
555
|
-
if (!state.insideQuotes) {
|
|
494
|
+
state.current = isEmptyString(state.current) ? piece : state.current + delimiter + piece;
|
|
495
|
+
const insideQuotes = countUnescaped(state.current, quote) % 2 === 1;
|
|
496
|
+
if (!insideQuotes) {
|
|
556
497
|
flushField(state, quote, trim, lenient);
|
|
557
498
|
}
|
|
558
499
|
};
|
|
@@ -561,7 +502,6 @@ var flushField = (state, quote, trim, lenient) => {
|
|
|
561
502
|
if (trim) fieldValue = fieldValue.trim();
|
|
562
503
|
state.fields.push(fieldValue);
|
|
563
504
|
state.current = "";
|
|
564
|
-
state.insideQuotes = false;
|
|
565
505
|
};
|
|
566
506
|
|
|
567
507
|
// src/presets/csv-divider.ts
|
|
@@ -613,27 +553,8 @@ function tryExtractQuery(input) {
|
|
|
613
553
|
const url = new URL(input);
|
|
614
554
|
return url.search.startsWith(QUERY_SEPARATORS.QUESTION_MARK) ? url.search.slice(1) : url.search;
|
|
615
555
|
} catch {
|
|
616
|
-
return
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
function extractQueryFromQuestionMark(input) {
|
|
620
|
-
const fragmentIndex = input.indexOf("#");
|
|
621
|
-
const withoutFragment = fragmentIndex >= 0 ? input.slice(0, fragmentIndex) : input;
|
|
622
|
-
const questionMarkIndex = withoutFragment.indexOf(
|
|
623
|
-
QUERY_SEPARATORS.QUESTION_MARK
|
|
624
|
-
);
|
|
625
|
-
if (questionMarkIndex < 0) {
|
|
626
|
-
return withoutFragment;
|
|
627
|
-
}
|
|
628
|
-
if (questionMarkIndex === 0) {
|
|
629
|
-
return withoutFragment.slice(1);
|
|
630
|
-
}
|
|
631
|
-
const prefix = withoutFragment.slice(0, questionMarkIndex);
|
|
632
|
-
const hasQuerySeparatorBefore = prefix.includes(QUERY_SEPARATORS.AMPERSAND) || prefix.includes(QUERY_SEPARATORS.EQUALS);
|
|
633
|
-
if (hasQuerySeparatorBefore) {
|
|
634
|
-
return withoutFragment;
|
|
556
|
+
return input;
|
|
635
557
|
}
|
|
636
|
-
return withoutFragment.slice(questionMarkIndex + 1);
|
|
637
558
|
}
|
|
638
559
|
function stripLeadingQuestionMark(query) {
|
|
639
560
|
return query.startsWith(QUERY_SEPARATORS.QUESTION_MARK) ? query.slice(1) : query;
|
package/dist/index.js
CHANGED
|
@@ -88,10 +88,7 @@ function isNoneMode(value) {
|
|
|
88
88
|
function normalizeSeparators(separators) {
|
|
89
89
|
return Array.from(new Set(separators)).filter(
|
|
90
90
|
(separator) => !isEmptyString(separator)
|
|
91
|
-
)
|
|
92
|
-
}
|
|
93
|
-
function createCacheKey(normalizedSeparators) {
|
|
94
|
-
return normalizedSeparators.join(CACHE_KEY_SEPARATOR);
|
|
91
|
+
);
|
|
95
92
|
}
|
|
96
93
|
var RegexCache = class {
|
|
97
94
|
cache = /* @__PURE__ */ new Map();
|
|
@@ -107,15 +104,6 @@ var RegexCache = class {
|
|
|
107
104
|
*/
|
|
108
105
|
get(separators) {
|
|
109
106
|
const key = this.createKey(separators);
|
|
110
|
-
return this.getByKey(key);
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Retrieves a cached RegExp for a precomputed cache key.
|
|
114
|
-
*
|
|
115
|
-
* @param key - Cache key string
|
|
116
|
-
* @returns Cached RegExp or null if not found
|
|
117
|
-
*/
|
|
118
|
-
getByKey(key) {
|
|
119
107
|
const regex = this.cache.get(key);
|
|
120
108
|
if (regex) {
|
|
121
109
|
this.cache.delete(key);
|
|
@@ -131,15 +119,6 @@ var RegexCache = class {
|
|
|
131
119
|
*/
|
|
132
120
|
set(separators, regex) {
|
|
133
121
|
const key = this.createKey(separators);
|
|
134
|
-
this.setByKey(key, regex);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Stores a RegExp in the cache for a precomputed cache key.
|
|
138
|
-
*
|
|
139
|
-
* @param key - Cache key string
|
|
140
|
-
* @param regex - Compiled RegExp to cache
|
|
141
|
-
*/
|
|
142
|
-
setByKey(key, regex) {
|
|
143
122
|
if (this.cache.has(key)) {
|
|
144
123
|
this.cache.delete(key);
|
|
145
124
|
}
|
|
@@ -160,7 +139,7 @@ var RegexCache = class {
|
|
|
160
139
|
*/
|
|
161
140
|
createKey(separators) {
|
|
162
141
|
const normalizedSeparators = normalizeSeparators(separators);
|
|
163
|
-
return
|
|
142
|
+
return normalizedSeparators.join(CACHE_KEY_SEPARATOR);
|
|
164
143
|
}
|
|
165
144
|
/**
|
|
166
145
|
* Gets current cache size for debugging/monitoring.
|
|
@@ -181,16 +160,15 @@ var RegexCache = class {
|
|
|
181
160
|
var regexCache = new RegexCache();
|
|
182
161
|
function getRegex(separators) {
|
|
183
162
|
if (isEmptyArray(separators)) return null;
|
|
163
|
+
const cached = regexCache.get(separators);
|
|
164
|
+
if (cached) return cached;
|
|
184
165
|
const uniqueSeparators = normalizeSeparators(separators);
|
|
185
166
|
if (uniqueSeparators.length === 0) {
|
|
186
167
|
return null;
|
|
187
168
|
}
|
|
188
|
-
const cacheKey = createCacheKey(uniqueSeparators);
|
|
189
|
-
const cached = regexCache.getByKey(cacheKey);
|
|
190
|
-
if (cached) return cached;
|
|
191
169
|
const pattern = uniqueSeparators.map(escapeRegExp).join("|");
|
|
192
170
|
const regex = new RegExp(`(?:${pattern})`, "g");
|
|
193
|
-
regexCache.
|
|
171
|
+
regexCache.set(separators, regex);
|
|
194
172
|
return regex;
|
|
195
173
|
}
|
|
196
174
|
function escapeRegExp(str) {
|
|
@@ -410,41 +388,13 @@ function dividePreserve(input, separator) {
|
|
|
410
388
|
if (isEmptyString(input)) return [""];
|
|
411
389
|
return divider(input, separator, { preserveEmpty: true });
|
|
412
390
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
for (let index = 0; index < text.length; index++) {
|
|
416
|
-
if (text[index] !== quote) continue;
|
|
417
|
-
if (text[index + 1] === quote) {
|
|
418
|
-
index++;
|
|
419
|
-
continue;
|
|
420
|
-
}
|
|
421
|
-
count++;
|
|
422
|
-
}
|
|
423
|
-
return count;
|
|
424
|
-
};
|
|
425
|
-
var countUnescapedMultiChar = (text, quote) => {
|
|
426
|
-
const escapedPair = quote + quote;
|
|
427
|
-
const quoteLength = quote.length;
|
|
428
|
-
const escapedPairLength = escapedPair.length;
|
|
391
|
+
function countUnescaped(text, quote) {
|
|
392
|
+
const pair = quote + quote;
|
|
429
393
|
let count = 0;
|
|
430
|
-
for (
|
|
431
|
-
|
|
432
|
-
index += escapedPairLength;
|
|
433
|
-
continue;
|
|
434
|
-
}
|
|
435
|
-
if (text.startsWith(quote, index)) {
|
|
436
|
-
count++;
|
|
437
|
-
index += quoteLength;
|
|
438
|
-
continue;
|
|
439
|
-
}
|
|
440
|
-
index++;
|
|
394
|
+
for (const chunk of dividePreserve(text, pair)) {
|
|
395
|
+
count += dividePreserve(chunk, quote).length - 1;
|
|
441
396
|
}
|
|
442
397
|
return count;
|
|
443
|
-
};
|
|
444
|
-
function countUnescaped(text, quote) {
|
|
445
|
-
if (isEmptyString(quote)) return 0;
|
|
446
|
-
if (quote.length === 1) return countUnescapedSingleChar(text, quote);
|
|
447
|
-
return countUnescapedMultiChar(text, quote);
|
|
448
398
|
}
|
|
449
399
|
function stripOuterQuotes(text, quoteChar, { lenient = true } = {}) {
|
|
450
400
|
const escapedPair = quoteChar + quoteChar;
|
|
@@ -496,8 +446,7 @@ var buildQuotedFields = (line, delimiter, quote, trim, lenient) => {
|
|
|
496
446
|
const pieces = dividePreserve(line, delimiter);
|
|
497
447
|
const state = {
|
|
498
448
|
fields: [],
|
|
499
|
-
current: ""
|
|
500
|
-
insideQuotes: false
|
|
449
|
+
current: ""
|
|
501
450
|
};
|
|
502
451
|
for (const piece of pieces) {
|
|
503
452
|
appendPiece(state, piece, delimiter, quote, trim, lenient);
|
|
@@ -507,18 +456,10 @@ var buildQuotedFields = (line, delimiter, quote, trim, lenient) => {
|
|
|
507
456
|
}
|
|
508
457
|
return state.fields;
|
|
509
458
|
};
|
|
510
|
-
var advanceQuoteState = (insideQuotes, segment, quote) => {
|
|
511
|
-
let nextInsideQuotes = insideQuotes;
|
|
512
|
-
for (const char of segment) {
|
|
513
|
-
if (char === quote) nextInsideQuotes = !nextInsideQuotes;
|
|
514
|
-
}
|
|
515
|
-
return nextInsideQuotes;
|
|
516
|
-
};
|
|
517
459
|
var appendPiece = (state, piece, delimiter, quote, trim, lenient) => {
|
|
518
|
-
|
|
519
|
-
state.current
|
|
520
|
-
|
|
521
|
-
if (!state.insideQuotes) {
|
|
460
|
+
state.current = isEmptyString(state.current) ? piece : state.current + delimiter + piece;
|
|
461
|
+
const insideQuotes = countUnescaped(state.current, quote) % 2 === 1;
|
|
462
|
+
if (!insideQuotes) {
|
|
522
463
|
flushField(state, quote, trim, lenient);
|
|
523
464
|
}
|
|
524
465
|
};
|
|
@@ -527,7 +468,6 @@ var flushField = (state, quote, trim, lenient) => {
|
|
|
527
468
|
if (trim) fieldValue = fieldValue.trim();
|
|
528
469
|
state.fields.push(fieldValue);
|
|
529
470
|
state.current = "";
|
|
530
|
-
state.insideQuotes = false;
|
|
531
471
|
};
|
|
532
472
|
|
|
533
473
|
// src/presets/csv-divider.ts
|
|
@@ -579,27 +519,8 @@ function tryExtractQuery(input) {
|
|
|
579
519
|
const url = new URL(input);
|
|
580
520
|
return url.search.startsWith(QUERY_SEPARATORS.QUESTION_MARK) ? url.search.slice(1) : url.search;
|
|
581
521
|
} catch {
|
|
582
|
-
return
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
function extractQueryFromQuestionMark(input) {
|
|
586
|
-
const fragmentIndex = input.indexOf("#");
|
|
587
|
-
const withoutFragment = fragmentIndex >= 0 ? input.slice(0, fragmentIndex) : input;
|
|
588
|
-
const questionMarkIndex = withoutFragment.indexOf(
|
|
589
|
-
QUERY_SEPARATORS.QUESTION_MARK
|
|
590
|
-
);
|
|
591
|
-
if (questionMarkIndex < 0) {
|
|
592
|
-
return withoutFragment;
|
|
593
|
-
}
|
|
594
|
-
if (questionMarkIndex === 0) {
|
|
595
|
-
return withoutFragment.slice(1);
|
|
596
|
-
}
|
|
597
|
-
const prefix = withoutFragment.slice(0, questionMarkIndex);
|
|
598
|
-
const hasQuerySeparatorBefore = prefix.includes(QUERY_SEPARATORS.AMPERSAND) || prefix.includes(QUERY_SEPARATORS.EQUALS);
|
|
599
|
-
if (hasQuerySeparatorBefore) {
|
|
600
|
-
return withoutFragment;
|
|
522
|
+
return input;
|
|
601
523
|
}
|
|
602
|
-
return withoutFragment.slice(questionMarkIndex + 1);
|
|
603
524
|
}
|
|
604
525
|
function stripLeadingQuestionMark(query) {
|
|
605
526
|
return query.startsWith(QUERY_SEPARATORS.QUESTION_MARK) ? query.slice(1) : query;
|