NPM酷庫045 js yaml,讀寫YAML

2021-09-14 02:27:22 字數 1289 閱讀 1018

npm酷庫,每天兩分鐘,了解乙個流行npm庫。·

在npm酷庫044中,我們了解到了相對於json格式更加易於編寫和維護的json5,今天我們繼續學習另外乙個更加簡單易讀的資料格式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

bill-to: &id001 # 錨點標記 id001

street: | # 多行字串

123 tornado alley

suite 16

city: east centerville

state: ks

ship-to: *id001 # 引用錨點標記id001的資料

specialdelivery: > # 多行字串

follow the yellow brick

road to the emerald city.

pay no attention to the

man behind the curtain.

...

yaml格式能夠和json格式互相轉換,yaml格式相對於json更加易於人類編寫和理解,所以更適合替代json用來編寫配置檔案。

js-yaml 是乙個專門用來讀寫yaml格式資料的庫,他可以將js物件轉換成yaml字串,也可以將yaml字串轉換為js物件。

const yaml = require('js-yaml');

const fs = require('fs');

let obj = yaml.safeload(fs.readfilesync('example.yml', 'utf8'));

let str = yaml.safedump(obj);

NPM酷庫 ip,IP位址處理

npm酷庫,每天兩分鐘,了解乙個流行npm庫。const ip require ip 獲取本機網絡卡ip ip.address 比較兩個ip是否相同 ip.isequal 1 0 1 true ip 表示格式互轉 ip.tobuffer 127.0.0.1 buffer 127,0,0,1 ip.t...

NPM酷庫 uuid,生成隨機ID

npm酷庫,每天兩分鐘,了解乙個流行npm庫。在中心化應用中,資料記錄的id往往是資料庫生成的自增id,但是在分布式應用中,就會存在一些問題 uuid 就是應用在分布式環境中的隨機字串id,uuid有統一的格式,uuid的標準型式包含32個16進製制數字,以連字型大小分為五段,形式為8 4 4 4 ...

NPM酷庫 minimatch,模式匹配字串

前兩天,我們學習了node.js中模式匹配檔案列表的 glob 和 glob 的增強版globby,今天,我們將了解 glob 的基礎庫 minimatch,用來模式匹配字串的庫。其實,glob庫支援的的各種模式都來自於minimatch。const minimatch require minima...