Instantly share code, notes, and snippets.

View check_range_validity.php
private function checkRangeValidity($value)
{
switch ($value = trim($value)) {
case $value[0] === '>':
case $value[0] === '<':
if (!ctype_digit(substr($value, 1))) {
return false;
}
return true;
break;
View get_date_diff.php
function getDateDiff($date1, $date2)
{
$diff = date_diff($date1, $date2);
switch ($diff) {
case ($d = $diff->y) > 0:
$res = 'year(s)';
break;
case ($d = $diff->m) > 0:
$res = 'month(s)';
View prototypical_inheritance_example.js
// Car Object constructor
function Car (brand, color) {
if (typeof brand === 'undefined' || typeof color === 'undefined') {
throw new Error('Please dont leave neither brand nor color empty');
}
this.brand = brand;
this.color = color;
}
View terminalcolors.py
#!/usr/bin/env python
# Copyright (C) 2006 by Johannes Zellner, <johannes@zellner.org>
# modified by mac@calmar.ws to fit my output needs
# modified by crncosta@carloscosta.org to fit my output needs
import sys
import os
def echo(msg):
os.system('echo -n "' + str(msg) + '"')
View create_files_with_random_names.py
import string
import random
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
for i in range(0, 140):
filename = id_generator()
open('file_' + id_generator() + '.txt', 'w')
View remove_numbers_from_filenames.py
import os
for filename in os.listdir("."):
newfilename = ''.join([i for i in filename if not i.isdigit()])
os.rename(filename, newfilename)
View get_highcharts_chart_types.js
$('#plotOptions-menu .menuitem>a:first-child').each(function(index, item) {
var $item = $(this).html();
console.log('const ' + $item.toUpperCase() + ' = \'' + $item + '\';');
});
View az_translit.sql
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
View sumResultOfAnyNumberOfFunctions.js
function sumResultOfAnyNumberOfFunctions() {
var sum = 0;
for (var i = 0; i < arguments.length; i++) {
sum += parseInt(arguments[i]());
}
return sum;
}
View event_emitter.js
function EventEmitter() {
this.evets = [];
}
EventEmitter.prototype.on = function(event, callback) {
this.events[event] = callback();
var args = arguments;
callback(args);
}