From: Jay Freeman (saurik) Date: Thu, 27 Dec 2012 03:48:43 +0000 (+0000) Subject: Export Unicode class as the module for easier use. X-Git-Url: https://git.saurik.com/utf16js.git/commitdiff_plain/7267a723a4383894c065276e2e43cc7b386d8f48 Export Unicode class as the module for easier use. --- diff --git a/utf16.js b/utf16.js index 87f9b91..b76fc71 100644 --- a/utf16.js +++ b/utf16.js @@ -2,7 +2,12 @@ if (typeof define !== 'function') { var define = require('amdefine')(module) } define(function(require) { -var decode = function(string, points) { +var utf16 = function(string) { + this.string = string; + this.length = utf16.strlen(string); +}; + +utf16.decode = function(string, points) { if (typeof points === "undefined") points = []; @@ -26,7 +31,7 @@ var decode = function(string, points) { return points; }; -var encode = function(points) { +utf16.encode = function(points) { var units = []; for (var i = 0, e = points.length; i != e; ++i) { var point = points[i]; @@ -39,7 +44,11 @@ var encode = function(points) { } return String.fromCharCode.apply(String, units); }; -var adjust = function(mode, string, after) { +utf16.fromCharCode = function() { + return new utf16(utf16.encode(arguments), arguments.length); +}; + +utf16.adjust = function(mode, string, after) { var before = after.slice(); var offset = 0, index = 0; @@ -78,84 +87,61 @@ var adjust = function(mode, string, after) { return after; }; -var point2unit = function(string, offsets) { - return adjust(false, string, offsets); +utf16.point2unit = function(string, offsets) { + return utf16.adjust(false, string, offsets); }; -var unit2point = function(string, offsets) { - return adjust(true, string, offsets); +utf16.unit2point = function(string, offsets) { + return utf16.adjust(true, string, offsets); }; -var charAt = function(string, offset) { - return substring(string, offset, offset + 1); +utf16.charAt = function(string, offset) { + return utf16.substring(string, offset, offset + 1); }; -var charCodeAt = function(string, offset) { - var char = charAt(string, offset); +utf16.charCodeAt = function(string, offset) { + var char = utf16.charAt(string, offset); if (char.length != 2) return char.charCodeAt(0); return 0x10000 | (char.charCodeAt(0) & 0x03ff) << 10 | char.charCodeAt(1) & 0x03ff }; -var length = function(string) { - return unit2point(string, [string.length])[0]; +utf16.strlen = function(string) { + return utf16.unit2point(string, [string.length])[0]; }; -var substr = function(string, start, length) { +utf16.substr = function(string, start, length) { var stop; if (typeof length !== "undefined") stop = start + length; - return substring(string, start, stop); + return utf16.substring(string, start, stop); }; -var substring = function(string, start, stop) { - var range = point2unit(string, [start, stop]); +utf16.substring = function(string, start, stop) { + var range = utf16.point2unit(string, [start, stop]); return string.substring(range[0], range[1]); }; -var Unicode = function(string) { - this.string = string; - this.length = length(string); -}; - -Unicode.fromCharCode = function() { - return encode(arguments); -}; - -Unicode.prototype.charAt = function(index) { - return new Unicode(charAt(this.string, index)); +utf16.prototype.charAt = function(index) { + return new utf16(utf16.charAt(this.string, index)); }; -Unicode.prototype.charCodeAt = function(index) { - return charCodeAt(this.string, index); +utf16.prototype.charCodeAt = function(index) { + return utf16.charCodeAt(this.string, index); }; -Unicode.prototype.substr = function(start, length) { - return new Unicode(substr(this.string, start, length)); +utf16.prototype.substr = function(start, length) { + return new utf16(utf16.substr(this.string, start, length)); }; -Unicode.prototype.substring = function(start, end) { - return new Unicode(substring(this.string, start, end)); +utf16.prototype.substring = function(start, end) { + return new utf16(utf16.substring(this.string, start, end)); }; -Unicode.prototype.toString = function() { +utf16.prototype.toString = function() { return this.string; }; -return { - decode: decode, - encode: encode, - - point2unit: point2unit, - unit2point: unit2point, - - charAt: charAt, - charCodeAt: charCodeAt, - length: length, - substr: substr, - substring: substring, - - Unicode: Unicode, -}; +return utf16; });