Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: Update to yaml 2 #44

Merged
merged 2 commits into from
Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']])]]);"
)
})
})