Javascript – 米诺的后花园 https://minuo.org 书写一段蜂花成蜜的往事…… Thu, 21 Apr 2022 19:23:29 +0000 zh-Hans hourly 1 https://wordpress.org/?v=6.5.8 https://minuo.org/wp-content/uploads/2024/05/minuo-icon-150x150.png Javascript – 米诺的后花园 https://minuo.org 32 32 我这样获取async函数返回值,可行吗? https://minuo.org/get-the-return-value-of-async-function https://minuo.org/get-the-return-value-of-async-function#respond Thu, 21 Apr 2022 19:22:11 +0000 https://minuo.org/?p=996 我这样获取async函数返回值,不知道对不对?

]]>
我这样获取async函数返回值,不知道对不对?

const crawler = async (url) => {
    const response = await axios.get(url);
    if (response.status == 200) {
        return response;
    }
}

crawler(baseUrl)
    .then(res => {
        console.log(res.status);
    }).catch((error) => {
        console.error(error.message)
    });
]]>
https://minuo.org/get-the-return-value-of-async-function/feed 0
一段方便下载国标文档的代码 https://minuo.org/a-code-snippet-convenient-for-downloading-national-standard-documents https://minuo.org/a-code-snippet-convenient-for-downloading-national-standard-documents#comments Sat, 02 Apr 2022 09:28:10 +0000 https://minuo.org/?p=955 国家标准委提供了“国家标准全文公开系统”,在这里可以查看并下载大部分的公开标准文本。

]]>
国家标准委提供了“国家标准全文公开系统”,在这里可以查看并下载大部分的公开标准文本。

但还有一部分标准只能在线浏览,不能直接下载。

国家标准全文公开系统
国家标准全文公开系统(http://openstd.samr.gov.cn/bzgk/gb/index

这个网站下载到的 pdf 文件经过了特殊的加密,需要 Adobe Acrobat 搭配网站提供的“FileOpen”插件(注:插件同时提供了 Windows、Mac 和 Linux 版本)才能打开,且不能复制文本。

当然,只要是能在线阅读,那就一定能获取源文件。

PDFViewerApplication.pdfDocument.saveDocument(PDFViewerApplication.pdfDocument.annotationStorage)
  .then(function (data) {
    PDFViewerApplication.downloadManager.download(new Blob([data], {
      type: "application/pdf"
    }), PDFViewerApplication.baseUrl, PDFViewerApplication._docFilename, "download");
  });

来源:https://xzonn.top/posts/How-To-Download-Chinese-Standard.html

]]>
https://minuo.org/a-code-snippet-convenient-for-downloading-national-standard-documents/feed 3
Javascript代码片段-查询字符串转换为JSON格式 https://minuo.org/javascript-code-snippet-convert-query-string-to-json-format https://minuo.org/javascript-code-snippet-convert-query-string-to-json-format#respond Tue, 29 Mar 2022 01:02:41 +0000 https://minuo.org/?p=946 将查询字符串"price>50,rating>4.5"转换成Json格式{ price: { '$lt': 60 }, rating: { '$gt': 4 } }

]]>
将查询字符串"price>50,rating>4.5"转换成Json格式{ price: { '$lt': 60 }, rating: { '$gt': 4 } }

const numericFilters = "price>50,rating>4.5";
const query = {};

if (numericFilters) {
    const operatorMap = {
      '>': '$gt',
      '>=': '$gte',
      '=': '$eq',
      '<': '$lt',
      '<=': '$lte',
    };
    const regEx = /\b(<|>|>=|=|<|<=)\b/g;
    let filters = numericFilters.replace(
      regEx,
      (match) => `-${operatorMap[match]}-`
    );

    const options = ['price', 'rating'];
    filters = filters.split(',').forEach((item) => {
      const [field, operator, value] = item.split('-');
      if (options.includes(field)) {
        query[field] = { [operator]: Number(value) };
      }
    });
  }

用到Javascriptreplace()方法和forEach()方法。

replace() 方法返回一个由替换值(replacement)替换部分或所有的模式(pattern)匹配项后的新字符串。模式可以是一个字符串或者一个正则表达式,替换值可以是一个字符串或者一个每次匹配都要调用的回调函数。如果pattern是字符串,则仅替换第一个匹配项。

指定一个函数作为第二个参数的情况下,当匹配执行后,该函数就会执行。 函数的返回值作为替换字符串。另外要注意的是,如果第一个参数是正则表达式,并且其为全局匹配模式,那么这个方法将被多次调用,每次匹配都会被调用。

]]>
https://minuo.org/javascript-code-snippet-convert-query-string-to-json-format/feed 0
如何通过包管理工具将Node.JS更新到最新版本(OSX) https://minuo.org/how-to-update-node-js-to-latest-version-osx-using-a-package-manager https://minuo.org/how-to-update-node-js-to-latest-version-osx-using-a-package-manager#respond Thu, 04 Mar 2021 23:31:02 +0000 http://minuo.me/?p=247 Node.Js很快受到欢迎,已成为开发人员最喜欢的语言,用于开发Rest API和构建高扩展性应用程序。

]]>
Node.Js很快受到欢迎,已成为开发人员最喜欢的语言,用于开发Rest API和构建高扩展性应用程序。

以下是在Mac Osx上将Node.JS更新到最新版本的笔记:

  1. 使用npm -v检查已安装npm的版本;
  2. 使用npm install npm@latest -g将其更新为最新版本;
  3. 使用npm -v检查当前npm的版本;
  4. 使用node -v检查 node 的当前版本;
  5. 运行sudo npm cache clean -f代码以清除npm的缓存;
  6. 要更新Node,需要npm的便捷n模块。使用命令sudo npm install -g n安装n模块;
  7. 使用命令sudo n stable安装最新的稳定版本的Node(要安装最新发布的版本,请使用n latest。或者,您可以运行n #.#.#以获取特定的Node版本);
  8. 使用node -v检查 node 的当前版本。

]]>
https://minuo.org/how-to-update-node-js-to-latest-version-osx-using-a-package-manager/feed 0