Skip to content

Instantly share code, notes, and snippets.

View .markdown
View .markdown
View 2.markdown
@Ai01
Ai01 / reactComponentTemplate
Last active Oct 29, 2018
react component template
View reactComponentTemplate
import React, { Component } from 'react';
import PropTypes from 'porp-types';
class Template extends Component {
state = {}
render(){
return (<div>template</div>);
}
}
View getVariableType
const getVariableType = (v) => {
const TYPES = {
undefined: 'undefined',
number: 'number',
boolean: 'boolean',
string: 'string',
'[object Function]': 'function',
'[object RegExp]': 'regexp',
'[object Array]': 'array',
'[object Date]': 'date',
@Ai01
Ai01 / Unflatten
Created Sep 6, 2017
将平级数据变为树结构
View Unflatten
// 将平级数据变为树结构
const arr = [{
id:1,
parent: null,
},{
id:2,
parent: null,
},{
id: 3,
parent: {id:1}
View map two item once
const arr = {
children: [1,2,3]
};
arr.childrenGroup = [];
arr.children.reduce((r, e, index) => {
const lenghtOfChildren = qcSort.children.length;
if (index % 2 === 1) {
arr.childrenGroup.push([r,e]);
} else if (lenghtOfChildren % 2 === 1 && index === lenghtOfChildren - 1) {
arr.childrenGroup.push([e]);
@Ai01
Ai01 / fetchApi
Last active Aug 8, 2017
fetchApi
View fetchApi
//get
fetch('url').then((res)=>{
if(res.ok){
res.json.then((data)=>{
console.log(data);
})
}
}).catch((err)=>{
console.log(err);
})
@Ai01
Ai01 / jsNumberCal
Last active Aug 2, 2017
js浮点数计算
View jsNumberCal
// js浮点数精确计算。需要在支持static method和class的js环境中运行
// 可以拆分为4个方法(在不支持class和static method)的环境中
// 需要继续完善的是如果x,y中存在几十位的小数那么可能超过安全数字
class Calculate {
// x,y是运算数字,n是精度
static add(x, y, n) {
const _arg1 = x.toString();
const _arg2 = y.toString();
const arg1Arr = _arg1.split('.');