Snippets for assignments in Eloquent JS book
function arrayToList(array) {
array = array.reverse()
let list = null
for (let i = 0; i < array.length; i++) list = { value: array[i], rest: list }
return list
}
function listToArray(list) {
let array = []
for (let node = list; node; node = node.rest) array.push(node.value)
return array
}
function prepend(value, list) {
return { value, rest: list }
}
function nth(list, n) {
if (!list) return undefined
else if (n === 0) return list.value
else return nth(list.rest, n - 1)
}
function deepEqual(a, b) {
if (a === b) return true
if (a !== null && b !== null) {
if (typeof a === 'object' && typeof b === 'object') {
let aKeys = Object.keys(a)
let bKeys = Object.keys(b)
if (aKeys.length === bKeys.length) {
for (let i = 0; i < aKeys.length; i++) {
if (aKeys[i] !== bKeys[i]) return false
if (!deepEqual(a[aKeys[i]], b[bKeys[i]])) return false
}
return true
} else return false
} else return false
} else return false
}
/* concise version */
function deepEqual(a, b) {
if (a === b) return true
if (a === null || typeof a !== object ||
b === null || typeof b !== object) return false
let aKeys = Object.keys(a), bKeys = Object.keys(b)
if (aKeys.length !== bKeys.length) return false
for (let key of aKeys) {
if (!bKeys.includes(key) || !deepEqual(a[key], b[key])) return false
}
return true
}
/* [^] (any character that is not in the empty set of characters) */
function stripComments(code) {
return code.replace(/\/\/.*|\/\*[^]*?\*\//g, "");
}
console.log(stripComments("1 + /* 2 */3"));
// → 1 + 3
console.log(stripComments("x = 10;// ten!"));
// → x = 10;
console.log(stripComments("1 /* a */+/* b */ 1"));
// → 1 + 1
function parseINI(string) {
// Start with an object to hold the top-level fields
let result = {};
let section = result;
string.split(/\r?\n/).forEach(line => {
let match;
if (match = line.match(/^(\w+)=(.*)$/)) {
section[match[1]] = match[2];
} else if (match = line.match(/^\[(.*)\]$/)) {
section = result[match[1]] = {};
} else if (!/^\s*(;.*)?$/.test(line)) {
throw new Error("Line '" + line + "' is not valid.");
}
});
return result;
}
console.log(parseINI(`
name=Vasilis
[address]
city=Tessaloniki`));
// → {name: "Vasilis", address: {city: "Tessaloniki"}}
function once(fn){
let returnValue;
let canRun = true;
return function runOnce(){
if(canRun) {
returnValue = fn.apply(this, arguments);
canRun = false;
}
return returnValue;
}
}
var processonce = once(process);
processonce(); //process
processonce(); //
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Method', 'POST,GET,OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
});
function createResolver(resolver) {
const baseResolver = resolver;
baseResolver.createResolver = childResolver => {
async function newResolver(parent, args, context) {
await resolver(parent, args, context);
return childResolver(parent, args, context);
}
return createResolver(newResolver);
};
return baseResolver;
}
export const requiresAuth = createResolver((parent, args, context) => {
if (!context.user || !context.user.id) {
throw new Error('Not authenticated');
}
});
export const requiresAdmin = requiresAuth.createResolver(
(parent, args, context) => {
if (!context.user.isAdmin) {
throw new Error('Requires admin access');
}
}
);