Skip to content

Instantly share code, notes, and snippets.

View riffrain's full-sized avatar
:octocat:

riffrain riffrain

:octocat:
  • Japan
View GitHub Profile
@riffrain
riffrain / deepFreeze.js
Last active April 26, 2020 13:02
Deep freeze Object
function deepFreeze(object) {
const clone = JSON.parse(JSON.stringify(object));
for (const name of Object.getOwnPropertyNames(clone)) {
if (typeof clone[name] === "object" && clone[name] !== null) {
clone[name] = deepFreeze(clone[name]);
}
}
return Object.freeze(clone);
}
@riffrain
riffrain / uuidv4.js
Last active August 28, 2023 16:18
Simple UUID(v4) Generator
function uuidv4_1() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
.split('')
.map((c) => {
if (c === 'x') return Math.floor(Math.random() * 0xf).toString(16); // [0-9a-f]
if (c === 'y') return Math.floor(Math.random() * 4 + 8).toString(16); // [89ab]
return c;
})
.join('');
}
@riffrain
riffrain / randomhexcolor.js
Created April 11, 2020 03:52
ramdom hex color
function randomHexColor() {
return '#'+(~~(Math.random()*0xffffff)).toString(16).padEnd(6,0);
}
@riffrain
riffrain / array2object.js
Created April 11, 2020 03:49
array2object object2array
function array2object(arr) {
return { ...arr };
}
@riffrain
riffrain / build and install.sh
Created January 12, 2020 01:47
build neovim and install it to local
git clone https://github.com/neovim/neovim
cd neovim
# if you want build stable release
git checkout stable
make CMAKE_BUILD_TYPE=Release \
CMAKE_EXTRA_FLAGS="-DCMAKE_INSTALL_PREFIX=$HOME/.local -DLibIntl_LIBRARY=/usr/lib/libintl.so.8 -DLibIntl_INCLUDE_DIR=/usr/include"
make install
# export PATH="$HOME/.local/bin:$PATH"
export const animateFrame = ((callback) => {
return window.requestAnimationFrame
|| function (cb) {
return window.setTimeout(cb, 1000/60);
};
})();
export const cancelAnimateFrame = window.cancelAnimationFrame || window.clearTimeout;
class DrawCanvas {
/**
* @param {String} id id of <canvas>
*/
constructor(id) {
this.lastPos = { x: 0, y: 0 };
this.paths = [];
this.drawing = false;
this.canvas = document.getElementById(id);
this.init();
@riffrain
riffrain / ghost
Created September 23, 2019 12:15
ghost blog start script for openrc (/etc/init.d/ghost)
#!/sbin/openrc-run
environment="production"
description="Ghost blogging platform"
extra_started_commands=""
extra_stopped_commands=""
ghost_root="/var/www/ghost"
pidfile="/run/ghost.pid"
command=/usr/bin/node
@riffrain
riffrain / is_natural_number.js
Last active October 23, 2019 12:52
数値型、文字型に関係なく自然数かチェックする
const isNaturalNumber = (value) => /^(?:0|[1-9][0-9]*)$/.test(value);
isNaturalNumber(0); // => true
isNaturalNumber(123); // => true
isNaturalNumber(0123); // => true
isNaturalNumber('0'); // => true
isNaturalNumber('123'); // => true
isNaturalNumber('0123'); // => false
@riffrain
riffrain / flatten_and_expand.js
Last active April 19, 2020 08:28
object flatten and expand
import _ from 'lodash';
const numberRE = /^(?:0|[1-9][0-9]*)$/;
const flatten = (originalData) => {
const result = {};
const stack = [];
let paths = [];
let data = JSON.parse(JSON.stringify(originalData));
while(true) {
data = { ...data };