aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rw-r--r--util/string.ts20
1 files changed, 20 insertions, 0 deletions
diff --git a/util/string.ts b/util/string.ts
index 327b884..934963e 100644
--- a/util/string.ts
+++ b/util/string.ts
@@ -32,6 +32,17 @@ declare global {
* `mapFn`
*/
map(mapFn: (char: string) => string): string;
+
+ /**
+ * @summary return length of the match of searchValue from startIndex (default: 0)
+ *
+ * Similar to String.prototype.startsWith, but returns the length of the
+ * match instead of a boolean true or false.
+ *
+ * @param searchString string to search for
+ * @param position index to search from (0 by default = start of string)
+ */
+ cmpLen(searchString: string, position?: number): number;
}
}
@@ -113,3 +124,12 @@ String.prototype.map = function(mapFn) {
return out;
}
+String.prototype.cmpLen = function(searchString, position = 0) {
+ let len = 0;
+ for (let i = 0; i < searchString.length; i++) {
+ if (i + position >= this.length) break;
+ if (this[i + position] == searchString[i]) len++;
+ }
+ return len;
+}
+