Skip to content

Instantly share code, notes, and snippets.

@formigone
formigone / Handler.php
Created December 17, 2014 16:48
Idea for a dependency injection service similar to Angular.js, but for handler function-based PHP routers like Slim Framework
<?php
/**
* Class Handler
*
* Playing around with reflection & DI. Handler is a routing class (think Express [node.js] or Slim [php]),
* with a built-in dependency injection system inspired by Angular.js.
*
* Consider this:
* - Use annotations in the handler callback to specify what class needs to be instantiated?
@formigone
formigone / gulpfile.js
Created April 1, 2015 12:11
Gulp Closure Compiler - using closure library
// As of this writing, the example on https://github.com/steida/gulp-closure-compiler doesn't
// explain how to compile your project using Google's Closure Library.
// Since the newest version of the compiler doesn't need a deps file anymore, we just need
// to provide all our module definitions (files with goog.provide) to the task src.
gulp.task('build', function () {
// Specify where your Closure Library is stored --------------------------vvvv
gulp.src(['main.js', 'src/**/*.js', 'node_modules/closure-library/closure/goog/**/*.js'])
// Everything else is the same as in the docs
@formigone
formigone / html5-blob-file-exporter.js
Created April 23, 2015 12:25
Save data from JavaScript to client
// This snippet came from some website and is not original
function saveTextAsFile()
{
var textToWrite = 'lorem ipsum';
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
var fileNameToSaveAs = 'html5-blob-to-file.txt'
var downloadLink = document.createElement('a');
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
@formigone
formigone / webpack-min.js
Last active August 29, 2015 14:22
Bundle with uglify + source maps
// CLI command
// -p = production (uglify)
// -d = source maps
// --watch (optional)
webpack index.js --output-file bundle.min.js -p -d --progress
@formigone
formigone / ad-blocker-detection.js
Last active August 29, 2015 14:23
Detect if ad blockers are present
/**
* Attempt to load a script from a popular ad network. Ad blockers will intercept the HTTP request.
*
* @param {string} url
* @return {Promise}
*/
function detectAdBlockerAsync(url){
var def = $.Deferred();
var script = document.createElement('script');
@formigone
formigone / xtree-utils.php
Last active August 29, 2015 14:25
Various traversal util methods for XTREE
/**
* @param int $nid
* @param array $props
* @param resource $xml
* @param bool $includeHidden
* @return array
*/
function getChildrenNodes($nid, array $props, &$xml, $includeHidden = false) {
$node = xtree_get_node($xml, "/story/body/tree//node[@id='$nid']");
@formigone
formigone / select-promoted.php
Last active August 29, 2015 14:27
Select weighted element from array: 1.0 vs 1.4
<?php
// Number of times to simulate selection
const MAX = 500000;
// Probability of selecting promoted element (40% more often than normal items)
const PROMOTED_PROB = 0.7142857142857143 /* 1 / 1.4 */;
$total = 0;
$in = array_map(function () {
@formigone
formigone / massive-upload.js
Created September 4, 2015 14:23
Upload huge files async using HTML5's FileReader API. File data is uploaded by chunks of data URL strings.
/* VIEW.html
<style>
.container {
margin: 10px auto;
padding: 10px;
border: 1px solid #333;
display: block;
overflow: auto;
text-align: center;
}
@formigone
formigone / twitch-active-streams.js
Created March 22, 2016 21:14
Fetch N pages of active users from Twitch.tv
const https = require('https');
// This being a dirty prototype, I'm not concerned about checking for min/max values for this...
const maxPages = process.argv[2] || 3;
function getStreams(pages, offset, data, cb){
// Offset could be too far. First response would include total streams (res._total). It also has links to next page...
const req = https.get({ hostname: 'api.twitch.tv', path: '/kraken/streams?stream_type=live&offset=' + offset }, function(res){
var body = '';
@formigone
formigone / jmeter-json-assertions.js
Last active May 11, 2016 14:13
A simple set of utility functions to help you perform assertions on JSON output from a JMeter stress test - **no plugins required**
var testUtil = {
/**
* Fails the assertion with the message provided.
*/
failWith: function (msg) {
AssertionResult.setFailure(true);
AssertionResult.setFailureMessage(msg);
},
isArray: function (obj) {