js-yaml簡單使用

安裝 js-yaml

npm install js-yaml


index.js

let fs = require("fs");
let content = fs.readFileSync("text.yaml",{encoding:"utf8"});

let yaml = require("js-yaml");
let result = yaml.load(content);
console.log(JSON.stringify(result, null, 2));
console.log(result.fn(1,3));
console.log(result.reg.test("test"));
console.log(result.undef);


text.yaml

#對象鍵值對
name: xxx
hash: { name: Steve, foo: bar } 
#數組
array:
    - Cat
    - Dog
    - Goldfish
array2: [Cat, Dog]
array3:
    -
        - a
        - b

#符合結構
languages:
    - ruby
    - [{name: perl,code: 2}]
    - 
        name: js
        code: 3  

#純量
parent: ~               
isSet: true
number: 12.30
date: 1976-07-31

#類型轉換
string: !!str 1
int: !!int "2"
bool: !!bool "true" 
fn: !!js/function function(a,b){return a+b;}
reg: !!js/regexp /test/
undef: !!js/undefined '' 

#引用
defaults: &defaults
  adapter:  postgres
  host:     localhost

newDefault:
    *defaults
    
development:
  database: myapp_development
  <<: *defaults


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章