Comparing version 1.0.1 to 1.0.2
128
lib/index.js
@@ -0,20 +1,10 @@ | ||
'use strict'; | ||
var maps = require('./maps.js'); | ||
/** | ||
* Atonic main method | ||
* Invalid chars | ||
*/ | ||
var atonic = module.exports = function atonic(text, names) { | ||
names = isArray(names) ? names : maps.NAMES; | ||
if (names.indexOf(maps.COMMON_NAME) < 0) { | ||
names.push(maps.COMMON_NAME); | ||
} | ||
names.sort(); | ||
var INVALID_CHARS = /[\u0301]/g; | ||
var map = getMap(names); | ||
return text.replace(/[^\u0000-\u007e]/g, function(c) { | ||
return map[c] || c; | ||
}); | ||
}; | ||
/** | ||
@@ -24,21 +14,19 @@ * Additional methods | ||
var methods = { | ||
lowerCase: [maps.COMMON_NAME, 'latin', 'cyrillic'], | ||
upperCase: [maps.COMMON_NAME, 'LATIN', 'CYRILLIC'], | ||
latin: [maps.COMMON_NAME, 'latin', 'LATIN'], | ||
latinLowerCase: [maps.COMMON_NAME, 'latin'], | ||
cyrillic: [maps.COMMON_NAME, 'cyrillic', 'CYRILLIC'], | ||
cyrillicLowerCase: [maps.COMMON_NAME, 'cyrillic'] | ||
lowerCase: [maps.COMMON_NAME, 'latin', 'cyrillic'], | ||
upperCase: [maps.COMMON_NAME, 'LATIN', 'CYRILLIC'], | ||
latin: [maps.COMMON_NAME, 'latin', 'LATIN'], | ||
latinLowerCase: [maps.COMMON_NAME, 'latin'], | ||
cyrillic: [maps.COMMON_NAME, 'cyrillic', 'CYRILLIC'], | ||
cyrillicLowerCase: [maps.COMMON_NAME, 'cyrillic'] | ||
}; | ||
/** | ||
* Build methods | ||
*/ | ||
for (var method in methods) { | ||
atonic[method] = buildMethod(methods[method]); | ||
function extend(obj, source) { | ||
for (var key in source) { | ||
obj[key] = source[key]; | ||
} | ||
return obj; | ||
} | ||
function buildMethod(names) { | ||
return function(text) { | ||
return atonic(text, names); | ||
}; | ||
function isArray(target) { | ||
return Array.isArray(target); | ||
} | ||
@@ -54,39 +42,65 @@ | ||
function getMap(names) { | ||
var key = names.join('-'); | ||
if (storage[key]) { | ||
return storage[key]; | ||
} | ||
var key = names.join('-'); | ||
if (storage[key]) { | ||
return storage[key]; | ||
} | ||
var result = {}; | ||
var value; | ||
var i; | ||
var allmaps = {}; | ||
var result = {}; | ||
var value; | ||
var i; | ||
var allmaps = {}; | ||
for (i = 0; i < names.length; i++) { | ||
var name = names[i]; | ||
if (maps.NAMES[name] < 0) throw new Error('Invalid map name: ' + name); | ||
extend(allmaps, maps[name]); | ||
} | ||
for (i = 0; i < names.length; i++) { | ||
var name = names[i]; | ||
if (maps.NAMES[name] < 0) { | ||
throw new Error('Invalid map name: ' + name); | ||
} | ||
extend(allmaps, maps[name]); | ||
} | ||
for (var c in allmaps) { | ||
value = allmaps[c]; | ||
for (i = 0; i < value.length; i++) { | ||
result[value[i]] = c; | ||
} | ||
} | ||
for (var c in allmaps) { | ||
value = allmaps[c]; | ||
for (i = 0; i < value.length; i++) { | ||
result[value[i]] = c; | ||
} | ||
} | ||
storage[key] = result; | ||
storage[key] = result; | ||
return result; | ||
return result; | ||
} | ||
function extend(obj, source) { | ||
for (var key in source) { | ||
obj[key] = source[key]; | ||
} | ||
return obj; | ||
/** | ||
* Atonic main method | ||
*/ | ||
var atonic = module.exports = function atonic(text, names) { | ||
names = isArray(names) ? names : maps.NAMES; | ||
if (names.indexOf(maps.COMMON_NAME) < 0) { | ||
names.push(maps.COMMON_NAME); | ||
} | ||
names.sort(); | ||
var map = getMap(names); | ||
text = text.replace(INVALID_CHARS, ''); | ||
return text.replace(/[^\u0000-\u007e]/g, function(c) { | ||
return map[c] || c; | ||
}); | ||
}; | ||
/** | ||
* Build a method | ||
*/ | ||
function buildMethod(names) { | ||
return function(text) { | ||
return atonic(text, names); | ||
}; | ||
} | ||
function isArray(target) { | ||
return Array.isArray(target); | ||
/** | ||
* Build methods | ||
*/ | ||
for (var method in methods) { | ||
atonic[method] = buildMethod(methods[method]); | ||
} |
@@ -0,1 +1,4 @@ | ||
/*eslint quotes:0*/ | ||
'use strict'; | ||
var maps = module.exports; | ||
@@ -2,0 +5,0 @@ |
{ | ||
"name": "atonic", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "remove diacritics from strings", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -25,2 +25,2 @@ # atonic | ||
``` | ||
``` |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var atonic = require('../lib'); | ||
@@ -6,26 +8,28 @@ var assert = require('chai').assert; | ||
it('atonic("") - all maps', function() { | ||
assert.equal('A,a,A,a,O,oУу У у Ч ч Г г Ы ы Г г Х х Х хЕ Е Ђ Г Є Ѕ І І Ј Љ Њ Ћ К И У Џ', atonic("Å,å,Ä,ä,Ö,öӰӱ Ӳ ӳ Ӵ ӵ Ӷ ӷ Ӹ ӹ Ӻ ӻ Ӽ ӽ Ӿ ӿЀ Ё Ђ Ѓ Є Ѕ І Ї Ј Љ Њ Ћ Ќ Ѝ Ў Џ")); | ||
assert.equal('Internationalizati0n', atonic("Iлtèrnåtïonɑlíƶatï߀ԉ")); | ||
assert.equal('AAAAAAAECEEEEIIII-Internationaliza-ЧчГгЫыГгХхХхЕ', atonic("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Iлtèrnåtïonɑlíƶa-ӴӵӶӷӸӹӺӻӼӽӾӿЀ")); | ||
assert.equal('A,a,A,a,O,oУу У у Ч ч Г г Ы ы Г г Х х Х хЕ Е Ђ Г Є Ѕ І І Ј Љ Њ Ћ К И У Џ', atonic('Å,å,Ä,ä,Ö,öӰӱ Ӳ ӳ Ӵ ӵ Ӷ ӷ Ӹ ӹ Ӻ ӻ Ӽ ӽ Ӿ ӿЀ Ё Ђ Ѓ Є Ѕ І Ї Ј Љ Њ Ћ Ќ Ѝ Ў Џ')); | ||
assert.equal('Internationalizati0n', atonic('Iлtèrnåtïonɑlíƶatï߀ԉ')); | ||
assert.equal('AAAAAAAECEEEEIIII-Internationaliza-ЧчГгЫыГгХхХхЕ', atonic('ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Iлtèrnåtïonɑlíƶa-ӴӵӶӷӸӹӺӻӼӽӾӿЀ')); | ||
assert.equal('Приморскии краи', atonic('Примо́рский край')); | ||
//assert.equal('AAAAAAAECEEEEIIII-Internationaliza-ЧчГгЫыГгХхХхЕ', atonic('℀ ℁ ℂ ℃ ℄ ℅ ℆ ℇ ℈ ℉ ℊ ℋ ℌ ℍ ℎ ℏⅈ')); | ||
}); | ||
it('atonic.lowerCase("")', function() { | ||
assert.equal('Å,a,Ä,a,Ö,oӰу Ӳ у Ӵ ч Ӷ г', atonic.lowerCase("Å,å,Ä,ä,Ö,öӰӱ Ӳ ӳ Ӵ ӵ Ӷ ӷ")); | ||
assert.equal('ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Internationaliza-ӴчӶгӸыӺгӼхӾхЀ', atonic.lowerCase("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Iлtèrnåtïonɑlíƶa-ӴӵӶӷӸӹӺӻӼӽӾӿЀ")); | ||
assert.equal('Å,a,Ä,a,Ö,oӰу Ӳ у Ӵ ч Ӷ г', atonic.lowerCase('Å,å,Ä,ä,Ö,öӰӱ Ӳ ӳ Ӵ ӵ Ӷ ӷ')); | ||
assert.equal('ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Internationaliza-ӴчӶгӸыӺгӼхӾхЀ', atonic.lowerCase('ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Iлtèrnåtïonɑlíƶa-ӴӵӶӷӸӹӺӻӼӽӾӿЀ')); | ||
}); | ||
it('atonic.upperCase("")', function() { | ||
assert.equal('A,å,A,ä,O,öУӱ У ӳ Ч ӵ Г ӷ', atonic.upperCase("Å,å,Ä,ä,Ö,öӰӱ Ӳ ӳ Ӵ ӵ Ӷ ӷ")); | ||
assert.equal('AAAAAAAECEEEEIIII-Iлtèrnåtïonɑlíƶa-ЧӵГӷЫӹГӻХӽХӿЕ', atonic.upperCase("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Iлtèrnåtïonɑlíƶa-ӴӵӶӷӸӹӺӻӼӽӾӿЀ")); | ||
assert.equal('A,å,A,ä,O,öУӱ У ӳ Ч ӵ Г ӷ', atonic.upperCase('Å,å,Ä,ä,Ö,öӰӱ Ӳ ӳ Ӵ ӵ Ӷ ӷ')); | ||
assert.equal('AAAAAAAECEEEEIIII-Iлtèrnåtïonɑlíƶa-ЧӵГӷЫӹГӻХӽХӿЕ', atonic.upperCase('ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Iлtèrnåtïonɑlíƶa-ӴӵӶӷӸӹӺӻӼӽӾӿЀ')); | ||
}); | ||
it('atonic.latin("")', function() { | ||
assert.equal('Internationalizati0n', atonic.latin("Iлtèrnåtïonɑlíƶatï߀ԉ")); | ||
assert.equal('AAAAAAAECEEEEIIII-Internationaliza', atonic.latin("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Iлtèrnåtïonɑlíƶa")); | ||
assert.equal('AAAAAAAECEEEEIIII-Internationaliza-ӴӵӶӷӸӹӺӻӼӽӾӿЀ', atonic.latin("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Iлtèrnåtïonɑlíƶa-ӴӵӶӷӸӹӺӻӼӽӾӿЀ")); | ||
assert.equal('Internationalizati0n', atonic.latin('Iлtèrnåtïonɑlíƶatï߀ԉ')); | ||
assert.equal('AAAAAAAECEEEEIIII-Internationaliza', atonic.latin('ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Iлtèrnåtïonɑlíƶa')); | ||
assert.equal('AAAAAAAECEEEEIIII-Internationaliza-ӴӵӶӷӸӹӺӻӼӽӾӿЀ', atonic.latin('ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Iлtèrnåtïonɑlíƶa-ӴӵӶӷӸӹӺӻӼӽӾӿЀ')); | ||
}); | ||
it('atonic.latinUpperCase("")', function() { | ||
assert.equal('AAAAAAAECEEEEIIII', atonic.latin("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ")); | ||
assert.equal('AAAAAAAECEEEEIIII', atonic.latin('ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ')); | ||
}); | ||
it('atonic.cyrillic("")', function() { | ||
assert.equal('ЕЕГГгҔҕЖжЗзкКк', atonic.cyrillic("ЀЁЃҒғҔҕҖҗҘҙҝҞҟ")); | ||
assert.equal('ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Iлtèrnåtïonɑlíƶa-ЧчГгЫыГгХхХхЕ', atonic.cyrillic("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Iлtèrnåtïonɑlíƶa-ӴӵӶӷӸӹӺӻӼӽӾӿЀ")); | ||
assert.equal('ЕЕГГгҔҕЖжЗзкКк', atonic.cyrillic('ЀЁЃҒғҔҕҖҗҘҙҝҞҟ')); | ||
assert.equal('ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Iлtèrnåtïonɑlíƶa-ЧчГгЫыГгХхХхЕ', atonic.cyrillic('ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ-Iлtèrnåtïonɑlíƶa-ӴӵӶӷӸӹӺӻӼӽӾӿЀ')); | ||
}); | ||
}); |
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
15414
8
335
26