Skip to content

Instantly share code, notes, and snippets.

View yarn-cache-clean.js
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
function findMetaFile(modulePath) {
const name = ".yarn-metadata.json";
function walkDir(dirPath) {
const subFiles = fs.readdirSync(dirPath);
View typescriptreact.json
{
"React_Class": {
"prefix": "comp",
"body": [
"export interface ${TM_FILENAME_BASE}Props {}",
"export interface ${TM_FILENAME_BASE}State {}",
"export class ${TM_FILENAME_BASE} extends React.PureComponent<${TM_FILENAME_BASE}Props, ${TM_FILENAME_BASE}State> {",
" render () {",
" return (",
" <div />",
View Semver2Number.ts
const defaultBitLength = 6
const maxBitLength = 16
function transformSemver2Number (semver: string): string {
const [n1, n2, n3] = semver.split('.').map(Number)
const maxNum = Math.max(n1, n2, n3)
const minBitLength = Math.ceil(Math.log2(maxNum))
const bitLength = Math.max(minBitLength, defaultBitLength)
if (bitLength > maxBitLength) {
throw new Error('semver too big!')
@ekoneko
ekoneko / test.js
Last active Aug 27, 2017
node file sync test
View test.js
const fs = require('fs');
const startTime = Date.now();
console.log('start at', startTime);
setTimeout(() => {
const endTime = Date.now();
console.log('0.01 sec later', endTime);
console.log(endTime - startTime)
}, 10)
View call_vim.js
const child_process = require('child_process');
const editor = process.env.EDITOR || 'vi';
const child = child_process.spawn(editor, ['/tmp/somefile.txt'], {
stdio: 'inherit'
});
child.on('exit', function (e, code) {
console.log("finished");
});
@ekoneko
ekoneko / pre-commit.sh
Last active Jun 14, 2017
ESLint Pre Commit Check
View pre-commit.sh
#!/bin/sh
# ESLint Pre Commit Check
# put to `.git/hooks/pre-commit`
eslint_check() {
files=$(git diff --cached --name-only --diff-filter=AM | grep '\.jsx\?$')
if [[ $files = "" ]] ; then
return
fi
failed=0
@ekoneko
ekoneko / git.sh
Created Apr 14, 2017
commonly cmd
View git.sh
# list git commit message
git log --oneline | head -n 4 | cut -d ' ' -f2-
View async-generator.js
/**
* 一个异步生成器的简单函数实现
* 演示参照下方exec
*/
var async = function (makeGenerator) {
return function () {
var generator = makeGenerator();
var continuer = function (result, value) {
if (result && result.done) {
return;
@ekoneko
ekoneko / paste image
Created Feb 24, 2016
html5 get ctrl + v image
View paste image
paste = (e) => {
var file, remoteImages;
if (typeof e.clipboardData.types === 'undefined' || e.clipboardData.types.length === 0 || e.clipboardData.types[0] !== 'Files') {
return;
}
file = e.clipboardData.items[0].getAsFile();
render(file);
e.stopPropagation();
@ekoneko
ekoneko / 摇一摇
Created Jan 7, 2016
mobile motion test
View 摇一摇
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="count" style="font-size:2em;font-weight=bold;"></div>
</body>
<script>