recodes
git pre-commit(code git commit eslint) git 代码提交eslint
npm i pre-commit -D
to `package.json`, if pre-commit no lint(无效时,如下)
scpripts: {
"precommit-msg": "echo 'Pre-commit checks...' && exit 0"
},
"pre-commit": [ "precommit-msg", "lint" ]
/// eslint
"lint": "eslint --ext .js,.jsx src/"
vue-cli 官方git-hooks
npm i lint-staged -D;
"gitHooks": {
"pre-commit": "lint-staged"
},
"lint-staged": {
"src/**/*.{js,vue,less}": [
"vue-cli-service lint",
"git add"
]
},
git submodule 子模块(可用来代码复用)
git submodule add `url`
》添加到指定路径
git submodule add `url` [path] [ex: src/tool]
》其他人,clone this project
git clone --recurse-submodules `url`
`
如下操作也ok
1, git clone `url`
2, git submodule init
3, git submodule update
`
web常用正侧集合
export const phone = /^1\d{10}$/
export const IDCard = /^\d{17}[\dXx]$/
// 中英文
export const zhEN = /^[a-zA-Z\u4e00-\u9fa5]+$/
// 中英文数字
export const zhEnNum = /^[a-zA-Z0-9\u4e00-\u9fa5]+$/
export const email = /^[A-Za-z0-9._%-]+@([A-Za-z0-9-]+\.)+[A-Za-z]{2,4}$/
export const Num = /^[0-9]+$/
// 国内座机号码
export const tel = /^\d{3,4}-\d{7,8}$/
interface FunctionType {
(...arg: any[]): any
}
type objType = {
[key: string]: any
}
export function deepCopy (original: any): any {
const dataType = Object.prototype.toString.call(original)
const isArray = dataType.includes('Array')
const o: any = isArray ? [] : {}
if (isArray || dataType.includes('Object')) {
const len = isArray ? original.length : Object.keys(original).length
for (let i = 0; i < len; i++) {
const k = isArray ? i : Object.keys(original)[i]
o[k] = deepCopy(original[k])
}
return o
}
return original
}
function debounce (fn: FunctionType, ts = 300, once = false): FunctionType {
let t: any = null
return function (this: any, ...args: any[]) {
clearTimeout(t)
const self = this
if (once) {
fn.apply(self, args)
return
}
t = setTimeout(() => {
fn.apply(self, args)
}, ts)
}
}
ts如果找不到某个dom,window属性
在src目录下新建typings.d.ts(或者根目录下);
并按如下定义
declare interface Window {
__wxjs_environment: any,
onBMapCallback: any,
BMap: any,
onWyDunCallback: any,
initNECaptcha: any,
wx: any
}
declare interface HTMLScriptElement {
onreadystatechange: any,
readyState: any
}
一个算法
- res = [1,2,3,4,7,8,9,11,12] 数组里就是用户选择的时间点;期望在页面上显示为字符串类型: 1-4点,7-9点,11-12点这种形式
const dealTime = res => {
const arr = []
let str = []
for (let i = 0; i < res.length; i++) {
if (res[i + 1] - res[i] === 1) {
if (!str.includes(res[i])) {
str.push(res[i])
}
str.push(res[i + 1])
} else {
arr.push(str.join(','))
str = []
}
}
return arr
}
检查给定的字符串是否是同构的
export const isSame = (s1,s2) => {
if (s1.length !== s2.length) return false
const strMap = {}
for (let i = 0; i < s1.length; i++) {
let [sA, sB] = [s1[i], s2[i]]
if (!strMap[sA]) {
strMap[sA] = sB
}
if (strMap[sA] !== sB) return false
}
return true
}
//方法2 indexOf()总是返回存在元素首次出现位置的索引index.
const isIsomorphic = function(s, t) {
for (let i = 0; i < s.length; i++) {
if(s.indexOf(s[i]) !== t.indexOf(t[i])) return false
}
return true
};
// 匹配有效的括号
export const vailSymbol = value => {
if (!value) return false
const aS = []
for (let i = 0; i < value.length; i++) {
if ('([{'.includes(value[i])) {
aS.push(value[i])
continue
}
const sl = aS.slice(-1) + value[i]
if (sl.length < 2) return false
if ('(){}[]'.includes(sl)) aS.pop()
}
return !aS.length
}
devServer config
devServer: {
clientLogLevel: 'warning', // eslint错误打印到控制台
proxy: {},
// 如果为 true ,在浏览器上全屏显示编译的errors或warnings。默认 false (关闭)
overlay: {
errors:true,
warnings:false
}
}
sort 两数和 被波拉契数列/爬楼第
// sort 冒泡
export const sort = a => {
if (!a) {
throw '需要一个数组参数'
}
console.log(a)
for (let i = 0; i < a.length - 1; i++) {
for (let j = i; j < a.length - 1 - i; j++) {
let x = a[j]
let y = a[j + 1]
if (x > y) {
const tem = a[j]
a[j] = a[j + 1]
a[j + 1] = tem
}
}
}
console.log(a)
}
// 楼梯走法
export const paLou = function paLou(n) {
const arr = []
arr[1] = 1
arr[2] = 2
if (n < 3) return n
if (n >= 3) {
for (let i = 3; i <= n; i++) {
arr[i] = arr[i - 1] + arr[i - 2]
}
}
return arr[n]
}
// 任意两数和
export const twoSum = function (arr, target) {
for (let i = 0; i < arr.length; i++) {
let tmp = target - arr[i]
if (tmp == arr[i]) { continue }
if (arr.indexOf(tmp) > -1) {
return [i, arr.indexOf(tmp)]
}
}
}
占位
1
2
3
4
5
6
7
8
9