es6/js snippets
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 arr = [1,2,3] | |
| const min = Math.min(...arr); | |
| const max = Math.max(...arr); | |
| console.log('min:', min, 'max:', max); // output => min: 1 max: 3 |
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
| # <IMAGE_NAME> - target image name in docker | |
| # docker logs -f $(docker ps -q --filter="ancestor=<IMAGE_NAME>") | |
| docker logs -f $(docker ps -q --filter="ancestor=mongo:3.3") |
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 deep2array = [ | |
| ['abc1', 'bcd1', 'def1'], | |
| ['abc2', 'bcd2', 'def2'], | |
| ['abc3', 'bcd3', 'def3'] | |
| ]; | |
| const shallowFlatten = (p, c) => [...p, ...c]; | |
| console.log(deep2array.reduce(shallowFlatten, [])) | |
| /* output: | |
| ["abc1", "bcd1", "def1", "abc2", "bcd2", "def2", "abc3", "bcd3", "def3"] |
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 array = [1, 2, 3, 1, 3, 4, 5, 2]; | |
| const uniqueArray = [...new Set(array)]; | |
| console.log(uniqueArray) | |
| // output: [1, 2, 3, 4, 5] |
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 array = [{id:1}, {id:2}, {id:3}, {id:4}, {id:5}, {id:2}, {id:6}, {id:4}, {id:7}]; | |
| function unique(array, propertyName) { | |
| return array.filter((e, i) => array.findIndex(a => a[propertyName] === e[propertyName]) === i); | |
| } | |
| console.log(unique(array, 'id')) | |
| /* | |
| output: | |
| [{"id":1},{"id":2},{"id":3},{"id":4},{"id":5},{"id":6},{"id":7}] |
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
| sudo su -l |
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
| // based on https://github.com/baalexander/node-portscanner/blob/master/lib/portscanner.js | |
| const {Socket} = require('net'); | |
| function connect(port, host, timeout, callback) { | |
| let connectionRefused = false; | |
| const socket = new Socket(); | |
| let status = null; | |
| let error = null; |
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
| awaitable = (func, ...args) => { | |
| return new Promise(resolve => { | |
| func.apply(this, [...args, (...a) => { | |
| if(a.length === 1){ | |
| return resolve(a[0]); | |
| } | |
| if(a.length === 2){ | |
| return resolve(a[1]); | |
| } | |
| return resolve(a); |
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 runNpmInstall() { | |
| return new Promise((resolve, reject) => { | |
| let cmd = 'npm'; | |
| if (os.platform() === 'win32') { | |
| cmd += '.cmd'; | |
| } | |
| npmProcess = spawn(cmd, ['install'], {cwd: path.join(__dirname, 'service_')}); | |
| npmProcess.on('error', e => { | |
| npmProcess = null; |
OlderNewer