Skip to content

Instantly share code, notes, and snippets.

View rpn3319's full-sized avatar

Raymond Phu Nguyen rpn3319

  • Ho Chi Minh City, Vietnam
  • 10:09 (UTC +07:00)
  • LinkedIn in/rpn19
View GitHub Profile
@rpn3319
rpn3319 / is_duplicate.js
Created May 20, 2019 10:46
js check if array is duplicate
// Using ES 6, shortest and most readable solution
(new Set(myArray)).size !== myArray.length
// Using Array.some, this is fastest solution
myArray.some((value, index, array) => array.includes(value, index + 1))
// Using filter
passphrases.filter(function (value, index, self) { return self.indexOf(value) === index; }).length !== passphrases.length
// Source for benchmark: https://jsbench.me/mqjvw8exl9
@rpn3319
rpn3319 / sql-add-type-if-not-exist.sql
Last active June 10, 2022 14:36
sql add type if not exist
DO $$ BEGIN
CREATE TYPE public.import_deliveries_delivery_state_enum AS ENUM
('10', '20');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
@rpn3319
rpn3319 / grammar.ts
Last active June 10, 2022 14:02
nodejs custom grammar syntax with chevrotain example
import { createToken, Lexer, CstParser } from 'chevrotain';
const Identifier = createToken({
name: 'Identifier',
pattern: /([\d\.]+|[\d\w]+)/,
});
const Amount = createToken({
name: 'Amount',
pattern: /\d+\.*\d*/,
longer_alt: Identifier,
@rpn3319
rpn3319 / recursive-function.js
Last active June 10, 2022 14:07
nodejs non-blocking promise recursive
async function execute() {
// processing ...
await new Promise((resolve) => {
setTimeout(async () => {
await execute();
resolve();
}, 1000);
});
}
@rpn3319
rpn3319 / jsontostruct.go
Last active June 10, 2022 13:59
golang json to struct example
package main
import (
"encoding/json"
"fmt"
)
type People struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
@rpn3319
rpn3319 / code-review-checklist.md
Last active June 10, 2022 14:44
coding career code review checklist
  • The concept or the WHY we need this PR ?
    • Definition/Motivation/Logic
    • Define my own Test cases and compare with PR's test cases
    • Migration
  • Code review, if any changes effect the logic, waiting for update, then do Code Review again
    • In the quick scan, does any code break the app
    • Need to update environment variables ? if yes, wait for update then check again
    • Need to update package.json, package-lock.json
    • Code base convention: does files structure follow the current code base convention ?
  • Code base Screaming principle: does files name present its main logic ?
@rpn3319
rpn3319 / class_decorator.ts
Last active June 10, 2022 14:30 — forked from remojansen/class_decorator.ts
typescript decorators
function logClass(target: any) {
// save a reference to the original constructor
var original = target;
// a utility function to generate instances of a class
function construct(constructor, args) {
var c : any = function () {
return constructor.apply(this, args);
}
@rpn3319
rpn3319 / ec2-user-data-install-apache-server.sh
Created September 23, 2020 16:01
aws ec2 user data install apache server
#!/bin/bash
# Install Apache server
yum update -y
yum install -y httpd.x86_64
systemctl start httpd.service
systemctl enable httpd.service
echo "Hello world $(hostname -f)" > /var/www/html/index.html
@rpn3319
rpn3319 / jest-e2e.json
Created June 28, 2021 08:35
nestjs jest config coverage & module name mapper
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"verbose": true,
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"moduleNameMapper": {
@rpn3319
rpn3319 / main.tf
Last active June 10, 2022 14:42
terraform aws ec2 web auto scale load balancing
provider "aws" {
region = "ap-southeast-1"
}
variable "server_port" {
description = "The port the server will use for HTTP requests"
type = number
default = 80
}