Skip to content

Commit

Permalink
Refactor YAML.createNode() internals, adding 3rd tag arg
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Dec 31, 2018
1 parent f0058d7 commit 29f399b
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 103 deletions.
71 changes: 35 additions & 36 deletions __tests__/createNode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import createNode from '../src/createNode'
import YAML from '../src/index'
import YAMLMap from '../src/schema/Map'
import Pair from '../src/schema/Pair'
Expand All @@ -9,81 +8,81 @@ import { YAMLSet } from '../src/schema/_set'
describe('scalars', () => {
describe('createNode(value, false)', () => {
test('boolean', () => {
const s = createNode(false, false)
const s = YAML.createNode(false, false)
expect(s).toBe(false)
})
test('null', () => {
const s = createNode(null, false)
const s = YAML.createNode(null, false)
expect(s).toBeInstanceOf(Scalar)
expect(s.value).toBe(null)
})
test('undefined', () => {
const s = createNode(undefined, false)
const s = YAML.createNode(undefined, false)
expect(s).toBeInstanceOf(Scalar)
expect(s.value).toBe(null)
})
test('number', () => {
const s = createNode(3, false)
const s = YAML.createNode(3, false)
expect(s).toBe(3)
})
test('string', () => {
const s = createNode('test', false)
const s = YAML.createNode('test', false)
expect(s).toBe('test')
})
})
})

describe('createNode(value, true)', () => {
test('boolean', () => {
const s = createNode(false, true)
const s = YAML.createNode(false, true)
expect(s).toBeInstanceOf(Scalar)
expect(s.value).toBe(false)
})
test('null', () => {
const s = createNode(null, true)
const s = YAML.createNode(null, true)
expect(s).toBeInstanceOf(Scalar)
expect(s.value).toBe(null)
})
test('undefined', () => {
const s = createNode(undefined, true)
const s = YAML.createNode(undefined, true)
expect(s).toBeInstanceOf(Scalar)
expect(s.value).toBe(null)
})
test('number', () => {
const s = createNode(3, true)
const s = YAML.createNode(3, true)
expect(s).toBeInstanceOf(Scalar)
expect(s.value).toBe(3)
})
test('string', () => {
const s = createNode('test', true)
const s = YAML.createNode('test', true)
expect(s).toBeInstanceOf(Scalar)
expect(s.value).toBe('test')
})
})

