Skip to content

Instantly share code, notes, and snippets.

View ethaizone's full-sized avatar
💭
It's same on Gitlab.

Nimit Suwannagate ethaizone

💭
It's same on Gitlab.
View GitHub Profile
@ethaizone
ethaizone / cmd.sh
Created May 12, 2018 07:05
MySQL - Dump and import with GZip
# Export
mysqldump -u root -p your_db | gzip -c > your_db_2018-05-12.sql.gz
# Export with datetime in filename
mysqldump -u root -p your_db | gzip -c > your_db_$(date +%Y-%m-%d-%H.%M.%S).sql.gz
# Import
zcat your_db_2018-05-12.sql.gz | mysql -u root -p your_db
@ethaizone
ethaizone / safe_math_operator.js
Created September 28, 2017 07:44
Example how to make math calculation with safe output
const countDecimals = function (value) {
if(Math.floor(value) === value) return 0;
return value.toString().split(".")[1].length || 0;
}
const operators = {
'+': (a, b) => {
let decimalLength = Math.pow(10, Math.max(countDecimals(a), countDecimals(b)))
return ((a*decimalLength) + (b*decimalLength))/decimalLength
},
@ethaizone
ethaizone / await.js
Created July 26, 2017 07:09
How to use await with promise. Javascript.
// USE BABEL WITH PRESET ES2017
// FUCKING AWESOME PROMISE EXAMPLE
// FIRST PROMISE
function a() {
return Promise.resolve([1, 2, 3, 4, 5, 6])
// return Promise.reject(new Error("xxxx"))
}
@ethaizone
ethaizone / pstorm
Last active June 11, 2018 08:27
[Windows] Make any GUI program and run from CLI. This example is PHPStorm.
#!/bin/sh
basedir=`dirname "$0"`
# This is like symlink for bash cmd such as mintty, GIT bash, Cygwin
# I pass parameters to it too.
"$basedir/pstorm.cmd" "$@"
# Note - I tried create symlink but it not work at bash environment
@ethaizone
ethaizone / createBlankArray.php
Last active November 28, 2016 09:18
Helper function for create blank array by number of each dimension.
<?php
/**
* Create blank array by number
*
* @param int Number of members in each dimension
* @return array
*/
function createBlankArray()
{
$dimensions = func_get_args();
@ethaizone
ethaizone / sortByArray.php
Last active October 20, 2016 04:14
sortByArray - helper function to sort Array with Array.
<?php
function sortByArray($array, $sortOrder, $options = SORT_REGULAR, $descending = false)
{
// Before first! LOL
// I just extend sortBy in Laravel collection to can use with normal array
// and it will sort data from another array.
// Perfect!!
// Before first. We will add diff array index to last item of sortOrder
@ethaizone
ethaizone / NumberNotation.php
Last active November 2, 2016 05:21
NumberNotation for Eloquent. If you need store data from checkbox and need to query it. This guy is hero. Don't store as stupid json.
<?php
namespace App\Models\Traits;
use Exception;
/**
* This is trait for use with eloquent model only.
* Develop by Nimit Suwannagate <[email protected]>
* I don't allow to modify anythings in this code.
@ethaizone
ethaizone / reactphp_socket_demo.php
Last active September 27, 2016 09:04
Demo tcp server on PHP.
<?php
// Composer install this package.
// https://github.com/reactphp/socket
// Command support: broadcast, who
require 'vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
@ethaizone
ethaizone / tcp_server.js
Last active September 25, 2016 14:19
Example tcp server write by NodeJS. Just telnet to port in config.
var net = require('net');
// Config
var config = {
server: {
port: 7890
}
};
@ethaizone
ethaizone / step1-comment-as-pseudocode.php
Created September 25, 2016 13:42
How-to make you don't forget to comment your code.
// create array for hold all dogs data
// just loop by get data from getNames()
// getNames() is generator so it's simple loop
// and we just need 5 dogs only
// each dog will name as value
// echo all dogs as json