Skip to content

Instantly share code, notes, and snippets.

@ttchengcheng
ttchengcheng / multi-panel.html
Last active September 5, 2018 15:02
multi-panel layout with flex
<html>
<head>
<style>
html,
body {
/* set height of html and body both to 100% to fill the page */
height: 100%;
width: 100%;
margin: 0px;
@ttchengcheng
ttchengcheng / language-independent.md
Created August 13, 2018 02:54
language independent useful snippet

把连续的字符替换掉, 比如把连续的 '/' 替换为 '/', 来自 这里

const prettyPath = urlObj.pathname.replace(/\/{2,}/g, "/");
@ttchengcheng
ttchengcheng / javascript.js
Last active July 26, 2018 03:19
Commonly used Javascript snippet
function jsonStr(obj, indent) {
return JSON.stringify(
obj,
// default result of JSON.stringify() for RegExp object is {}
(key, value)=> {
if (value instanceof RegExp) {
return value.toString(); // RegExp.toString() returns string like '/\\.(t|j)sx?$/'
}
return value;
},
@ttchengcheng
ttchengcheng / cpppatterns.md
Created May 25, 2018 08:30
Some C++ patterns

C++ patterns

singleton

// http://www.modernescpp.com/index.php/thread-safe-initialization-of-a-singleton
// fast and thread safe

class MySingleton{
public:
@ttchengcheng
ttchengcheng / CIEDE2000.cpp
Last active May 23, 2018 06:55
using CIEDE2000 to calculate color difference
// reference: https://zh.wikipedia.org/wiki/%E9%A2%9C%E8%89%B2%E5%B7%AE%E5%BC%82
// 1. CIEDE2000 uses Lab which is a color format that better to tell the difference by human vision.
// 2. There is no directly conversion from RGB to Lab, but RGB to XYZ to Lab.
// https://github.com/gfiumara/CIEDE2000/blob/master/CIEDE2000.cpp
constexpr double
CIEDE2000::deg2Rad(
const double deg)
{
return (deg * (M_PI / 180.0));
@ttchengcheng
ttchengcheng / get_github_starred.js
Last active November 10, 2017 06:49
Use octonode to get all starred repositories of your github
// https://github.com/pksunkara/octonode
const github = require('octonode')
// get one page of starred repos
// starred() is a Pagination API: https://github.com/pksunkara/octonode#pagination
function _getPageStarred (ghme, page) {
return new Promise(function (resolve, reject) {
ghme.starred(page, (err, repoList) => {
err ? reject(err) : resolve(repoList)
})
@ttchengcheng
ttchengcheng / file-checksum-stream.js
Last active October 24, 2017 08:53
transform(duplex) stream generates file checksum
/* usage:
* const iStream = fs.createReadStream('./oceans.mp4');
*
* const mStream = new ChecksumStream('md5');
* mStream.on('done', function(sum) {
* console.log(sum);
* })
*
* const oStream = fs.createWriteStream('o/oceans.mp4');
*
@ttchengcheng
ttchengcheng / brewsrc.sh
Last active October 12, 2017 03:27
switch sources of Homebrew
# switch sources of Homebrew
_brewsrc() {
# save current working dir
pwd=$(PWD)
if [ "$1" = "0" ]; then
echo "restore brew source to default"
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"