satan's notebooks

  • Untitled - /satan/paulv
    Last edited 7 years ago
    const fs = require('fs'); const keypad = [ [1,2,3], [4,5,6], [7,8,9] ]; let posX = 1; let posY = 1; /* const keypad = [ [ , ,1, , ], [ ,2,3,4, ], [5,6,7,8,9], [ ,'A','B','C', ], [ , ,'D', , ] ]; let posX = 0; let posY = 2; */ const testCase = `ULL RRDDD LURDL UUUUD`; function move(direction) { switch (direction) { case 'U': if (keypad[posY-1] && keypad[posY-1][posX]) { posY -= 1; } break; case 'D': if (keypad[posY+1] && keypad[posY+1][posX]) { posY += 1; } break; case 'L': if (keypad[posY][posX-1]) { posX -= 1; } break; case 'R': if (keypad[posY][posX+1]) { posX += 1; } break; } } function getNumber(line) { line.split('').filter(move); return keypad[posY][posX]; } function getCode(input) { return input.trim().split('\n').map(getNumber).join(''); } console.log(getCode(testCase)); // fs.readFile('input.txt', 'utf8', function (err, contents) { // console.log(getCode(contents)); // });
  • Arrow function on prototypes - /satan/arrow-function-on-prototypes
    Last edited 7 years ago
    function Example(id){ this.id = id; } Example.prototype.idFunction = function(m) { console.log(`idFunction: ${this.id}`); } Example.prototype.idArrowFunction = () => { console.log(`idArrowFunction: ${this.id}`); } var anotherArrowFunction = () => { console.log(`anotherArrowFunction: ${this.id}`); } Example.prototype.anotherArrowFunction = anotherArrowFunction; var m1 = new Example('my id'); m1.idFunction(); m1.idArrowFunction(); m1.anotherArrowFunction(); class AnotherExample { constructor(id) { this.id = id; } idFunction () { console.log(`idFunction: ${this.id}`); } } const m2 = new AnotherExample('my other id'); m2.idFunction();
  • gulp stream end example - /satan/gulp-end-stream
    Last edited 7 years ago
    var gulp = require('gulp'); var wait = require('gulp-wait'); var stream = gulp.src('.'); stream .pipe(wait(1000)) .on('end', function(){ console.log('stream has ended'); }); console.log('stream is active'); stream.end();