Skip to content

Instantly share code, notes, and snippets.

@kyle-west
Created February 12, 2019 16:13
Show Gist options
  • Save kyle-west/7401fd24f11fa38f11b9b068e895408d to your computer and use it in GitHub Desktop.
Save kyle-west/7401fd24f11fa38f11b9b068e895408d to your computer and use it in GitHub Desktop.
String Class Extension for determining if a string has unicode characters or not.
/**
* @method containsUnicode
* @extends {String}
*
* @returns {Boolean} - True iff <String> contains one or more Unicode Characters
*/
String.prototype.containsUnicode = function () {
return this.split('').reduce((a,x)=> a || x.charCodeAt(0) > 255, false)
}
@kyle-west
Copy link
Author

Usage:

let happyEN = "happy";
let happyZH = "快乐";

console.log("happyEN contains Unicode Chars:", happyEN.containsUnicode()); // "happyEN contains Unicode Chars: false"
console.log("happyZH contains Unicode Chars:", happyZH.containsUnicode()); // "happyZH contains Unicode Chars: true"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment