diff options
author | Bryan Berry <bryan@olenepal.org> | 2010-01-18 15:01:42 (GMT) |
---|---|---|
committer | Bryan Berry <bryan@olenepal.org> | 2010-01-18 15:01:42 (GMT) |
commit | d1cf37f15429f51844003619f41be23653479186 (patch) | |
tree | c48afd46855da8c5076a5c2bb449946f510bdc6c | |
parent | a30259202ae2bc7ffad2b89bde0bbb8eccba9a32 (diff) |
added Karma.convertNumToLocale method along w/ unit tests
-rwxr-xr-x | js/karma.js | 30 | ||||
-rwxr-xr-x | tests/js/tests.js | 127 |
2 files changed, 101 insertions, 56 deletions
diff --git a/js/karma.js b/js/karma.js index 75a8fd3..3d8c6f8 100755 --- a/js/karma.js +++ b/js/karma.js @@ -215,6 +215,36 @@ Karma.shuffle = function (oldList) { return newList; }; + +/** + * Converts a number to numerals in the specified locale. Currently only + * supports Nepali + * @param {Number} Number to be converted + * @param {locale} locale that number should be converted to + * @returns {String} Unicode string for localized numeral + */ +Karma.convertNumToLocale = function(num, locale){ + //48 is the base for western numerals + var convertDigit = function(digit){ + + var numBase = 48; + var prefix = "u00"; + + if (locale === "ne"){ + prefix = "u0"; + numBase = 2406; + } + + return '\\' + prefix + + (numBase + parseInt(digit)).toString(16); + }; + + var charArray = num.toString().split("").map(convertDigit); + console.log(charArray.join('')); + return eval('"' + charArray.join('') + '"'); +}; + + // Below are geometry and math helper methods /** diff --git a/tests/js/tests.js b/tests/js/tests.js index b37c0b9..2a10e65 100755 --- a/tests/js/tests.js +++ b/tests/js/tests.js @@ -72,7 +72,7 @@ }; - module("Module Helpers"); + module("helpers"); test("Basic Requirements", function() { @@ -142,6 +142,75 @@ }); + //Karma.shuffle + test('Karma.shuffle', function(){ + var list = [1,2,3,4,5,6,7,8,9,10]; + var newList = []; + var isShuffled = false; + + for (var i = 0; i < 10; i++){ + newList = Karma.shuffle(list); + for (var j = 0; j < newList.length; j++){ + if(newList[j] !== list[j]){ + isShuffled = true; + break; + } + } + if (isShuffled === false){ + break; + } + } + ok(isShuffled, "Shuffles each time"); + }); + + test('Karma.convertNumToLocale', + function(){ + expect(4); + ok(Karma.convertNumToLocale(0, 'ne') === '\u0966', + 'Number converted correctly'); + ok(Karma.convertNumToLocale(5, 'ne') === '\u096B', + 'Number converted correctly'); + ok(Karma.convertNumToLocale(555, 'ne') === '\u096B\u096B\u096B', + 'Number converted correctly'); + ok(Karma.convertNumToLocale(900, 'ne') === '\u096F\u0966\u0966', + 'Number converted correctly'); + + }); + + //Karma.radians + test('Karma.radians', + function(){ + expect(1); + ok(Karma.radians(50) >= 0.87 && + Karma.radians(50) <= 0.88, + "correct result computed"); + }); + + //Karma.distance2 + test('Karma.distance2', + function(){ + ok(Karma.distance2({x: 1, y:2}, {x: 9, y: 15}) === 64, + "returns correct value"); + }); + + //Karma.distance + test('Karma.distance', + function(){ + ok(Karma.distance({x: 1, y:2}, {x: 9, y: 15}) === 8, + "returns correct value"); + }); + + //Karma.rand + test('Karma.rand', + function(){ + var rand = Karma.rand(5, 8); + ok(rand >= 5 && rand <= 8, + "Generates valid range of numbers"); + } + ); + + + module("Module Karma core library"); @@ -710,61 +779,7 @@ //Karma._makeVideos tests - //Karma.shuffle - test('Karma.shuffle', function(){ - var list = [1,2,3,4,5,6,7,8,9,10]; - var newList = []; - var isShuffled = false; - - for (var i = 0; i < 10; i++){ - newList = Karma.shuffle(list); - for (var j = 0; j < newList.length; j++){ - if(newList[j] !== list[j]){ - isShuffled = true; - break; - } - } - if (isShuffled === false){ - break; - } - } - ok(isShuffled, "Shuffles each time"); - }); - - //Karma.radians - test('Karma.radians', - function(){ - expect(1); - ok(Karma.radians(50) >= 0.87 && - Karma.radians(50) <= 0.88, - "correct result computed"); - }); - - //Karma.distance2 - test('Karma.distance2', - function(){ - ok(Karma.distance2({x: 1, y:2}, {x: 9, y: 15}) === 64, - "returns correct value"); - }); - - //Karma.distance - test('Karma.distance', - function(){ - ok(Karma.distance({x: 1, y:2}, {x: 9, y: 15}) === 8, - "returns correct value"); - }); - - //Karma.rand - test('Karma.rand', - function(){ - var rand = Karma.rand(5, 8); - ok(rand >= 5 && rand <= 8, - "Generates valid range of numbers"); - } - ); - - - + test("k.canvas['testCanvas'].strokeStyle('#ffffff') " + "sets strokeStyle correctly", function(){ |