Instantly share code, notes, and snippets.

Akira TANAKA akirattii

View GitHub Profile
View Logger.js
/*
* A logger module, which is a simple `winston`(v3) wrapper.
* @version 1.0.1
* lastUpdated: 2019-02-13
*
* # Usage:
* ```js
* const logCfg = {
* // Label name. It's up to you:
* "label": "server",
View BarcodeScanner.js
/**
Barcode Scanner library
@author: Akira TANAKA <tanaka.akira.2006@gmail.com> (http://mint.pepper.jp)
@dependencies:
+ jquery
+ quagga (https://serratus.github.io/quaggaJS/)
## Usage example:
View ValidatorBase.js
const Big = require("big.js");
/**
* Validator base class.
* Create your validator subclass derived from this class.
* Assuming that it's used to validate a browser's posting data on server-side.
*
* ## NOTE: `check*()` methods:
*
* If validation failed in a checking method named `check*()`,
* some errors are set to `errors` which is passed as a parameter, and also returned as a method's result.
View how-to-re-idex-auto-increment-id.sql
--
-- How to re-index the auto_increment id.
--
-- 0) Create an original table:
CREATE TABLE `hoge` (
`id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
View csv-transform-example.js
// Define the transform rule
const transformRules = [
upperCase, // col idx 0: To uppercase. ``upperCase` is function.
{ "1":"1st", "2":"2nd", "3":"3rd" }, // col idx 1: 1=>"1st", 2=>"2nd", ...
{ "A":"PlanA", "B":"PlanB", "C":"PlanC" },// col idx 2: "A"=>"PlanA", ...
];
const fs = require("fs");
const readStream = fs.createReadStream(__dirname + "/input.csv" );
const writeStream = fs.createWriteStream(__dirname + "/output.csv", { encoding: "utf8" } );
View sublimetext-ignore-goto-definition-by-pattern.md

Sublime Text 3: Ignores some "goto definition" candidates by file/folder patterns filter

Open Project -> Edit Project

{
  "folders": [
    {
      "path": "hoge-project",
      "follow_symlinks": true,
View async-iterator-example.js
const fs = require("fs");
const inputFilePath = process.argv[2];
console.log("file:", inputFilePath);
async function main(inputFilePath) {
const readStream = fs.createReadStream(inputFilePath, { encoding: 'utf8', highWaterMark: 1024 });
// NOTE: Async-Iterator starts with NodeJS v10, but it's experimental yet. (Current latest LTS version: v10.13.0)
for await (const chunk of readStream) {
View chart.js-simple-stock-chart-example.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Char.js Simple Stock Chart Example</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js" integrity="sha256-XF29CBwU1MWLaGEnsELogU6Y6rcc5nCkhhx89nFMIDQ=" crossorigin="anonymous"></script>
View parsePaymentURIScheme.js
/**
* Payments URI Scheme Parser
* @param {String} - eg. "bitcoin:1XXX?amount=123&comment=%66%77"
* @see also: https://github.com/bitcoin/bips/blob/master/bip-0021.mediawiki
*/
function parsePaymentURIScheme(s) {
const tmp = s.match(/^(\w+)\:(\w+)\??(.+)?/);
const protocol = (tmp && tmp.length >= 1) ? tmp[1] : null;
const address = (tmp && tmp.length >= 2) ? tmp[2] : null;
const qstr = (tmp && tmp.length >= 3) ? tmp[3] : null;
View generate-entropy-on-mouse-moving.js
// const entropy = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
const entropy = [];
let captureStart = false;
/**
* Mouse Moving Entropy Generator on browser.
* Returns an entropy which is 16 bytes long array of unsigned char integer (0-255).
*/
$(document).on("mousemove", "html", function(e) {
const MAX_LEN = 16; // size of entropy's array