Created
March 8, 2020 13:57
unicode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const forEachChar = (str, fn) => { | |
| if (str.length === 0) { | |
| return; | |
| } | |
| const iter = str[Symbol.iterator](); | |
| let it = iter.next(); | |
| let prev = it.value; | |
| do { | |
| it = iter.next(); | |
| const char = it.value; | |
| if (isCombiningMark(char)) { | |
| prev += char; | |
| } else { | |
| if (fn(prev) === false) { | |
| return; | |
| } | |
| prev = char; | |
| } | |
| } while (!it.done); | |
| }; | |
| const charArrayLength = (str) => { | |
| let len = 0; | |
| forEachChar(str, () => { | |
| len++; | |
| }); | |
| return len; | |
| }; | |
| const toCharArray = (str) => { | |
| const arr = []; | |
| forEachChar(str, (char) => { | |
| arr.push(char); | |
| }); | |
| return arr; | |
| }; | |
| const transCharArrayOffset = (str, originOffset) => { | |
| let i = 0; | |
| forEachChar(str, (char) => { | |
| if (originOffset <= 0) { | |
| return false; | |
| } | |
| originOffset -= char.length; | |
| i++; | |
| }); | |
| return i; | |
| }; | |
| const REG_COMBINING_MARK = /[\u0300-\u036f\u1ab0-\u1aff\u1dc0-\u1dff\u20d0-\u20ff\ufe20-\ufe2f]/; | |
| const isCombiningMark = (character) => { | |
| return REG_COMBINING_MARK.test(character); | |
| }; | |
| toCharArray("1🧡2"); | |
| toCharArray("H̹̙̦̮͉̩̗̗ͧ̇̏̊̾Eͨ͆͒̆ͮ̃͏̷̮̣̫̤̣ ̵̞̹̻̀̉̓ͬ͑͡ͅCͯ̂͐͏̨̛͔̦̟͈̻O̜͎͍͙͚̬̝̣̽ͮ͐͗̀ͤ̍̀͢M̴̡̲̭͍͇̼̟̯̦̉̒͠Ḛ̛̙̞̪̗ͥͤͩ̾͑̔͐ͅṮ̴̷̷̗̼͍̿̿̓̽͐H̙̙̔̄͜"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment