-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
120 lines (97 loc) · 3.87 KB
/
example.js
File metadata and controls
120 lines (97 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const {
StringEditImpl,
StringReplacementImpl,
OffsetRangeImpl,
} = require('./dist/index.js');
console.log('=== Enhanced adelphos Library Demo (VSCode Compatible) ===\n');
// 1. Advanced String Edit Operations
console.log('1. Advanced String Edit Operations:');
const text = 'Hello world! This is a test.';
console.log(`Original text: "${text}"`);
// Create multiple replacements
const edit1 = StringReplacementImpl.replace(
OffsetRangeImpl.ofStartAndLength(0, 5), // "Hello"
'Hi'
);
const edit2 = StringReplacementImpl.replace(
OffsetRangeImpl.ofStartAndLength(6, 5), // "world"
'there'
);
const edit3 = StringReplacementImpl.insert(13, ' amazing'); // Insert after "!"
const stringEdit = StringEditImpl.create([edit1, edit2, edit3]);
console.log('String edits:', stringEdit.edits.map(e => ({
range: `${e.range.start}-${e.range.end}`,
newText: e.newText
})));
const result = stringEdit.apply(text);
console.log(`Result: "${result}"`);
console.log();
// 2. Edit Neutrality Check
console.log('2. Edit Neutrality Check:');
const neutralEdit = StringReplacementImpl.replace(
OffsetRangeImpl.ofStartAndLength(0, 5), // "Hello"
'Hello' // Same text
);
console.log(`Is neutral edit neutral? ${neutralEdit.isNeutralOn(text)}`);
const nonNeutralEdit = StringReplacementImpl.replace(
OffsetRangeImpl.ofStartAndLength(0, 5), // "Hello"
'Hi'
);
console.log(`Is non-neutral edit neutral? ${nonNeutralEdit.isNeutralOn(text)}`);
console.log();
// 3. Common Prefix/Suffix Removal
console.log('3. Common Prefix/Suffix Removal:');
const originalText = 'prefix_CHANGE_suffix';
const editWithCommon = StringReplacementImpl.replace(
OffsetRangeImpl.ofStartAndLength(0, originalText.length),
'prefix_MODIFIED_suffix'
);
console.log(`Original: "${originalText}"`);
console.log(`Full replacement: "${editWithCommon.newText}"`);
const optimized = editWithCommon.removeCommonSuffixPrefix(originalText);
console.log(`Optimized edit range: ${optimized.range.start}-${optimized.range.end}`);
console.log(`Optimized edit text: "${optimized.newText}"`);
console.log();
// 4. Edit Inversion
console.log('4. Edit Inversion:');
const originalForInverse = 'The quick brown fox';
const editForInverse = StringEditImpl.replace(
OffsetRangeImpl.ofStartAndLength(4, 5), // "quick"
'slow'
);
console.log(`Original: "${originalForInverse}"`);
const modified = editForInverse.apply(originalForInverse);
console.log(`Modified: "${modified}"`);
const inverse = editForInverse.inverse(originalForInverse);
const restored = inverse.apply(modified);
console.log(`Restored: "${restored}"`);
console.log(`Successfully restored: ${restored === originalForInverse}`);
console.log();
// 5. OffsetRange Operations
console.log('5. OffsetRange Operations:');
const range1 = new OffsetRangeImpl(5, 10);
const range2 = new OffsetRangeImpl(8, 15);
console.log(`Range 1: ${range1.start}-${range1.end} (length: ${range1.length})`);
console.log(`Range 2: ${range2.start}-${range2.end} (length: ${range2.length})`);
console.log(`Ranges intersect: ${range1.intersectsOrTouches(range2)}`);
const intersection = range1.intersect(range2);
if (intersection) {
console.log(`Intersection: ${intersection.start}-${intersection.end}`);
}
const shifted = range1.delta(3);
console.log(`Range 1 shifted by 3: ${shifted.start}-${shifted.end}`);
console.log();
// 6. Complex Edit Composition
console.log('6. Complex Edit Composition:');
const baseText = 'function hello() { return "world"; }';
console.log(`Base: "${baseText}"`);
// Multiple edits
const edits = [
StringReplacementImpl.replace(OffsetRangeImpl.ofStartAndLength(9, 5), 'greet'), // hello -> greet
StringReplacementImpl.replace(OffsetRangeImpl.ofStartAndLength(24, 7), '"Hello!"'), // "world" -> "Hello!"
];
const compositeEdit = StringEditImpl.create(edits);
const finalResult = compositeEdit.apply(baseText);
console.log(`Result: "${finalResult}"`);
console.log();
console.log('=== Demo Complete ===');