Skip to content

Instantly share code, notes, and snippets.

View fdorantesm's full-sized avatar

Fernando Dorantes fdorantesm

View GitHub Profile
@fdorantesm
fdorantesm / README.md
Created June 13, 2018 15:11 — forked from joyrexus/README.md
Vanilla JS equivalents of jQuery methods

Sans jQuery

Events

// jQuery
$(document).ready(function() {
  // code
})
@fdorantesm
fdorantesm / helpers,js
Last active October 2, 2018 17:21
Base64 File helpers
function toBytes(value){
var unit = value.substr(-2)
var size = value.replace(unit, '')
var result
switch(unit){
case 'kb': result = size * 1000; break;
case 'mb': result = size * 1000000; break;
case 'gb': result = size * 1000000000; break;
case 'tb': result = size * 1000000000000; break;
@fdorantesm
fdorantesm / common.js
Last active March 11, 2019 17:05
Send emails using nodemailer
import fs from 'fs'
const $app = process.env.APP_PATH
const $src = process.env.SRC_PATH
export function view(path) {
const file = `${$app}/views/${path}.pug`
if (fs.existsSync(file)) {
@fdorantesm
fdorantesm / uploader.js
Created May 5, 2019 15:50
Cloudinary uploader
import fs from 'fs'
import streamifier from 'streamifier'
import cloudinary from 'cloudinary'
cloudinary.config({
cloud_name: process.env.CLOUDINARY_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET
});
@fdorantesm
fdorantesm / token-generator.js
Created July 30, 2019 17:58 — forked from ziluvatar/token-generator.js
Example of refreshing tokens with jwt
/**
* Example to refresh tokens using https://github.com/auth0/node-jsonwebtoken
* It was requested to be introduced at as part of the jsonwebtoken library,
* since we feel it does not add too much value but it will add code to mantain
* we won't include it.
*
* I create this gist just to help those who want to auto-refresh JWTs.
*/
const jwt = require('jsonwebtoken');
@fdorantesm
fdorantesm / functions.php
Created August 26, 2019 15:56
Convert base64 to image and validation helpers
function base64ToImage($imageData, $path){
$data = 'data:image/png;base64,AAAFBfj42Pj4';
list($type, $imageData) = explode(';', $imageData);
list(,$extension) = explode('/',$type);
list(,$imageData) = explode(',', $imageData);
$fileName = uniqid().'.'.$extension;
$imageData = base64_decode($imageData);
file_put_contents($path."/".$fileName, $imageData);
return $fileName;
}
@fdorantesm
fdorantesm / conekta.js
Created August 31, 2019 06:34
Conekta Tokenizer
if (process.client) {
if (window.Conekta) {
window.Conekta.setPublicKey('key_AVyNyZZdY5AsiEEvhpm5crA')
}
}
export default class ConektaService {
static createToken (tokenParams) {
const response = new Promise((resolve, reject) => {
if (!window.Conekta.card.validateNumber(tokenParams.card.number)) {
db.Shopping.updateMany({
_id: {
$nin: [
ObjectId("5e0002b5ee120c01b5492a3a"),
ObjectId("5e0001272f7790405e7c64ed"),
ObjectId("5e000302fbbe813c744409b3"),
ObjectId("5e0002c7fbbe813c744409b0"),
ObjectId("5e0001e02f7790405e7c64fa"),
ObjectId("5e0002a22f7790405e7c6505"),
ObjectId("5e0001ec2f7790405e7c64fd"),
@fdorantesm
fdorantesm / orderBy.php
Created January 3, 2020 16:26
Order by attribute
<?
function orderBy($items, $attr, $order)
{
$sortedItems = [];
foreach ($items as $item) {
$key = is_object($item) ? $item->{$attr} : $item[$attr];
$sortedItems[$key] = $item;
}
if ($order === 'desc') {
@fdorantesm
fdorantesm / aws.js
Created March 13, 2020 17:44
AWS Helpers
const AWS = require('aws-sdk')
const uuidBase62 = require('uuid-base62')
const mailcomposer = require('mailcomposer')
/**
* Upload file to AWS S3 Service
* @param {Buffer} f
* @param {String} path
* @param {String} filename Uploaded file's name
* @return {Promise<Object>} S3 Promise