Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. // Generator utilities
  2.  
  3.  
  4. const Generator = Object.getPrototypeOf(function*() {})
  5.  
  6.  
  7. Generator.range = function*(start, end, step=1) {
  8. let number = start
  9.  
  10. function stop() {
  11. return step >= 0 ? number >= end : number <=end
  12. }
  13.  
  14. while (!stop()) {
  15. yield number
  16. number += step
  17. }
  18. }
  19.  
  20.  
  21. Generator.prototype.forEach = function(callback) {
  22. let index = 0
  23. for (const item of this) {
  24. callback(item, index++, this)
  25. }
  26. }
  27.  
  28.  
  29. Generator.prototype.until = function*(condition) {
  30. for (const item of this) {
  31. if (!condition(item)) {
  32. yield item
  33. } else {
  34. return
  35. }
  36. }
  37. }
  38.  
  39.  
  40. Generator.prototype.exhaust = function(n) {
  41. for (let i = 0; i < n; i++) {
  42. this.next()
  43. }
  44. }
  45.  
  46.  
  47. Generator.prototype.dropFirst = function*(n) {
  48. this.exhaust(n)
  49. yield* this
  50. }
  51.  
  52.  
  53. module.exports = Generator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement