Skip to content

Instantly share code, notes, and snippets.

View jmjuanes's full-sized avatar

Josemi Juanes jmjuanes

View GitHub Profile
@jmjuanes
jmjuanes / mustache.js
Created April 3, 2023 16:12
Mustache template engine
const escapedChars = {
"&": "&",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#039;",
};
const escape = str => {
return str.toString().replace(/[&<>\"']/g, m => escapedChars[m]);
@jmjuanes
jmjuanes / template.js
Created April 3, 2023 16:09
Tiny template engine
const compile = (str, data, delimiters = /<%|%>/) => {
const expressionRegex = /(^( )?(if|for|else|switch|case|break|{|}|let|const|var))(.*)?/g;
const result = [
"const r = [];",
...str.split(delimiters).map((t, i) => {
return i % 2 === 0 ? `r.push(\`${t}\`);` : (t.match(expressionRegex) ? t : `r.push(${t});`);
}),
`return r.join("");`,
];
return new Function(result.join("")).apply(data);
@jmjuanes
jmjuanes / .tmux.conf
Last active April 14, 2020 16:46
My .tmux.conf configuration file
########
##
## #tmux commands
## Reload tmux configuration: tmux source ~/.tmux.conf
##
## tmux links
## https://tomlankhorst.nl/iterm-tmux-vim-true-color/
##
########
@jmjuanes
jmjuanes / .vimrc
Last active April 24, 2021 23:12
My .vimrc file
"
" Installation:
" 1. Set up vundle for vim (https://github.com/VundleVim/Vundle.vim):
" $ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
" 2. Download this `.vimrc` file and save it in your home folder.
" 3. Launch vim in your console and run :PluginInstall.
"
set nocompatible
syntax on
@jmjuanes
jmjuanes / mongo_middleware.js
Created March 16, 2016 13:17
MongoDB Middleware
//Connection url
//mongodb://USERNAME:PASSWORD@DBHOST:DBPORT/DBNAME
//Import dependencies
var MongoClient = require('mongodb').MongoClient;
//Database
var db = null;
//Connect to database
@jmjuanes
jmjuanes / gbackup.js
Last active March 16, 2016 13:17
Do backups to Google Drive using RClone from command line
#!/usr/bin/env node
//Remember to make the script executable
//chmod u+x gbackup.js
//Configuration
var rclone = '/Users/jmjuanes/bin/rclone'; //Rclone path
var exclude = '/srv/.bin/gbackup_exclude.txt'; //Exclude file
var command = '{rclone} sync {origin} gdrive:{remote} --exclude-from {exclude}'; //RClone command
//Import dependencies
@jmjuanes
jmjuanes / FindBinary.js
Created March 9, 2016 11:55
Binary search in an array.
//Function for find the position in a cover array
function FindBinary(arr, position)
{
//Get the min index
var minIndex = 0;
//Get the max index
var maxIndex = arr.length - 1;
//Check the array start
@jmjuanes
jmjuanes / StringToLowercase.cpp
Created November 6, 2015 23:55
Convert string to Lowercase (resp, Uppercase) in C++
#include <algorithm>
#include <string>
std::string data = "Abc";
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
@jmjuanes
jmjuanes / GitHubOauth.js
Last active October 11, 2015 14:35
GitHub Simple OAuth
/*
GitHub Simple OAuth
=================================================================================
## Requisites: you need the next Node.JS modules:
- express, v4.x
- express-session
- request
## Auth: Add the next urls to yor login router:
@jmjuanes
jmjuanes / TabStringToArray.cpp
Last active October 4, 2015 11:57
Converts a tabulated string to an array
//Number of elements of the array
const int N = 11;
//Function Tabulated String to array
void TabStringToArray(string str, string arr[])
{
//Aux vars
int pos = 0;
//Read all