@nyaomaru/divider 2.0.15 → 2.0.17
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 +45 -28
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +45 -28
- package/package.json +3 -2
package/dist/index.cjs
CHANGED
|
@@ -355,6 +355,13 @@ function applyDividerOptions(result, options) {
|
|
|
355
355
|
return output;
|
|
356
356
|
}
|
|
357
357
|
|
|
358
|
+
// src/utils/transform-divider-input.ts
|
|
359
|
+
function transformDividerInput(input, transform, options) {
|
|
360
|
+
const result = isString(input) ? transform(input) : input.map(transform);
|
|
361
|
+
const resolvedOptions = options ?? {};
|
|
362
|
+
return applyDividerOptions(result, resolvedOptions);
|
|
363
|
+
}
|
|
364
|
+
|
|
358
365
|
// src/utils/separator.ts
|
|
359
366
|
function classifySeparators(args) {
|
|
360
367
|
return args.reduce(
|
|
@@ -367,11 +374,15 @@ function classifySeparators(args) {
|
|
|
367
374
|
);
|
|
368
375
|
}
|
|
369
376
|
|
|
370
|
-
// src/
|
|
371
|
-
function
|
|
372
|
-
const
|
|
373
|
-
const
|
|
374
|
-
return
|
|
377
|
+
// src/core/divider-plan.ts
|
|
378
|
+
function createDividerPlan(args) {
|
|
379
|
+
const { cleanedArgs, options } = extractOptions(args);
|
|
380
|
+
const { numSeparators, strSeparators } = classifySeparators(cleanedArgs);
|
|
381
|
+
return {
|
|
382
|
+
numSeparators,
|
|
383
|
+
strSeparators,
|
|
384
|
+
options
|
|
385
|
+
};
|
|
375
386
|
}
|
|
376
387
|
|
|
377
388
|
// src/core/divider.ts
|
|
@@ -385,8 +396,7 @@ function divider(input, ...args) {
|
|
|
385
396
|
if (isEmptyArray(args)) {
|
|
386
397
|
return ensureStringArray(input);
|
|
387
398
|
}
|
|
388
|
-
const {
|
|
389
|
-
const { numSeparators, strSeparators } = classifySeparators(cleanedArgs);
|
|
399
|
+
const { numSeparators, strSeparators, options } = createDividerPlan(args);
|
|
390
400
|
const applyDivision = (str) => divideString(str, numSeparators, strSeparators, {
|
|
391
401
|
preserveEmpty: options.preserveEmpty
|
|
392
402
|
});
|
|
@@ -641,11 +651,11 @@ var buildPathSegments = (input, collapse) => collapse ? divider(input, PATH_SEPA
|
|
|
641
651
|
var trimSegments2 = (segments, trim) => trim ? segments.map((segment) => segment.trim()) : segments;
|
|
642
652
|
var applyCollapseRules = (segments, collapse) => collapse ? segments.filter((segment) => !isEmptyString(segment)) : segments;
|
|
643
653
|
|
|
644
|
-
// src/
|
|
645
|
-
function
|
|
654
|
+
// src/utils/query.ts
|
|
655
|
+
function extractQuery(input) {
|
|
646
656
|
try {
|
|
647
657
|
const url = new URL(input);
|
|
648
|
-
return
|
|
658
|
+
return stripLeadingQuestionMark(url.search);
|
|
649
659
|
} catch {
|
|
650
660
|
return extractQueryFromQuestionMark(input);
|
|
651
661
|
}
|
|
@@ -664,40 +674,47 @@ function extractQueryFromQuestionMark(input) {
|
|
|
664
674
|
}
|
|
665
675
|
const prefix = withoutFragment.slice(0, questionMarkIndex);
|
|
666
676
|
const hasQuerySeparatorBefore = prefix.includes(QUERY_SEPARATORS.AMPERSAND) || prefix.includes(QUERY_SEPARATORS.EQUALS);
|
|
667
|
-
|
|
668
|
-
return withoutFragment;
|
|
669
|
-
}
|
|
670
|
-
return withoutFragment.slice(questionMarkIndex + 1);
|
|
677
|
+
return hasQuerySeparatorBefore ? withoutFragment : withoutFragment.slice(questionMarkIndex + 1);
|
|
671
678
|
}
|
|
672
679
|
function stripLeadingQuestionMark(query) {
|
|
673
680
|
return query.startsWith(QUERY_SEPARATORS.QUESTION_MARK) ? query.slice(1) : query;
|
|
674
681
|
}
|
|
675
|
-
function
|
|
676
|
-
const
|
|
677
|
-
if (
|
|
678
|
-
return [
|
|
682
|
+
function splitQueryPair(part) {
|
|
683
|
+
const keyValue = dividePreserve(part, QUERY_SEPARATORS.EQUALS);
|
|
684
|
+
if (keyValue.length === 1) return [keyValue[0] ?? "", ""];
|
|
685
|
+
return [
|
|
686
|
+
keyValue[0] ?? "",
|
|
687
|
+
keyValue.slice(1).join(QUERY_SEPARATORS.EQUALS)
|
|
688
|
+
];
|
|
679
689
|
}
|
|
680
|
-
function
|
|
681
|
-
let
|
|
690
|
+
function decodeQueryField(text, mode, trim) {
|
|
691
|
+
let decoded = text;
|
|
682
692
|
if (mode === QUERY_DECODE_MODES.AUTO) {
|
|
683
|
-
|
|
693
|
+
decoded = decoded.replace(/\+/g, " ");
|
|
684
694
|
try {
|
|
685
|
-
|
|
695
|
+
decoded = decodeURIComponent(decoded);
|
|
686
696
|
} catch {
|
|
687
697
|
}
|
|
688
698
|
}
|
|
689
|
-
|
|
690
|
-
return t;
|
|
699
|
+
return trim ? decoded.trim() : decoded;
|
|
691
700
|
}
|
|
692
|
-
function
|
|
701
|
+
function parseQueryPairs(input, { mode = QUERY_DECODE_MODES.AUTO, trim = false } = {}) {
|
|
693
702
|
if (input.length === 0) return [];
|
|
694
|
-
const query = stripLeadingQuestionMark(
|
|
703
|
+
const query = stripLeadingQuestionMark(extractQuery(input));
|
|
695
704
|
if (query.length === 0) return [];
|
|
696
705
|
return dividePreserve(query, QUERY_SEPARATORS.AMPERSAND).map((part) => {
|
|
697
|
-
const [key, value] =
|
|
698
|
-
return [
|
|
706
|
+
const [key, value] = splitQueryPair(part);
|
|
707
|
+
return [
|
|
708
|
+
decodeQueryField(key, mode, trim),
|
|
709
|
+
decodeQueryField(value, mode, trim)
|
|
710
|
+
];
|
|
699
711
|
});
|
|
700
712
|
}
|
|
713
|
+
|
|
714
|
+
// src/presets/query-divider.ts
|
|
715
|
+
function queryDivider(input, options = {}) {
|
|
716
|
+
return parseQueryPairs(input, options);
|
|
717
|
+
}
|
|
701
718
|
// Annotate the CommonJS export names for ESM import in node:
|
|
702
719
|
0 && (module.exports = {
|
|
703
720
|
csvDivider,
|
package/dist/index.d.cts
CHANGED
|
@@ -278,6 +278,6 @@ declare function pathDivider(input: string, options?: PathDividerOptions): Divid
|
|
|
278
278
|
* @param options Parsing options: { mode?: 'auto' | 'raw'; trim?: boolean }.
|
|
279
279
|
* @returns Array of [key, value] string tuples in input order.
|
|
280
280
|
*/
|
|
281
|
-
declare function queryDivider(input: string,
|
|
281
|
+
declare function queryDivider(input: string, options?: QueryDividerOptions): DividerArrayResult;
|
|
282
282
|
|
|
283
283
|
export { type CsvDividerOptions, type DividerArg, type DividerArgs, type DividerArrayResult, type DividerEmptyOptions, type DividerExcludeMode, type DividerInferredOptions, type DividerInput, type DividerLoopEmptyOptions, type DividerLoopOptions, type DividerLoopOptionsLike, type DividerOptions, type DividerResult, type DividerReturn, type DividerSeparator, type DividerSeparators, type DividerStringResult, type EmailDividerOptions, type ExtractedDividerOptions, type NumericSeparator, type PathDividerOptions, type QueryDecodeMode, type QueryDividerOptions, type StringArrayInput, type StringInput, type StringSeparator, csvDivider, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString, emailDivider, pathDivider, queryDivider };
|
package/dist/index.d.ts
CHANGED
|
@@ -278,6 +278,6 @@ declare function pathDivider(input: string, options?: PathDividerOptions): Divid
|
|
|
278
278
|
* @param options Parsing options: { mode?: 'auto' | 'raw'; trim?: boolean }.
|
|
279
279
|
* @returns Array of [key, value] string tuples in input order.
|
|
280
280
|
*/
|
|
281
|
-
declare function queryDivider(input: string,
|
|
281
|
+
declare function queryDivider(input: string, options?: QueryDividerOptions): DividerArrayResult;
|
|
282
282
|
|
|
283
283
|
export { type CsvDividerOptions, type DividerArg, type DividerArgs, type DividerArrayResult, type DividerEmptyOptions, type DividerExcludeMode, type DividerInferredOptions, type DividerInput, type DividerLoopEmptyOptions, type DividerLoopOptions, type DividerLoopOptionsLike, type DividerOptions, type DividerResult, type DividerReturn, type DividerSeparator, type DividerSeparators, type DividerStringResult, type EmailDividerOptions, type ExtractedDividerOptions, type NumericSeparator, type PathDividerOptions, type QueryDecodeMode, type QueryDividerOptions, type StringArrayInput, type StringInput, type StringSeparator, csvDivider, divider, dividerFirst, dividerLast, dividerLoop, dividerNumberString, emailDivider, pathDivider, queryDivider };
|
package/dist/index.js
CHANGED
|
@@ -321,6 +321,13 @@ function applyDividerOptions(result, options) {
|
|
|
321
321
|
return output;
|
|
322
322
|
}
|
|
323
323
|
|
|
324
|
+
// src/utils/transform-divider-input.ts
|
|
325
|
+
function transformDividerInput(input, transform, options) {
|
|
326
|
+
const result = isString(input) ? transform(input) : input.map(transform);
|
|
327
|
+
const resolvedOptions = options ?? {};
|
|
328
|
+
return applyDividerOptions(result, resolvedOptions);
|
|
329
|
+
}
|
|
330
|
+
|
|
324
331
|
// src/utils/separator.ts
|
|
325
332
|
function classifySeparators(args) {
|
|
326
333
|
return args.reduce(
|
|
@@ -333,11 +340,15 @@ function classifySeparators(args) {
|
|
|
333
340
|
);
|
|
334
341
|
}
|
|
335
342
|
|
|
336
|
-
// src/
|
|
337
|
-
function
|
|
338
|
-
const
|
|
339
|
-
const
|
|
340
|
-
return
|
|
343
|
+
// src/core/divider-plan.ts
|
|
344
|
+
function createDividerPlan(args) {
|
|
345
|
+
const { cleanedArgs, options } = extractOptions(args);
|
|
346
|
+
const { numSeparators, strSeparators } = classifySeparators(cleanedArgs);
|
|
347
|
+
return {
|
|
348
|
+
numSeparators,
|
|
349
|
+
strSeparators,
|
|
350
|
+
options
|
|
351
|
+
};
|
|
341
352
|
}
|
|
342
353
|
|
|
343
354
|
// src/core/divider.ts
|
|
@@ -351,8 +362,7 @@ function divider(input, ...args) {
|
|
|
351
362
|
if (isEmptyArray(args)) {
|
|
352
363
|
return ensureStringArray(input);
|
|
353
364
|
}
|
|
354
|
-
const {
|
|
355
|
-
const { numSeparators, strSeparators } = classifySeparators(cleanedArgs);
|
|
365
|
+
const { numSeparators, strSeparators, options } = createDividerPlan(args);
|
|
356
366
|
const applyDivision = (str) => divideString(str, numSeparators, strSeparators, {
|
|
357
367
|
preserveEmpty: options.preserveEmpty
|
|
358
368
|
});
|
|
@@ -607,11 +617,11 @@ var buildPathSegments = (input, collapse) => collapse ? divider(input, PATH_SEPA
|
|
|
607
617
|
var trimSegments2 = (segments, trim) => trim ? segments.map((segment) => segment.trim()) : segments;
|
|
608
618
|
var applyCollapseRules = (segments, collapse) => collapse ? segments.filter((segment) => !isEmptyString(segment)) : segments;
|
|
609
619
|
|
|
610
|
-
// src/
|
|
611
|
-
function
|
|
620
|
+
// src/utils/query.ts
|
|
621
|
+
function extractQuery(input) {
|
|
612
622
|
try {
|
|
613
623
|
const url = new URL(input);
|
|
614
|
-
return
|
|
624
|
+
return stripLeadingQuestionMark(url.search);
|
|
615
625
|
} catch {
|
|
616
626
|
return extractQueryFromQuestionMark(input);
|
|
617
627
|
}
|
|
@@ -630,40 +640,47 @@ function extractQueryFromQuestionMark(input) {
|
|
|
630
640
|
}
|
|
631
641
|
const prefix = withoutFragment.slice(0, questionMarkIndex);
|
|
632
642
|
const hasQuerySeparatorBefore = prefix.includes(QUERY_SEPARATORS.AMPERSAND) || prefix.includes(QUERY_SEPARATORS.EQUALS);
|
|
633
|
-
|
|
634
|
-
return withoutFragment;
|
|
635
|
-
}
|
|
636
|
-
return withoutFragment.slice(questionMarkIndex + 1);
|
|
643
|
+
return hasQuerySeparatorBefore ? withoutFragment : withoutFragment.slice(questionMarkIndex + 1);
|
|
637
644
|
}
|
|
638
645
|
function stripLeadingQuestionMark(query) {
|
|
639
646
|
return query.startsWith(QUERY_SEPARATORS.QUESTION_MARK) ? query.slice(1) : query;
|
|
640
647
|
}
|
|
641
|
-
function
|
|
642
|
-
const
|
|
643
|
-
if (
|
|
644
|
-
return [
|
|
648
|
+
function splitQueryPair(part) {
|
|
649
|
+
const keyValue = dividePreserve(part, QUERY_SEPARATORS.EQUALS);
|
|
650
|
+
if (keyValue.length === 1) return [keyValue[0] ?? "", ""];
|
|
651
|
+
return [
|
|
652
|
+
keyValue[0] ?? "",
|
|
653
|
+
keyValue.slice(1).join(QUERY_SEPARATORS.EQUALS)
|
|
654
|
+
];
|
|
645
655
|
}
|
|
646
|
-
function
|
|
647
|
-
let
|
|
656
|
+
function decodeQueryField(text, mode, trim) {
|
|
657
|
+
let decoded = text;
|
|
648
658
|
if (mode === QUERY_DECODE_MODES.AUTO) {
|
|
649
|
-
|
|
659
|
+
decoded = decoded.replace(/\+/g, " ");
|
|
650
660
|
try {
|
|
651
|
-
|
|
661
|
+
decoded = decodeURIComponent(decoded);
|
|
652
662
|
} catch {
|
|
653
663
|
}
|
|
654
664
|
}
|
|
655
|
-
|
|
656
|
-
return t;
|
|
665
|
+
return trim ? decoded.trim() : decoded;
|
|
657
666
|
}
|
|
658
|
-
function
|
|
667
|
+
function parseQueryPairs(input, { mode = QUERY_DECODE_MODES.AUTO, trim = false } = {}) {
|
|
659
668
|
if (input.length === 0) return [];
|
|
660
|
-
const query = stripLeadingQuestionMark(
|
|
669
|
+
const query = stripLeadingQuestionMark(extractQuery(input));
|
|
661
670
|
if (query.length === 0) return [];
|
|
662
671
|
return dividePreserve(query, QUERY_SEPARATORS.AMPERSAND).map((part) => {
|
|
663
|
-
const [key, value] =
|
|
664
|
-
return [
|
|
672
|
+
const [key, value] = splitQueryPair(part);
|
|
673
|
+
return [
|
|
674
|
+
decodeQueryField(key, mode, trim),
|
|
675
|
+
decodeQueryField(value, mode, trim)
|
|
676
|
+
];
|
|
665
677
|
});
|
|
666
678
|
}
|
|
679
|
+
|
|
680
|
+
// src/presets/query-divider.ts
|
|
681
|
+
function queryDivider(input, options = {}) {
|
|
682
|
+
return parseQueryPairs(input, options);
|
|
683
|
+
}
|
|
667
684
|
export {
|
|
668
685
|
csvDivider,
|
|
669
686
|
divider,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nyaomaru/divider",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.
|
|
4
|
+
"version": "2.0.17",
|
|
5
5
|
"description": "To divide string or string[] with a given separator",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
7
7
|
"module": "./dist/index.js",
|
|
@@ -35,9 +35,9 @@
|
|
|
35
35
|
"author": "nyaomaru",
|
|
36
36
|
"license": "MIT",
|
|
37
37
|
"devDependencies": {
|
|
38
|
+
"@eslint/js": "^9.26.0",
|
|
38
39
|
"@swc/core": "^1.15.21",
|
|
39
40
|
"@swc/jest": "^0.2.39",
|
|
40
|
-
"@eslint/js": "^9.26.0",
|
|
41
41
|
"@types/jest": "^30.0.0",
|
|
42
42
|
"@types/node": "^22.15.12",
|
|
43
43
|
"@typescript-eslint/eslint-plugin": "^8.32.1",
|
|
@@ -64,6 +64,7 @@
|
|
|
64
64
|
},
|
|
65
65
|
"scripts": {
|
|
66
66
|
"test": "jest",
|
|
67
|
+
"test:types": "tsc -p tests/tsconfig.json",
|
|
67
68
|
"test:performance": "jest --config jest.performance.config.cjs",
|
|
68
69
|
"test:unit": "jest --testPathIgnorePatterns=performance",
|
|
69
70
|
"lint": "eslint .",
|