Last active
December 26, 2017 08:57
-
-
Save hanmd82/bba8dc3c7d91e2ba782833ca507794f6 to your computer and use it in GitHub Desktop.
ES6 katas for Generators - http://es6katas.org/
This file contains 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
// 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()); | |
}); | |
it('as a function expression, by adding a `*` after `function`', function() { | |
let g = function* () {}; | |
assertIsGenerator(g()); | |
}); | |
it('inside an object by prefixing the function name with `*`', function() { | |
let obj = { | |
* g() {} | |
}; | |
assertIsGenerator(obj.g()); | |
}); | |
it('computed generator names, are just prefixed with a `*`', function() { | |
const generatorName = 'g'; | |
let obj = { | |
* [generatorName]() {} | |
}; | |
assertIsGenerator(obj.g()); | |
}); | |
it('inside a class the same way', function() { | |
const generatorName = 'g'; | |
class Klazz { | |
* [generatorName]() {} | |
} | |
assertIsGenerator(new Klazz().g()); | |
}); | |
function assertIsGenerator(gen) { | |
const toStringed = '' + gen; | |
assert.equal(toStringed, '[object Generator]'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment