-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathreplace-placeholders.js
More file actions
46 lines (42 loc) · 1.07 KB
/
replace-placeholders.js
File metadata and controls
46 lines (42 loc) · 1.07 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
import {
CURSOR_PLACEHOLDER,
RANGE_END_PLACEHOLDER,
RANGE_START_PLACEHOLDER,
} from "./constants.js";
const indexProperties = [
{
property: "cursorOffset",
placeholder: CURSOR_PLACEHOLDER,
},
{
property: "rangeStart",
placeholder: RANGE_START_PLACEHOLDER,
},
{
property: "rangeEnd",
placeholder: RANGE_END_PLACEHOLDER,
},
];
/**
@param {string} originalText
@param {any} originalOptions
*/
function replacePlaceholders(originalText, originalOptions) {
const indexes = indexProperties
.map(({ property, placeholder }) => {
const value = originalText.indexOf(placeholder);
return value === -1 ? undefined : { property, value, placeholder };
})
.filter(Boolean)
.sort((a, b) => a.value - b.value);
const options = { ...originalOptions };
let text = originalText;
let offset = 0;
for (const { property, value, placeholder } of indexes) {
text = text.replace(placeholder, "");
options[property] = value + offset;
offset -= placeholder.length;
}
return { text, options };
}
export { replacePlaceholders };