-
-
Notifications
You must be signed in to change notification settings - Fork 356
Expand file tree
/
Copy pathGlobalFilter.ts
More file actions
85 lines (67 loc) · 2.75 KB
/
Copy pathGlobalFilter.ts
File metadata and controls
85 lines (67 loc) · 2.75 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
import * as RegExpTools from '../lib/RegExpTools';
import { getSettings, updateSettings } from './Settings';
export class GlobalFilter {
static empty = '';
static get(): string {
const { globalFilter } = getSettings();
return globalFilter;
}
static set(value: string) {
updateSettings({ globalFilter: value });
}
static reset() {
updateSettings({ globalFilter: GlobalFilter.empty });
}
static isEmpty(): boolean {
return GlobalFilter.get() === GlobalFilter.empty;
}
static equals(tag: string): boolean {
return GlobalFilter.get() === tag;
}
static includedIn(description: string): boolean {
const globalFilter = GlobalFilter.get();
return description.includes(globalFilter);
}
static prependTo(description: string): string {
return GlobalFilter.get() + ' ' + description;
}
static addGlobalFilterToDescriptionDependingOnSettings(description: string): string {
if (GlobalFilter.shouldAddGlobalFilter(description)) {
return GlobalFilter.prependTo(description);
} else {
return description;
}
}
private static shouldAddGlobalFilter(description: string): boolean {
const { autoInsertGlobalFilter } = getSettings();
return !GlobalFilter.isEmpty() && autoInsertGlobalFilter && !GlobalFilter.includedIn(description);
}
static removeAsWordFromDependingOnSettings(description: string): string {
const { removeGlobalFilter } = getSettings();
if (removeGlobalFilter) {
return GlobalFilter.removeAsWordFrom(description);
}
return description;
}
/**
* Search for the global filter for the purpose of removing it from the description, but do so only
* if it is a separate word (preceding the beginning of line or a space and followed by the end of line
* or a space), because we don't want to cut-off nested tags like #task/subtag.
* If the global filter exists as part of a nested tag, we keep it untouched.
*/
static removeAsWordFrom(description: string): string {
if (GlobalFilter.isEmpty()) {
return description;
}
// This matches the global filter (after escaping it) only when it's a complete word
const theRegExp = RegExp('(^|\\s)' + RegExpTools.escapeRegExp(GlobalFilter.get()) + '($|\\s)', 'ug');
if (description.search(theRegExp) > -1) {
description = description.replace(theRegExp, '$1$2').replace(' ', ' ').trim();
}
return description;
}
static removeAsSubstringFrom(description: string): string {
const globalFilter = GlobalFilter.get();
return description.replace(globalFilter, '').trim();
}
}