#!/usr/bin/env node; /****************************************************************************** * This program demonstrates basic string functions. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ const Utils = require('./Utils'); const main = async () => { const alphabet = "abcdefghijklmnopqrstuvwxyzabc"; const greekAlphabet = "αβγδεζηθικλμνξοπρσςτυφχψωαβγ"; const emoji = "😃😇🥰🤪🤑😴🤒🥵🥶🤯🥳😎😥😱😡🤬💀👽🤖😺🙈🙉🙊😃😇🥰"; console.log("Length: " + Utils.cpLength(alphabet)); console.log("charAt(17): " + Utils.uniAt(alphabet, 17)); console.log("codePointAt(17): " + Utils.cpAt(alphabet, 17)); console.log("substr(23, 26): " + Utils.cpSubstr(alphabet, 23, 3)); console.log("prefix(6): " + Utils.cpSubstr(alphabet, 0, 6)); console.log("right_tail(6): " + Utils.cpSubstr(alphabet, 6)); console.log("suffix(6): " + Utils.cpSubstr(alphabet, Utils.cpLength(alphabet) - 6)); console.log("find(\'def\'): " + Utils.cpIndexOf(alphabet, "def")); console.log("find(\'def\') is not found: " + (Utils.cpIndexOf(alphabet, "def") === -1)); console.log("find(\'bug\'): " + Utils.cpIndexOf(alphabet, "bug")); console.log("find(\'bug\') is not found: " + (Utils.cpIndexOf(alphabet, "bug") === -1)); console.log("rfind(\'abc\'): " + Utils.cpLastIndexOf(alphabet, "abc")); console.log("rfind(\'abc\') is not found: " + (Utils.cpLastIndexOf(alphabet, "abc") === -1)); console.log("rfind(\'bug\'): " + Utils.cpLastIndexOf(alphabet, "bug")); console.log("rfind(\'bug\') is not found: " + (Utils.cpLastIndexOf(alphabet, "bug") === -1)); console.log("Length: " + Utils.cpLength(greekAlphabet)); console.log("charAt(17): " + Utils.uniAt(greekAlphabet, 17)); console.log("codePointAt(17): " + Utils.cpAt(greekAlphabet, 17)); console.log("substr(23, 26): " + Utils.cpSubstr(greekAlphabet, 23, 3)); console.log("prefix(6): " + Utils.cpSubstr(greekAlphabet, 0, 6)); console.log("right_tail(6): " + Utils.cpSubstr(greekAlphabet, 6)); console.log("suffix(6): " + Utils.cpSubstr(greekAlphabet, Utils.cpLength(greekAlphabet) - 6)); console.log("find(\'δεζ\'): " + Utils.cpIndexOf(greekAlphabet, "δεζ")); console.log("find(\'δεζ\') is not found: " + (Utils.cpIndexOf(greekAlphabet, "δεζ") === -1)); console.log("find(\'bug\'): " + Utils.cpIndexOf(greekAlphabet, "bug")); console.log("find(\'bug\') is not found: " + (Utils.cpIndexOf(greekAlphabet, "bug") === -1)); console.log("rfind(\'αβγ\'): " + Utils.cpLastIndexOf(greekAlphabet, "αβγ")); console.log("rfind(\'αβγ\') is not found: " + (Utils.cpLastIndexOf(greekAlphabet, "αβγ") === -1)); console.log("rfind(\'bug\'): " + Utils.cpLastIndexOf(greekAlphabet, "bug")); console.log("rfind(\'bug\') is not found: " + (Utils.cpLastIndexOf(greekAlphabet, "bug") === -1)); /****************************************************************************** * Because strings are represented using UTF-16 it takes two characters to * represent one codepoint for these emoji. So lengths and positions are twice * what we normally would expect when dealing with Unicode characters. So the * standard string functions basically only work correctly with chraracters from * the BMP. Use the functions from Utils module to work correctly with all * Unicode characters. *****************************************************************************/ console.log("Length: " + Utils.cpLength(emoji)); console.log("charAt(16): " + Utils.uniAt(emoji, 16)); console.log("codePointAt(16): " + Utils.cpAt(emoji, 16)); console.log("substr(20, 24): " + Utils.cpSubstr(emoji, 20, 4)); console.log("prefix(6): " + Utils.cpSubstr(emoji, 0, 6)); console.log("right_tail(6): " + Utils.cpSubstr(emoji, 6)); console.log("suffix(6): " + Utils.cpSubstr(emoji, Utils.cpLength(emoji) - 6)); console.log("find(\'😱😡🤬\'): " + Utils.cpIndexOf(emoji, "😱😡🤬")); console.log("find(\'😱😡🤬\') is not found: " + (Utils.cpIndexOf(emoji, "😱😡🤬") === -1)); console.log("find(\'bug\'): " + Utils.cpIndexOf(emoji, "bug")); console.log("find(\'bug\') is not found: " + (Utils.cpIndexOf(emoji, "bug") === -1)); console.log("rfind(\'😃😇🥰\'): " + Utils.cpLastIndexOf(emoji, "😃😇🥰")); console.log("rfind(\'😃😇🥰\') is not found: " + (Utils.cpLastIndexOf(emoji, "😃😇🥰") === -1)); console.log("rfind(\'bug\'): " + Utils.cpLastIndexOf(emoji, "bug")); console.log("rfind(\'bug\') is not found: " + (Utils.cpLastIndexOf(emoji, "bug") === -1)); } main().catch( e => { console.error(e) } );