Browse ALL the snippets ! - Page 1 of 6

Here's an alias for docker-compose intelligently running with a custom script, a custom-location YAML configuration file, or neither, depending on the situation

docker-compose() {
  if [[ -f "ops/dev/compose" ]]; then
    ops/dev/compose ${@:1}
  elif [[ -f "ops/dev/docker-compose.yml" ]]; then
    docker-compose -f ops/dev/docker-compose.yml ${@:1}
  else
    docker-compose ${@:1}
  fi
}

alias dc='docker-compose'

Turns a Headers instance into a plain JS object to be able to log it or interact with it more easily

Array.from(headers.keys()).reduce((acc, key) => Object.assign(acc, { [key]: headers.get(key) }), {})

Taking advantage of native ES6+ module caching, it's easy to set up reusable instances in your codebase

import Axios from "axios";

const axios = Axios.create("lalala");

export {
  axios
};

// Use with: import { axios } from "path/to/dic";

Simple sample function to intersect a bunch of arrays

function intersect(...arrays) {
  return arrays.reduce((acc, cur) => {
    if (acc.length === 0) {
      return cur;
    }

    return acc.filter(role => cur.includes(role));
  }, []);
}

If you ever need to delay a promise (for debugging purposes and whatnot), use the following:

function delay(ms) {
    return (data) => new Promise(resolve => { setTimeout(() => resolve(data), ms); });
}

If using gulp-livereload without a browser extension, run this task in development mode to automatically inject the required script tag:

var insertLines = requir('gulp-insert-lines');

gulp.task('livereloadTag', function () {
    gulp.src('/path/to/template.html')
        .pipe(insertLines({
            before: /<\/body>/,
            lineBefore: '<script src="http://localhost:35729/livereload.js"></script>'
        }))
        .pipe(gulp.dest(distPath));
});

Some libraries still register themselves to the global scope, and don't formalize their dependencies through a package.json or bower.json file. In such a case, if you don't wanna include the whole Underscore.js source in your repository, follow along:

window._ = {
    bind: function(func, context) {
        return Function.prototype.bind.apply(func, Array.prototype.slice.call(arguments, 1));
    },

    extend: function(obj) {
        Array.prototype.forEach.call(Array.prototype.slice.call(arguments, 1), function(source) {
            if (!source) {
                return;
            }

            for (var prop in source) {
                obj[prop] = source[prop];
            }
        });

        return obj;
    }
};

This function will output an array containing all traits used by the given class and its ascendance, and all the traits they use themselves, etc.

function getRecursiveTraits($class)
{
    $reflection = new \ReflectionClass($class);
    $traits = array_keys($reflection->getTraits());

    foreach ($traits as $trait) {
        $traits = array_merge($traits, getRecursiveTraits($trait));
    }

    if ($parent = $reflection->getParentClass()) {
        $traits = array_merge($traits, static::getRecursiveTraits($parent->getName()));
    }

    return $traits;
}

This is one of the many undocumented aspects of Prestashop. If you ever need to render a template yourself, do something along those lines :

$tpl_dir = $this->context->smarty->getTemplateDir();
is_array($tpl_dir) && $tpl_dir = array_pop($tpl_dir); // Extract actuel template directory path from the retrieved array
$tpl_dir = preg_replace('#tpl(/|\\\\)$#', '', $tpl_dir); // Remove extra, non-existent tree level "tpl/"

$tpl = $this->context->smarty->createTemplate(
    $tpl_dir.'partials/category_menu.tpl',
    array(
        // these are the variables we are feeding our template with
        // the following allows you to use Prestashop's $link methods
        'link' => $this->context->link
    )
);

$html = $this->context->smarty->fetch($tpl);

Here's how to match an empty route on a controller with PHP 5.3+ Silex framework :

$app
    ->get(
        '/{uri}',
        function () {
        }
    )
    ->assert('uri', '.{0}')
    ->value('uri', '');