muggle-string 0.2.0 → 0.2.2
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/out/binarySearch.d.ts +1 -0
- package/out/binarySearch.js +25 -0
- package/out/index.d.ts +9 -14
- package/out/index.js +66 -37
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function binarySearch(offsets: number[], start: number): number;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.binarySearch = void 0;
|
|
4
|
+
function binarySearch(offsets, start) {
|
|
5
|
+
let low = 0;
|
|
6
|
+
let high = offsets.length - 1;
|
|
7
|
+
while (low <= high) {
|
|
8
|
+
const mid = Math.floor((low + high) / 2);
|
|
9
|
+
const midValue = offsets[mid];
|
|
10
|
+
if (midValue < start) {
|
|
11
|
+
low = mid + 1;
|
|
12
|
+
}
|
|
13
|
+
else if (midValue > start) {
|
|
14
|
+
high = mid - 1;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
low = mid;
|
|
18
|
+
high = mid;
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return Math.max(Math.min(low, high, offsets.length - 1), 0);
|
|
23
|
+
}
|
|
24
|
+
exports.binarySearch = binarySearch;
|
|
25
|
+
//# sourceMappingURL=binarySearch.js.map
|
package/out/index.d.ts
CHANGED
|
@@ -1,25 +1,20 @@
|
|
|
1
1
|
declare const NO_DATA_SYMBOL: unique symbol;
|
|
2
|
-
export type Segment<T = typeof NO_DATA_SYMBOL> = T extends typeof NO_DATA_SYMBOL ?
|
|
3
|
-
export type SegmentWithoutData = WithString<_SegmentWithoutData>;
|
|
4
|
-
export type SegmentWithData<T = any> = WithString<_SegmentWithData<T>>;
|
|
5
|
-
type WithString<T> = T | string;
|
|
6
|
-
type _SegmentWithoutData = [
|
|
2
|
+
export type Segment<T = typeof NO_DATA_SYMBOL> = WithString<T extends typeof NO_DATA_SYMBOL ? [
|
|
7
3
|
string,
|
|
8
|
-
// text
|
|
9
4
|
string | undefined,
|
|
10
|
-
// source
|
|
11
5
|
number | [number, number]
|
|
12
|
-
]
|
|
13
|
-
type _SegmentWithData<T = any> = [
|
|
6
|
+
] : [
|
|
14
7
|
string,
|
|
15
|
-
// text
|
|
16
8
|
string | undefined,
|
|
17
|
-
// source
|
|
18
9
|
number | [number, number],
|
|
19
10
|
T
|
|
20
|
-
]
|
|
11
|
+
]>;
|
|
12
|
+
type WithString<T> = T | string;
|
|
21
13
|
export declare function getLength(segments: Segment<any>[]): number;
|
|
22
14
|
export declare function toString<T extends Segment<any>>(segments: T[]): string;
|
|
23
|
-
export declare function
|
|
24
|
-
export declare function
|
|
15
|
+
export declare function create(source: string): Segment[];
|
|
16
|
+
export declare function replace<T extends Segment<any>>(segments: T[], pattern: string | RegExp, ...replacers: (T | ((match: string) => T))[]): void;
|
|
17
|
+
export declare function replaceAll<T extends Segment<any>>(segments: T[], pattern: RegExp, ...replacers: (T | ((match: string) => T))[]): void;
|
|
18
|
+
export declare function replaceSourceRange<T extends Segment<any>>(segments: T[], source: string | undefined, startOffset: number, endOffset: number, ...newSegments: T[]): boolean;
|
|
19
|
+
export declare function replaceRange<T extends Segment<any>>(segments: T[], startOffset: number, endOffset: number, ...newSegments: T[]): void;
|
|
25
20
|
export {};
|
package/out/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.replaceRange = exports.replaceSourceRange = exports.replaceAll = exports.replace = exports.create = exports.toString = exports.getLength = void 0;
|
|
4
|
+
const binarySearch_1 = require("./binarySearch");
|
|
4
5
|
const NO_DATA_SYMBOL = Symbol();
|
|
5
6
|
function getLength(segments) {
|
|
6
7
|
let length = 0;
|
|
@@ -14,36 +15,88 @@ function toString(segments) {
|
|
|
14
15
|
return segments.map(s => typeof s === 'string' ? s : s[0]).join('');
|
|
15
16
|
}
|
|
16
17
|
exports.toString = toString;
|
|
17
|
-
function
|
|
18
|
+
function create(source) {
|
|
19
|
+
return [[source, undefined, 0]];
|
|
20
|
+
}
|
|
21
|
+
exports.create = create;
|
|
22
|
+
function replace(segments, pattern, ...replacers) {
|
|
18
23
|
const str = toString(segments);
|
|
19
24
|
const match = str.match(pattern);
|
|
20
25
|
if (match && match.index !== undefined) {
|
|
21
26
|
const startOffset = match.index;
|
|
22
27
|
const endOffset = startOffset + match[0].length;
|
|
23
|
-
|
|
28
|
+
replaceRange(segments, startOffset, endOffset, ...replacers.map(replacer => typeof replacer === 'function' ? replacer(match[0]) : replacer));
|
|
24
29
|
}
|
|
25
30
|
}
|
|
26
31
|
exports.replace = replace;
|
|
27
|
-
function
|
|
32
|
+
function replaceAll(segments, pattern, ...replacers) {
|
|
33
|
+
const str = toString(segments);
|
|
34
|
+
const allMatch = str.matchAll(pattern);
|
|
35
|
+
let length = str.length;
|
|
36
|
+
let lengthDiff = 0;
|
|
37
|
+
for (const match of allMatch) {
|
|
38
|
+
if (match.index !== undefined) {
|
|
39
|
+
const startOffset = match.index + lengthDiff;
|
|
40
|
+
const endOffset = startOffset + match[0].length;
|
|
41
|
+
replaceRange(segments, startOffset, endOffset, ...replacers.map(replacer => typeof replacer === 'function' ? replacer(match[0]) : replacer));
|
|
42
|
+
const newLength = getLength(segments);
|
|
43
|
+
lengthDiff += newLength - length;
|
|
44
|
+
length = newLength;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.replaceAll = replaceAll;
|
|
49
|
+
function replaceSourceRange(segments, source, startOffset, endOffset, ...newSegments) {
|
|
50
|
+
for (const segment of segments) {
|
|
51
|
+
if (typeof segment === 'string') {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (segment[1] === source) {
|
|
55
|
+
const segmentStart = typeof segment[2] === 'number' ? segment[2] : segment[2][0];
|
|
56
|
+
const segmentEnd = typeof segment[2] === 'number' ? segment[2] + segment[0].length : segment[2][1];
|
|
57
|
+
if (segmentStart <= startOffset && segmentEnd >= endOffset) {
|
|
58
|
+
const inserts = [];
|
|
59
|
+
if (startOffset > segmentStart) {
|
|
60
|
+
inserts.push(trimSegmentEnd(segment, startOffset - segmentStart));
|
|
61
|
+
}
|
|
62
|
+
for (const newSegment of newSegments) {
|
|
63
|
+
inserts.push(newSegment);
|
|
64
|
+
}
|
|
65
|
+
if (endOffset < segmentEnd) {
|
|
66
|
+
inserts.push(trimSegmentStart(segment, endOffset - segmentEnd));
|
|
67
|
+
}
|
|
68
|
+
combineStrings(inserts);
|
|
69
|
+
segments.splice(segments.indexOf(segment), 1, ...inserts);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
exports.replaceSourceRange = replaceSourceRange;
|
|
77
|
+
function replaceRange(segments, startOffset, endOffset, ...newSegments) {
|
|
28
78
|
const offsets = toOffsets(segments);
|
|
29
|
-
const startIndex =
|
|
30
|
-
const endIndex =
|
|
79
|
+
const startIndex = (0, binarySearch_1.binarySearch)(offsets, startOffset);
|
|
80
|
+
const endIndex = (0, binarySearch_1.binarySearch)(offsets, endOffset);
|
|
31
81
|
const startSegment = segments[startIndex];
|
|
32
82
|
const endSegment = segments[endIndex];
|
|
33
83
|
const startSegmentStart = offsets[startIndex];
|
|
84
|
+
const endSegmentStart = offsets[endIndex];
|
|
34
85
|
const endSegmentEnd = offsets[endIndex] + (typeof endSegment === 'string' ? endSegment.length : endSegment[0].length);
|
|
35
86
|
const inserts = [];
|
|
36
87
|
if (startOffset > startSegmentStart) {
|
|
37
88
|
inserts.push(trimSegmentEnd(startSegment, startOffset - startSegmentStart));
|
|
38
89
|
}
|
|
39
|
-
|
|
90
|
+
for (const newSegment of newSegments) {
|
|
91
|
+
inserts.push(newSegment);
|
|
92
|
+
}
|
|
40
93
|
if (endOffset < endSegmentEnd) {
|
|
41
|
-
inserts.push(trimSegmentStart(endSegment, endOffset -
|
|
94
|
+
inserts.push(trimSegmentStart(endSegment, endOffset - endSegmentStart));
|
|
42
95
|
}
|
|
43
96
|
combineStrings(inserts);
|
|
44
97
|
segments.splice(startIndex, endIndex - startIndex + 1, ...inserts);
|
|
45
98
|
}
|
|
46
|
-
exports.
|
|
99
|
+
exports.replaceRange = replaceRange;
|
|
47
100
|
function combineStrings(segments) {
|
|
48
101
|
for (let i = segments.length - 1; i >= 1; i--) {
|
|
49
102
|
if (typeof segments[i] === 'string' && typeof segments[i - 1] === 'string') {
|
|
@@ -74,6 +127,9 @@ function trimSegmentStart(segment, trimStart) {
|
|
|
74
127
|
const originalString = segment[0];
|
|
75
128
|
const originalRange = segment[2];
|
|
76
129
|
const newString = originalString.slice(trimStart);
|
|
130
|
+
if (trimStart < 0) {
|
|
131
|
+
trimStart += originalString.length;
|
|
132
|
+
}
|
|
77
133
|
const newRange = typeof originalRange === 'number' ? originalRange + trimStart : [originalRange[0] + trimStart, originalRange[1]];
|
|
78
134
|
return [
|
|
79
135
|
newString,
|
|
@@ -91,31 +147,4 @@ function toOffsets(segments) {
|
|
|
91
147
|
}
|
|
92
148
|
return offsets;
|
|
93
149
|
}
|
|
94
|
-
|
|
95
|
-
let start = 0;
|
|
96
|
-
let end = offsets.length - 1;
|
|
97
|
-
while (start < end) {
|
|
98
|
-
const mid = Math.floor((start + end) / 2);
|
|
99
|
-
if (offsets[mid] < searchOffset) {
|
|
100
|
-
start = mid + 1;
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
end = mid;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return start;
|
|
107
|
-
}
|
|
108
|
-
function binarySearchEndIndex(offsets, searchOffset) {
|
|
109
|
-
let start = 0;
|
|
110
|
-
let end = offsets.length - 1;
|
|
111
|
-
while (start < end) {
|
|
112
|
-
const mid = Math.floor((start + end) / 2);
|
|
113
|
-
if (offsets[mid] <= searchOffset) {
|
|
114
|
-
start = mid + 1;
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
end = mid;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return start;
|
|
121
|
-
}
|
|
150
|
+
//# sourceMappingURL=index.js.map
|