Skip to content

Instantly share code, notes, and snippets.

@brianswisher
Last active January 4, 2016 04:02
Show Gist options
  • Select an option

  • Save brianswisher/42dfbbf9a8631841380b to your computer and use it in GitHub Desktop.

Select an option

Save brianswisher/42dfbbf9a8631841380b to your computer and use it in GitHub Desktop.
Common Characters
((libs) => {
var promises = [];
libs.forEach((src) => {
promises.push(new Promise(resolve => {
if (!document.querySelector(`[src="${src}"]`)) {
const script = document.createElement("script");
script.src = src;
script.async = false;
script.onload = () => {
resolve();
};
document.head.appendChild(script);
} else {
resolve();
}
}));
});
function commonChars(a, b) {
var input = (function(){
var input1 = a.split(""),
input2 = b.split(""),
scan = {},
filter = {};
return {
scan: function(){
input1.forEach(function(c){
scan[c] = c;
});
return this
},
filter: function(){
input2.forEach(function(c){
if(scan[c]) filter[c] = c;
});
return this
},
get: function(){
return Object.keys(filter)
}
}
})(a, b);
return input.
scan().
filter().
get()
}
return Promise.all(promises)
.then(() => {
expect(commonChars("a", "b").join("")).toEqual("");
expect(commonChars("a", "ba").join("")).toEqual("a");
expect(commonChars("a", "baa").join("")).toEqual("a");
expect(commonChars("ca", "bcaa").join("")).toEqual("ca");
expect(commonChars("Hello there, How are you?", "I'm good, thanks.").join("")).toEqual(" o,tha");
console.log("All tests passed.");
});
})([
"https://wzrd.in/standalone/expect@latest"
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment