transliteration
Advanced tools
Comparing version 0.0.5 to 0.0.7
@@ -1,52 +0,78 @@ | ||
var punycode = require('punycode'); | ||
// Credit: https://github.com/bestiejs/punycode.js/blob/master/LICENSE-MIT.txt | ||
var ucs2decode = function(string) { | ||
var output = [], | ||
counter = 0, | ||
length = string.length, | ||
value, | ||
extra; | ||
while (counter < length) { | ||
value = string.charCodeAt(counter++); | ||
if (value >= 0xD800 && value <= 0xDBFF && counter < length) { | ||
// high surrogate, and there is a next character | ||
extra = string.charCodeAt(counter++); | ||
if ((extra & 0xFC00) == 0xDC00) { // low surrogate | ||
output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); | ||
} else { | ||
// unmatched surrogate; only append this code unit, in case the next | ||
// code unit is the high surrogate of a surrogate pair | ||
output.push(value); | ||
counter--; | ||
} | ||
} else { | ||
output.push(value); | ||
} | ||
} | ||
return output; | ||
} | ||
var transliteration = { | ||
transliterate: function(str, unknown) { | ||
var codemap = {}; | ||
unknown = unknown || '?'; | ||
str = punycode.ucs2.decode(str); | ||
strNew = ''; | ||
Array.prototype.forEach.call(str, function(ord) { | ||
if(ord > 0xffff) { | ||
strNew += unknown; | ||
var transliteration = function(str, unknown) { | ||
var codemap = {}, ord, ascii; | ||
unknown = unknown || '?'; | ||
str = ucs2decode(str); | ||
strNew = ''; | ||
for(i in str) { | ||
ord = str[i]; | ||
if(ord > 0xffff) { | ||
strNew += unknown; | ||
continue; | ||
} | ||
var bank = ord >> 8; | ||
if(typeof codemap[bank] === 'undefined') { | ||
var filename = '../data/x' + bank.toString(16) + '.js'; | ||
try { | ||
codemap[bank] = require(filename); | ||
} | ||
var bank = ord >> 8; | ||
if(typeof codemap[bank] === 'undefined') { | ||
var filename = '../data/x' + bank.toString(16) + '.js'; | ||
try { | ||
codemap[bank] = require(filename); | ||
catch(e) { | ||
codemap[bank] = []; | ||
for(var i = 0; i < 256; i++) { | ||
codemap[bank].push(unknown); | ||
} | ||
catch(e) { | ||
codemap[bank] = []; | ||
for(var i = 0; i < 256; i++) { | ||
codemap[bank].push(unknown); | ||
} | ||
} | ||
} | ||
ord = 0xff & ord; | ||
var str = codemap[bank][ord]; | ||
if(str === null) str = unknown; | ||
strNew += str; | ||
}); | ||
return strNew; | ||
}, | ||
slugify: function(str, options) { | ||
var defaultOptions = { | ||
lowercase: true, | ||
separator: '-', | ||
} | ||
options = options || {}; | ||
Array.prototype.forEach.call(Object.getOwnPropertyNames(defaultOptions), function(prop) { | ||
if(typeof options[prop] === 'undefined') options[prop] = defaultOptions[prop]; | ||
}); | ||
var slug = transliteration.transliterate(str).replace(/[^a-zA-Z0-9]+/g, options.separator); | ||
if(options.lowercase) { | ||
slug = slug.toLowerCase(); | ||
} | ||
// remove leading and trailing separator | ||
if(slug[0] == options.separator) slug = slug.substr(1); | ||
if(slug[slug.length -1] == options.separator) slug = slug.substr(0, slug.length -1); | ||
return slug; | ||
ord = 0xff & ord; | ||
ascii = codemap[bank][ord]; | ||
if(ascii === null) ascii = unknown; | ||
strNew += ascii; | ||
}; | ||
return strNew; | ||
}; | ||
transliteration.transliterate = transliteration; | ||
transliteration.slugify = function(str, options) { | ||
var defaultOptions = { | ||
lowercase: true, | ||
separator: '-' | ||
}, key; | ||
options = options || {}; | ||
for(prop in defaultOptions) { | ||
if(typeof options[prop] === 'undefined') options[prop] = defaultOptions[prop]; | ||
} | ||
} | ||
var slug = transliteration.transliterate(str).replace(/[^a-zA-Z0-9]+/g, options.separator); | ||
if(options.lowercase) { | ||
slug = slug.toLowerCase(); | ||
} | ||
// remove leading and trailing separator | ||
if(slug[0] == options.separator) slug = slug.substr(1); | ||
if(slug[slug.length -1] == options.separator) slug = slug.substr(0, slug.length -1); | ||
return slug; | ||
}; | ||
module.exports = transliteration; |
{ | ||
"name": "transliteration", | ||
"version": "0.0.5", | ||
"description": "Transliteration module for node.js. Transliterate unicode characters into latin ones. Supports all common unicode characters including CJK.", | ||
"version": "0.0.7", | ||
"description": "Transliteration module for node.js. Transliterate unicode characters into latin characters. Supports all common unicode characters including CJK.", | ||
"main": "lib/transliteration.js", | ||
@@ -21,3 +21,3 @@ "scripts": { | ||
"author": "Andy Hu", | ||
"license": "BSD-2-Clause", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -24,0 +24,0 @@ "url": "https://github.com/andyhu/node-transliteration/issues" |
@@ -13,3 +13,3 @@ # Transliteration | ||
### transliterate(str, unknown) | ||
### transliteration(str, unknown) | ||
@@ -20,6 +20,6 @@ Transliterate `str`. Unknown characters will be converted to `unknown` | ||
```javascript | ||
var transliteration = require('transliteration'); | ||
transliteration.transliterate('你好,世界'); // Ni Hao ,Shi Jie | ||
transliteration.transliterate('Γεια σας, τον κόσμο'); // Geia sas, ton kosmo | ||
transliteration.transliterate('안녕하세요, 세계'); // annyeonghaseyo, segye | ||
var tr = require('transliteration'); | ||
tr('你好,世界'); // Ni Hao ,Shi Jie | ||
tr('Γεια σας, τον κόσμο'); // Geia sas, ton kosmo | ||
tr('안녕하세요, 세계'); // annyeonghaseyo, segye | ||
``` | ||
@@ -41,5 +41,11 @@ | ||
```javascript | ||
var transliteration = require('transliteration'); | ||
transliteration.slugify('你好,世界'); // ni-hao-shi-jie | ||
transliteration.slugify('你好,世界', {lowercase: false, separator: '_'}); // Ni_Hao_Shi_Jie | ||
var slugify = require('transliteration').slugify; | ||
slugify('你好,世界'); // ni-hao-shi-jie | ||
slugify('你好,世界', {lowercase: false, separator: '_'}); // Ni_Hao_Shi_Jie | ||
``` | ||
### Client side usage | ||
``` | ||
bower install transliteration | ||
``` | ||
You can also use this module in the browser. Please check example.html for detailed usage. |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
926536
188
2467
48
1