Skip to content

Instantly share code, notes, and snippets.

View neos1803's full-sized avatar
🥽
Diving

Nanda W. Purba neos1803

🥽
Diving
View GitHub Profile
@neos1803
neos1803 / mal-auth-handler.js
Last active March 17, 2022 07:39
Function to handle MAL API Authentication in Express
const puppeteer = require("puppeteer");
const crypto = require("crypto");
const querystring = require("querystring");
const axios = require("axios").default;
require("dotenv").config();
static async login(req, res) {
try {
const code_challenge = crypto.randomBytes(64).toString("hex");
const code_verifier = code_challenge;
@neos1803
neos1803 / script.sql
Last active March 17, 2022 07:39
List all tables referenced by a specific column name (by FK) PostgreSQL
select table_schema, table_name
from information_schema.columns
where table_schema not in
('information_schema','pg_catalog')
and column_name = 'id_masa' -- insert referencing column name, eq: id_masa
@neos1803
neos1803 / columns.sql
Last active March 17, 2022 07:38
Show all columns from table PostgreSQL
SELECT column_name
FROM information_schema.columns
WHERE table_name='table_name';
@neos1803
neos1803 / regex.js
Last active March 17, 2022 07:40
Example of Characters Constraint Regex
// Check string contains only one space
const pattern = new RegExp(/^\S+(?: \S+)*$/);
// Check if string is url
const pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i');
@neos1803
neos1803 / readFileSize.js
Last active March 17, 2022 07:37
Read file size from readStream javascript
function getFileSize(stream?: any, callback?): Promise<any> {
return new Promise((resolve, reject) => {
let bytes=0;
stream.on("data", (chunk) => {
bytes += chunk.length;
});
stream.on("end", () => {
#!/bin/bash
#Examples to create docx files with month as input
month=$1
for i in {1..31}
do
touch "2021-${month}-${i}_Nanda.docx"
done
@neos1803
neos1803 / function-script.sql
Created September 13, 2021 08:42
Listen to change in one column on PostgreSQL
create or replace function test_notify()
returns trigger
language plpgsql
as $function$
begin
if ((new.status = '1' and old.status = '0') or (new.status = '0' and old.status = '1')) then
perform pg_notify('testEvent', row_to_json(new)::text);
end if;
return null;
end;
@neos1803
neos1803 / log.js
Created March 4, 2022 02:34
Show complete data in console.log node js
const util = require('util')
console.log(util.inspect(myObject, {showHidden: false, depth: null, colors: true}))
// alternative shortcut
console.log(util.inspect(myObject, false, null, true /* enable colors */))
@neos1803
neos1803 / min-max-sum-array.js
Created March 17, 2022 07:36
Function to return the minimum sum and maximum sum of an array
function minMaxSum(array = []) {
const sorted = array.sort((a, b) => a - b) // Ascended sort
const minSum = sorted.slice(0, array.length/2).reduce((p, c) => p+=c);
const maxSum = sorted.slice(array.length/2, array.length).reduce((p, c) => p+=c);
return [minSum, maxSum]
}
@neos1803
neos1803 / script.js
Created March 18, 2022 04:14
AM/PM to 24-Hour-Based Time Converter Javascript
function amPmConversion(time = "") {
const arr = time.split(":");
const amPm = arr.slice(2,3).join("").split("").slice(2,4).join("");
let converted = ["", arr[1], arr.slice(2,3).join("").split("").slice(0,2).join("")];
if (amPm === "PM") {
if (arr[0] === "12") {
converted[0] = "12"