-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathendsWith.js
More file actions
25 lines (22 loc) · 763 Bytes
/
endsWith.js
File metadata and controls
25 lines (22 loc) · 763 Bytes
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
class EndsWith {
constructor(str, searchString, position = undefined) {
if (typeof str !== 'string' || typeof searchString !== 'string') {
throw new Error('Input must be strings');
}
this.str = str;
this.searchString = searchString;
this.position = position;
}
execute() {
if (typeof this.position !== 'undefined') {
if (this.position < 0) {
return false;
}
const startPos = Math.min(this.position, this.str.length - this.searchString.length);
return this.str.slice(startPos).endsWith(this.searchString);
} else {
return this.str.endsWith(this.searchString);
}
}
}
module.exports = EndsWith;