-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext-utils.js
More file actions
33 lines (30 loc) · 1 KB
/
text-utils.js
File metadata and controls
33 lines (30 loc) · 1 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
export function countOccurrences(text, search) {
if (!text || !search) return 0;
let count = 0;
let pos = 0;
while ((pos = text.indexOf(search, pos)) !== -1) {
count++;
pos += search.length;
}
return count;
}
export function getAllIndices(text, search) {
const indices = [];
let pos = 0;
while ((pos = text.indexOf(search, pos)) !== -1) {
indices.push(pos);
pos += search.length;
}
return indices;
}
export function countMatches(source, text, throwIfNotFound = true) {
const count = (source.match(new RegExp(text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g')) || []).length;
if (throwIfNotFound && count === 0) throw new Error(`old_text not found in file.`);
return count;
}
export function parseColor(colorStr) {
if (!colorStr) return null;
const hex = colorStr.replace('#', '');
if (hex.length !== 6) return null;
return { red: parseInt(hex.substring(0, 2), 16) / 255, green: parseInt(hex.substring(2, 4), 16) / 255, blue: parseInt(hex.substring(4, 6), 16) / 255 };
}