0%

网上找到原因了:**StackOverFlow**

iPadPro reports navigator.platform the browser as ‘MacIntel’, but that is the same as other platforms.

解决方法

判断是不是多点触控 navigator.maxTouchPoints

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function isIOS() {
if (/iPad|iPhone|iPod/.test(navigator.platform)) {
return true;
} else {
return (
navigator.maxTouchPoints &&
navigator.maxTouchPoints > 2 &&
/MacIntel/.test(navigator.platform)
);
}
}

function isIpadOS() {
return (
navigator.maxTouchPoints &&
navigator.maxTouchPoints > 2 &&
/MacIntel/.test(navigator.platform)
);
}

问题

初始化的 vant 项目中 ts 想用 any,eslint 插什么手不让用(no-explicit-any)?

解决问题

解法方法很简单,添加下面规则

1
2
3
rules: {
"@typescript-eslint/no-explicit-any": "off"
},

Git 拉取代码的时候总是自动转换成 CRLF,虽然提交的时候变成 LF 了,但是拉取下来变成 CRLF 令人烦不胜烦。

全局设置

直接使用命令设置

1
git config --global core.autocrlf input

说明

1
2
3
4
5
6
7
8
# 提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true

# 提交时转换为LF,检出时不转换
git config --global core.autocrlf input

# 提交检出均不转换
git config --global core.autocrlf false

后记

这篇文章不错可以看看
https://stackoverflow.com/questions/1967370/git-replacing-lf-with-crlf

iOS 点击 input 输入框时页面会放大,查了一下,加个meta就可以解决问题了

代码

1
<meta name="viewport" content="width=device-width, initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">

width=device-width:表示宽度是设备屏幕的宽度
height=device-height:表示高度是设备屏幕的高度
initial-scale - 初始的缩放比例
minimum-scale - 允许用户缩放到的最小比例
maximum-scale - 允许用户缩放到的最大比例
user-scalable - 用户是否可以手动缩放

后记

代码转载自:https://www.jianshu.com/p/fe40f8c46120

需求

富文本编辑器包含 html 标签,期望去掉 html 标签只保留文本

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function extractContent(s) {
var span = document.createElement('span');
span.innerHTML = s;
return span.textContent || span.innerText;
};
const str = `<!DOCTYPE HTML>
<html>
<head>
<title>#Persistent</title>
<meta http-equiv=\\"Content-Type\\" content=\\"text/html; charset=iso-8859-1\\">
<meta http-equiv=\\"X-UA-Compatible\\" content=\\"IE=edge\\">
<link href=\\"../static/theme.css\\" rel=\\"stylesheet\\" type=\\"text/css\\"/>
<script src=\\"../static/content.js\\" type=\\"text/javascript\\"></script>
</head>
<body>
</html>
`;
extractContent(str)
阅读全文 »

需求

富文本编辑器包含 html 标签,期望去掉 html 标签只保留文本

正则

1
<.+?>

js 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const regex = /<.+?>/gm;
const str = `<!DOCTYPE HTML>
<html>
<head>
<title>#Persistent</title>
<meta http-equiv=\\"Content-Type\\" content=\\"text/html; charset=iso-8859-1\\">
<meta http-equiv=\\"X-UA-Compatible\\" content=\\"IE=edge\\">
<link href=\\"../static/theme.css\\" rel=\\"stylesheet\\" type=\\"text/css\\"/>
<script src=\\"../static/content.js\\" type=\\"text/javascript\\"></script>
</head>
<body>
</html>
`;
str.replace(regex, '')
阅读全文 »

创建一个这样的数组:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[ ...Array(N).keys() ].map( i => i+1);

Array(N).fill().map((_, i) => i+1);

Array.from(Array(N), (_, i) => i+1)

Array.from({ length: N }, (_, i) => i+1)

Array.from({length: 5}, (v, k) => k+1);
// [1,2,3,4,5]

Array.from(Array(10).keys())
//=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[...Array(10).keys()]
//=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Array.from({length: 10}, (_, i) => i + 1)
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

var N = 10;
Array.apply(null, {length: N}).map(Number.call, Number)
// result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Array.apply(null, {length: N}).map(Function.call, Math.random)
// result: [0.7082694901619107, 0.9572225909214467, 0.8586748542729765, 0.8653848143294454, 0.008339877473190427, 0.9911756622605026, 0.8133423360995948, 0.8377588465809822, 0.5577575915958732, 0.16363654541783035]

