Skip to content

Instantly share code, notes, and snippets.

@marcojetson
marcojetson / jquery.removeClass.js
Last active August 29, 2015 14:28
Add regex support to jQuery's removeClass
(function ($) {
'use strict';
$.fn.originalRemoveClass = $.fn.removeClass;
$.fn.removeClass = function (arg) {
if (arg instanceof RegExp) {
return this.originalRemoveClass(function (i, css) {
var remove = [];
$.each(css.split(/\s+/), function (i, cls) {
@marcojetson
marcojetson / progressBar.php
Last active May 16, 2018 17:46
Simple and customisable command line progress bar in PHP
<?php
/**
* Creates a progress bar
*
* @param string $template Template to use, available placeholders are %finished, %unfinished, %percent and %eta
* @param int $width Width of the progress bar
* @return callable A function that renders the bar and takes the percent as only argument
*/
function progressBar(
@marcojetson
marcojetson / duration.php
Created July 30, 2015 09:29
Take a number of seconds and return it in weeks/days/hours/minutes/seconds format. (like mIRC script's $duration identifier)
<?php
/**
* @param int $seconds
* @return string
*/
function duration($seconds)
{
foreach (['wk' => 604800, 'day' => 86400, 'hr' => 3600, 'min' => 60, 'sec' => 1] as $symbol => $subunit) {
$value = (int) floor($seconds / $subunit);
@marcojetson
marcojetson / ObjectId.php
Created July 24, 2015 15:06
Mongo inspired unique and sortable id generation
<?php
class ObjectId
{
/** @var string */
private $value;
/**
* @param string $value
*/
@marcojetson
marcojetson / IDateTime.php
Last active August 29, 2015 14:25
DateTime supporting microseconds
<?php
class IDateTime extends \DateTime
{
public function __construct($time = 'now', $timezone = null)
{
if ($time === null || $time === 'now') {
$ts = microtime(true);
$micro = sprintf('%06d',($ts - floor($ts)) * 1000000);
$time = date('Y-m-d H:i:s.' . $micro, $ts);
@marcojetson
marcojetson / lilidns-updater.sh
Last active August 29, 2015 14:25
lilidns.com cli host updater
#!/bin/bash
if [ -z "$1" ]
then
echo "Usage $0 <token> [ip]"
exit
fi
if [ -n "$2" ]
then
@marcojetson
marcojetson / fix-api.php
Created June 19, 2015 09:31
PHP API is weird
<?php
/**
* Checks if a value exists in an array
*
* @param array $haystack
* @param mixed $needle
* @param bool $strict
* @return bool
*/
@marcojetson
marcojetson / jquery-ajaxforms.js
Created June 16, 2015 08:56
convert forms to ajax forms
$('body').on('submit', 'form.ajax', function () {
var form = $(this);
$.ajax({
type: form.attr('method') || 'GET',
url: form.attr('action') || location.href,
data: form.serialize(),
success: function (data, textStatus, jqXHR) {
form.trigger('success', [data, textStatus, jqXHR]);
},
@marcojetson
marcojetson / quine.php
Created June 15, 2015 14:12
quine.php
<? $a='<? $a=\'%s\';printf($a,addslashes($a));';printf($a,addslashes($a));
@marcojetson
marcojetson / brainfuck.c
Last active August 29, 2015 13:56
brainfuck interpreter
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *p = malloc(1024);
int l[10], j = 0, i = 0;
while (argv[1][i]) {
switch (argv[1][i++]) {