Skip to content

Instantly share code, notes, and snippets.

@mugyu
mugyu / Gemfile
Last active April 17, 2021 06:32
example of the gem in ruby
gem 'mugyu', git: 'https://gist.github.com/4740c9312e70712fa52bad3ac2294fa0.git'
def lerp(a, b, t)
a * (1 - t) + b * t
end
def inverseLerp(a, b, c)
v = (c - a) / (b - a)
v = 0 if v < 0
v = 1 if v > 1
v
end
@mugyu
mugyu / JavaScript.Array.Shuffle.js
Created January 29, 2020 10:08
JavaScrpt の Array に shuffle メソッドを実装 Fisher–Yates shuffle
Array.prototype.shuffle = function() {
var array = this;
for(var i = array.length - 1; i > 0; --i) {
// V8 の Math.random() のアルゴリズムは xorshift128+ らしい
var j = Math.floor(Math.random() * i);
var tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
return array;

Windows BATスクリプトでヒアドキュメントもどき

batch file

(
echo hoge
echo piyo
echo puyo
echo fuga
echo gaoh
<?php
function define_php_versions()
{
$version = explode('.', PHP_VERSION);
define('PHP_VERSION_MAJOR', (int)$version[0]);
define('PHP_VERSION_MINOR', (int)$version[1]);
define('PHP_VERSION_RELEASE', (int)$version[2]);
}
@mugyu
mugyu / json_error.php
Last active December 1, 2019 05:50
json parse error strings in PHP
<?php
class JSONErrorException extends \Exception {}
function json_error($error_code, $exceptionable = TRUE)
{
static $json_errors = NULL;
if ($json_errors === NULL)
{
$json_errors = [
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
@mugyu
mugyu / string_unaccent.rb
Created August 31, 2019 18:55
アクセントを付した文字やなんやかんやを 7bit ascii の文字に変換
class String
UNACCENT = {
'©' => '(C)',
'«' => '<<',
'­' => '-',
'®' => '(R)',
'»' => '>>',
'¼' => ' 1/4',
'½' => ' 1/2',
'¾' => ' 3/4',
@mugyu
mugyu / get_datetime_string.js
Last active August 28, 2019 06:36
現在時刻を `YYYYMMDDHHmm` のフォーマットにして返す in JavaScript
/**
* 取得 現在年月日時分
* @return{string} 現在時刻をベースにした YYYYMMDDHHmm
*/
function get_datetime_string() {
const datetime = new Date();
const year = datetime.getFullYear();
const month = datetime.getMonth() + 1;
const day = datetime.getDate();
const hours = datetime.getHours();
@mugyu
mugyu / stringPadding.js
Created August 9, 2019 07:25
String.prototype.padStart() が使えない場合のゼロ埋めなど
/**
* 文字列のリピート
*/
function stringRepeat(length, string) {
return Array(length + 1).join(string)
}
/**
* なんかの文字埋め
*/
@mugyu
mugyu / define_date_format_mysql.php
Created July 19, 2019 06:58
MySQLの日付関連フォーマット in PHP。多分、他のデータベースでも使えると思うけど、どうなんだろう?
<?php
define('DATE_MySQL_DATE', 'Y-m-d');
define('DATE_MySQL_TIME', 'H:i:s');
define('DATE_MySQL_YEAR', 'Y');
define('DATE_MySQL_DATETIME', DATE_MySQL_DATE . ' ' . DATE_MySQL_TIME);
$now = time();
echo date(DATE_ATOM, $now), "\n"; // => 2019-07-19T15:51:27+09:00
echo date(DATE_RSS, $now), "\n"; // => Fri, 19 Jul 2019 15:51:27 +0900
echo date(DATE_MySQL_DATE, $now), "\n"; // => 2019-07-19