Skip to content

Instantly share code, notes, and snippets.

View choegyumin's full-sized avatar

Gyumin Choi (Alvin) choegyumin

View GitHub Profile
@choegyumin
choegyumin / APIError.ts
Last active April 5, 2023 09:30
`HTTPError` (Not Working!)
/** Server-side */
interface APIErrorDTO {
code: string;
message: string;
}
/** @todo Fix types */
interface APIErrorArguments {
error: unknown;
status: number;
@choegyumin
choegyumin / maybe.ts
Created November 20, 2019 08:06
TypeScript Monads (Maybe, Just, Nothing)
class Maybe<T> {
public value: T | null | undefined;
constructor(value?: T) {
this.value = value;
}
public fmap<R>(transform: (value: T) => R | null | undefined): Just<R> | Nothing {
if (this.value === undefined || this.value === null) { return new Nothing(); }
return Maybe.fromNullable<R>(transform(this.value));
@choegyumin
choegyumin / event.namespace.js
Last active April 27, 2021 11:07
Namespacing Event Listener in Vanilla Javascript (:see_no_evil: Monkey Patch)
(function () {
'use strict';
EventTarget.prototype.on = function (event, listener, options) {
if (arguments.length < 2)
throw new TypeError("Failed to execute 'on' on 'EventTarget': 2 arguments required, but only " + arguments.length + " present.");
var typeOfListener = typeof listener;
if (typeOfListener !== 'function' && typeOfListener !== 'object')
throw new TypeError("Failed to execute 'on' on 'EventTarget': The callback provided as parameter 2 is not an object.");
@choegyumin
choegyumin / date.extend.js
Last active April 27, 2021 11:07
Extending feature (add, format) for Date object in Vanilla Javascript (:see_no_evil: Monkey Patch)
(function () {
'use strict';
var MONTHS_NAME = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var DAYS_NAME = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; // Weekdays
var DEFAULT_FORMAT = 'YYYY-MM-DDTHH:mm:ssZ';
var FORMAT_REGEX = /\[.*?\]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g;
var STRIP_BRACKETS_REGEX = /\[|\]/g;
Date.prototype.addDate = function (value) { // Day
@choegyumin
choegyumin / html-parser.js
Last active September 19, 2018 12:58
CodeSpitz 3기 3회차 과제
const elementProperties = (input) => {
input = input.trim();
const hasAttrs = input.indexOf(' ') + 1;
if (!hasAttrs) return {name: input, attributes: {}};
const inputAttrs = input.substring(hasAttrs, input.length).trim();
const length = inputAttrs.length;
const attrs = {};
let i = 0;
while (i < length) {
const start = i;
@choegyumin
choegyumin / multiplication-table.js
Last active September 14, 2018 08:42
CodeSpitz 3기 2회차 과제
const generator = function*(i, j) {
for (let a = 1; a <= i; a++) {
for (let b = 1; b <= j; b++) yield [a, b, a * b];
}
};
for (const [i, j, k] of generator(9, 9)) {
console.log(`${i} x ${j} = ${k}`);
}