后记

代码都是从网上抄下来的,Mark一下
https://stackoverflow.com/questions/3746725/how-to-create-an-array-containing-1-n

最近在写一个 vscode 插件,需要参考一下 CODELF 的功能,看了源码之后提取出其中核心部分代码,nodejs 下,网页端都可以跑

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const axios = require('axios');
let variableRepoMapping = {};
axios
.get('https://searchcode.com/api/codesearch_I/?q=soup&p=1&per_page=100')
.then((res) => {
console.log(`res`, res);
let { data } = res;
let { results } = data;
console.log(`parseVariableList`, parseVariableList(results, 'soup'));
});

function getKeyWordReg(keyword) {
return new RegExp(
'([\\-_\\w\\d\\/\\$]{0,}){0,1}' + keyword + '([\\-_\\w\\d\\$]{0,}){0,1}',
'gi'
);
}

function getKeyWroddRegs(keywords) {
return keywords.split(' ').reduce((accumulator, curr) => {
if (curr.length && curr.length > 1) {
return accumulator.concat(getKeyWordReg(curr));
}
return accumulator;
}, []);
}

function parseVariableList(results, keywords) {
let vals = [],
variables = [];
results.forEach((res) => {
res.repo = res.repo.replace('git://github.com', 'https://github.com');
//filter codes
const lineStr = Object.keys(res.lines)
.reduce((accu, line) => {
let lstr = res.lines[line];
//no base64
if (!(/;base64,/g.test(lstr) && lstr.length > 256)) {
return accu.concat(lstr);
}
return accu;
}, [])
.join('')
.replace(/\r\n/g, ' '); // remove \r\n
//match variables
getKeyWroddRegs(keywords).forEach((reg) => {
(lineStr.match(reg) || []).forEach((val) => {
//remove "-" and "/" from the start and the end
val = val.replace(/^(\-|\/)*/, '').replace(/(\-|\/)*$/, '');
updateVariableRepoMapping(val, res);
if (
!/\//g.test(val) /*exclude links*/ &&
vals.indexOf(val) === -1 &&
vals.indexOf(val.toLowerCase()) === -1 &&
vals.indexOf(val.toUpperCase()) === -1 &&
val.length < 64 /*too long*/
) {
vals.push(val);
variables.push({
keyword: val,
repoLink: res.repo,
repoLang: res.language,
});
}
});
});
});
return variables.map((val) => {
val.repoList = getVariableRepoMapping(val.keyword);
return val;
});
}

function updateVariableRepoMapping(val, repo) {
if (!/\//g.test(val) /*exclude links*/ && val.length < 64 /*too long*/) {
val = `__${val.toLowerCase()}`;
variableRepoMapping[val] = variableRepoMapping[val] || [];
if (!variableRepoMapping[val].find((key) => key.id == repo.id)) {
repo.lines = null;
delete repo.lines;
variableRepoMapping[val].push(repo);
}
}
}

function getVariableRepoMapping(val) {
val = `__${val.toLowerCase()}`;
return variableRepoMapping[val];
}

function isZH(val) {
let isZH = false;
val
.replace(/\s+/gi, '+')
.split('+')
.forEach((key) => {
if (/[^\x00-\xff]/gi.test(key)) {
isZH = true;
}
});
return isZH;
}

array –> set

1
2
let array = [1, 2, 3, 4];
let set = new Set(array);

set –> array

1
2
let demo = new Set([1, 2, 3, 4]);
Array.from(demo);

递归一个对象,拿到里面的key,并且按层次输出到数组中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 输入
let input = {
a: {
b: '123',
c: {
d: 456,
},
},
b: {
e: 789,
},
f: 222,
};

// 输出
let output = ['a.b', 'a.c.d', 'b.e', 'f'];

解决方法

一点也不高明的递归方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function deepTree(tree) {
let arr = [];
for (const key in tree) {
if (tree.hasOwnProperty.call(tree, key)) {
const element = tree[key];
if (Object.prototype.toString.call(element) == '[object Object]') {
deepTree(element).forEach((element) => {
arr.push(`${key}.${element}`);
});
} else {
arr.push(`${key}`);
}
}
}
return arr;
}

console.log(`deepTree(input)`, deepTree(input))

来自德巨的分享

群友分享了广度优先遍历和深度优先遍历

阅读全文 »