Skip to content

Instantly share code, notes, and snippets.

View akirattii's full-sized avatar

Akira TANAKA akirattii

View GitHub Profile
@akirattii
akirattii / insert-html-tag-to-message.js
Created October 24, 2019 01:22
Example: How to insert html tags efficiently.
/*
* Below example converts from "1hoge2foo3bar456" to "1<b>hoge</b>2<b>foo</b>3<b>bar</b>456".
*/
const tag = "b"; // <b> tag.
// sample message
let msg = "1hoge2foo3bar456";
/* insert point indice */
@akirattii
akirattii / get-absolute-path-example.js
Created April 9, 2019 08:03
NodeJS: Get an absolute path from any path.
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 March 7, 2019 05:18
MEMO: Pure JS super simple basic type validation functions.
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
Last active May 2, 2023 01:29
NodeJS: How to run a process (also a shell script etc) on background separated with the parent node process.
const spawn = require('child_process').spawn;
// a command you want to execute.
const command = "node cli/hoge.js --aaa --bbb=123";
const parts = command.split(" ");
const cmd = parts[0];
const args = parts.splice(1);
// a background process is running!
@akirattii
akirattii / Logger.js
Last active November 20, 2021 19:53
NodeJS: A simple winston logger wrapper, which is the daily logging rotation (even hourly, minutely and secondly rotation) available.
/*
* 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 January 31, 2019 01:39
HTML: Barcode Scanner on modern browser
/**
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 January 19, 2019 23:02
NodeJS: A validator base class to create your own validator to validate posted data from browser.
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 July 14, 2023 06:37
[MySQL] How to re-index auto_increment id of a table to make use of resting sequence ids when it reached its max limit.
--
-- 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 January 6, 2019 23:00
[NodeJS] CSV data transform example using stream
// 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 December 5, 2018 00:19
Sublime Text 3: Ignores some "goto definition" candidates by file/folder patterns filter

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

Open Project -> Edit Project

{
  "folders": [
    {
      "path": "hoge-project",
 "follow_symlinks": true,