apply-edit 0.1.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.
@@ -0,0 +1,115 @@
1
+ export type Tolerance = 'exact' | 'whitespace' | 'unicode';
2
+ export type Strategy =
3
+ | 'exact'
4
+ | 'trailing-whitespace'
5
+ | 'indent-shift'
6
+ | 'line-trimmed'
7
+ | 'whitespace-collapsed'
8
+ | 'unicode-normalized'
9
+ | 'escape-tolerant'
10
+ | `unescaped:${'exact' | 'trailing-whitespace' | 'indent-shift' | 'line-trimmed' | 'whitespace-collapsed' | 'unicode-normalized'}`;
11
+ export type LineEnding = 'lf' | 'crlf' | 'cr';
12
+ export type FailureCode = 'empty-old' | 'no-change' | 'not-found' | 'ambiguous' | 'occurrence-out-of-range' | 'overlap';
13
+
14
+ export interface Edit {
15
+ /** Text to find. Matched exactly first, then with whitespace, indentation, Unicode punctuation and escape tolerance. */
16
+ oldString: string;
17
+ /** Replacement text. Re-indented to the matched region when a non-exact strategy located it. */
18
+ newString: string;
19
+ /** Replace every match instead of refusing ambiguity. Default false. */
20
+ replaceAll?: boolean;
21
+ /** Select one match when several exist: 1-based from the start, negative from the end. */
22
+ occurrence?: number;
23
+ /** 1-based line where the match is expected; selects among candidates within three lines, otherwise only warns. */
24
+ line?: number;
25
+ }
26
+
27
+ export interface Options {
28
+ /** Loosest strategy allowed: 'exact', 'whitespace' (through whitespace-collapsed) or 'unicode' (default). */
29
+ tolerance?: Tolerance;
30
+ /** Recover literal escape sequences such as \n and \" in oldString. Default true. */
31
+ unescape?: boolean;
32
+ /** Compute the closest region on not-found. Default true. */
33
+ diagnostics?: boolean;
34
+ }
35
+
36
+ export interface Match {
37
+ /** UTF-16 offsets in the original text, end exclusive. */
38
+ start: number;
39
+ end: number;
40
+ /** 1-based inclusive lines in the original text. */
41
+ startLine: number;
42
+ endLine: number;
43
+ }
44
+
45
+ export interface Candidate {
46
+ startLine: number;
47
+ endLine: number;
48
+ }
49
+
50
+ export interface ClosestRegion {
51
+ startLine: number;
52
+ endLine: number;
53
+ /** Bigram similarity of the trimmed lines, 0 to 1. */
54
+ similarity: number;
55
+ /** Line-numbered lines of the closest region. */
56
+ preview: string;
57
+ /** First differing lines, at most five. */
58
+ differences: Array<{line: number; expected: string; actual: string}>;
59
+ }
60
+
61
+ export interface Failure {
62
+ ok: false;
63
+ code: FailureCode;
64
+ message: string;
65
+ lineEnding: LineEnding;
66
+ bom: boolean;
67
+ strategy?: Strategy;
68
+ candidates?: Candidate[];
69
+ closest?: ClosestRegion;
70
+ /** Index of the failing edit in applyEdits. */
71
+ index?: number;
72
+ }
73
+
74
+ export interface ApplyResult {
75
+ ok: true;
76
+ text: string;
77
+ strategy: Strategy;
78
+ replaced: number;
79
+ matches: Match[];
80
+ reindented: boolean;
81
+ warnings: string[];
82
+ lineEnding: LineEnding;
83
+ bom: boolean;
84
+ }
85
+
86
+ export interface FindResult {
87
+ ok: true;
88
+ strategy: Strategy;
89
+ matches: Match[];
90
+ lineEnding: LineEnding;
91
+ bom: boolean;
92
+ }
93
+
94
+ export interface AppliedEdit {
95
+ index: number;
96
+ strategy: Strategy;
97
+ matches: Match[];
98
+ reindented: boolean;
99
+ warnings: string[];
100
+ }
101
+
102
+ export interface ApplyEditsResult {
103
+ ok: true;
104
+ text: string;
105
+ applied: AppliedEdit[];
106
+ lineEnding: LineEnding;
107
+ bom: boolean;
108
+ }
109
+
110
+ /** Apply one edit. Never applies to more than one location unless replaceAll is set; ambiguity and misses are reported, not guessed. */
111
+ export function applyEdit(text: string, edit: Edit, options?: Options): ApplyResult | Failure;
112
+ /** Resolve every edit against the original text, refuse overlaps, and apply all of them or none. */
113
+ export function applyEdits(text: string, edits: Edit[], options?: Options): ApplyEditsResult | Failure;
114
+ /** Locate oldString without changing anything. */
115
+ export function findEdit(text: string, oldString: string, options?: Options): FindResult | Failure;
@@ -0,0 +1,115 @@
1
+ export type Tolerance = 'exact' | 'whitespace' | 'unicode';
2
+ export type Strategy =
3
+ | 'exact'
4
+ | 'trailing-whitespace'
5
+ | 'indent-shift'
6
+ | 'line-trimmed'
7
+ | 'whitespace-collapsed'
8
+ | 'unicode-normalized'
9
+ | 'escape-tolerant'
10
+ | `unescaped:${'exact' | 'trailing-whitespace' | 'indent-shift' | 'line-trimmed' | 'whitespace-collapsed' | 'unicode-normalized'}`;
11
+ export type LineEnding = 'lf' | 'crlf' | 'cr';
12
+ export type FailureCode = 'empty-old' | 'no-change' | 'not-found' | 'ambiguous' | 'occurrence-out-of-range' | 'overlap';
13
+
14
+ export interface Edit {
15
+ /** Text to find. Matched exactly first, then with whitespace, indentation, Unicode punctuation and escape tolerance. */
16
+ oldString: string;
17
+ /** Replacement text. Re-indented to the matched region when a non-exact strategy located it. */
18
+ newString: string;
19
+ /** Replace every match instead of refusing ambiguity. Default false. */
20
+ replaceAll?: boolean;
21
+ /** Select one match when several exist: 1-based from the start, negative from the end. */
22
+ occurrence?: number;
23
+ /** 1-based line where the match is expected; selects among candidates within three lines, otherwise only warns. */
24
+ line?: number;
25
+ }
26
+
27
+ export interface Options {
28
+ /** Loosest strategy allowed: 'exact', 'whitespace' (through whitespace-collapsed) or 'unicode' (default). */
29
+ tolerance?: Tolerance;
30
+ /** Recover literal escape sequences such as \n and \" in oldString. Default true. */
31
+ unescape?: boolean;
32
+ /** Compute the closest region on not-found. Default true. */
33
+ diagnostics?: boolean;
34
+ }
35
+
36
+ export interface Match {
37
+ /** UTF-16 offsets in the original text, end exclusive. */
38
+ start: number;
39
+ end: number;
40
+ /** 1-based inclusive lines in the original text. */
41
+ startLine: number;
42
+ endLine: number;
43
+ }
44
+
45
+ export interface Candidate {
46
+ startLine: number;
47
+ endLine: number;
48
+ }
49
+
50
+ export interface ClosestRegion {
51
+ startLine: number;
52
+ endLine: number;
53
+ /** Bigram similarity of the trimmed lines, 0 to 1. */
54
+ similarity: number;
55
+ /** Line-numbered lines of the closest region. */
56
+ preview: string;
57
+ /** First differing lines, at most five. */
58
+ differences: Array<{line: number; expected: string; actual: string}>;
59
+ }
60
+
61
+ export interface Failure {
62
+ ok: false;
63
+ code: FailureCode;
64
+ message: string;
65
+ lineEnding: LineEnding;
66
+ bom: boolean;
67
+ strategy?: Strategy;
68
+ candidates?: Candidate[];
69
+ closest?: ClosestRegion;
70
+ /** Index of the failing edit in applyEdits. */
71
+ index?: number;
72
+ }
73
+
74
+ export interface ApplyResult {
75
+ ok: true;
76
+ text: string;
77
+ strategy: Strategy;
78
+ replaced: number;
79
+ matches: Match[];
80
+ reindented: boolean;
81
+ warnings: string[];
82
+ lineEnding: LineEnding;
83
+ bom: boolean;
84
+ }
85
+
86
+ export interface FindResult {
87
+ ok: true;
88
+ strategy: Strategy;
89
+ matches: Match[];
90
+ lineEnding: LineEnding;
91
+ bom: boolean;
92
+ }
93
+
94
+ export interface AppliedEdit {
95
+ index: number;
96
+ strategy: Strategy;
97
+ matches: Match[];
98
+ reindented: boolean;
99
+ warnings: string[];
100
+ }
101
+
102
+ export interface ApplyEditsResult {
103
+ ok: true;
104
+ text: string;
105
+ applied: AppliedEdit[];
106
+ lineEnding: LineEnding;
107
+ bom: boolean;
108
+ }
109
+
110
+ /** Apply one edit. Never applies to more than one location unless replaceAll is set; ambiguity and misses are reported, not guessed. */
111
+ export function applyEdit(text: string, edit: Edit, options?: Options): ApplyResult | Failure;
112
+ /** Resolve every edit against the original text, refuse overlaps, and apply all of them or none. */
113
+ export function applyEdits(text: string, edits: Edit[], options?: Options): ApplyEditsResult | Failure;
114
+ /** Locate oldString without changing anything. */
115
+ export function findEdit(text: string, oldString: string, options?: Options): FindResult | Failure;