Skip to content

Instantly share code, notes, and snippets.

View harish2704's full-sized avatar

Harish Karumuthil harish2704

View GitHub Profile
@harish2704
harish2704 / flattern-object.js
Created January 18, 2017 05:59
Flatten a nested json into a plain json
var handlers = {
Object: flattenObject,
Array: flattenArray,
};
function router(val, store, prefix, i ) {
var kind, handler, key = prefix? prefix + '.'+ i : i; ;
if( val === undefined || val === null ){
return store[ key ] = val;
}
@harish2704
harish2704 / grub.cfg
Created May 1, 2017 05:41
Boot ubuntu iso file
set menu_color_normal=white/black
set menu_color_highlight=black/light-gray
# File name of ISO file
set ubu_path=/ubuntu-16.04-desktop-amd64.iso
menuentry "Try Ubuntu without installing Failsafe" {
# Search for iso file in all paritions and set the partition as root
@harish2704
harish2704 / generate-certificate-openssl.sh
Created June 16, 2017 09:40
Generate Root CA and self-singed certificate using openssl
#!/usr/bin/env bash
DomainName="yourdomain.example.com";
echo "Generating Root CA key"
[[ -f rootCA.key ]] || openssl genrsa -des3 -out rootCA.key 2048
echo "Generating Root CA certificate"
[[ -f rootCA.pem ]] || openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.pem
@harish2704
harish2704 / multiline-to-js.js
Last active June 20, 2017 06:08
multiline-to-js.js
const getStdin = require('get-stdin');
/* Usage */
/*
>> cat file.html | node multiline-to-js.js
*/
/* Input */
/*
<div class="tab-pane active" >
<div class="card-box">
@harish2704
harish2704 / catch-if.js
Created June 22, 2017 06:52
catchIf syntax sugar for ES6 Promise. can be applied to any Promise library
/*
Adds a catchIf method to Promise.
Usage
Promise.reject( new TypeError() )
.catchIf( TypeError, function handler(err){ console.log('TypeError handled')} )
.catchIf( RangeError, function handler(err){ console.log('RangeError handled')} )
*/
function addCatchIf ( promiseClass ){
promiseClass.prototype.catchIf = function(){
@harish2704
harish2704 / DataBuffer.js
Created July 7, 2017 20:59
An in-memory data buffer class for Sequelize models. Can be used for buffering findOrCreate / create / upsert queries
/* ഓം ബ്രഹ്മാർപ്പണം. */
/*
* DataBuffer.js
* Created: Fri Jul 07 2017 23:14:58 GMT+0530 (IST)
* Copyright 2017 Harish.K<harish2704@gmail.com>
*/
/**
* DataBuffer
@harish2704
harish2704 / sequelize-forEach-batch.js
Created July 7, 2017 21:09
Do any task using all the items in a Sequlize model. Just write the task alone. ItemMaper will take care about batch execution and limit
class ItemMaper{
constructor( { model, batchSize, limit }, sequelizeOpts ){
this.model = model;
this.batchSize = batchSize || 10;
this.limit = limit || Infinity;
this.sequelizeOpts = sequelizeOpts || {};
}
async forEachBatch( fn ){
let offset = -this.batchSize,
@harish2704
harish2704 / repl-log-promise.js
Created July 19, 2017 15:19
Log promise in Node js repl. This will add a command ".log" which can be used like ".log Promise.resolve(123)" Raw
function logSuccess( a, b, c ){ console.log('Success'); global.uu = arguments; global.aa = a; global.bb = b; global.cc = c; }
function logError( e ){ console.log( 'Error'); global.ee = e; }
function log( promise ){ promise.then(logSuccess).catch(logError); }
function doLog( task ){ var cmd =`log(${task})`; this.outputStream.write(cmd); this.context.vm.runInThisContext(cmd); this.displayPrompt();}
repl.repl.defineCommand('log', doLog);
@harish2704
harish2704 / reflection-middleware.js
Created August 7, 2017 07:09
Simple Nodejs-Express reflection API
module.exports = function( app ){
app.post('/__reflection', function( req, res, next ){
const ip = req.headers[ 'x-forwarded-for' ] || req.connection.remoteAddress;
console.log( 'IP', ip );
if( ip !== '::ffff:127.0.0.1' ){
return next();
}
let fn;
try {
eval( 'fn = ' + req.body.code );
@harish2704
harish2704 / dummy-express-server.js
Created August 7, 2017 11:06
Dummy express server for logging in comming request
var express = require('express');
var app = express();
var port = process.env.PORT || 4005;
app.use( function( req, res ){
[
'method',
'url',
'headers',
'query',
'body'