Skip to content

Instantly share code, notes, and snippets.

@liunian
liunian / gist:9338301
Last active April 26, 2024 03:30
Human Readable File Size with PHP
<?php
# http://jeffreysambells.com/2012/10/25/human-readable-filesize-php
function human_filesize($bytes, $decimals = 2) {
$size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}
echo human_filesize(filesize('example.zip'));
@liunian
liunian / gist:9757295
Last active April 26, 2024 03:30
References
{
'Safari Developer Library': 'https://developer.apple.com/library/safari/navigation/index.html',
'Safari HTML Reference': 'https://developer.apple.com/library/safari/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/Attributes.html',
'Safari Web Content Guide': 'https://developer.apple.com/library/ios/documentation/AppleApplications/Reference/SafariWebContent/Introduction/Introduction.html'
}
/**
* find all markdown files in given directory, (here just current directory)
* and then fetch their title to generate index markdown file
*
* use -v to show verbose detail
* use -o to specify output file, default to ./index.md
* use -d to specify scanner directory, such as -d the_directory, default to ./
*/
var fs = require('fs'),
path = require('path')
{
decodeSpecialUnicode: function(str) {
var ptn = /\\u([0-f]{4})/ig;
return str.replace(ptn, function(match, group) {
return String.fromCharCode(parseInt(group, 16));
});
},
encodeSpecialUnicode: function(str) {
@liunian
liunian / to_ascii.js
Last active April 26, 2024 03:29
把超出 ascii 范围的字符(如中文)等转为基于 ascii 范围字符的转义表达形式(https://github.com/mishoo/UglifyJS2/blob/master/lib/output.js#L76
// from https://github.com/mishoo/UglifyJS2/blob/harmony/lib/output.js
function to_ascii(str) {
return str.replace(/[\ud800-\udbff][\udc00-\udfff]|[\u0000-\u001f\u007f-\uffff]/g, function (ch) {
var code = get_full_char_code(ch, 0).toString(16);
if (code.length <= 2) {
while (code.length < 2) code = "0" + code;
return "\\x" + code;
} else {
while (code.length < 4) code = "0" + code;
@liunian
liunian / clone-json.js
Last active April 26, 2024 03:29
clone json
// from: https://jsperf.com/deep-copy-vs-json-stringify-json-parse/15
function recursiveDeepCopy(o) {
var newO,
i;
if (typeof o !== 'object') {
return o;
}
if (!o) {
// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function(){
var cache = {};
this.tmpl = function tmpl(str, data){
// Figure out if we're getting a template, or if we need to
// load the template - and be sure to cache the result.
var fn = !/\W/.test(str) ?
cache[str] = cache[str] ||
@liunian
liunian / optimg.fish
Last active April 21, 2023 14:25
optimize images
#!/usr/bin/env fish
# use optipng to optimize png
if type -q optipng
set pngs (command find . -name "*.png")
for i in $pngs
# echo $i
optipng -strip all $i
end
else
@liunian
liunian / Wallpapers.py
Last active September 4, 2021 15:07
Download wallpaper from https://wall.alphacoders.com/
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import bs4
import re
import os
import sys
from multiprocessing import Pool, cpu_count
try:
@liunian
liunian / cur-git-branch-name.sh
Last active August 31, 2019 03:10
is git dirty
git rev-parse --abbrev-ref HEAD