Skip to content

Instantly share code, notes, and snippets.

View AoiYamada's full-sized avatar
:octocat:

AoiYamada AoiYamada

:octocat:
View GitHub Profile
@AoiYamada
AoiYamada / DggMDwcLDgIEBQEABAMDCw.js
Last active April 28, 2018 11:11
DggMDwcLDgIEBQEABAMDCw
'use strict';
/**
* Deep object flattener
* flatten an object, the message will be pull out, array index/nested key will be chained to be a new key.
* @param {object} obj - the target object
* @param {string} [prefix = ''] - the prefix of each key
* @return {object} flattened object
*/
function deepFlatten(obj, prefix = '') {
@AoiYamada
AoiYamada / curry.js
Last active May 2, 2022 15:56
a function to currify another function
/**
 * @param {Function} fn - The function wanted to be currified
* @return {Function} a currified function
 */
function curry(fn) {
return curryHelper(fn);
}
// private helper, not expose to client
function curryHelper(fn, memory = [], ...args) {
@AoiYamada
AoiYamada / csv-to-array.js
Last active April 28, 2018 12:32
transform csv file to 2D array
/**
* Transform CSV to a 2D array
* @param {String} csvString
* @param {String} [separator=,]
*
*/
function csvToArray(csvString, separator = ',') {
const sRegExp = new RegExp(`(^"|${separator}"|{linebreak}")(.*?)("${separator}|"{linebreak}|"$)`, 'g'); // matching field contents
return csvString
.replace(/\r/g, '') // remove part of window linebreak symbols
@AoiYamada
AoiYamada / brackets-balanced.js
Created May 1, 2018 13:54
check the given string is Brackets Balanced
function isBalanced(val) {
if(val.length) {
// strip off the nestest braces
const replaced_val = replaceBraces(val);
if(val === replaced_val)
return 'NO';
else
return isBalanced(replaced_val);
} else {
return 'YES';
@AoiYamada
AoiYamada / certbot setup guideline.md
Last active January 2, 2023 18:16
Certbot auto renew SSL guideline for CentOS 6, 7

Certbot auto renew SSL guideline for CentOS 6, 7

Sign a ssl cert and renew by cronjob.

Prerequisite

softwares:

  • wget
  • nginx
  • nano
@AoiYamada
AoiYamada / extractContents.js
Last active September 30, 2018 21:17
Remove html tag, css, js of html string to extract the contents
/**
* Remove html tag, css, js of html string to extract the contents
* @param {String} html
* @return {String} contents of the html
*/
function extractContents(html) {
return html
.replace(/(\n|\r|\t)/gm, '') // remove linebreaks
.replace(/<(style|script|link|noscript).*?>.*?<\/(style|script|link|noscript)>/g, '') // remove css, js blocks
.replace(/<!--.*?-->/g, '') // remove comments
@AoiYamada
AoiYamada / Counting.js
Last active September 30, 2018 21:19
Extend Array to support swap, combination, permutation... for general counting purpose
class Counting extends Array {
constructor(...items) {
if (items.length > 1) super(...items);
else {
super();
this.push(...items);
}
}
swap(idx1, idx2) {
const length = this.length;
@AoiYamada
AoiYamada / .babelrc
Created September 30, 2018 21:36
Babel 6 setting to support IE 9+
// http://imweb.io/topic/59dc5a8b856028aa249e2a58
// https://segmentfault.com/a/1190000006930013
// 總之,IE真的是毒瘤...
{
"presets": [
["es2015",
{
"loose": true
}],
"stage-0"
@AoiYamada
AoiYamada / post-receive.sh
Created December 11, 2019 17:07 — forked from benfrain/post-receive.sh
post-receive hook for multiple branches
#!/bin/bash
while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
if [ "master" == "$branch" ]; then
git --work-tree=./path/under/root/dir/live-site/ checkout -f $branch
echo 'Changes pushed live.'
fi
@AoiYamada
AoiYamada / n+1.sql
Last active April 4, 2021 13:02
N + 1 Problems
SELECT * FROM singers LIMIT 5;
SELECT * FROM songs WHERE singer_id = 1;
SELECT * FROM songs WHERE singer_id = 2;
SELECT * FROM songs WHERE singer_id = 3;
SELECT * FROM songs WHERE singer_id = 4;
SELECT * FROM songs WHERE singer_id = 5;