Skip to content

Instantly share code, notes, and snippets.

View EarMaster's full-sized avatar

Nico Wiedemann EarMaster

View GitHub Profile
@EarMaster
EarMaster / formatFilesize.ts
Last active August 3, 2022 00:30
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).
enum FileSizeSystems {
Decimal = 'decimal',
Binary = 'binary',
}
const formatFilesize = (size: number, decimals: number = 1, system: FileSizeSystems = FileSizeSystems.Decimal): string => {
const fileSizes = {
[FileSizeSystems.Decimal]: ['Byte', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
[FileSizeSystems.Binary]: ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'],
}
const base = {
@EarMaster
EarMaster / formatFilesize.cs
Created June 19, 2019 10:54
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).
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 March 7, 2018 13:51
CLI function to build old versions of Titanium and automatically select according XCode and npm versions.
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 June 19, 2019 10:56
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).
<?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 June 24, 2014 16:07
The Simpsons in CSS with a little 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 April 14, 2020 12:02
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).
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 December 19, 2012 10:01
polyfill for forEach array iteration function
/**
* 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" );
/**
* Kohler
*/
body {
background: #d0d0d0;
min-height: 100%;
font-family: sans-serif;
}
ul {
@EarMaster
EarMaster / dabblet.css
Created July 26, 2012 08:42
iPad opacity test
/**
* 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 July 20, 2012 10:03
Polyfill for function bind
/**
* 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,