js-codepoints
Advanced tools
Comparing version 0.0.2 to 0.0.4
/* | ||
* $Id: codepoints.js,v 0.2 2013/03/29 07:04:43 dankogai Exp dankogai $ | ||
* $Id: codepoints.js,v 0.4 2013/04/02 07:29:42 dankogai Exp dankogai $ | ||
* | ||
@@ -15,8 +15,14 @@ * codepoints.js | ||
// utilities | ||
var dummyfun = function(){}; | ||
var defineProperty = Object.defineProperty || dummyfun; | ||
var defineProperty = Object.defineProperty; | ||
var install = function(o, k, f) { | ||
if (o[k]) return; | ||
o[k] = f; | ||
defineProperty(o, k, {enumerable:false}) | ||
if (Object.defaultProperty) { | ||
Object.defaultProperty( | ||
o, k, | ||
{value: f, writable: true, configurable: true} | ||
); | ||
} else { | ||
if (o[k]) return; | ||
o[k] = f; | ||
if (defineProperty) defineProperty(o, k, {enumerable: false}); | ||
} | ||
}; | ||
@@ -29,12 +35,12 @@ var fromCharCode = String.fromCharCode; | ||
install(String.prototype, 'codePointsOf', function() { | ||
return this.replace(re_uchars, '_').length; | ||
return ('' + this).replace(re_uchars, '_').length; | ||
}); | ||
if (defineProperty !== dummyfun) { | ||
if (defineProperty) { | ||
defineProperty(String.prototype, 'codepoints', { | ||
get: function() { return this.codePointsOf() } | ||
get: function() { return ('' + this).codePointsOf() } | ||
}); | ||
} | ||
install(String.prototype, 'forEachUChar', function(f, ctx) { | ||
var i = 0, s = this; | ||
this.replace(re_uchars, function(u) { | ||
var i = 0, s = '' + this; | ||
s.replace(re_uchars, function(u) { | ||
f.call(ctx, u, i++, s); | ||
@@ -44,16 +50,14 @@ }); | ||
install(String.prototype, 'toArrayOfUChars', function() { | ||
var ret = []; | ||
this.replace(re_uchars, function(cp){ ret.push(cp) }); | ||
return ret; | ||
return ('' + this).match(re_uchars); | ||
}); | ||
install(String.prototype, 'uCharAt', function(n) { | ||
return this.toArrayOfUChars()[n] | ||
return ('' + this).toArrayOfUChars()[n]; | ||
}); | ||
// ES6 proposals | ||
install(String, 'fromCodePoint', function(n) { | ||
if (n < 0 && n > 0x10FFFF) | ||
if (n < 0 && n > 0x10FFFF) | ||
throw new RangeError('invalid codepoint'); | ||
if (n < 0xFFFF) return fromCharCode(n); | ||
var offset = n - 0x10000, | ||
hi = (offset >>> 10) + 0xD800, | ||
hi = (offset >>> 10) + 0xD800, | ||
lo = (offset & 0x3FF) + 0xDC00; | ||
@@ -63,6 +67,6 @@ return fromCharCode(hi) + fromCharCode(lo); | ||
install(String.prototype, 'codePointAt', function(n) { | ||
var c = this.toArrayOfUChars()[n]; | ||
var c = ('' + this).toArrayOfUChars()[n]; | ||
if (!c) return NaN; | ||
if (c.length === 1) return c.charCodeAt(0); | ||
return 0x10000 | ||
return 0x10000 | ||
+ (c.charCodeAt(0) - 0xD800) * 0x400 | ||
@@ -69,0 +73,0 @@ + (c.charCodeAt(1) - 0xDC00); |
@@ -1,1 +0,1 @@ | ||
(function(root){"use strict";var dummyfun=function(){};var defineProperty=Object.defineProperty||dummyfun;var install=function(o,k,f){if(o[k])return;o[k]=f;defineProperty(o,k,{enumerable:false})};var fromCharCode=String.fromCharCode;var re_uchars=/([\uD800-\uDBFF][\uDC00-\uDFFFF]|[\S\s])/g;install(RegExp,"RE_UCHARS",re_uchars);install(String.prototype,"codePointsOf",function(){return this.replace(re_uchars,"_").length});if(defineProperty!==dummyfun){defineProperty(String.prototype,"codepoints",{get:function(){return this.codePointsOf()}})}install(String.prototype,"forEachUChar",function(f,ctx){var i=0,s=this;this.replace(re_uchars,function(u){f.call(ctx,u,i++,s)})});install(String.prototype,"toArrayOfUChars",function(){var ret=[];this.replace(re_uchars,function(cp){ret.push(cp)});return ret});install(String.prototype,"uCharAt",function(n){return this.toArrayOfUChars()[n]});install(String,"fromCodePoint",function(n){if(n<0&&n>1114111)throw new RangeError("invalid codepoint");if(n<65535)return fromCharCode(n);var offset=n-65536,hi=(offset>>>10)+55296,lo=(offset&1023)+56320;return fromCharCode(hi)+fromCharCode(lo)});install(String.prototype,"codePointAt",function(n){var c=this.toArrayOfUChars()[n];if(!c)return NaN;if(c.length===1)return c.charCodeAt(0);return 65536+(c.charCodeAt(0)-55296)*1024+(c.charCodeAt(1)-56320)})})(this); | ||
(function(root){"use strict";var defineProperty=Object.defineProperty;var install=function(o,k,f){if(Object.defaultProperty){Object.defaultProperty(o,k,{value:f,writable:true,configurable:true})}else{if(o[k])return;o[k]=f;if(defineProperty)defineProperty(o,k,{enumerable:false})}};var fromCharCode=String.fromCharCode;var re_uchars=/([\uD800-\uDBFF][\uDC00-\uDFFFF]|[\S\s])/g;install(RegExp,"RE_UCHARS",re_uchars);install(String.prototype,"codePointsOf",function(){return(""+this).replace(re_uchars,"_").length});if(defineProperty){defineProperty(String.prototype,"codepoints",{get:function(){return(""+this).codePointsOf()}})}install(String.prototype,"forEachUChar",function(f,ctx){var i=0,s=""+this;s.replace(re_uchars,function(u){f.call(ctx,u,i++,s)})});install(String.prototype,"toArrayOfUChars",function(){return(""+this).match(re_uchars)});install(String.prototype,"uCharAt",function(n){return(""+this).toArrayOfUChars()[n]});install(String,"fromCodePoint",function(n){if(n<0&&n>1114111)throw new RangeError("invalid codepoint");if(n<65535)return fromCharCode(n);var offset=n-65536,hi=(offset>>>10)+55296,lo=(offset&1023)+56320;return fromCharCode(hi)+fromCharCode(lo)});install(String.prototype,"codePointAt",function(n){var c=(""+this).toArrayOfUChars()[n];if(!c)return NaN;if(c.length===1)return c.charCodeAt(0);return 65536+(c.charCodeAt(0)-55296)*1024+(c.charCodeAt(1)-56320)})})(this); |
{ | ||
"name": "js-codepoints", | ||
"version": "0.0.2", | ||
"version": "0.0.4", | ||
"description": "make your javascript handle unicode codepoints more correctly", | ||
@@ -5,0 +5,0 @@ "main": "codepoints.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12863
151