Skip to content

Commit

Permalink
chore!: Update to yaml 2 (#44)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Some of the options supported by yaml change;
see its documentation for details: https://eemeli.org/yaml
  • Loading branch information
eemeli authored Apr 9, 2022
1 parent 0a058d7 commit cc1a2c2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ file.hello === 'world'

## Options

In addition to all [`yaml` options](https://eemeli.org/yaml/#options), the loader supports the following additional options:
In addition to all [`yaml` options](https://eemeli.org/yaml/#options) used by its parsing methods,
the loader supports the following additional options:

### `asJSON`

Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,27 @@ module.exports = function yamlLoader(src) {
return next(value)
})

const jsOpt = Object.assign({}, options, { onAnchor: addRef })
if (asJSON) {
jsOpt.json = true
jsOpt.mapAsMap = false
}

let res
if (asStream) {
const stream = YAML.parseAllDocuments(src, options)
res = []
for (const doc of stream) {
for (const warn of doc.warnings) this.emitWarning(warn)
for (const err of doc.errors) throw err
res.push(doc.toJSON(null, addRef))
res.push(doc.toJS(jsOpt))
}
} else {
const doc = YAML.parseDocument(src, options)
for (const warn of doc.warnings) this.emitWarning(warn)
for (const err of doc.errors) throw err
if (namespace) doc.contents = doc.getIn(namespace.split('.'))
res = doc.toJSON(null, addRef)
res = doc.toJS(jsOpt)
}

if (asJSON) return JSON.stringify(res)
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"dependencies": {
"javascript-stringify": "^2.0.1",
"loader-utils": "^2.0.0",
"yaml": "^1.10.0"
"yaml": "^2.0.0"
},
"devDependencies": {
"jest": "^27.5.1",
Expand Down
21 changes: 18 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('aliased objects', () => {
const res = loader.call(ctx, src)
expect(res).toBe("var v1 = ['foo'];\nexport default {foo:v1,bar:v1};")
})

test('document stream', () => {
const ctx = { query: { asStream: true } }
const src = 'foo: &foo [foo]\nbar: *foo\n---\nfoo: &foo [foo]\nbar: *foo'
Expand All @@ -28,9 +29,7 @@ describe('options.asJSON', () => {
test('throw error if there is a parse error', () => {
const ctx = { query: { asJSON: true } }
const src = '---\nhello: world\nhello: 2'
expect(() => loader.call(ctx, src)).toThrow(
/^Map keys must be unique; "hello" is repeated/
)
expect(() => loader.call(ctx, src)).toThrow(/Map keys must be unique/)
})
})

Expand Down Expand Up @@ -64,3 +63,19 @@ describe('options.asStream', () => {
)
})
})

describe('yaml options', () => {
test('options.intAsBigInt', () => {
const ctx = { query: { intAsBigInt: true } }
const res = loader.call(ctx, 'answer: 42')
expect(res).toBe("export default {answer:BigInt('42')};")
})

test('options.mapAsMap', () => {
const ctx = { query: { mapAsMap: true } }
const res = loader.call(ctx, '---\nhello:\n world: plop')
expect(res).toBe(
"export default new Map([['hello',new Map([['world','plop']])]]);"
)
})
})

0 comments on commit cc1a2c2

Please sign in to comment.