oxjs/source/Ox.Unicode/Ox.Unicode.js

105 lines
3.3 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
Ox.load.Unicode = function(options, callback) {
Ox.getJSON(Ox.PATH + 'Ox.Unicode/json/Ox.Unicode.json?' + Ox.VERSION, function(chars) {
2011-10-06 00:30:09 +00:00
//@ Constants ----------------------------------------------------------
//@ Ox.UNICODE_CHARACTERS <o> Unicode characters
Ox.UNICODE_CHARACTERS = chars;
var byASCII = {}, byName = {}, bySection = [], byType = {};
Ox.forEach(chars, function(data, char) {
if (data.ascii) {
byASCII[data.ascii] = byASCII[data.ascii] || [];
byASCII[data.ascii].push(char);
}
data.names.forEach(function(name) {
byName[name] = char;
});
bySection[data.section] = bySection[data.section] || [];
bySection[data.section].push(char);
byType[data.type] = byType[data.type] || [];
byType[data.type].push(char);
});
2011-10-06 00:30:09 +00:00
//@ Functions ----------------------------------------------------------
/*@
Ox.getCharactersByASCII <f> Returns all unicode equivalents of a given ASCII string
(ascii) -> <[s]> Unicode Characters
ascii <s> ASCII string
> Ox.getCharactersByASCII('A').length
36
@*/
Ox.getCharactersByASCII = function(ascii) {
return byASCII[ascii] || '';
};
/*@
Ox.getCharacterByName <f> Returns a character for a given unicode name
(name) -> <s> Character
> Ox.getCharacterByName('Skull and Crossbones')
'\u2620'
@*/
Ox.getCharacterByName = function(name) {
return byName[name.toUpperCase()] || '';
};
/*@
Ox.getCharactersBySection <f> Returns all characters in a given section
(section) -> <[s]> Characters
section <s> Unicode section name
> Ox.getCharactersBySection('Arabic').length
252
@*/
Ox.getCharactersBySection = function(section) {
return bySection[section.toUpperCase()] || '';
};
/*@
Ox.getCharactersByType <f> Returns all characters of a given type
(type) -> <[s]> Characters
type <s> Unicode character type
> Ox.getCharactersByType('Uppercase Latin Alphabet').join('')
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
@*/
Ox.getCharactersByType = function(type) {
return byType[type.toUpperCase()] || '';
};
/*@
Ox.sortASCII <f> Sorts an array of unicode strings
(arr) -> <[s]> Sorted array
> Ox.sortASCII(['å', 'b', 'ç', 'd', 'é'])
['å', 'b', 'ç', 'd', 'é']
@*/
2012-06-18 17:11:39 +00:00
// FIXME: should be Ox.sortUnicode
Ox.sortASCII = function(arr) {
return Ox.sort(arr, function(str) {
return Ox.toASCII(str);
});
};
/*@
Ox.toASCII <f> Replaces a unicode string with its ASCII equivalent
(unicode) -> <s> ASCII string
> Ox.toASCII('Åbçdé')
'Abcde'
> Ox.toASCII('\u2162 \u33a6')
'3 km3'
@*/
Ox.toASCII = function(str) {
return Ox.map(str, function(chr) {
return chars[chr].ascii || chr;
2012-05-23 15:13:38 +00:00
});
};
callback(true);
});
2012-05-23 15:13:38 +00:00
}