describe('arrays', () => {
test('createNode([])', () => {
const s = createNode([])
const s = YAML.createNode([])
expect(s).toBeInstanceOf(YAMLSeq)
expect(s.items).toHaveLength(0)
})
test('createNode([true], false)', () => {
const s = createNode([true], false)
const s = YAML.createNode([true], false)
expect(s).toBeInstanceOf(YAMLSeq)
expect(s.items).toMatchObject([true])
})
describe('[3, ["four", 5]]', () => {
const array = [3, ['four', 5]]
test('createNode(value, false)', () => {
const s = createNode(array, false)
const s = YAML.createNode(array, false)
expect(s).toBeInstanceOf(YAMLSeq)
expect(s.items).toHaveLength(2)
expect(s.items[0]).toBe(3)
expect(s.items[1]).toBeInstanceOf(YAMLSeq)
expect(s.items[1].items).toMatchObject(['four', 5])
})
test('createNode(value, true)', () => {
const s = createNode(array, true)
const s = YAML.createNode(array, true)
expect(s).toBeInstanceOf(YAMLSeq)
expect(s.items).toHaveLength(2)
expect(s.items[0].value).toBe(3)
Expand All @@ -97,22 +96,22 @@ describe('arrays', () => {
const doc = new YAML.Document()
doc.contents = array
expect(String(doc)).toBe(res)
doc.contents = createNode(array, false)
doc.contents = YAML.createNode(array, false)
expect(String(doc)).toBe(res)
doc.contents = createNode(array, true)
doc.contents = YAML.createNode(array, true)
expect(String(doc)).toBe(res)
})
})
})

describe('objects', () => {
test('createNode({})', () => {
const s = createNode({})
const s = YAML.createNode({})
expect(s).toBeInstanceOf(YAMLMap)
expect(s.items).toHaveLength(0)
})
test('createNode({ x: true }, false)', () => {
const s = createNode({ x: true }, false)
const s = YAML.createNode({ x: true }, false)
expect(s).toBeInstanceOf(YAMLMap)
expect(s.items).toHaveLength(1)
expect(s.items[0]).toBeInstanceOf(Pair)
Expand All @@ -121,7 +120,7 @@ describe('objects', () => {
describe('{ x: 3, y: [4], z: { w: "five", v: 6 } }', () => {
const object = { x: 3, y: [4], z: { w: 'five', v: 6 } }
test('createNode(value, false)', () => {
const s = createNode(object, false)
const s = YAML.createNode(object, false)
expect(s).toBeInstanceOf(YAMLMap)
expect(s.items).toHaveLength(3)
expect(s.items).toMatchObject([
Expand All @@ -136,7 +135,7 @@ describe('objects', () => {
])
})
test('createNode(value, true)', () => {
const s = createNode(object, true)
const s = YAML.createNode(object, true)
expect(s).toBeInstanceOf(YAMLMap)
expect(s.items).toHaveLength(3)
expect(s.items).toMatchObject([
Expand All @@ -163,37 +162,37 @@ z:
const doc = new YAML.Document()
doc.contents = object
expect(String(doc)).toBe(res)
doc.contents = createNode(object, false)
doc.contents = YAML.createNode(object, false)
expect(String(doc)).toBe(res)
doc.contents = createNode(object, true)
doc.contents = YAML.createNode(object, true)
expect(String(doc)).toBe(res)
})
})
})

describe('Set', () => {
test('createNode(new Set)', () => {
const s = createNode(new Set())
const s = YAML.createNode(new Set())
expect(s).toBeInstanceOf(YAMLSeq)
expect(s.items).toHaveLength(0)
})
test('createNode(new Set([true]), false)', () => {
const s = createNode(new Set([true]), false)
const s = YAML.createNode(new Set([true]), false)
expect(s).toBeInstanceOf(YAMLSeq)
expect(s.items).toMatchObject([true])
})
describe("Set { 3, Set { 'four', 5 } }", () => {
const set = new Set([3, new Set(['four', 5])])
test('createNode(set, false)', () => {
const s = createNode(set, false)
const s = YAML.createNode(set, false)
expect(s).toBeInstanceOf(YAMLSeq)
expect(s.items).toHaveLength(2)
expect(s.items[0]).toBe(3)
expect(s.items[1]).toBeInstanceOf(YAMLSeq)
expect(s.items[1].items).toMatchObject(['four', 5])
})
test('createNode(set, true)', () => {
const s = createNode(set, true)
const s = YAML.createNode(set, true)
expect(s).toBeInstanceOf(YAMLSeq)
expect(s.items).toHaveLength(2)
expect(s.items[0].value).toBe(3)
Expand All @@ -207,9 +206,9 @@ describe('Set', () => {
const doc = new YAML.Document()
doc.contents = set
expect(String(doc)).toBe(res)
doc.contents = createNode(set, false)
doc.contents = YAML.createNode(set, false)
expect(String(doc)).toBe(res)
doc.contents = createNode(set, true)
doc.contents = YAML.createNode(set, true)
expect(String(doc)).toBe(res)
})
test('Schema#createNode() - YAML 1.2', () => {
Expand Down Expand Up @@ -237,12 +236,12 @@ describe('Set', () => {

describe('Map', () => {
test('createNode(new Map)', () => {
const s = createNode(new Map())
const s = YAML.createNode(new Map())
expect(s).toBeInstanceOf(YAMLMap)
expect(s.items).toHaveLength(0)
})
test('createNode(new Map([["x", true]]), false)', () => {
const s = createNode(new Map([['x', true]]), false)
const s = YAML.createNode(new Map([['x', true]]), false)
expect(s).toBeInstanceOf(YAMLMap)
expect(s.items).toHaveLength(1)
expect(s.items[0]).toBeInstanceOf(Pair)
Expand All @@ -255,7 +254,7 @@ describe('Map', () => {
[new Map([['w', 'five'], ['v', 6]]), 'z']
])
test('createNode(map, false)', () => {
const s = createNode(map, false)
const s = YAML.createNode(map, false)
expect(s).toBeInstanceOf(YAMLMap)
expect(s.items).toHaveLength(3)
expect(s.items).toMatchObject([
Expand All @@ -270,7 +269,7 @@ describe('Map', () => {
])
})
test('createNode(map, true)', () => {
const s = createNode(map, true)
const s = YAML.createNode(map, true)
expect(s).toBeInstanceOf(YAMLMap)
expect(s.items).toHaveLength(3)
expect(s.items).toMatchObject([
Expand All @@ -297,9 +296,9 @@ y:
const doc = new YAML.Document()
doc.contents = map
expect(String(doc)).toBe(res)
doc.contents = createNode(map, false)
doc.contents = YAML.createNode(map, false)
expect(String(doc)).toBe(res)
doc.contents = createNode(map, true)
doc.contents = YAML.createNode(map, true)
expect(String(doc)).toBe(res)
})
})
Expand All @@ -308,7 +307,7 @@ y:
describe('toJSON()', () => {
test('Date', () => {
const date = new Date('2018-12-22T08:02:52Z')
const node = createNode(date)
const node = YAML.createNode(date)
expect(node.value).toBe(date.toJSON())
})
})
43 changes: 0 additions & 43 deletions src/createNode.js

This file was deleted.

16 changes: 15 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* global console */

import parseCST from './cst/parse'
import createNode from './createNode'
import YAMLDocument from './Document'
import { YAMLSemanticError } from './errors'
import Schema from './schema'

const defaultOptions = {
keepNodeTypes: true,
Expand All @@ -13,6 +13,20 @@ const defaultOptions = {
version: '1.2'
}

function createNode(value, wrapScalars = true, tag) {
if (tag === undefined && typeof wrapScalars === 'string') {
tag = wrapScalars
wrapScalars = true
}
const options = Object.assign(
{},
YAMLDocument.defaults[defaultOptions.version],
defaultOptions
)
const schema = new Schema(options)
return schema.createNode(value, wrapScalars, tag)
}

class Document extends YAMLDocument {
constructor(options) {
super(Object.assign({}, defaultOptions, options))
Expand Down
36 changes: 34 additions & 2 deletions src/schema/failsafe.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
import YAMLMap from './Map'
import Pair from './Pair'
import YAMLSeq from './Seq'
import { str } from './_string'
import parseMap from './parseMap'
import parseSeq from './parseSeq'

function createMap(schema, obj, wrapScalars) {
const map = new YAMLMap()
if (obj instanceof Map) {
for (const [key, value] of obj) {
const k = schema.createNode(key, wrapScalars)
const v = schema.createNode(value, wrapScalars)
map.items.push(new Pair(k, v))
}
} else if (obj && typeof obj === 'object') {
map.items = Object.keys(obj).map(key => {
const k = schema.createNode(key, wrapScalars)
const v = schema.createNode(obj[key], wrapScalars)
return new Pair(k, v)
})
}
return map
}

function createSeq(schema, obj, wrapScalars) {
const seq = new YAMLSeq()
if (obj && obj[Symbol.iterator]) {
for (const it of obj) {
const v = schema.createNode(it, wrapScalars)
seq.items.push(v)
}
}
return seq
}

export const map = {
nodeClass: YAMLMap,
createNode: createMap,
default: true,
nodeClass: YAMLMap,
tag: 'tag:yaml.org,2002:map',
resolve: parseMap,
stringify: (value, ctx, onComment, onChompKeep) =>
value.toString(ctx, onComment, onChompKeep)
}

export const seq = {
nodeClass: YAMLSeq,
createNode: createSeq,
default: true,
nodeClass: YAMLSeq,
tag: 'tag:yaml.org,2002:seq',
resolve: parseSeq,
stringify: (value, ctx, onComment, onChompKeep) =>
Expand Down

0 comments on commit 29f399b

Please sign in to comment.