修复fromt matter工具bug
This commit is contained in:
parent
10a439427f
commit
1278fed56a
|
|
@ -19,7 +19,7 @@ module.exports = {
|
|||
|
||||
"/05.关于/": [["01.关于.md","关于","/about"]],
|
||||
|
||||
"/06.收藏夹/": [["01.网站和工具.md","网站和工具","/bookmarks"],["02.库.md","库","/pages/90f6badc8a904646"]],
|
||||
"/06.收藏夹/": [["01.网站和工具.md","网站和工具","/bookmarks"],["02.库.md","库","/pages/90f6badc8a904646"],["03.库2.md","库2","/pages/82d92771ca361ac3"]],
|
||||
|
||||
"/07.时间轴/": [["01.时间轴.md","时间轴","/timeline"]],
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,78 @@
|
|||
---
|
||||
title: 比typeof运算符更准确的类型判断
|
||||
date: 2020-04-13 15:56:54
|
||||
permalink: /pages/fd4a16d56b83c1bc
|
||||
---
|
||||
# 比typeof运算符更准确的类型判断
|
||||
|
||||
不同数据类型的`Object.prototype.toString`方法返回值如下。
|
||||
|
||||
- 数值:返回`[object Number]`。
|
||||
- 字符串:返回`[object String]`。
|
||||
- 布尔值:返回`[object Boolean]`。
|
||||
- undefined:返回`[object Undefined]`。
|
||||
- null:返回`[object Null]`。
|
||||
- 数组:返回`[object Array]`。
|
||||
- arguments 对象:返回`[object Arguments]`。
|
||||
- 函数:返回`[object Function]`。
|
||||
- Error 对象:返回`[object Error]`。
|
||||
- Date 对象:返回`[object Date]`。
|
||||
- RegExp 对象:返回`[object RegExp]`。
|
||||
- 其他对象:返回`[object Object]`。
|
||||
|
||||
这就是说,`Object.prototype.toString`可以看出一个值到底是什么类型。
|
||||
|
||||
```js
|
||||
Object.prototype.toString.call(2) // "[object Number]"
|
||||
Object.prototype.toString.call('') // "[object String]"
|
||||
Object.prototype.toString.call(true) // "[object Boolean]"
|
||||
Object.prototype.toString.call(undefined) // "[object Undefined]"
|
||||
Object.prototype.toString.call(null) // "[object Null]"
|
||||
Object.prototype.toString.call(Math) // "[object Math]"
|
||||
Object.prototype.toString.call({}) // "[object Object]"
|
||||
Object.prototype.toString.call([]) // "[object Array]"
|
||||
```
|
||||
|
||||
利用这个特性,可以写出一个比`typeof`运算符更准确的类型判断函数。
|
||||
|
||||
```js
|
||||
var type = function (o){
|
||||
var s = Object.prototype.toString.call(o)
|
||||
return s.match(/\[object (.*?)\]/)[1].toLowerCase()
|
||||
}
|
||||
type({}); // "object"
|
||||
type([]); // "array"
|
||||
type(5); // "number"
|
||||
type(null); // "null"
|
||||
type(); // "undefined"
|
||||
type(/abcd/); // "regex"
|
||||
type(new Date()); // "date"
|
||||
```
|
||||
|
||||
在上面这个`type`函数的基础上,还可以加上专门判断某种类型数据的方法。
|
||||
|
||||
```js
|
||||
var type = function (o){
|
||||
var s = Object.prototype.toString.call(o);
|
||||
return s.match(/\[object (.*?)\]/)[1].toLowerCase();
|
||||
};
|
||||
|
||||
['Null',
|
||||
'Undefined',
|
||||
'Object',
|
||||
'Array',
|
||||
'String',
|
||||
'Number',
|
||||
'Boolean',
|
||||
'Function',
|
||||
'RegExp'
|
||||
].forEach(function (t) {
|
||||
type['is' + t] = function (o) {
|
||||
return type(o) === t.toLowerCase();
|
||||
};
|
||||
});
|
||||
|
||||
type.isObject({}) // true
|
||||
type.isNumber(NaN) // true
|
||||
type.isRegExp(/abc/) // true
|
||||
```
|
||||
|
|
@ -61,7 +61,7 @@ article: false
|
|||
### Emoji表情
|
||||
* [emoji表情](https://emojipedia.org/)
|
||||
* [emoji表情备忘录](https://www.webfx.com/tools/emoji-cheat-sheet)
|
||||
> window系统下按<kbd>Win</kbd>+<kbd>.</kbd>快速打开表情选择框
|
||||
> windows系统下按<kbd>Win</kbd>+<kbd>.</kbd>快速打开表情选择框
|
||||
|
||||
### 图片压缩
|
||||
* [tinypng图片压缩](https://tinypng.com) 压缩png很有用
|
||||
|
|
@ -72,6 +72,7 @@ article: false
|
|||
* [百度脑图](https://naotu.baidu.com) 思维导图
|
||||
|
||||
### CSS相关
|
||||
* [CSS生成器](https://neumorphism.io/)
|
||||
* [CSS渐变生成器](https://www.colorzilla.com/gradient-editor/)
|
||||
* [CSS3-Box Shadow(阴影)](https://www.html.cn/tool/css3Preview/Box-Shadow.html)
|
||||
* [贝塞尔曲线生成器 ](https://cubic-bezier.com)
|
||||
|
|
@ -79,8 +80,11 @@ article: false
|
|||
### CDN加速
|
||||
* [jsDelivr](http://www.jsdelivr.com/) 国外的一家优秀的公共 CDN 服务提供商
|
||||
|
||||
### 其他
|
||||
* [Linux命令手册](https://ipcmen.com/)
|
||||
### 正则
|
||||
* [正则可视化](https://regex101.com/)
|
||||
|
||||
### 其他
|
||||
* [Linux命令手册](https://ipcmen.com/)
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@
|
|||
color #888
|
||||
border-radius 50%
|
||||
padding 0
|
||||
transition all .2s ease
|
||||
transition left .2s ease
|
||||
&:hover
|
||||
box-shadow 0 2px 6px rgba(0,0,0,.15)
|
||||
.icon
|
||||
|
|
|
|||
|
|
@ -53,7 +53,10 @@ function main() {
|
|||
}
|
||||
|
||||
if (mark) {
|
||||
const newData = YAML.stringify(matterData) + '---\r\n' + fileMatterObj.content;
|
||||
if(matterData.date && type(matterData.date) === 'date') {
|
||||
matterData.date = repairDate(matterData.date) // 修复时间格式
|
||||
}
|
||||
const newData = YAML.stringify(matterData).replace(/\n\s{2}/g,"\n") + '---\r\n' + fileMatterObj.content;
|
||||
fs.writeFileSync(file.filePath, newData); // 写入
|
||||
console.log(`update FrontMatter:${file.filePath} `)
|
||||
}
|
||||
|
|
@ -66,8 +69,7 @@ function main() {
|
|||
matterData.permalink = file.permalink;
|
||||
}
|
||||
// 修复date时区和格式被修改的问题 (并非更新date的值)
|
||||
const date = new Date(matterData.date);
|
||||
matterData.date = `${date.getUTCFullYear()}-${zero(date.getUTCMonth()+1)}-${zero(date.getUTCDate())} ${zero(date.getUTCHours())}:${zero(date.getUTCMinutes())}:${zero(date.getUTCSeconds())}`;
|
||||
matterData.date = repairDate(matterData.date);
|
||||
|
||||
const newData2 = YAML.stringify(JSON.parse(JSON.stringify(matterData))) + '---\r\n' + fileMatterObj.content;
|
||||
fs.writeFileSync(file.filePath, newData2); // 写入
|
||||
|
|
@ -80,6 +82,18 @@ function main() {
|
|||
|
||||
}
|
||||
|
||||
// 类型判断
|
||||
function type(o){
|
||||
var s = Object.prototype.toString.call(o)
|
||||
return s.match(/\[object (.*?)\]/)[1].toLowerCase()
|
||||
}
|
||||
|
||||
// 修复date时区格式的问题
|
||||
function repairDate(date) {
|
||||
date = new Date(date);
|
||||
return `${date.getUTCFullYear()}-${zero(date.getUTCMonth()+1)}-${zero(date.getUTCDate())} ${zero(date.getUTCHours())}:${zero(date.getUTCMinutes())}:${zero(date.getUTCSeconds())}`;
|
||||
}
|
||||
|
||||
// 日期的格式
|
||||
function dateFormat(date) {
|
||||
return `${date.getFullYear()}-${zero(date.getMonth()+1)}-${zero(date.getDate())} ${zero(date.getHours())}:${zero(date.getMinutes())}:${zero(date.getSeconds())}`
|
||||
|
|
|
|||
Loading…
Reference in New Issue