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
| 'use strict' | |
| class Attrs extends Array { | |
| constructor(els, name) { | |
| super() | |
| Object.assign(this, {els, name}) | |
| } | |
| get() { | |
| return Object.assign(this, this.els.map(el => el.getAttribute(name))) | |
| } |
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
| function* cycle(list) { | |
| const arr = [...list] | |
| let i = 0 | |
| for (;;) { | |
| i = (i + ((yield arr[i]) || 1)) % arr.length | |
| } | |
| } | |
| const it = cycle(['a', 'b', 'c']) |
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
| if (!Object.values && !Object.entries) { | |
| const values = o => Object.keys(o).map(x => o[x]) | |
| const entries = o => Object.keys(o).map(x => [x, o[x]]) | |
| Object.assign(Object, {values, entries}) | |
| } |
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
| class Deferred extends Promise { | |
| constructor() { | |
| let resolve, reject | |
| super((...args) => [resolve, reject] = args) | |
| Object.assign(this, {resolve, reject}) | |
| } | |
| } | |
| Deferred.tasks = (list, limit = list.length) => { | |
| const promises = list.map(task => { | |
| function exec(iterator) { |
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 flattenDeep = x => Array.isArray(x) ? [].concat(...x.map(flattenDeep)) : x |
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 fizzBuzz = (a, b) => Array.from(Array(b - a), (_, i) =>a + i).map(x => `${!(x % 5) ? 'Fizz' : ''}${!(x % 3) ? 'Buzz' : ''}` || x) |
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
| class SilenceablePromise<T> extends Promise<T> { | |
| silence: () => void | |
| constructor( | |
| executor: ( | |
| resolve: (value?: T | PromiseLike<T>) => void, | |
| reject: (reason?: any) => void, | |
| ) => void, | |
| ) { | |
| let silence: any |
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
| export const autobind = (target: any, propertyKey: string, descriptor: PropertyDescriptor) => { | |
| Object.defineProperty(target, propertyKey, { | |
| ...descriptor, | |
| value: descriptor.value.bind(target), | |
| }) | |
| } |
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 Range = (a, b) => Array.from({ length: b - a }, (_, i) => a + i) | |
| const NumTest = (n, text) => k => !(k % n) ? text : '' | |
| const Tests = data => Object.entries(data).map(([n, text]) => NumTest(n, text)) | |
| const RangeTest = tests => n => tests.map(fn => fn(n)).join('') || n | |
| const RangeMap = (a, b, tests) => Range(a, b).map(RangeTest(tests)) | |
| const fizzBuzz = (a, b) => RangeMap(a, b, Tests({ | |
| 5: 'Fizz', | |
| 3: 'Buzz', | |
| })) |
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
| class LazyArray extends Array { | |
| constructor() { | |
| super() | |
| this[Array.species] = new.target | |
| } | |
| *lazyMap(fn) { | |
| for (let i = 0; i < this.length; i++) { | |
| yield fn(this[i]) | |
| } |