From fa68eb21bf1d0460303cae3a0233e2d6cefd00ca Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sun, 2 Jul 2023 20:27:46 +0200 Subject: broken furigana adder :( --- util/string.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'util') 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; +} + -- cgit v1.2.3