Skip to content

Instantly share code, notes, and snippets.

@JaeYeopHan
JaeYeopHan / flatten.js
Created February 24, 2018 10:23
JavaScript function utils - Deep Flatten
function flatten(arr) {
return function f(arr, newArr) {
arr.forEach(function(v)) {
Array.isArray(v) ? f(v, newArr) : newArr.push(v);
});
return newArr;
}(arr, []);
}
@JaeYeopHan
JaeYeopHan / requiredParam.js
Created February 26, 2018 01:50
JavaScript code snippets - Required parameter function
export function requiredParam (param) {
const requiredParamError = new Error(
`Required parameter, "${param}" is missing.`
)
// preserve original stack trace
if (typeof Error.captureStackTrace === ‘function’) {
Error.captureStackTrace(
requiredParamError,
requiredParam
)
@JaeYeopHan
JaeYeopHan / pipe.js
Created February 26, 2018 01:52
JavaScript functions - pipe
export function pipe(...fns) {
return param => fns.reduce(
(result, fn) => fn(result),
param
)
}
@JaeYeopHan
JaeYeopHan / getFilterdPair.ts
Created March 8, 2018 07:40
Filtering object by passing checker function to util
export const getFilteredPair = (base: object, checker: (value: any) => boolean): object => {
return Object.keys(base).reduce((acc, key) => {
if (checker(key)) {
acc[key] = base[key];
}
return acc;
}, {});
}
@JaeYeopHan
JaeYeopHan / plugin.js
Last active March 27, 2018 04:54
plugin.js
const minify = require('html-minifier').minify;
const fs = require('fs');
class HtmlMinifierPlugin {
constructor(options) {
this.options = options;
}
apply(compiler) {
compiler.plugin("emit", (compilation, callback) => {
fs.readFile(this.options.template, 'utf-8', (err, html) => {
export const delay = (time: number) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, time);
});
};
@JaeYeopHan
JaeYeopHan / store.js
Created April 20, 2018 04:56
Simple store sample code
class Store {
constructor(state = {}, actions = {}) {
this.state = state;
this.actions = actions;
this.dispatch = (action, data) => {
const newMergedData = this.actions[action](this.state, data);
Object.assign(this.state, newMergedData);
};
}
}
@JaeYeopHan
JaeYeopHan / Marker.ts
Last active April 24, 2018 11:10
Example code to Marker component in post of state management posting
export class Marker extends EventEmitter {
constructor(
private id: number,
) {
super();
document.querySelector("marker").addEventListener("click", () => {
// do something
this.trigger("card_action", this.id);
});
@JaeYeopHan
JaeYeopHan / Card.tsx
Created April 24, 2018 11:16
Example code react version to Card component in post of state management posting
class Card extends React.Component<CardProps> {
do() {
// do something...
}
render() {
const { index } = this.props;
return ( ... );
}
}
@JaeYeopHan
JaeYeopHan / simple_redux.js
Created May 4, 2018 04:52
Sample code of simple redux
const counter = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
}