Created
June 27, 2021 14:05
-
-
Save koshuang/825b7e3d480c999a2cd99ac636ae4be4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const _ = require('lodash'); | |
const log = { | |
describe(str) { | |
console.log(`describe: ${str}`); | |
}, | |
it(str) { | |
console.log(`\t it ${str}`); | |
}, | |
assertEquals(str) { | |
console.log(`\t\tResult: ${str}`); | |
}, | |
error(str) { | |
console.log(`\t\tResult: ${str}`); | |
}, | |
}; | |
test(); | |
function test() { | |
describe('add', function() { | |
it('should return a empty object when input is undefined', function() { | |
function afterAdded(sum) { | |
console.log('Sum:', sum); | |
} | |
const actual = add(1, 2, afterAdded); | |
}); | |
}); | |
} | |
const x = { | |
val: 2, | |
}; | |
const f = x => Object.assign({}, x, { val: x.val + 1 }); | |
const g = x => Object.assign({}, x, { val: x.val * 2 }); | |
console.log(f(g(x))); | |
function add(a, b) { | |
console.log(new Date()); | |
return a + b; | |
} | |
function describe(description, fn) { | |
log.describe(description); | |
fn(); | |
} | |
function it(sentence, fn) { | |
log.it(sentence); | |
fn(); | |
} | |
// curry function | |
function addNew(a) { | |
return function(b) { | |
return a + b; | |
}; | |
} | |
// curry function | |
function multiple(a) { | |
return function(b) { | |
return a * b; | |
}; | |
} | |
const inc = addNew(1); | |
const double = multiple(2); | |
console.log(inc(1)); | |
console.log(double(3)); | |
const logF = console.log.bind(); | |
// setTimeout(function() { | |
// console.log('Hi'); | |
// }, 3000); | |
// function hoisting | |
// event loop | |
// apply(), call(), bind() | |
function generateTimeout(second) { | |
// Closure | |
return function(fn) { | |
setTimeout(fn, second * 1000); | |
}; | |
} | |
// const timeout3s = generateTimeout(3); | |
/* | |
timeout3s = function(fn) { | |
setTimeout(function() { | |
logF('Hi'); | |
}, 3 * 1000); | |
} | |
*/ | |
// timeout3s(function() { | |
// logF('Hi'); | |
// }); | |
const menuItems = [ | |
{ name: 'Home', path: '/home' }, | |
{ name: 'Dashboard', path: '/dashboard' }, | |
]; | |
// url=> https://localhost:3000/dashboard?xxx=1 | |
function subIsActive(menuItem) { | |
const menuItems = Array.isArray(menuItem) ? menuItem : [menuItem]; | |
return menuItems.some(menuItem => { | |
return '/dashboard?xxx=1'.indexOf(menuItem.path) === 0; | |
// current path starts with this path string | |
}); | |
} | |
console.log(subIsActive(menuItems[1])); | |
function assertEquals(expected, actual) { | |
if (_.isEqual(expected, actual)) { | |
log.assertEquals('Passed'); | |
} else { | |
log.error(`${JSON.stringify(actual)} is not expected as ${JSON.stringify(expected)}`); | |
} | |
} | |
function assertError(fn, message = '') { | |
try { | |
fn(); | |
log.error('Expected an error'); | |
} catch (error) { | |
log.assertEquals('Passed'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment