Skip to content

Instantly share code, notes, and snippets.

View drodsou's full-sized avatar

David Rodriguez Souto drodsou

View GitHub Profile
@drodsou
drodsou / ubuntu12server_mate.md
Last active July 30, 2016 08:58 — forked from Frandy/ubuntu12server_mate.textile
[linux, procedure] From ubuntu server to mate desktop
@drodsou
drodsou / mono_fix.java
Last active July 30, 2016 08:57
[algorithm] Reading audio buffer
int divider = 1;
if (output.getSampleFrequency() < 44100) divider *= 2;
if (output.getChannelCount() == 1) divider *= 2;
short[] pcm = output.getBuffer();
for (int i=0; i < pcm.length/divider; i+=1) {
outStream.write(pcm[i] & 0xff);
outStream.write((pcm[i] >> 8 ) & 0xff);
@drodsou
drodsou / groupusers.sh
Last active July 30, 2016 08:45
[bash] List users of a group in linux (as primary and as secondary)
#!/bin/bash
GID=`cat /etc/group|grep $1| awk 'BEGIN { FS = ":" } ; { print $3 }'`
RE=".*:.*:.*:${GID}"
echo "As PRIMARY"
echo "----------"
cat /etc/passwd|grep $RE | awk 'BEGIN { FS = ":" } ; { print $1 }'| sort -f | awk '{print}' ORS=' '
printf "\n\nAs SECONDARY\n"
echo "-------------"
cat /etc/group|grep $1 | awk 'BEGIN { FS = ":" } ; { print $4 }' |sed 's/,/\n/g' | grep '[^\$]$' | sort -f | awk '{print}' ORS=' '
@drodsou
drodsou / mergeDeep.js
Last active July 30, 2016 09:03
Like Object.assign but recursive and cloning arrays, not linking them
// ---------------------------------------------------------------------
// javascript
// Like Object.assign but recursive and cloning arrays, not linking them
// ---------------------------------------------------------------------
function mergeDeep(/* obj1, obj2, ...*/) {
const isObject = (o)=>o && o !== undefined && o !== null && typeof o === 'object' && !Array.isArray(o)
let output = {}
Object.keys(arguments).forEach( a=>{
let o = arguments[a]
@drodsou
drodsou / writeFileSyncRecursive.js
Last active June 5, 2024 15:57
Like writeFileSync but creating all folder paths if not exist
// -- updated in 2020/04/19 covering the issues in the comments to this point
// -- remember you also have things like `ensureDirSync` from https://github.com/jprichardson/node-fs-extra/blob/master/docs/ensureDir-sync.md
const fs = require('fs')
function writeFileSyncRecursive(filename, content, charset) {
// -- normalize path separator to '/' instead of path.sep,
// -- as / works in node for Windows as well, and mixed \\ and / can appear in the path
let filepath = filename.replace(/\\/g,'/');
// -- preparation to allow absolute paths as well
@drodsou
drodsou / readdirSyncRecursive.js
Last active May 1, 2020 18:14
readdirSync but recursive - also known as walkDirSync (concise glob alternative)
const fs = require('fs');
const dir = (d) => fs.readdirSync(d, {withFileTypes:true})
.map(f=>f.isDirectory() ? dir(d + '/' + f.name) : d + '/' + f.name)
.flat();
// console.log(dir('somedir'));
// can use .filter() afterwards, of course
@drodsou
drodsou / httpServeFolder.js
Created July 30, 2016 09:00
Like http-server but just one function with no dependencies
// -------------------------------------
function httpServeFolder(baseDir, port) {
http.createServer(function (request, response) {
var filePath = baseDir + request.url.split('?')[0];
if (!path.extname(filePath)) filePath = filePath + (filePath[filePath.length-1]=='/' ? '' : '/') + 'index.html';
var extname = path.extname(filePath)
var exttype = {'.html':'text/html', '.js':'text/javascript', '.css' :'text/css', '.json':'application/json', '.png':'image/png', '.jpg':'image/jpg', '.ico':'image/x-icon'}
var contentType = (exttype[extname]) ? (exttype[extname]) : 'text/plain';
@drodsou
drodsou / watchFileOrFolder.js
Last active August 5, 2016 22:27
Like fs.watch but with a timeout to not trigger n times each change
// tames fs.watch to not trigger n times each
// fn (event, filename)
// --------------------------------
function watchFileOrFolder(fileOrFolder, timeout, fn) {
let paused=false
fs.watch(fileOrFolder, {persistent: true, recursive : true}, function (event, fileName) {
if (!paused) {
fn(event,fileName)
paused=true
setTimeout(()=>paused=false,timeout)
@drodsou
drodsou / responsiveMenu.css
Last active August 17, 2016 19:53
CSS-only menu, responsive and accesible
<!--
css only responsive menu (3 or more levels)
uses flexbox, tested in chrome, firefox, IE11, Safari 7+
Key tips:
- position: nav:relative, ul:absolute
- elements: <li><p> to define any element, link or not, not only <li>. The <p> is what gets borders and colors
- mobile button: checkbox on/off
other dropdown may be check boxs as well, as plain hover can cause trouble
-->
@drodsou
drodsou / createAsyncFn.js
Last active March 30, 2017 09:36
Generator deasync pattern
/**
* returns a function that runs an async generator (one which yields async functions), usings auto yield/next thanks to an 'args.next'
* The created function expects as parameter an optional object with a {cb:callbackFn} property plus aditional custom arguments
* Can also make a onliner gen runner with it: function runAsyncGen(gen, args) { newAsyncFn(gen)(args) }
*/
function createAsyncFn (gen) {
return function(args={}, cb) {
//var cb = (typeof args === 'function') ? args : args.cb
it = gen(args) // create iterator from generator, and pass a common object to communicate