Skip to content

Commit

Permalink
made getLAngVal more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
cr8onski committed Jan 28, 2016
1 parent f81954b commit 7e0483d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/xapi-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
20 changes: 19 additions & 1 deletion test/tests/test.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 7e0483d

Please sign in to comment.