forked from eemeli/yaml
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move tag.createNode to static nodeClass.from
Re: eemeli#457
- Loading branch information
Showing
8 changed files
with
83 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,15 @@ | ||
import type { CreateNodeContext } from '../../doc/createNode.js' | ||
import { isMap } from '../../nodes/identity.js' | ||
import { createPair } from '../../nodes/Pair.js' | ||
import { YAMLMap } from '../../nodes/YAMLMap.js' | ||
import type { CollectionTag } from '../types.js' | ||
import type { Schema } from '../Schema.js' | ||
|
||
function createMap(schema: Schema, obj: unknown, ctx: CreateNodeContext) { | ||
const { keepUndefined, replacer } = ctx | ||
const map = new YAMLMap(schema) | ||
const add = (key: unknown, value: unknown) => { | ||
if (typeof replacer === 'function') value = replacer.call(obj, key, value) | ||
else if (Array.isArray(replacer) && !replacer.includes(key)) return | ||
if (value !== undefined || keepUndefined) | ||
map.items.push(createPair(key, value, ctx)) | ||
} | ||
if (obj instanceof Map) { | ||
for (const [key, value] of obj) add(key, value) | ||
} else if (obj && typeof obj === 'object') { | ||
for (const key of Object.keys(obj)) add(key, (obj as any)[key]) | ||
} | ||
if (typeof schema.sortMapEntries === 'function') { | ||
map.items.sort(schema.sortMapEntries) | ||
} | ||
return map | ||
} | ||
|
||
export const map: CollectionTag = { | ||
collection: 'map', | ||
createNode: createMap, | ||
default: true, | ||
nodeClass: YAMLMap, | ||
tag: 'tag:yaml.org,2002:map', | ||
resolve(map, onError) { | ||
if (!isMap(map)) onError('Expected a mapping for this tag') | ||
return map | ||
} | ||
}, | ||
createNode: (schema, obj, ctx) => YAMLMap.from(schema, obj, ctx), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,15 @@ | ||
import { CreateNodeContext, createNode } from '../../doc/createNode.js' | ||
import { isSeq } from '../../nodes/identity.js' | ||
import { YAMLSeq } from '../../nodes/YAMLSeq.js' | ||
import type { Schema } from '../Schema.js' | ||
import type { CollectionTag } from '../types.js' | ||
|
||
function createSeq(schema: Schema, obj: unknown, ctx: CreateNodeContext) { | ||
const { replacer } = ctx | ||
const seq = new YAMLSeq(schema) | ||
if (obj && Symbol.iterator in Object(obj)) { | ||
let i = 0 | ||
for (let it of obj as Iterable<unknown>) { | ||
if (typeof replacer === 'function') { | ||
const key = obj instanceof Set ? it : String(i++) | ||
it = replacer.call(obj, key, it) | ||
} | ||
seq.items.push(createNode(it, undefined, ctx)) | ||
} | ||
} | ||
return seq | ||
} | ||
|
||
export const seq: CollectionTag = { | ||
collection: 'seq', | ||
createNode: createSeq, | ||
default: true, | ||
nodeClass: YAMLSeq, | ||
tag: 'tag:yaml.org,2002:seq', | ||
resolve(seq, onError) { | ||
if (!isSeq(seq)) onError('Expected a sequence for this tag') | ||
return seq | ||
} | ||
}, | ||
createNode: (schema, obj, ctx) => YAMLSeq.from(schema, obj, ctx), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters