muggle-string 0.2.0 → 0.2.1

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.
@@ -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
@@ -20,6 +20,9 @@ type _SegmentWithData<T = any> = [
20
20
  ];
21
21
  export declare function getLength(segments: Segment<any>[]): number;
22
22
  export declare function toString<T extends Segment<any>>(segments: T[]): string;
23
- export declare function replace<T extends Segment<any>>(segments: T[], pattern: string | RegExp, replacer: string | ((match: string) => T)): void;
24
- export declare function update<T extends Segment<any>>(segments: T[], startOffset: number, endOffset: number, newSegment: T): void;
23
+ export declare function create(source: string): Segment[];
24
+ export declare function replace<T extends Segment<any>>(segments: T[], pattern: string | RegExp, ...replacers: (string | ((match: string) => T))[]): void;
25
+ export declare function replaceAll<T extends Segment<any>>(segments: T[], pattern: RegExp, ...replacers: (string | ((match: string) => T))[]): void;
26
+ export declare function replaceSourceRange<T extends Segment<any>>(segments: T[], source: string | undefined, startOffset: number, endOffset: number, ...newSegments: T[]): void;
27
+ export declare function replaceRange<T extends Segment<any>>(segments: T[], startOffset: number, endOffset: number, ...newSegments: T[]): void;
25
28
  export {};
package/out/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.update = exports.replace = exports.toString = exports.getLength = void 0;
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 replace(segments, pattern, replacer) {
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
- update(segments, startOffset, endOffset, typeof replacer === 'string' ? replacer : replacer(match[0]));
28
+ replaceRange(segments, startOffset, endOffset, ...replacers.map(replacer => typeof replacer === 'string' ? replacer : replacer(match[0])));
24
29
  }
25
30
  }
26
31
  exports.replace = replace;
27
- function update(segments, startOffset, endOffset, newSegment) {
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 === 'string' ? replacer : replacer(match[0])));
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;
71
+ }
72
+ }
73
+ }
74
+ throw new Error(`Source range not found: ${source} ${startOffset} ${endOffset}`);
75
+ }
76
+ exports.replaceSourceRange = replaceSourceRange;
77
+ function replaceRange(segments, startOffset, endOffset, ...newSegments) {
28
78
  const offsets = toOffsets(segments);
29
- const startIndex = binarySearchStartIndex(offsets, startOffset);
30
- const endIndex = binarySearchEndIndex(offsets, endOffset);
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
- inserts.push(newSegment);
90
+ for (const newSegment of newSegments) {
91
+ inserts.push(newSegment);
92
+ }
40
93
  if (endOffset < endSegmentEnd) {
41
- inserts.push(trimSegmentStart(endSegment, endOffset - startSegmentStart));
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.update = update;
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
- function binarySearchStartIndex(offsets, searchOffset) {
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "muggle-string",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "main": "out/index.js",
5
5
  "license": "MIT",
6
6
  "files": [