js-codepoints
Advanced tools
Comparing version 0.0.1 to 0.0.2
/* | ||
* $Id: codepoints.js,v 0.1 2013/03/29 06:12:16 dankogai Exp dankogai $ | ||
* $Id: codepoints.js,v 0.2 2013/03/29 07:04:43 dankogai Exp dankogai $ | ||
* | ||
@@ -27,7 +27,8 @@ * codepoints.js | ||
// number of codepoints in the string -- needs ES5 | ||
install(String.prototype, 'codePointsOf', function() { | ||
return this.replace(re_uchars, '_').length; | ||
}); | ||
if (defineProperty !== dummyfun) { | ||
defineProperty(String.prototype, 'codepoints', { | ||
get: function() { | ||
return this.replace(re_uchars, '_').length; | ||
} | ||
get: function() { return this.codePointsOf() } | ||
}); | ||
@@ -34,0 +35,0 @@ } |
@@ -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);if(defineProperty!==dummyfun){defineProperty(String.prototype,"codepoints",{get:function(){return this.replace(re_uchars,"_").length}})}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 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); |
{ | ||
"name": "js-codepoints", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "make your javascript handle unicode codepoints more correctly", | ||
@@ -5,0 +5,0 @@ "main": "codepoints.js", |
@@ -11,8 +11,9 @@ [![build status](https://secure.travis-ci.org/dankogai/js-codepoints.png)](http://travis-ci.org/dankogai/js-codepoints) | ||
````javascript | ||
'𠮷野家'.length; // 4 // unfortunately | ||
'𠮷野家'.codepoints; // 3 // naturally | ||
String.fromCodePoint(0x20BB7); // '𠮷' | ||
'𠮷野家'.codePointAt(0); // 0x20BB7 | ||
'𠮷野家'.toArrayOfUChar(); // ['𠮷','野','家'] | ||
'𠮷野家'.replace(RegExp.RE_UCHAR, '.'); // '...' | ||
'𠮷野家'.length; // 4 // unfortunately | ||
'𠮷野家'.codepoints; // 3 // naturally | ||
String.fromCodePoint(0x20BB7); // '𠮷' | ||
'𠮷野家'.codePointAt(0); // 0x20BB7 | ||
'𠮷野家'.uCharAt(0); // 𠮷 | ||
'𠮷野家'.toArrayOfUChars(); // ['𠮷','野','家'] | ||
'𠮷野家'.replace(RegExp.RE_UCHARS, '!'); // '!!!' | ||
```` |
@@ -28,2 +28,8 @@ /* | ||
}); | ||
describe('String.prototype.codePointsOf()', function(){ | ||
it('"' + yoshinoya + '".codePointsOf() === 3', | ||
eq(yoshinoya.codePointsOf(), 3)); | ||
it('吉野家".codePointsOf() === 3', | ||
eq("吉野家".codePointsOf(), 3)); | ||
}); | ||
if (Object.defineProperty) describe( | ||
@@ -30,0 +36,0 @@ 'String.prototype.codepoints', function(){ |
Sorry, the diff of this file is not supported yet
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
11178
147
19