Skip to content

Instantly share code, notes, and snippets.

@ilio
ilio / min-max-es6.js
Created August 28, 2017 12:46
javascript es6 min | max in array
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
@ilio
ilio / attach-to-docker-logs.sh
Last active August 28, 2017 13:54
How to attach to logs of image hosted in docker
# <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")
@ilio
ilio / es6-shallow-flatten.js
Created September 6, 2017 18:58
javascript es6 shallow array flatten for 2,3...n levels
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"]
const array = [1, 2, 3, 1, 3, 4, 5, 2];
const uniqueArray = [...new Set(array)];
console.log(uniqueArray)
// output: [1, 2, 3, 4, 5]
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}]
sudo su -l
@ilio
ilio / es6-js-snippets.md
Last active September 28, 2017 20:51
js/es6 snippets

es6/js snippets

@ilio
ilio / nodejsScanPorts.js
Last active April 3, 2018 21:24
node.js scan open ports
// 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;
@ilio
ilio / copyRecursive.js
Created June 10, 2018 18:38
node copy files recursive async
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);
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;