Skip to content

Instantly share code, notes, and snippets.

@Loupax
Loupax / Enum.php
Created September 25, 2011 23:59
PHP Enum
abstract class Enum{
//To use this Enum class, just extend it and fill it with costants ;)
public static final function get_const_list(){
$r = new ReflectionClass(get_called_class());
/*I chose to flip the array because it's more flexible to work with integers as keys
*rather than strings...
*/
return array_flip($r->getConstants());
@Loupax
Loupax / gist:1722717
Created February 2, 2012 10:06
A function I made for use in CodeIgniter, search entire result set and when the match is evaluated, returns the value specified of said row
/**
* Searches a CI resultset, and when the match is evaluated, returns the specified value
* Both params of $match array are required
*
* Example of use: if you want to get the ID of an item where the row 'type' is 'user'
* you do:
*
* Take note: The comparison doesn't take account of whitespace characters before and
* after the data of the result sets rows
*
@Loupax
Loupax / gist:2940690
Created June 16, 2012 09:28
Group sorting
function group_sort($array, $order)
{
$new = array();
foreach ($array as $row){
foreach ($order as $ord){
$new[$ord][] = $row[$ord];
}
}
$str = '';
@Loupax
Loupax / gist:3048566
Created July 4, 2012 17:47
Group sorting Final
/**
* Sorts a multidimentional array in groups. The sorting sequence is provided by using an array
* that contains the keys that should be grouped
*
* @param array $array
* @param array $order
*/
function group_sort(&$array, $order)
{
if(!$array)
@Loupax
Loupax / gist:6628913
Created September 19, 2013 19:50
An example of how to do connected sortable lists that use shared object pools in angular.js
<!doctype html>
<html ng-app="app">
<head>
</head>
<body>
<div>
<div ng-controller="ListsController">
<!-- sortable-connect-with specifies the element that will be connected to this -->
<!-- sortable-limit tells the directive that this element cannot contain more elements than those specified -->
@Loupax
Loupax / ClientDB
Last active August 29, 2015 13:56
ClientDB, a library that allows you to emulate db api in the client.
var ClientDB = function(){
var DB = this;
var localStorage = window.localStorage;
// This object will contain an array for every data type
var tables = {};
// This object will contain the latest id for every table in the app.
var table_ids = {};
// This record will contain any records that are deleted from the local DB, but are not yet synched with the server
var deleted_records = {};
@Loupax
Loupax / 1DCollisionDetection
Last active August 29, 2015 14:08
1D collision detection
var collisionsExist = false;
rows.map(function(row){
row.timebands.map(function(timeband){
if(timeband !== $scope.timeband){
var newTb = fixTimeband($scope.timeband), testTb = fixTimeband(timeband);
// The new timeband starts before the test timeband starts, and ends after the test timeband ends
if(newTb.to <= testTb.to && newTb.from >= testTb.from){
collisionsExist = true;
}
@Loupax
Loupax / Shims.js
Created January 20, 2015 14:48
Shims
(function(){
// We do not know if this file is used inside a web script or a webworker script,
// so we use this hack to get access to the current global object
var get = eval;
var window = get("this");
// Some versions of IE have no set method in the Uint8ClampedArray.
// This was propably a bug that was fixed in a later update but we'll add it here
// just to be sure...
Uint8ClampedArray.prototype.set = Uint8ClampedArray.prototype.set || function(data){
@Loupax
Loupax / RandomUsernameGenerator
Last active August 29, 2015 14:21
Random username generator
/**
* Extending the user creation functionality
*/
if(Meteor.isServer){
Accounts.onCreateUser(function(options, user){
user.username = Meteor.call('generateUsername');
var existing = Meteor.users.find({username: user.username}, {limit:1, fields:{_id:1}}).fetch().length;
if(existing){
// Get the total number of usernames that start with the existing username, add
// their total count at the end to make it unique
#!/bin/bash
tail "$@" | awk '
{matched=0}
/INFO:/ {matched=1; print "\033[0;37m" $0 "\033[0m"} # WHITE
/NOTICE:/ {matched=1; print "\033[0;36m" $0 "\033[0m"} # CYAN
/WARNING:/ {matched=1; print "\033[0;34m" $0 "\033[0m"} # BLUE
/ERROR:/ {matched=1; print "\033[0;31m" $0 "\033[0m"} # RED
/ALERT:/ {matched=1; print "\033[0;35m" $0 "\033[0m"} # PURPLE
matched==0 {print "\033[0;33m" $0 "\033[0m"} # YELLOW