Skip to content

Instantly share code, notes, and snippets.

@slavafomin
slavafomin / nodejs-custom-es6-errors.md
Last active March 9, 2024 12:03
Custom ES6 errors in Node.js

Here's how you could create custom error classes in Node.js using latest ES6 / ES2015 syntax.

I've tried to make it as lean and unobtrusive as possible.

Defining our own base class for errors

errors/AppError.js

@kottenator
kottenator / simple-pagination.js
Created July 13, 2015 20:44
Simple pagination algorithm
// Implementation in ES6
function pagination(c, m) {
var current = c,
last = m,
delta = 2,
left = current - delta,
right = current + delta + 1,
range = [],
rangeWithDots = [],
l;
@danharper
danharper / CatchAllOptionsRequestsProvider.php
Last active April 20, 2024 01:53
Lumen with CORS and OPTIONS requests
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
/**
* If the incoming request is an OPTIONS request
* we will register a handler for the requested route
*/
class CatchAllOptionsRequestsProvider extends ServiceProvider {
@bendc
bendc / nodelist-iteration.js
Created January 13, 2015 14:39
ES6: Iterating over a NodeList
var elements = document.querySelectorAll("div"),
callback = (el) => { console.log(el); };
// Spread operator
[...elements].forEach(callback);
// Array.from()
Array.from(elements).forEach(callback);
// for...of statement
@mindplay-dk
mindplay-dk / rm_r.php
Last active July 28, 2021 20:41
Recursively delete a directory and all of it's contents - USE AT YOUR OWN RISK!
<?php
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
# http://stackoverflow.com/a/3352564/283851
/**
* Recursively delete a directory and all of it's contents - e.g.the equivalent of `rm -r` on the command-line.
@oranblackwell
oranblackwell / bootstrap-3-clearfix.md
Last active January 17, 2018 10:27
Auto clearfix for Bootstrap 3's cols. (From within a loop)

Auto-insert Bootstrap 3's clearfix for differnt viewport sizes.

<?php 
/**
 * Bootstrap auto clearfix from inside loops.
 * Put this function at the start of the loop before other bootstrap cols are called.
 *
 * @param       int   $i The count from within the loop where the function is called. Starting with 0.
 * @param       array $args ['xs'=> 12, 'sm' => 6, 'md' => 4, 'lg' => 3]
@tristanfisher
tristanfisher / Ansible-Vault how-to.md
Last active April 3, 2024 13:55
A short tutorial on how to use Vault in your Ansible workflow. Ansible-vault allows you to more safely store sensitive information in a source code repository or on disk.

Working with ansible-vault


I've been using a lot of Ansible lately and while almost everything has been great, finding a clean way to implement ansible-vault wasn't immediately apparent.

What I decided on was the following: put your secret information into a vars file, reference that vars file from your task, and encrypt the whole vars file using ansible-vault encrypt.

Let's use an example: You're writing an Ansible role and want to encrypt the spoiler for the movie Aliens.

@rpowis
rpowis / force-download-remote-file.php
Last active October 5, 2021 22:44
Check for remote file and force the user to download if it exists.
$file = 'http://www.example.com/path/to/file.ext';
$headers = array_change_key_case(get_headers($file, TRUE));
$filesize = $headers['content-length'][1];
if ( $headers[0] != 'HTTP/1.1 404 Not Found' ) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
@todgru
todgru / launchd.md
Last active April 20, 2023 17:54
Launchd and plist, replace cron in Mac OS X

#launchd Usage

I have a bash script called foo.sh that takes one command line argument, bar. I want it to run every 60 seconds and load at startup.

  • an XML plist is Apple Property List
  • com.mydomain.foo.plist Name of launchd plist file should be a reverse fqdn, like (this may not be required, but convention)
  • com.mydomain.foo.plist lives in $HOME/Library/LaunchAgents and is ran as that user.
  • com.mydomain.foo.plist can also live /Library/LaunchDaemons or /Library/LaunchAgents, have requirements, ran as root
  • Load plist with launchctl load com.mydomain.foo.plist
  • Unload plist with lauchctl unload com.mydomain.foo.plist
@bennylope
bennylope / remotefiles.conf
Created October 19, 2011 01:30
nginx remote file proxying
location ~* ^/remote-files/(http[s]*://)(.*?)/(.*) {
# Do not allow people to mess with this location directly
# Only internal redirects are allowed
internal;
# nginx has to be able to resolve the remote URLs
resolver 8.8.8.8;
# Location-specific logging
#access_log /usr/local/etc/nginx/logs/internal_redirect.access.log main;