Skip to content

Instantly share code, notes, and snippets.

@exbotanical
Last active September 15, 2020 06:02
Show Gist options
  • Select an option

  • Save exbotanical/312ba3cb7358fedfdc80a64f8a50b92d to your computer and use it in GitHub Desktop.

Select an option

Save exbotanical/312ba3cb7358fedfdc80a64f8a50b92d to your computer and use it in GitHub Desktop.
code golf cont. - APIs, generators, memoized fns, currying fun, etc
class TestController {
constructor(dataSource) {
this.data = dataSource;
}
dispatch(obj) {
switch(obj.method) {
case "GET":
return this.handleGet(obj.query);
case "DELETE":
return this.handleDelete(obj.query);
case "POST":
return this.handlePost(obj.payload);
case "PUT":
return this.handlePut(obj.payload, obj.query);
}
}
handleGet(query) {
const matchedData = this._fetchItems(query);
return this._assembleResponse(200, matchedData);
}
handleDelete(query) {
if (!query) {
return this._assembleResponse(400);
}
const matchedData = this._fetchItems(query);
if (!matchedData.length) {
return this._assembleResponse(400);
}
for (let item of matchedData) {
this.data.splice(this.data.indexOf(item), 1);
}
return this._assembleResponse(200);
}
handlePost(payload) {
if (!Object.keys(payload).length) {
return this._assembleResponse(400);
}
this.data.push(payload);
return this._assembleResponse(201, [payload]);
}
handlePut(payload, query) {
if (!Object.keys(payload).length || !query) {
return this._assembleResponse(400);
}
const matchedData = this._fetchItems(query);
if (!matchedData.length) {
return this._assembleResponse(400);
}
const excise = this.data.findIndex(i => i.id == payload.id);
this.data.splice(excise, 1, payload);
return this._assembleResponse(202, [payload]);
}
_assembleResponse(status, body = []) {
return {
status,
body
};
}
_fetchItems(query) {
const [field, prop] = this._parseQuery(query);
return this.data.filter(item => item[field] == prop);
}
_parseQuery(query) {
const queryString = query.substring(1);
return [...queryString.split("=")];
}
}
/* Prompt: https://www.codewars.com/kata/5f1e3fba8f51e10010cf446a/train/javascript */
const hexToRGB = hex => ({
r: parseInt(hex.substr(1, 2), 16),
g: parseInt(hex.substr(3, 2), 16),
b: parseInt(hex.substr(5, 2), 16)
});
// code golf version
const hexStringToRGB = hex => ({ r: (x = parseInt(hex.substr(1), 16)) >> 16 & 255, g: x >> 8 & 255, b: x & 255 });
const generateName = () => {
do {
// one of those rare instances during which we need var
var m = generateString(6)(65, 90);
} while (photoManager.nameExists(m));
return m;
}
// curried fn
const generateString = len => {
return function(min, max) {
const result = [];
for (let i = 0; i < len; i++) {
result.push(randomUTF16Char(min, max));
}
return String.fromCharCode(...result);
}
}
// helper fn
const randomUTF16Char = (min, max) => Math.floor(Math.random() * (max - min) + min);
// absurd one-liner
const generateName = (i = 60466175) => (++i).toString(36);
/*
Prompt
You have to create a function for generating random and unique image filenames.
Create a function for generating a random 6 character string which will be used to access the photo URL.
To make sure the name is not already in use, you are given access to an PhotoManager object.
*/
class Event {
constructor() {
this.handlers = new Set();
}
subscribe(fn) {
this.handlers.add(fn);
}
unsubscribe(fn) {
this.handlers.delete(fn);
}
emit(...args) {
this.handlers.forEach(handler => handler(...args));
}
}
const validISBN10 = isbn => {
if (isbn.length < 10) return false;
const m = [...isbn].reduce((acc, cur, idx) => acc + (cur === "X" && idx === 9 ? 10 : cur) * (idx + 1), 0);
if (!m) return false;
return m % 11 === 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment