Created
August 28, 2018 23:19
-
-
Save sebinsua/67416ae6f7ca4051d3a7dc924f83d806 to your computer and use it in GitHub Desktop.
window-generator.js
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
| #!/usr/bin/env node | |
| function window(values = [], step = 1, size = 1) { | |
| function* _window() { | |
| for (let i = 0; i < values.length; i += step) { | |
| const items = values.slice(i, i + size); | |
| yield items; | |
| } | |
| } | |
| const generator = _window(); | |
| generator.step = _step => { | |
| step = _step; | |
| return generator; | |
| }; | |
| generator.size = _size => { | |
| size = _size; | |
| return generator; | |
| }; | |
| return generator; | |
| } | |
| function debug(properties = [], name) { | |
| const numberOfProperties = properties.length; | |
| const lastIndex = numberOfProperties - 1; | |
| return function debugWithProperties(values = []) { | |
| let output = `${name}: `; | |
| for (let i = 0; i < values.length; i++) { | |
| const property = properties[i % numberOfProperties]; | |
| output += `${property}=${values[i]}`; | |
| const isAtEnd = i + 1 === values.length; | |
| if (i % numberOfProperties === lastIndex && !isAtEnd) { | |
| output += '\n'; | |
| } else { | |
| output += ' '; | |
| } | |
| } | |
| console.log(output); | |
| } | |
| } | |
| const rgb = debug(['r', 'g', 'b'], 'pixel'); | |
| const pixels = new Uint8Array([ | |
| 123, 123, 123, 0.8, | |
| 255, 255, 255, 1.0, | |
| 100, 100, 100, 0.5 | |
| ]); | |
| for (const value of window(pixels).step(4).size(3)) { | |
| console.log('value:', value); | |
| rgb(value); | |
| } | |
| // Array.from( | |
| // window(pixels).step(4).size(3) | |
| // ).map( | |
| // ([ r, g, b]) => console.log(r, g, b) | |
| // ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment