Instantly share code, notes, and snippets.

View u.ts
function FuckingPredeterminedMethodName(fuckinName: string) {
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
target.__fuckingPredeterminedMethodNameCalled__ = target.__fuckingPredeterminedMethodNameCalled__ || {}
target.__fuckingPredeterminedMethodNameCalled__[fuckinName] = 0
target[fuckinName] = function() {
if (++ target.__fuckingPredeterminedMethodNameCalled__[fuckinName] > 3) {
throw new Error('Stop it!')
} else {
console.warn(`Do not call me ${ fuckinName }.`)
View sample.ts
type ResponseBase = {};
type SuccessResponse = ResponseBase & { data: any };
type ErrorResponse = ResponseBase & { error: string };
type Callback = (response: SuccessResponse | ErrorResponse) => void;
function call(callback: Callback) {
callback(<SuccessResponse> { data: {} });
}
call(response => {
View message-box.js
export default {
/**
* @param {Vue} Vue
* @param {any} options
*/
install(Vue, options) {
/**
* @typedef {object} MessageBox
* @property {boolean} confirmed - To be set true if confirmed, otherwise false
* @property {VueComponent} instance - Instance of VueComponent for $msgbox()
View build-module.js
const path = require('path')
const { spawn } = require('child_process')
const fs = require('fs')
const spawnPromise = (command, args = [], options = {}) => {
return new Promise((resolve, reject) => {
const cp = spawn(command, args, { stdio: 'inherit', ...options })
cp.on('error', error => reject(error))
cp.on('close', (code, signal) => code === 0 ? resolve() : reject(new Error(`Exited with code ${ code }`)))
})
View promise-sample.js
const doAsync1 = () => client.get().then(doSomething1).catch(e => { onError1(e); throw e })
const doAsync2 = () => client.get().then(doSomething2).catch(e => { onError2(e); throw e })
const doAsync3 = () => client.get().then(doSomething3).catch(e => { onError3(e); throw e })
const doAsync4 = () => client.get().then(doSomething4).catch(e => { onError4(e); throw e })
const doAsync5 = () => client.get().then(doSomething5).catch(e => { onError5(e); throw e })
const doAsync6 = () => client.get().then(doSomething6).catch(e => { onError6(e); throw e })
const doAsyncSerial = () => doAsync1().then(() => doAsync2()).then(() => doAsync2()).then(doSomethingSerial).catch(e => { onErrorSerial(e); throw e })
const doAsyncParallel = () => Promise.all([doAsync4, doAsync5, doAsync6]).then(doSomethingParallel).catch(e => { onErrorParallel(e); throw e })
const doAsync = () => doAsyncSerial().then(doAsyncParallel)
View sample.yml
AWSTemplateFormatVersion: 2010-09-09
Description: VPC + EB + RDS (MySQL) + Rails (Puma)
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the bastion host and Elastic Beanstalk hosts
Type: AWS::EC2::KeyPair::KeyName
SSHLocation:
View is_zip.js
function isZip(path, callback) {
fs.open(path, 'r', (error, fd) => {
if (error)
return callback(error)
fs.read(fd, Buffer.alloc(4), 0, 4, 0, (error, bytesRead, buffer) => {
if (error)
return callback(error)
callback(null, buffer.readUInt32LE(0) === 0x04034b50)
View validation-sample.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title></title>
<style>
.errors {
color: red;
}
</style>
View store-main.js
import { ipcMain, BrowserWindow } from 'electron'
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state = {
count: 0
}
View future_and_promise.js
class F extends Promise {
onComplete(callback) {
this.then(callback, callback);
}
}
class P {
constructor() {
this.future = new F((resolve, reject) => {
this.trySuccess = resolve;