Skip to content

Commit

Permalink
added utils.makePronounDict().
Browse files Browse the repository at this point in the history
  • Loading branch information
toolness committed Dec 26, 2011
1 parent 416aee1 commit 64b9bac
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
37 changes: 37 additions & 0 deletions javascript/grammar.js
@@ -1,5 +1,42 @@
var Grammar = (function() {
var PRONOUNS = {
male: {
himself: "himself",
his: "his",
him: "him",
he: "he"
},
female: {
himself: "herself",
his: "her",
him: "her",
he: "she"
},
neuter: {
himself: "itself",
his: "its",
him: "it",
he: "it"
},
you: {
himself: "yourself",
his: "your",
him: "you",
he: "you"
}
};

var utils = {
makePronounDict: function(gender, prefix) {
if (!prefix)
prefix = "";
if (!(gender in PRONOUNS))
throw new Error("invalid gender: " + gender);
var dict = {};
for (var name in PRONOUNS[gender])
dict[prefix + name] = PRONOUNS[gender][name];
return dict;
},
applyDict: function(string, dict) {
return string.replace(/%\(([a-zA-Z0-9_]+)\)s/g, function(str, term) {
return dict[term].toString();
Expand Down
16 changes: 16 additions & 0 deletions javascript/test/index.html
Expand Up @@ -13,6 +13,22 @@ <h2 id="qunit-userAgent"></h2>
<script>
module("grammar");

test("utils.makePronounDict()", function() {
var makePronounDict = Grammar.utils.makePronounDict;
deepEqual(makePronounDict("neuter"), {
"himself": "itself",
"his": "its",
"him": "it",
"he": "it"
});
deepEqual(makePronounDict("neuter", "u_"), {
"u_himself": "itself",
"u_his": "its",
"u_him": "it",
"u_he": "it"
});
});

test("utils.applyDict()", function() {
var applyDict = Grammar.utils.applyDict;
equal(applyDict("%(hi)s dog", {hi: "cat"}), "cat dog");
Expand Down

0 comments on commit 64b9bac

Please sign in to comment.