# yaml

YAML 是一个可读性高,用来表达资料序列化的格式。常用于存储一些配置文件,如:

receipt:     Oz-Ware Purchase Invoice
date:        2012-08-06
customer:
    given:   Dorothy
    family:  Gale
   
items:
    - part_no:   A4786
      descrip:   Water Bucket (Filled)
      price:     1.47
      quantity:  4

    - part_no:   E1628
      descrip:   High Heeled "Ruby" Slippers
      size:      8
      price:     133.7
      quantity:  1

Yaml 和 json 可以互相转换(在线转换工具 (opens new window)),其实可以把 yaml 看成 json 文件的变形,其中短横线看做为数组,其他看作为对象。上面的 yaml 可以转换成如下 json:

{
    "receipt": "Oz-Ware Purchase Invoice",
    "date": "2012-08-06T00:00:00.000Z",
    "customer": {
        "given": "Dorothy",
        "family": "Gale"
    },
    "items": [
        {
            "part_no": "A4786",
            "descrip": "Water Bucket (Filled)",
            "price": 1.47,
            "quantity": 4
        },
        {
            "part_no": "E1628",
            "descrip": "High Heeled \"Ruby\" Slippers",
            "size": 8,
            "price": 133.7,
            "quantity": 1
        }
    ]
}

npm 包 yaml (opens new window) 也可实现该功能:

import fs from 'fs'
import YAML from 'yaml'

const file = fs.readFileSync('./file.yml', 'utf8')
YAML.parse(file)

举一个场景,后端提供了一份 yaml 文件的 url,前端如何将其下载并读取内容呢?

// 根据获取 yaml 配置文件并转 json
function getYaml(url) {
  let that = this;
  let fileUrl = url;
  let xhr = new XMLHttpRequest();
  xhr.open('get', fileUrl, true);
  xhr.responseType = 'blob';
  xhr.onload = function () {
    if (this.status === 200) {
      const reader = new FileReader();
      reader.readAsText(this.response);
      reader.onload = function () {
        that.json = yaml.parse(reader.result);
        that.parseJson(that.json);
      };
    }
  };
  xhr.send();
}

使用 axios 封装一下:

这里有两层异步操作,第一步是 axios.get,第二步是文件读取 reader

第二层利用 Promise 封装,使用 async / await 来得到 最终结果。

function downloadYaml(url) {
  return axios.get(url, {
      responseType: 'blob',
    })
    .then(res => {
      return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsText(res.data);
        reader.onload = function () {
          resolve(reader.result);
        };
      });
    })
});

async function getYaml(url){
  const res = await downloadYaml(url);
  this.json = yaml.parse(res);
  this.parseJson(this.json);
}

getYaml(url);

// 简化后相当于
function a() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(111)
        }, 1000)
    }).then(() => {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve(222)
            }, 1000)
        })
    })
}

async function b() {
    const x = await a();
    console.log(x) // 222
}

b();

请求的时候我们可以看到,请求文件的响应类型为 application/octet-stream (opens new window),是二进制数据流的文件。

最后更新时间: 5/19/2022, 8:46:54 PM