Skip to content

Instantly share code, notes, and snippets.

Akira TANAKA akirattii

View GitHub Profile
@akirattii
akirattii / get-absolute-path-example.js
Created Apr 9, 2019
NodeJS: Get an absolute path from any path.
View get-absolute-path-example.js
const path = require("path");
const os = require("os");
console.log("/foo/bar/file.txt", "=>", getAbsolutePath("/foo/bar/file.txt"));
console.log("foo/bar/file.txt", "=>", getAbsolutePath("foo/bar/file.txt"));
console.log("../../foo/bar/file.txt", "=>", getAbsolutePath("../../foo/bar/file.txt"));
console.log("~/file.txt", "=>", getAbsolutePath("~/file.txt"));
console.log("/~/file.txt", "=>", getAbsolutePath("/~/file.txt"));
console.log("/../../file.txt", "=>", getAbsolutePath("/../../file.txt"));
@akirattii
akirattii / simple-basic-type-validation-funcs.js
Created Mar 7, 2019
MEMO: Pure JS super simple basic type validation functions.
View simple-basic-type-validation-funcs.js
function isNumeric(s) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function isURL(s) {
return (/^https?\:\/\/(.+)/.test(s));
}
function isJSON(s) {
let o;
@akirattii
akirattii / example.js
Created Feb 26, 2019
NodeJS: How to run a process (also a shell script etc) on background separated with the parent node process.
View example.js
const spawn = require('child_process').spawn;
// a command you want to execute.
const command = "node cli/hoge.js --aaa --bbb=123";
const parts = cmd.split(" ");
const cmd = parts[0];
const args = parts.splice(1);
// a background process is running!
@akirattii
akirattii / Logger.js
Last active Feb 13, 2019
NodeJS: A simple winston logger wrapper, which is the daily logging rotation (even hourly, minutely and secondly rotation) available.
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",
@akirattii
akirattii / BarcodeScanner.js
Created Jan 31, 2019
HTML: Barcode Scanner on modern browser
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:
@akirattii
akirattii / ValidatorBase.js
Last active Jan 19, 2019
NodeJS: A validator base class to create your own validator to validate posted data from browser.
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.
@akirattii
akirattii / how-to-re-idex-auto-increment-id.sql
Last active Jan 16, 2019
[MySQL] How to re-index auto_increment id of a table to make use of resting sequence ids when it reached its max limit.
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;
@akirattii
akirattii / csv-transform-example.js
Last active Jan 6, 2019
[NodeJS] CSV data transform example using stream
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" } );
@akirattii
akirattii / sublimetext-ignore-goto-definition-by-pattern.md
Created Dec 5, 2018
Sublime Text 3: Ignores some "goto definition" candidates by file/folder patterns filter
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,
@akirattii
akirattii / async-iterator-example.js
Created Nov 13, 2018
NodeJS v10: An example of Async-Iterator with Stream API
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) {
You can’t perform that action at this time.