Skip to content

Instantly share code, notes, and snippets.

View Ugarz's full-sized avatar

Ugo Ugarz

View GitHub Profile
@Ugarz
Ugarz / iife.md
Last active March 16, 2017 17:04
Sum some Design Patterns Technics in Javascript

IIFE

(function () {
   console.log('bonjour')
}) ();

Named IIFE

(showName = function (name) {
@Ugarz
Ugarz / prototypal_pattern.md
Last active February 6, 2018 13:00
Reflexion around Prototype pattern in Javascript

Prototypal pattern

Let's say I want a provider to request a file

// Create the Provider
const Provider = function(datas) {
    if(!datas || !datas.provider) console.warn('Nope, need a provider')
    const { provider } = datas;
    this.provider = provider;
}
@Ugarz
Ugarz / module_pattern.md
Last active March 21, 2017 15:21
Reflexions around module pattern in javascript

Module pattern in javascript

module.exports = (params) => {
    // create SOAP client with params
    const soapClient = easysoap.createClient(params);
    return {
        request: function(json) {
            // convert json to xml
 soapClient.call(soapXML);
@Ugarz
Ugarz / readme.md
Created March 23, 2017 14:48 — forked from coolaj86/how-to-publish-to-npm.md
How to publish packages to NPM

Getting Started with NPM (as a developer)

If you haven't already set your NPM author info, now you should:

npm set init.author.name "Your Name"
npm set init.author.email "you@example.com"
npm set init.author.url "http://yourblog.com"

npm adduser

@Ugarz
Ugarz / index.md
Last active July 6, 2017 13:12
Async / Await

Thoughts on javascript Async / Await

Some thoughts and playing around Async / Await with Javascript.

// Basic function setting name to this object
function _setName(name){
    this.name = name
}
@Ugarz
Ugarz / manipulate_image_base64.md
Last active April 26, 2018 15:18
How to manipulate image throught base64 with Nodejs

Basic image manipulation with base64

var fs = require('fs');

function base64_encode(file) {
    console.time('encoding')
    var bitmap = fs.readFileSync(file);
    var result =  new Buffer(bitmap).toString('base64');
 console.timeEnd('encoding')
@Ugarz
Ugarz / resizr.md
Last active July 31, 2017 10:46
Resize an image with the gm library, quick'n'easy

Simple image resizer with node

For more api, see here

const fs = require('fs');
const gm = require('gm');

gm('id.jpg')
.toBuffer('JPG', function (err, buffer) {
 if (err) return console.error(err);
@Ugarz
Ugarz / index.md
Last active July 24, 2017 14:34
Templating a file with Lodash

Discover the advanced templating function with lodash

Moar informations here at the Lodash documentation

Create a file index.js and copy paste it. Do not forget to

npm init -y &&
npm i lodash fs nodemon -D

Paste this into the index.js

@Ugarz
Ugarz / slugify.md
Last active August 2, 2017 09:31 — forked from mathewbyrne/slugify.js
Javascript Slugify

Simple Slugy for strings

function slugify(text) {
  return text.toString().toLowerCase()
    .replace(/\s+/g, '-')           // Replace spaces with -
    .replace(/[^\w\-]+/g, '')       // Remove all non-word chars
    .replace(/\-\-+/g, '-')         // Replace multiple - with single -
    .replace(/^-+/, '')             // Trim - from start of text
 .replace(/-+$/, ''); // Trim - from end of text
@Ugarz
Ugarz / getFileExtension.md
Last active August 2, 2017 09:30
Get a file extension with javascript (no regex)

Get a file extension simply, no regex

Full credits to this thread

const getFileExtension = (filename) => {
  const splitedName = filename.split('.');
  console.log(splitedName) // [ 'fileblabla', 'pdf' ]
  if (splitedName.length === 1 || (splitedName[0] === '' && splitedName.length === 2)) {
 return '';