diff --git a/src/xapi-util.js b/src/xapi-util.js index 9589447..f5aa4ba 100644 --- a/src/xapi-util.js +++ b/src/xapi-util.js @@ -39,26 +39,44 @@ ADL.xapiutil.getLangVal = function (langprop) { //test for undefined first + if (!langprop) return; +//let's get wordy for a bit and think things out +//is this really the best way to do this? don't know but let's do it +//and then look at how to make it better + //so we've tested and there is something passed in at this point + //next we grab the keys in an array so we can easily get to what we want var options = Object.keys(langprop); // test that langprop is a dict (obj) // skips if not a dict(obj) and returns + //is undefined what we want to return - + //so maybe the calling function will have to test and then get id or something if (options.length == 0) return undefined; + //at this point something was passed in and that something has some kind of properties that we have the keys for in an array + //so let's get the language and see if we can find a match + //note we only go from what we are given and whittle down until we find a match or had back the first option if we find nothing + //should we have looked harder to make sure this is a dictionary, or worked to find a more complex match, i'm kind of hoping not because i'm happy to not do work that won't really make it better, but on the other hand if this really would make this better at what it it doing, then I want to do it var lang = ADL.xapiutil.getLang(), - ret = langprop[options[0]];//make undefined + ret, //make undefined dispGotten = false; do { //test and retest + //if the key works, unlock the door if (langprop[lang]) { ret = langprop[lang]; dispGotten = true; } + //if not, adjust the key and set up to try again else if (lang.indexOf('-')) { lang = lang.substring(0, lang.lastIndexOf('-')); +// console.log('lang is now', lang); } + //there was another else option, but the while test takes care of stopping if there is nothing left of our string } while (!dispGotten && lang !==""); + //and here we return what we've found or haven't + //let's go see if this will all run, and let's make some tests to try it out return ret; }; diff --git a/test/tests/test.util.js b/test/tests/test.util.js index 46f1d13..ddb5866 100644 --- a/test/tests/test.util.js +++ b/test/tests/test.util.js @@ -53,7 +53,21 @@ describe('testing xAPI utilities', function () { (util.getLangVal(s2.verb.display)).should.eql("recommend"); }); it('should return the first display option if language code does not match any of the keys', function () { - (util.getLangVal(s6.verb.display)).should.eql("établi"); + ("undefined").should.eql(typeof util.getLangVal(s6.verb.display)); + // (util.getLangVal(s6.verb.display)).should.eql("établi"); + }); +//what else do we want to test + it('should throw out junk', function () { + ("undefined").should.eql(typeof util.getLangVal(s5.actor)); + ("undefined").should.eql(typeof util.getLangVal(s5.verb)); + ("undefined").should.eql(typeof util.getLangVal(s5.object)); + ("undefined").should.eql(typeof util.getLangVal(s5)); + }); + it('should quit if we pass in nothing to it', function () { + ("undefined").should.eql(typeof util.getLangVal()); + }); + it('should get the proper display if given a proper dictionary even a couple levels deep', function () { + (util.getLangVal(stmts['Object-Sub-Statement-with-StatementRef'].object.verb.display)).should.eql(stmts['Object-Sub-Statement-with-StatementRef'].object.verb.display.en); }); }); @@ -217,6 +231,10 @@ describe('testing xAPI utilities', function () { it('should return unknown, if those do not work', function () { ('unknown').should.eql(util.getObjectIdString(s6.object)); }); +//what if we pass this something bogus + it('should return unknown, if you pass it junk', function () { + ("unknown").should.eql(util.getObjectIdString(stmts)); + }); }); describe('test getObjectDisplay', function () {