Last active
July 7, 2017 06:19
-
-
Save nhusher/a05f522c9142de13168b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Takes a string and performs the count operation: | |
function counts(s) { | |
var reg = /([\u00BF-\u1FFF\u2C00-\uD7FF\w\d][^,]+),\s*(\d+)/; | |
// Reduce over each line in the string, modifying an accumulator object at every iteration | |
return s.split("\n").reduce(function (a, c) { | |
if (c = reg.exec(c)) { // The regex matches, so it's of the form NAME,NUM, at least in part | |
// c[0] = full regex string | |
// c[1] = the first match (the name) | |
// c[2] = the second match (the number) | |
var name = c[1].trim().toLowerCase(), // normalize the name | |
number = +c[2]; // fast-cast the number, which will always match \d+ | |
a[name] = a[name] ? a[name] + number : number; | |
} | |
return a; | |
}, {}); | |
} | |
var test = [ | |
"George Bluth,10", // spaces in name, no whitespace between comma, capitalization | |
" george bluth ,5", // test trimming of names | |
"14 Jaguar, 12", // test names with numbers (Mayan), space between comma and number | |
"Georges Suerat, ok", // test invalid numbers | |
"Georges Suerat, 1.25", // test decimal numbers | |
"ånné, 0x5c", // test non-english names, hexidecimal numbers | |
].join('\n'); | |
var result = counts(test); | |
console.log(result); | |
console.log(15 === result["george bluth"]); | |
console.log(12 === result["14 jaguar"]); | |
console.log(1 === result["georges seurat"]); | |
console.log(0 === result["ånné"]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var navigationItem = { | |
url:"", | |
label:"", | |
children:[ | |
{ | |
url:"http://www.something.com/", | |
label:"Something", | |
children:[] | |
}, | |
{ | |
url:"http://somethingelse.net", | |
label:"Something Else", | |
children: [ | |
{ | |
url:"http://yetanother.org", | |
label:"Another One", | |
children:[] | |
} | |
/* maybe more of these? */ | |
] | |
} | |
] | |
}; | |
// domRenderer :: ([DOMElement], String, String) -> DOMElement | |
// domRenderer :: [DOMElement] -> DOMElement | |
function domRenderer(renderedChildren, url, label) { | |
var childList = document.createElement('ol'); | |
renderedChildren.forEach(childList.appendChild, childList); | |
if(arguments.length > 1) { | |
var menuNode = document.createElement('li'), | |
link = document.createElement('a'); | |
link.href = url; | |
link.innerHTML = label; | |
menuNode.appendChild(link); | |
menuNode.appendChild(childList); | |
return menuNode; | |
} else { | |
return childList; | |
} | |
} | |
// stringRenderer :: ([String], String, String) => String | |
// stringRenderer :: [String] -> String | |
function stringRenderer(renderedChildren, url, label) { | |
var ret = '', | |
childList = '<ol>' + renderedChildren.join('') + '</ol>'; | |
if(arguments.length > 1) { | |
ret += '<li>'; | |
ret += '<a href="' + url + '">' + label + '</a>'; | |
ret += childList; | |
ret += '</li>'; | |
} else { | |
ret = childList; | |
} | |
return ret; | |
} | |
function menuChildLocator(item) { return item.children; } | |
function treeRenderer(nodeList, renderer, childLocator) { | |
return renderer(nodeList.map(function(item) { | |
return renderer(treeRenderer(childLocator(item), renderer), item.url, item.label); | |
})); | |
} | |
document.body.appendChild(treeRenderer(navigationItem.children, domRenderer, menuChildLocator)); | |
// or: | |
document.body.innerHTML = treeRenderer(navigationItem.children, stringRenderer, menuChildLocator); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function palindrome(s) { | |
// Special case: empty strings are not palindromes | |
if(!s) { return false; } | |
// sanitize string by translating to uppercase and checking only the first line | |
// poems that read backwards and forwards are cute, but outside the scope of this fn | |
s = s.split('\n').pop().replace(/[^\u00BF-\u1FFF\u2C00-\uD7FF\w\d]/g, "").toUpperCase(); | |
// compare the first half to the second half | |
return s.slice(0, s.length / 2).split("").reverse().join("") === s.slice(Math.ceil(s.length / 2)); | |
} | |
console.log(false === palindrome("")); | |
console.log(true === palindrome("racecar")); | |
console.log(true === palindrome("Racecar")); | |
console.log(true === palindrome("race,car")); | |
console.log(true === palindrome(" race, car")); | |
console.log(true === palindrome("anna")); | |
console.log(true === palindrome("ånnå")); | |
console.log(false === palindrome("ånna")); | |
console.log(false === palindrome("ånné")); | |
console.log(true === palindrome("Ah, Satan sees Natasha!")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment