diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-07-02 20:27:46 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-07-02 20:27:46 +0200 |
commit | fa68eb21bf1d0460303cae3a0233e2d6cefd00ca (patch) | |
tree | 2a7026d2ed3710f6b455fd867f8258452100136e /util | |
parent | f4963b89ee542592e9ae95ca29d74ddc57841c3f (diff) |
broken furigana adder :(
Diffstat (limited to 'util')
-rw-r--r-- | util/string.ts | 20 |
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; +} + |