Skip to content

Instantly share code, notes, and snippets.

View hanmd82's full-sized avatar

Mingding Han hanmd82

View GitHub Profile
@hanmd82
hanmd82 / es6-promises.js
Last active March 18, 2022 22:15
ES6 katas for Promises - http://es6katas.org/
// 75: Promise - basics
describe('a Promise represents an operation that hasn`t completed yet, but is expected in the future', function() {
it('`Promise` is a global function', function() {
const expectedType = 'function';
assert.equal(typeof Promise, expectedType);
});
describe('the constructor', function() {
@hanmd82
hanmd82 / es6-arrow-functions.js
Last active December 26, 2017 07:52
ES6 katas for Arrow functions - http://es6katas.org/
// 5: arrow functions - basics
describe('arrow functions', function() {
it('are shorter to write', function() {
var func = () => {
return 'I am func';
};
assert.equal(func(), 'I am func');
});
@hanmd82
hanmd82 / es6-template-strings.js
Last active December 26, 2017 07:54
ES6 katas for Template strings - http://es6katas.org/
// 1: template strings - basics
describe('a template string, is wrapped in ` (backticks) instead of \' or "', function() {
describe('by default, behaves like a normal string', function() {
it('just surrounded by backticks', function() {
var str = `like a string`;
assert.equal(str, 'like a string');
});
@hanmd82
hanmd82 / es6-object-literals.js
Last active June 29, 2019 04:16
ES6 katas for Object literals - http://es6katas.org/
// 9: object-literals - basics
describe('The object literal allows for new shorthands', () => {
const x = 1;
const y = 2;
describe('with variables', () => {
it('the short version for `{x: x}` is {x}', () => {
const short = {y};
@hanmd82
hanmd82 / es6-block-scope.js
Last active December 26, 2017 07:57
ES6 katas for Block scope - http://es6katas.org/
// 7: block scope - let
describe('`let` restricts the scope of the variable to the current block', () => {
describe('`let` vs. `var`', () => {
it('`var` works as usual', () => {
if (true) {
var varX = true;
}
@hanmd82
hanmd82 / es6-destructuring.js
Last active December 26, 2017 08:19
ES6 katas for Destructuring - http://es6katas.org/
// 10: destructuring - array
describe('destructuring arrays makes shorter code', () => {
it('extract value from array, e.g. extract 0 into x like so `let [x] = [0];`', () => {
let [firstValue] = [1];
assert.strictEqual(firstValue, 1);
});
it('swap two variables, in one operation', () => {
@hanmd82
hanmd82 / es6-generators.js
Last active December 26, 2017 08:57
ES6 katas for Generators - http://es6katas.org/
// https://davidwalsh.name/es6-generators
// 49: Generator - creation
describe('generator can be created in multiple ways', function() {
it('the most common way is by adding `*` after `function`', function() {
function* g() {}
assertIsGenerator(g());
});
@hanmd82
hanmd82 / es6-rest-spread-operators.js
Last active December 27, 2017 06:28
ES6 katas for Rest and Spread operators - http://es6katas.org/
// 18: rest - as-parameter
describe('rest in function params', () => {
it('must be the last parameter', () => {
const fn = (...rest) => {
assert.deepEqual([1, 2], rest);
};
fn(1, 2);
});
@hanmd82
hanmd82 / es6-default-params.js
Created December 27, 2017 06:46
ES6 katas for default parameters - http://es6katas.org/
// 57: Default parameters - basics
describe('default parameters make function parameters more flexible', () => {
it('define it using an assignment to the parameter `function(param=1){}`', function() {
let number = (int = 0) => int;
assert.equal(number(), 0);
});
@hanmd82
hanmd82 / es6-classes.js
Last active January 2, 2018 14:48
ES6 katas for Classes - http://es6katas.org/
// 22: class - creation
describe('class creation', () => {
it('is as simple as `class XXX {}`', function() {
class TestClass {};
const instance = new TestClass();
assert.equal(typeof instance, 'object');
});