Skip to content

Instantly share code, notes, and snippets.

View peatiscoding's full-sized avatar
🏠
Working from home

peatiscoding peatiscoding

🏠
Working from home
View GitHub Profile
@peatiscoding
peatiscoding / prisoner-by-kasidid.ts
Created October 15, 2023 02:41
Implementation of the Prisoner
import { shuffle } from 'lodash'
export interface Box {
/**
* Value written on top of the Box
*/
boxIndex: number
/**
* Value written inside the Box
@peatiscoding
peatiscoding / impls.ts
Last active August 11, 2023 07:56
Modularized Dependency Injection using JavaScript's Proxy Object
export type InstanceCreator<S, T> = (others: Readonly<S>) => T
export type InstanceCreators<S> = {
[K in keyof S]: InstanceCreator<S, S[K]>
}
/**
* Singleton implementation provider
*
* Example Usage:
@peatiscoding
peatiscoding / assertThaiId.ts
Created September 4, 2021 00:14
Validate Thai Id (checksum) using TypeScript
const assertThaiId = (thaiId: string): boolean => {
const m = thaiId.match(/(\d{12})(\d)/)
if (!m) {
console.warn('Bad input from user, invalid thaiId=', thaiId)
throw new Error('thai-id-must-be-13-digits')
}
const digits = m[1].split('');
const sum = digits.reduce((total: number, digit: string, i: number) => {
return total + (13 - i) * +digit;
}, 0)
@peatiscoding
peatiscoding / nuxt.config.js
Last active August 9, 2020 02:19
A Fixing nuxt SSR when the proxy do not pass over the complete URL path to your service.
const buildTimeConfig = {
base: process.env.APP_BASE_PATH || '/',
}
export default {
env: {
...buildTimeConfig
},
serverMiddleware: [
{
@peatiscoding
peatiscoding / aes-256-gcm-tunnel.ts
Last active July 18, 2020 08:46
Example usage of AES128 to encrypt/decrypt message with GCM
import { createDecipheriv, randomBytes, CipherGCM, HexBase64BinaryEncoding, createCipheriv, Decipher, pbkdf2Sync } from 'crypto'
const gcmTagSize = 16
const nonceSize = 12
export default class Tunnel {
private getDecipher: (iv: Buffer, authTag: Buffer) => Decipher
private getCipher: (iv: Buffer) => CipherGCM
@peatiscoding
peatiscoding / create_jks.sh
Created February 2, 2019 09:19
A Shell script to convert Certificate in PEM + private key to JKS
#!/bin/sh
# Before start make sure you have created a CSR and issued a CRT files from Server.
#
# openssl req -new -newkey rsa:2048 -nodes -keyout private.key -out ${1}.csr
#
# unzip ${1}.zip
# cat ${1}.crt > ${1}.pem
# cat ${1}.ca-bundle >> ${1}.pem
import * as React from 'react'
import { RootState } from '../store'
import { connect } from 'react-redux'
import { login } from '../store/session/actions'
import { AccessToken } from '../store/session/reducers'
import { ThunkDispatch } from 'redux-thunk'
interface State {
}
import * as React from 'react'
import { BrowserRouter as Router, Route } from 'react-router-dom'
import { Provider } from 'react-redux'
import store from './store'
import Dashboard from './pages/Dashboard'
import Login from './pages/Login'
// Style
import './App.css';
// store/session/actions.ts
import { AnyAction } from 'redux';
export interface SetAction {
type: 'SET'
accessToken: string
}
export interface Fetching {
@peatiscoding
peatiscoding / youtube-video-ids.js
Created August 23, 2018 11:51
(JS) Regular Expression to match youtube video links
const rgx = /(?:https?:)?\/{2}(?:www\.)?youtu\.?be(?:\/|\.com\/watch\?v\=|\.com\/v\/|\.com\/embed\/)?([\w-]*)[?&]?.*/
// Support:
// - Simple = https://www.youtube.com/watch?v=I2aC297uaZ4&t=120
// - Short = https://youtube.com/embed/_fIABbpAsjQ?t=120
// - Shorter = https://youtu.be/I2aC297uaZ4?t=9
// - Even Shorter = //youtu.be/I2aC297uaZ4?t=9
// TEST:
[