Skip to content

Instantly share code, notes, and snippets.

Avatar

Nico Wiedemann EarMaster

View GitHub Profile
@EarMaster
EarMaster / formatFilesize.cs
Created Jun 19, 2019
Formats a filesize into a human readable format. First parameter is filesize in bytes, second parameter is the precision of the resulting decimal number and the last parameter is the system (choose between 'decimal' aka SI-based or 'binary' based calculation).
View formatFilesize.cs
public enum FileSizeSystem { Decimal, Binary }
public static string FormatFilesize (int Size) { return FormatFilesize(Size, 1, FileSizeSystem.Decimal); }
public static string FormatFilesize (int Size, int Decimals) { return FormatFilesize(Size, Decimals, FileSizeSystem.Decimal); }
public static string FormatFilesize (int Size, FileSizeSystem System) { return FormatFilesize(Size, 1, System); }
public static string FormatFilesize (int Size, int Decimals, FileSizeSystem SizeSystem) {
float OutputSize = (float)Size;
string[] Units;
switch (SizeSystem) {
case FileSizeSystem.Binary:
Units = new string[]{ "Byte", "kiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" };
@EarMaster
EarMaster / build-titanium.sh
Last active Mar 7, 2018
CLI function to build old versions of Titanium and automatically select according XCode and npm versions.
View build-titanium.sh
function build-titanium() {
if [ ! -f tiapp.xml ]; then
echo "File tiapp.xml not found."
else
echo "Parsing tiapp.xml."
outputDir="./dist"
oldNodeVersion=$(node -v | sed "s/v//")
buildAndroid=$(grep -c "<target device=\"android\">true</target>" tiapp.xml)
buildiPad=$(grep -c "<target device=\"ipad\">true</target>" tiapp.xml)
buildiPhone=$(grep -c "<target device=\"iphone\">true</target>" tiapp.xml)
@EarMaster
EarMaster / formatFilesize.php
Last active Jun 19, 2019
Formats a filesize into a human readable format. First parameter is filesize in bytes, second parameter is the precision of the resulting float and the last parameter is the system (choose between 'decimal' aka SI-based or 'binary' based calculation).
View formatFilesize.php
<?php
function format_filesize($size, $decimals=1, $system='decimal') {
$size = intval($size);
$fileSizes = array(
'decimal' => array('Byte', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'),
'binary' => array('Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')
);
for ($n=0; $size>($system=='decimal'?1000:1024); $n++)
$size = $size/($system=='decimal'?1000:1024);
return round($size, $decimals).' '.$fileSizes[$system][$n];
@EarMaster
EarMaster / simpsons.js
Created Jun 24, 2014
The Simpsons in CSS with a little JS
View simpsons.js
(function () {
var mouse = { x: 0, y: 0 };
document.addEventListener('mousemove', function (event) {
mouse.x = (event.clientX || event.pageX)/window.innerWidth;
mouse.y = (event.clientY || event.pageY)/window.innerHeight;
}, false);
// shim layer with setTimeout fallback
// @see http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
var requestAnimFrame = (function(){
return window.requestAnimationFrame ||
@EarMaster
EarMaster / formatFilesize.js
Last active Apr 14, 2020
Formats a filesize into a human readable format. First parameter is filesize in bytes, second parameter is the precision of the resulting float and the last parameter is the system (choose between 'decimal' aka SI-based or 'binary' based calculation).
View formatFilesize.js
var formatFilesize = function (size, decimals, system) {
size = parseInt(size, 10)
if (!decimals && decimals!==0) decimals = 1
if (!system) system = 'decimal'
var fileSizes = {
decimal: ['Byte', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
binary: ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'],
}
var base = system=='decimal' ? 1000 : 1024
var steps = Math.floor(Math.log(size) / Math.log(base))
@EarMaster
EarMaster / forEach.polyfill.js
Created Dec 19, 2012
polyfill for forEach array iteration function
View forEach.polyfill.js
/**
* Polyfill for forEach array iteration function
*
* @see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach
*/
if (!Array.prototype.forEach) {
Array.prototype.forEach = function forEach(callback, thisArg) {
var T, k;
if (this==null)
throw new TypeError( "this is null or not defined" );
View dabblet.css
/**
* Kohler
*/
body {
background: #d0d0d0;
min-height: 100%;
font-family: sans-serif;
}
ul {
@EarMaster
EarMaster / dabblet.css
Created Jul 26, 2012
iPad opacity test
View dabblet.css
/**
* iPad opacity test
*/
body {
background: #f06;
background: linear-gradient(45deg, #f06, yellow);
-webkit-overflow-scrolling: touch;
min-height: 100%;
}
li {
@EarMaster
EarMaster / bind.polyfill.js
Created Jul 20, 2012
Polyfill for function bind
View bind.polyfill.js
/**
* Polyfill for function bind
*
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
*/
if (!Function.prototype.bind) Function.prototype.bind = function (oThis) {
if (typeof this !== "function")
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,