Skip to content

Instantly share code, notes, and snippets.

@iinsta
iinsta / ns.js
Last active August 3, 2017 15:05
'use strict';
function GeneratePassword(length) {
var str1='qwertyuioplkjhgfdsazxcvbnm';
var str2='QWERTYUIOPLKJHGFDSAZXCVBNM';
var str3='1234567890';
var str4='!@#$%^&*.,';
var str = [str1,str2,str3,str4].join('')
var res = '';
for (var i=0; i < length; i++) {
@iinsta
iinsta / ReactBasicComponent.js
Last active October 23, 2017 16:36
react async load componet demo
import React from 'react'
export default React
export class BasicComponent extends React.Component {
componentWillUnmount() {
this.willUnmount && this.willUnmount()
this.__unmount = true
}
componentDidMount() {
@iinsta
iinsta / speedtest.js
Last active January 28, 2018 18:22
speed test js
function mitt(all) {
all = all || Object.create(null)
return {
on(type, cb){
(all[type] || (all[type] = [])).push(cb);
},
off(type, cb){
if (all[type]) {
all[type].splice(all[type].indexOf(cb) >>> 0, 1);
}
@iinsta
iinsta / likefetch.js
Last active December 4, 2017 21:28
No fetch :) just XHR, because we can abort the request now!
/*
* usage
*
* componentDidMount() {
* likefetch('www.example.com/api/', {method: 'post', body: JSON.stringify({data: 123})}, c => this.xhr = c)
* }
* componentWillUnmount() {
* this.xhr && this.xhr.abort()
* }
*
@iinsta
iinsta / downloadPictureWithEXIFandRename.js
Created April 22, 2018 15:54
Download photos with EXIF data in browser
var piexif = require('piexif');
/* usage:
downloadPictureWithEXIFandRename('https://images.unsplash.com/photo-1502120492606-fba13cc63721?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=289f912c1f9e61361e64ac022a2a6ccc&auto=format&fit=crop&w=668&q=80', 'hello!')
*/
function downloadPictureWithEXIFandRename(pictureUrl, pictureName, callback) {
var xhr = new XMLHttpRequest();
var a = document.createElement('a');
xhr.responseType = "arraybuffer";
xhr.open('GET', pictureUrl, true);
@iinsta
iinsta / sort&seatch.js
Last active May 12, 2018 06:24
二分查找和快速排序
function swap(arr, i, j) {
[arr[i], arr[j]] = [arr[j], arr[i]]
}
function quickSort(arr, low, high) {
let i, last;
if (low < high) {
last = low;
for (i = low + 1; i <= high; i++) {
@iinsta
iinsta / long-poll.js
Last active June 13, 2018 17:38
long poll pratice
const p = require('polka')();
const debounce = require('lodash/debounce');
p.get('/', (req, res) => {
res.end('hello world')
})
const maxConnections = 10*10000;
const todos = ['hello'];
const users = Object.create(null);
@iinsta
iinsta / redux-better-thunk.js
Created July 21, 2018 04:00
better redux thunk
function getNewState(getState, isOriginal) {
const state = getState();
return isOriginal ? state : object.assign({}, state);
}
function createThunkMiddleware(extraArgument, errorHandler) {
return ({ dispatch, getState }) => next => action => {
if (typeof action === 'function') {
const result = action(dispatch, (isOriginal) => getNewState(getState, isOriginal), extraArgument);
if (errorHandler && result.catch) result.catch(errorHandler);
return result;
@iinsta
iinsta / react 实践小坑.md
Created July 21, 2018 12:47
react 实践小坑

避免直接赋值更改 this.props 传递的值

this.props 传递的值是引用至上层的,如果直接更改,其实也是更改了上层的数据,需要避免,要不然会发生一些很奇怪的意外情况;

@iinsta
iinsta / build-my-own-ui-component-element.md
Last active July 27, 2018 01:58
打造自己的ui组件库起步

常用组件

  • 弹框, 使用 react-modal
  • tooltip 或者 popover ,使用 react-popper 或者基于 popperjs 自己鲁一个;
  • setState 统一使用 react-values 或者 react-powerplug
  • tab 组件,使用 react-tabs
  • 日期组件

常用功能函数