Skip to content

Commit

Permalink
Add YAML.scalarOptions (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Mar 28, 2020
1 parent 7325c5a commit 1d73cf0
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docs
Submodule docs updated 1 files
+36 −45 source/includes/options.md
41 changes: 41 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { parse as parseCST } from './cst/parse'
import { Document as YAMLDocument } from './Document'
import { YAMLSemanticError } from './errors'
import { Schema } from './schema'
import {
binaryOptions,
boolOptions,
intOptions,
nullOptions,
strOptions
} from './tags/options'
import { warn } from './warnings'

const defaultOptions = {
Expand All @@ -17,6 +24,39 @@ const defaultOptions = {
version: '1.2'
}

const scalarOptions = {
get binary() {
return binaryOptions
},
set binary(opt) {
Object.assign(binaryOptions, opt)
},
get bool() {
return boolOptions
},
set bool(opt) {
Object.assign(boolOptions, opt)
},
get int() {
return intOptions
},
set int(opt) {
Object.assign(intOptions, opt)
},
get null() {
return nullOptions
},
set null(opt) {
Object.assign(nullOptions, opt)
},
get str() {
return strOptions
},
set str(opt) {
Object.assign(strOptions, opt)
}
}

function createNode(value, wrapScalars = true, tag) {
if (tag === undefined && typeof wrapScalars === 'string') {
tag = wrapScalars
Expand Down Expand Up @@ -81,5 +121,6 @@ export const YAML = {
parseAllDocuments,
parseCST,
parseDocument,
scalarOptions,
stringify
}
7 changes: 3 additions & 4 deletions tests/doc/YAML-1.2.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { YAML } from '../../src/index'
import { strOptions } from '../../src/tags/options'

const collectionKeyWarning =
'Keys with collection values will be stringified as YAML due to JS Object restrictions. Use mapAsMap: true to avoid this.'
Expand Down Expand Up @@ -1840,15 +1839,15 @@ matches %: 20`,
let origFoldOptions

beforeAll(() => {
origFoldOptions = strOptions.fold
strOptions.fold = {
origFoldOptions = YAML.scalarOptions.str.fold
YAML.scalarOptions.str.fold = {
lineWidth: 20,
minContentWidth: 0
}
})

afterAll(() => {
strOptions.fold = origFoldOptions
YAML.scalarOptions.str.fold = origFoldOptions
})

for (const section in spec) {
Expand Down
7 changes: 3 additions & 4 deletions tests/doc/foldFlowLines.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
FOLD_QUOTED
} from '../../src/foldFlowLines'
import { YAML } from '../../src/index'
import { strOptions } from '../../src/tags/options'

describe('plain', () => {
const src = 'abc def ghi jkl mno pqr stu vwx yz\n'
Expand Down Expand Up @@ -195,15 +194,15 @@ describe('end-to-end', () => {
let origFoldOptions

beforeAll(() => {
origFoldOptions = strOptions.fold
strOptions.fold = {
origFoldOptions = YAML.scalarOptions.str.fold
YAML.scalarOptions.str.fold = {
lineWidth: 20,
minContentWidth: 0
}
})

afterAll(() => {
strOptions.fold = origFoldOptions
YAML.scalarOptions.str.fold = origFoldOptions
})

test('more-indented folded block', () => {
Expand Down
7 changes: 3 additions & 4 deletions tests/doc/parse.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'fs'
import path from 'path'
import { YAML } from '../../src/index'
import { intOptions } from '../../src/tags/options'

describe('tags', () => {
describe('implicit tags', () => {
Expand Down Expand Up @@ -142,11 +141,11 @@ describe('number types', () => {
describe('asBigInt: true', () => {
let prevAsBigInt
beforeAll(() => {
prevAsBigInt = intOptions.asBigInt
intOptions.asBigInt = true
prevAsBigInt = YAML.scalarOptions.int.asBigInt
YAML.scalarOptions.int.asBigInt = true
})
afterAll(() => {
intOptions.asBigInt = prevAsBigInt
YAML.scalarOptions.int.asBigInt = prevAsBigInt
})

test('Version 1.1', () => {
Expand Down
7 changes: 3 additions & 4 deletions tests/doc/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { YAML } from '../../src/index'
import { Pair } from '../../src/schema/Pair'
import { Type } from '../../src/constants'
import { stringifyString } from '../../src/stringify'
import { strOptions } from '../../src/tags/options'

for (const [name, version] of [
['YAML 1.1', '1.1'],
Expand Down Expand Up @@ -114,14 +113,14 @@ for (const [name, version] of [
describe('string', () => {
let origFoldOptions
beforeAll(() => {
origFoldOptions = strOptions.fold
strOptions.fold = {
origFoldOptions = YAML.scalarOptions.str.fold
YAML.scalarOptions.str.fold = {
lineWidth: 20,
minContentWidth: 0
}
})
afterAll(() => {
strOptions.fold = origFoldOptions
YAML.scalarOptions.str.fold = origFoldOptions
})

test('plain', () => {
Expand Down
11 changes: 5 additions & 6 deletions tests/doc/types.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { YAML } from '../../src/index'
import { Scalar } from '../../src/schema/Scalar'
import { YAMLSeq } from '../../src/schema/Seq'
import { strOptions } from '../../src/tags/options'
import { binary } from '../../src/tags/yaml-1.1/binary'
import { YAMLOMap } from '../../src/tags/yaml-1.1/omap'
import { YAMLSet } from '../../src/tags/yaml-1.1/set'

let origFoldOptions

beforeAll(() => {
origFoldOptions = strOptions.fold
strOptions.fold = {
origFoldOptions = YAML.scalarOptions.str.fold
YAML.scalarOptions.str.fold = {
lineWidth: 20,
minContentWidth: 0
}
})

afterAll(() => {
strOptions.fold = origFoldOptions
YAML.scalarOptions.str.fold = origFoldOptions
})

describe('json schema', () => {
Expand Down Expand Up @@ -259,7 +258,7 @@ description:
genericStr += String.fromCharCode(generic[i])
expect(canonicalStr).toBe(genericStr)
expect(canonicalStr.substr(0, 5)).toBe('GIF89')
strOptions.fold.lineWidth = 80
YAML.scalarOptions.str.fold.lineWidth = 80
expect(String(doc))
.toBe(`canonical: !!binary "R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J\\
+fn5OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/\\
Expand All @@ -271,7 +270,7 @@ generic: !!binary |-
ZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLCAgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYN
G84BwwEeECcgggoBADs=
description: The binary value above is a tiny arrow encoded as a gif image.\n`)
strOptions.fold.lineWidth = 20
YAML.scalarOptions.str.fold.lineWidth = 20
})

test('!!bool', () => {
Expand Down
7 changes: 3 additions & 4 deletions tests/yaml-test-suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import fs from 'fs'
import path from 'path'

import { YAML } from '../src/index'
import { strOptions } from '../src/tags/options'
import { testEvents } from '../src/test-events'

const testDirs = fs
Expand All @@ -29,15 +28,15 @@ const matchJson = (docs, json) => {
let origFoldOptions

beforeAll(() => {
origFoldOptions = strOptions.fold
strOptions.fold = {
origFoldOptions = YAML.scalarOptions.str.fold
YAML.scalarOptions.str.fold = {
lineWidth: 20,
minContentWidth: 0
}
})

afterAll(() => {
strOptions.fold = origFoldOptions
YAML.scalarOptions.str.fold = origFoldOptions
})

testDirs.forEach(dir => {
Expand Down

0 comments on commit 1d73cf0

Please sign in to comment.