Comparing version 2.3.0-4 to 2.3.0-5
@@ -1,3 +0,5 @@ | ||
import { isNode, isMap } from '../nodes/identity.js'; | ||
import { isNode } from '../nodes/identity.js'; | ||
import { Scalar } from '../nodes/Scalar.js'; | ||
import { YAMLMap } from '../nodes/YAMLMap.js'; | ||
import { YAMLSeq } from '../nodes/YAMLSeq.js'; | ||
import { resolveBlockMap } from './resolve-block-map.js'; | ||
@@ -7,25 +9,11 @@ import { resolveBlockSeq } from './resolve-block-seq.js'; | ||
function composeCollection(CN, ctx, token, tagToken, onError) { | ||
let coll; | ||
switch (token.type) { | ||
case 'block-map': { | ||
coll = resolveBlockMap(CN, ctx, token, onError); | ||
break; | ||
} | ||
case 'block-seq': { | ||
coll = resolveBlockSeq(CN, ctx, token, onError); | ||
break; | ||
} | ||
case 'flow-collection': { | ||
coll = resolveFlowCollection(CN, ctx, token, onError); | ||
break; | ||
} | ||
} | ||
if (!tagToken) | ||
return coll; | ||
const tagName = ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg)); | ||
if (!tagName) | ||
return coll; | ||
// Cast needed due to: https://github.com/Microsoft/TypeScript/issues/3841 | ||
function resolveCollection(CN, ctx, token, onError, tagName, tag) { | ||
const coll = token.type === 'block-map' | ||
? resolveBlockMap(CN, ctx, token, onError, tag) | ||
: token.type === 'block-seq' | ||
? resolveBlockSeq(CN, ctx, token, onError, tag) | ||
: resolveFlowCollection(CN, ctx, token, onError, tag); | ||
const Coll = coll.constructor; | ||
// If we got a tagName matching the class, or the tag name is '!', | ||
// then use the tagName from the node class used to create it. | ||
if (tagName === '!' || tagName === Coll.tagName) { | ||
@@ -35,4 +23,28 @@ coll.tag = Coll.tagName; | ||
} | ||
const expType = isMap(coll) ? 'map' : 'seq'; | ||
let tag = ctx.schema.tags.find(t => t.collection === expType && t.tag === tagName); | ||
if (tagName) | ||
coll.tag = tagName; | ||
return coll; | ||
} | ||
function composeCollection(CN, ctx, token, tagToken, onError) { | ||
const tagName = !tagToken | ||
? null | ||
: ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg)); | ||
const expType = token.type === 'block-map' | ||
? 'map' | ||
: token.type === 'block-seq' | ||
? 'seq' | ||
: token.start.source === '{' | ||
? 'map' | ||
: 'seq'; | ||
// shortcut: check if it's a generic YAMLMap or YAMLSeq | ||
// before jumping into the custom tag logic. | ||
if (!tagToken || | ||
!tagName || | ||
tagName === '!' || | ||
(tagName === YAMLMap.tagName && expType === 'map') || | ||
(tagName === YAMLSeq.tagName && expType === 'seq') || | ||
!expType) { | ||
return resolveCollection(CN, ctx, token, onError, tagName); | ||
} | ||
let tag = ctx.schema.tags.find(t => t.tag === tagName && t.collection === expType); | ||
if (!tag) { | ||
@@ -45,8 +57,13 @@ const kt = ctx.schema.knownTags[tagName]; | ||
else { | ||
onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true); | ||
coll.tag = tagName; | ||
return coll; | ||
if (kt?.collection) { | ||
onError(tagToken, 'BAD_COLLECTION_TYPE', `${kt.tag} used for ${expType} collection, but expects ${kt.collection}`, true); | ||
} | ||
else { | ||
onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true); | ||
} | ||
return resolveCollection(CN, ctx, token, onError, tagName); | ||
} | ||
} | ||
const res = tag.resolve(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options); | ||
const coll = resolveCollection(CN, ctx, token, onError, tagName, tag); | ||
const res = tag.resolve?.(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options) ?? coll; | ||
const node = isNode(res) | ||
@@ -53,0 +70,0 @@ ? res |
@@ -9,4 +9,5 @@ import { Pair } from '../nodes/Pair.js'; | ||
const startColMsg = 'All mapping items must start at the same column'; | ||
function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) { | ||
const map = new YAMLMap(ctx.schema); | ||
function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError, tag) { | ||
const NodeClass = tag?.nodeClass ?? YAMLMap; | ||
const map = new NodeClass(ctx.schema); | ||
if (ctx.atRoot) | ||
@@ -13,0 +14,0 @@ ctx.atRoot = false; |
@@ -5,4 +5,5 @@ import { YAMLSeq } from '../nodes/YAMLSeq.js'; | ||
function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError) { | ||
const seq = new YAMLSeq(ctx.schema); | ||
function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError, tag) { | ||
const NodeClass = tag?.nodeClass ?? YAMLSeq; | ||
const seq = new NodeClass(ctx.schema); | ||
if (ctx.atRoot) | ||
@@ -9,0 +10,0 @@ ctx.atRoot = false; |
@@ -12,8 +12,7 @@ import { isPair } from '../nodes/identity.js'; | ||
const isBlock = (token) => token && (token.type === 'block-map' || token.type === 'block-seq'); | ||
function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onError) { | ||
function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onError, tag) { | ||
const isMap = fc.start.source === '{'; | ||
const fcName = isMap ? 'flow map' : 'flow sequence'; | ||
const coll = isMap | ||
? new YAMLMap(ctx.schema) | ||
: new YAMLSeq(ctx.schema); | ||
const NodeClass = (tag?.nodeClass ?? (isMap ? YAMLMap : YAMLSeq)); | ||
const coll = new NodeClass(ctx.schema); | ||
coll.flow = true; | ||
@@ -20,0 +19,0 @@ const atRoot = ctx.atRoot; |
@@ -77,3 +77,5 @@ import { Alias } from '../nodes/Alias.js'; | ||
? tagObj.createNode(ctx.schema, value, ctx) | ||
: new Scalar(value); | ||
: typeof tagObj?.nodeClass?.from === 'function' | ||
? tagObj.nodeClass.from(ctx.schema, value, ctx) | ||
: new Scalar(value); | ||
if (tagName) | ||
@@ -80,0 +82,0 @@ node.tag = tagName; |
@@ -50,3 +50,3 @@ class YAMLError extends Error { | ||
if (end && end.line === line && end.col > col) { | ||
count = Math.min(end.col - col, 80 - ci); | ||
count = Math.max(1, Math.min(end.col - col, 80 - ci)); | ||
} | ||
@@ -53,0 +53,0 @@ const pointer = ' '.repeat(ci) + '^'.repeat(count); |
@@ -5,3 +5,3 @@ import { stringifyCollection } from '../stringify/stringifyCollection.js'; | ||
import { isPair, isScalar, MAP } from './identity.js'; | ||
import { Pair } from './Pair.js'; | ||
import { Pair, createPair } from './Pair.js'; | ||
import { isScalarValue } from './Scalar.js'; | ||
@@ -30,2 +30,30 @@ | ||
/** | ||
* A generic collection parsing method that can be extended | ||
* to other node classes that inherit from YAMLMap | ||
*/ | ||
static from(schema, obj, ctx) { | ||
const { keepUndefined, replacer } = ctx; | ||
const map = new this(schema); | ||
const add = (key, value) => { | ||
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[key]); | ||
} | ||
if (typeof schema.sortMapEntries === 'function') { | ||
map.items.sort(schema.sortMapEntries); | ||
} | ||
return map; | ||
} | ||
/** | ||
* Adds a value to the collection. | ||
@@ -32,0 +60,0 @@ * |
@@ -0,1 +1,2 @@ | ||
import { createNode } from '../doc/createNode.js'; | ||
import { stringifyCollection } from '../stringify/stringifyCollection.js'; | ||
@@ -87,2 +88,17 @@ import { Collection } from './Collection.js'; | ||
} | ||
static from(schema, obj, ctx) { | ||
const { replacer } = ctx; | ||
const seq = new this(schema); | ||
if (obj && Symbol.iterator in Object(obj)) { | ||
let i = 0; | ||
for (let it of obj) { | ||
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; | ||
} | ||
} | ||
@@ -89,0 +105,0 @@ function asItemIndex(key) { |
import { isMap } from '../../nodes/identity.js'; | ||
import { createPair } from '../../nodes/Pair.js'; | ||
import { YAMLMap } from '../../nodes/YAMLMap.js'; | ||
function createMap(schema, obj, ctx) { | ||
const { keepUndefined, replacer } = ctx; | ||
const map = new YAMLMap(schema); | ||
const add = (key, value) => { | ||
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[key]); | ||
} | ||
if (typeof schema.sortMapEntries === 'function') { | ||
map.items.sort(schema.sortMapEntries); | ||
} | ||
return map; | ||
} | ||
const map = { | ||
collection: 'map', | ||
createNode: createMap, | ||
default: true, | ||
@@ -39,5 +13,6 @@ nodeClass: YAMLMap, | ||
return map; | ||
} | ||
}, | ||
createNode: (schema, obj, ctx) => YAMLMap.from(schema, obj, ctx) | ||
}; | ||
export { map }; |
@@ -1,23 +0,6 @@ | ||
import { createNode } from '../../doc/createNode.js'; | ||
import { isSeq } from '../../nodes/identity.js'; | ||
import { YAMLSeq } from '../../nodes/YAMLSeq.js'; | ||
function createSeq(schema, obj, ctx) { | ||
const { replacer } = ctx; | ||
const seq = new YAMLSeq(schema); | ||
if (obj && Symbol.iterator in Object(obj)) { | ||
let i = 0; | ||
for (let it of obj) { | ||
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; | ||
} | ||
const seq = { | ||
collection: 'seq', | ||
createNode: createSeq, | ||
default: true, | ||
@@ -30,5 +13,6 @@ nodeClass: YAMLSeq, | ||
return seq; | ||
} | ||
}, | ||
createNode: (schema, obj, ctx) => YAMLSeq.from(schema, obj, ctx) | ||
}; | ||
export { seq }; |
@@ -42,2 +42,8 @@ import { isScalar, isPair } from '../../nodes/identity.js'; | ||
} | ||
static from(schema, iterable, ctx) { | ||
const pairs = createPairs(schema, iterable, ctx); | ||
const omap = new this(); | ||
omap.items = pairs.items; | ||
return omap; | ||
} | ||
} | ||
@@ -66,10 +72,5 @@ YAMLOMap.tag = 'tag:yaml.org,2002:omap'; | ||
}, | ||
createNode(schema, iterable, ctx) { | ||
const pairs = createPairs(schema, iterable, ctx); | ||
const omap = new YAMLOMap(); | ||
omap.items = pairs.items; | ||
return omap; | ||
} | ||
createNode: (schema, iterable, ctx) => YAMLOMap.from(schema, iterable, ctx) | ||
}; | ||
export { YAMLOMap, omap }; |
import { isMap, isPair, isScalar } from '../../nodes/identity.js'; | ||
import { createPair, Pair } from '../../nodes/Pair.js'; | ||
import { Pair, createPair } from '../../nodes/Pair.js'; | ||
import { YAMLMap, findPair } from '../../nodes/YAMLMap.js'; | ||
@@ -60,2 +60,13 @@ | ||
} | ||
static from(schema, iterable, ctx) { | ||
const { replacer } = ctx; | ||
const set = new this(schema); | ||
if (iterable && Symbol.iterator in Object(iterable)) | ||
for (let value of iterable) { | ||
if (typeof replacer === 'function') | ||
value = replacer.call(iterable, value, value); | ||
set.items.push(createPair(value, null, ctx)); | ||
} | ||
return set; | ||
} | ||
} | ||
@@ -69,2 +80,3 @@ YAMLSet.tag = 'tag:yaml.org,2002:set'; | ||
tag: 'tag:yaml.org,2002:set', | ||
createNode: (schema, iterable, ctx) => YAMLSet.from(schema, iterable, ctx), | ||
resolve(map, onError) { | ||
@@ -80,13 +92,2 @@ if (isMap(map)) { | ||
return map; | ||
}, | ||
createNode(schema, iterable, ctx) { | ||
const { replacer } = ctx; | ||
const set = new YAMLSet(schema); | ||
if (iterable && Symbol.iterator in Object(iterable)) | ||
for (let value of iterable) { | ||
if (typeof replacer === 'function') | ||
value = replacer.call(iterable, value, value); | ||
set.items.push(createPair(value, null, ctx)); | ||
} | ||
return set; | ||
} | ||
@@ -93,0 +94,0 @@ }; |
@@ -46,3 +46,3 @@ import { stringifyNumber } from '../../stringify/stringifyNumber.js'; | ||
parts | ||
.map(n => (n < 10 ? '0' + String(n) : String(n))) | ||
.map(n => String(n).padStart(2, '0')) | ||
.join(':') | ||
@@ -49,0 +49,0 @@ .replace(/000000\d*$/, '') // % 60 may introduce error |
@@ -5,2 +5,4 @@ 'use strict'; | ||
var Scalar = require('../nodes/Scalar.js'); | ||
var YAMLMap = require('../nodes/YAMLMap.js'); | ||
var YAMLSeq = require('../nodes/YAMLSeq.js'); | ||
var resolveBlockMap = require('./resolve-block-map.js'); | ||
@@ -10,25 +12,11 @@ var resolveBlockSeq = require('./resolve-block-seq.js'); | ||
function composeCollection(CN, ctx, token, tagToken, onError) { | ||
let coll; | ||
switch (token.type) { | ||
case 'block-map': { | ||
coll = resolveBlockMap.resolveBlockMap(CN, ctx, token, onError); | ||
break; | ||
} | ||
case 'block-seq': { | ||
coll = resolveBlockSeq.resolveBlockSeq(CN, ctx, token, onError); | ||
break; | ||
} | ||
case 'flow-collection': { | ||
coll = resolveFlowCollection.resolveFlowCollection(CN, ctx, token, onError); | ||
break; | ||
} | ||
} | ||
if (!tagToken) | ||
return coll; | ||
const tagName = ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg)); | ||
if (!tagName) | ||
return coll; | ||
// Cast needed due to: https://github.com/Microsoft/TypeScript/issues/3841 | ||
function resolveCollection(CN, ctx, token, onError, tagName, tag) { | ||
const coll = token.type === 'block-map' | ||
? resolveBlockMap.resolveBlockMap(CN, ctx, token, onError, tag) | ||
: token.type === 'block-seq' | ||
? resolveBlockSeq.resolveBlockSeq(CN, ctx, token, onError, tag) | ||
: resolveFlowCollection.resolveFlowCollection(CN, ctx, token, onError, tag); | ||
const Coll = coll.constructor; | ||
// If we got a tagName matching the class, or the tag name is '!', | ||
// then use the tagName from the node class used to create it. | ||
if (tagName === '!' || tagName === Coll.tagName) { | ||
@@ -38,4 +26,28 @@ coll.tag = Coll.tagName; | ||
} | ||
const expType = identity.isMap(coll) ? 'map' : 'seq'; | ||
let tag = ctx.schema.tags.find(t => t.collection === expType && t.tag === tagName); | ||
if (tagName) | ||
coll.tag = tagName; | ||
return coll; | ||
} | ||
function composeCollection(CN, ctx, token, tagToken, onError) { | ||
const tagName = !tagToken | ||
? null | ||
: ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg)); | ||
const expType = token.type === 'block-map' | ||
? 'map' | ||
: token.type === 'block-seq' | ||
? 'seq' | ||
: token.start.source === '{' | ||
? 'map' | ||
: 'seq'; | ||
// shortcut: check if it's a generic YAMLMap or YAMLSeq | ||
// before jumping into the custom tag logic. | ||
if (!tagToken || | ||
!tagName || | ||
tagName === '!' || | ||
(tagName === YAMLMap.YAMLMap.tagName && expType === 'map') || | ||
(tagName === YAMLSeq.YAMLSeq.tagName && expType === 'seq') || | ||
!expType) { | ||
return resolveCollection(CN, ctx, token, onError, tagName); | ||
} | ||
let tag = ctx.schema.tags.find(t => t.tag === tagName && t.collection === expType); | ||
if (!tag) { | ||
@@ -48,8 +60,13 @@ const kt = ctx.schema.knownTags[tagName]; | ||
else { | ||
onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true); | ||
coll.tag = tagName; | ||
return coll; | ||
if (kt?.collection) { | ||
onError(tagToken, 'BAD_COLLECTION_TYPE', `${kt.tag} used for ${expType} collection, but expects ${kt.collection}`, true); | ||
} | ||
else { | ||
onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true); | ||
} | ||
return resolveCollection(CN, ctx, token, onError, tagName); | ||
} | ||
} | ||
const res = tag.resolve(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options); | ||
const coll = resolveCollection(CN, ctx, token, onError, tagName, tag); | ||
const res = tag.resolve?.(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options) ?? coll; | ||
const node = identity.isNode(res) | ||
@@ -56,0 +73,0 @@ ? res |
import type { ParsedNode } from '../nodes/Node.js'; | ||
import { YAMLMap } from '../nodes/YAMLMap.js'; | ||
import type { BlockMap } from '../parse/cst.js'; | ||
import { CollectionTag } from '../schema/types.js'; | ||
import type { ComposeContext, ComposeNode } from './compose-node.js'; | ||
import type { ComposeErrorHandler } from './composer.js'; | ||
export declare function resolveBlockMap({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, bm: BlockMap, onError: ComposeErrorHandler): YAMLMap.Parsed<ParsedNode, ParsedNode | null>; | ||
export declare function resolveBlockMap({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, bm: BlockMap, onError: ComposeErrorHandler, tag?: CollectionTag): YAMLMap.Parsed<ParsedNode, ParsedNode | null>; |
@@ -11,4 +11,5 @@ 'use strict'; | ||
const startColMsg = 'All mapping items must start at the same column'; | ||
function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) { | ||
const map = new YAMLMap.YAMLMap(ctx.schema); | ||
function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError, tag) { | ||
const NodeClass = tag?.nodeClass ?? YAMLMap.YAMLMap; | ||
const map = new NodeClass(ctx.schema); | ||
if (ctx.atRoot) | ||
@@ -15,0 +16,0 @@ ctx.atRoot = false; |
import { YAMLSeq } from '../nodes/YAMLSeq.js'; | ||
import type { BlockSequence } from '../parse/cst.js'; | ||
import { CollectionTag } from '../schema/types.js'; | ||
import type { ComposeContext, ComposeNode } from './compose-node.js'; | ||
import type { ComposeErrorHandler } from './composer.js'; | ||
export declare function resolveBlockSeq({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, bs: BlockSequence, onError: ComposeErrorHandler): YAMLSeq.Parsed<import("../index.js").ParsedNode>; | ||
export declare function resolveBlockSeq({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, bs: BlockSequence, onError: ComposeErrorHandler, tag?: CollectionTag): YAMLSeq.Parsed<import("../index.js").ParsedNode>; |
@@ -7,4 +7,5 @@ 'use strict'; | ||
function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError) { | ||
const seq = new YAMLSeq.YAMLSeq(ctx.schema); | ||
function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError, tag) { | ||
const NodeClass = tag?.nodeClass ?? YAMLSeq.YAMLSeq; | ||
const seq = new NodeClass(ctx.schema); | ||
if (ctx.atRoot) | ||
@@ -11,0 +12,0 @@ ctx.atRoot = false; |
import { YAMLMap } from '../nodes/YAMLMap.js'; | ||
import { YAMLSeq } from '../nodes/YAMLSeq.js'; | ||
import type { FlowCollection } from '../parse/cst.js'; | ||
import { CollectionTag } from '../schema/types.js'; | ||
import type { ComposeContext, ComposeNode } from './compose-node.js'; | ||
import type { ComposeErrorHandler } from './composer.js'; | ||
export declare function resolveFlowCollection({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, fc: FlowCollection, onError: ComposeErrorHandler): YAMLMap.Parsed<import("../index.js").ParsedNode, import("../index.js").ParsedNode | null> | YAMLSeq.Parsed<import("../index.js").ParsedNode>; | ||
export declare function resolveFlowCollection({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, fc: FlowCollection, onError: ComposeErrorHandler, tag?: CollectionTag): YAMLMap.Parsed<import("../index.js").ParsedNode, import("../index.js").ParsedNode | null> | YAMLSeq.Parsed<import("../index.js").ParsedNode>; |
@@ -14,8 +14,7 @@ 'use strict'; | ||
const isBlock = (token) => token && (token.type === 'block-map' || token.type === 'block-seq'); | ||
function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onError) { | ||
function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onError, tag) { | ||
const isMap = fc.start.source === '{'; | ||
const fcName = isMap ? 'flow map' : 'flow sequence'; | ||
const coll = isMap | ||
? new YAMLMap.YAMLMap(ctx.schema) | ||
: new YAMLSeq.YAMLSeq(ctx.schema); | ||
const NodeClass = (tag?.nodeClass ?? (isMap ? YAMLMap.YAMLMap : YAMLSeq.YAMLSeq)); | ||
const coll = new NodeClass(ctx.schema); | ||
coll.flow = true; | ||
@@ -22,0 +21,0 @@ const atRoot = ctx.atRoot; |
@@ -79,3 +79,5 @@ 'use strict'; | ||
? tagObj.createNode(ctx.schema, value, ctx) | ||
: new Scalar.Scalar(value); | ||
: typeof tagObj?.nodeClass?.from === 'function' | ||
? tagObj.nodeClass.from(ctx.schema, value, ctx) | ||
: new Scalar.Scalar(value); | ||
if (tagName) | ||
@@ -82,0 +84,0 @@ node.tag = tagName; |
import type { LineCounter } from './parse/line-counter'; | ||
export type ErrorCode = 'ALIAS_PROPS' | 'BAD_ALIAS' | 'BAD_DIRECTIVE' | 'BAD_DQ_ESCAPE' | 'BAD_INDENT' | 'BAD_PROP_ORDER' | 'BAD_SCALAR_START' | 'BLOCK_AS_IMPLICIT_KEY' | 'BLOCK_IN_FLOW' | 'DUPLICATE_KEY' | 'IMPOSSIBLE' | 'KEY_OVER_1024_CHARS' | 'MISSING_CHAR' | 'MULTILINE_IMPLICIT_KEY' | 'MULTIPLE_ANCHORS' | 'MULTIPLE_DOCS' | 'MULTIPLE_TAGS' | 'TAB_AS_INDENT' | 'TAG_RESOLVE_FAILED' | 'UNEXPECTED_TOKEN'; | ||
export type ErrorCode = 'ALIAS_PROPS' | 'BAD_ALIAS' | 'BAD_DIRECTIVE' | 'BAD_DQ_ESCAPE' | 'BAD_INDENT' | 'BAD_PROP_ORDER' | 'BAD_SCALAR_START' | 'BLOCK_AS_IMPLICIT_KEY' | 'BLOCK_IN_FLOW' | 'DUPLICATE_KEY' | 'IMPOSSIBLE' | 'KEY_OVER_1024_CHARS' | 'MISSING_CHAR' | 'MULTILINE_IMPLICIT_KEY' | 'MULTIPLE_ANCHORS' | 'MULTIPLE_DOCS' | 'MULTIPLE_TAGS' | 'TAB_AS_INDENT' | 'TAG_RESOLVE_FAILED' | 'UNEXPECTED_TOKEN' | 'BAD_COLLECTION_TYPE'; | ||
export type LinePos = { | ||
@@ -4,0 +4,0 @@ line: number; |
@@ -52,3 +52,3 @@ 'use strict'; | ||
if (end && end.line === line && end.col > col) { | ||
count = Math.min(end.col - col, 80 - ci); | ||
count = Math.max(1, Math.min(end.col - col, 80 - ci)); | ||
} | ||
@@ -55,0 +55,0 @@ const pointer = ' '.repeat(ci) + '^'.repeat(count); |
@@ -8,3 +8,3 @@ import { CreateNodeContext } from '../doc/createNode.js'; | ||
import type { ToJSContext } from './toJS.js'; | ||
export declare function createPair(key: unknown, value: unknown, ctx: CreateNodeContext): Pair<import("./Node.js").Node, import("./Alias.js").Alias | import("./Scalar.js").Scalar<unknown> | import("./YAMLMap.js").YAMLMap<unknown, unknown> | import("./YAMLSeq.js").YAMLSeq<unknown>>; | ||
export declare function createPair(key: unknown, value: unknown, ctx: CreateNodeContext): Pair<import("./Node.js").Node, import("./YAMLMap.js").YAMLMap<unknown, unknown> | import("./Scalar.js").Scalar<unknown> | import("./Alias.js").Alias | import("./YAMLSeq.js").YAMLSeq<unknown>>; | ||
export declare class Pair<K = unknown, V = unknown> { | ||
@@ -11,0 +11,0 @@ readonly [NODE_TYPE]: symbol; |
import type { BlockMap, FlowCollection } from '../parse/cst.js'; | ||
import type { Schema } from '../schema/Schema.js'; | ||
import type { StringifyContext } from '../stringify/stringify.js'; | ||
import { CreateNodeContext } from '../util.js'; | ||
import { Collection } from './Collection.js'; | ||
@@ -23,2 +24,7 @@ import type { ParsedNode, Range } from './Node.js'; | ||
/** | ||
* A generic collection parsing method that can be extended | ||
* to other node classes that inherit from YAMLMap | ||
*/ | ||
static from(schema: Schema, obj: unknown, ctx: CreateNodeContext): YAMLMap<unknown, unknown>; | ||
/** | ||
* Adds a value to the collection. | ||
@@ -25,0 +31,0 @@ * |
@@ -31,2 +31,30 @@ 'use strict'; | ||
/** | ||
* A generic collection parsing method that can be extended | ||
* to other node classes that inherit from YAMLMap | ||
*/ | ||
static from(schema, obj, ctx) { | ||
const { keepUndefined, replacer } = ctx; | ||
const map = new this(schema); | ||
const add = (key, value) => { | ||
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(Pair.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[key]); | ||
} | ||
if (typeof schema.sortMapEntries === 'function') { | ||
map.items.sort(schema.sortMapEntries); | ||
} | ||
return map; | ||
} | ||
/** | ||
* Adds a value to the collection. | ||
@@ -33,0 +61,0 @@ * |
@@ -0,1 +1,2 @@ | ||
import { CreateNodeContext } from '../doc/createNode.js'; | ||
import type { BlockSequence, FlowCollection } from '../parse/cst.js'; | ||
@@ -58,2 +59,3 @@ import type { Schema } from '../schema/Schema.js'; | ||
toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string; | ||
static from(schema: Schema, obj: unknown, ctx: CreateNodeContext): YAMLSeq<unknown>; | ||
} |
'use strict'; | ||
var createNode = require('../doc/createNode.js'); | ||
var stringifyCollection = require('../stringify/stringifyCollection.js'); | ||
@@ -89,2 +90,17 @@ var Collection = require('./Collection.js'); | ||
} | ||
static from(schema, obj, ctx) { | ||
const { replacer } = ctx; | ||
const seq = new this(schema); | ||
if (obj && Symbol.iterator in Object(obj)) { | ||
let i = 0; | ||
for (let it of obj) { | ||
if (typeof replacer === 'function') { | ||
const key = obj instanceof Set ? it : String(i++); | ||
it = replacer.call(obj, key, it); | ||
} | ||
seq.items.push(createNode.createNode(it, undefined, ctx)); | ||
} | ||
} | ||
return seq; | ||
} | ||
} | ||
@@ -91,0 +107,0 @@ function asItemIndex(key) { |
'use strict'; | ||
var identity = require('../../nodes/identity.js'); | ||
var Pair = require('../../nodes/Pair.js'); | ||
var YAMLMap = require('../../nodes/YAMLMap.js'); | ||
function createMap(schema, obj, ctx) { | ||
const { keepUndefined, replacer } = ctx; | ||
const map = new YAMLMap.YAMLMap(schema); | ||
const add = (key, value) => { | ||
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(Pair.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[key]); | ||
} | ||
if (typeof schema.sortMapEntries === 'function') { | ||
map.items.sort(schema.sortMapEntries); | ||
} | ||
return map; | ||
} | ||
const map = { | ||
collection: 'map', | ||
createNode: createMap, | ||
default: true, | ||
@@ -41,5 +15,6 @@ nodeClass: YAMLMap.YAMLMap, | ||
return map; | ||
} | ||
}, | ||
createNode: (schema, obj, ctx) => YAMLMap.YAMLMap.from(schema, obj, ctx) | ||
}; | ||
exports.map = map; |
'use strict'; | ||
var createNode = require('../../doc/createNode.js'); | ||
var identity = require('../../nodes/identity.js'); | ||
var YAMLSeq = require('../../nodes/YAMLSeq.js'); | ||
function createSeq(schema, obj, ctx) { | ||
const { replacer } = ctx; | ||
const seq = new YAMLSeq.YAMLSeq(schema); | ||
if (obj && Symbol.iterator in Object(obj)) { | ||
let i = 0; | ||
for (let it of obj) { | ||
if (typeof replacer === 'function') { | ||
const key = obj instanceof Set ? it : String(i++); | ||
it = replacer.call(obj, key, it); | ||
} | ||
seq.items.push(createNode.createNode(it, undefined, ctx)); | ||
} | ||
} | ||
return seq; | ||
} | ||
const seq = { | ||
collection: 'seq', | ||
createNode: createSeq, | ||
default: true, | ||
@@ -32,5 +15,6 @@ nodeClass: YAMLSeq.YAMLSeq, | ||
return seq; | ||
} | ||
}, | ||
createNode: (schema, obj, ctx) => YAMLSeq.YAMLSeq.from(schema, obj, ctx) | ||
}; | ||
exports.seq = seq; |
import type { CreateNodeContext } from '../doc/createNode.js'; | ||
import type { Schema } from './Schema.js'; | ||
import type { Node } from '../nodes/Node.js'; | ||
@@ -9,2 +8,3 @@ import type { Scalar } from '../nodes/Scalar.js'; | ||
import type { StringifyContext } from '../stringify/stringify.js'; | ||
import type { Schema } from './Schema.js'; | ||
interface TagBase { | ||
@@ -75,10 +75,18 @@ /** | ||
* If set, used to select this tag when stringifying. | ||
* | ||
* If the class provides a static `from` method, then that | ||
* will be used if the tag object doesn't have a `createNode` method. | ||
*/ | ||
nodeClass?: new () => Node; | ||
nodeClass?: { | ||
new (schema?: Schema): Node; | ||
from?: (schema: Schema, obj: unknown, ctx: CreateNodeContext) => Node; | ||
}; | ||
/** | ||
* Turns a value into an AST node. | ||
* If returning a non-`Node` value, the output will be wrapped as a `Scalar`. | ||
* | ||
* Note: this is required if nodeClass is not provided. | ||
*/ | ||
resolve(value: YAMLMap.Parsed | YAMLSeq.Parsed, onError: (message: string) => void, options: ParseOptions): unknown; | ||
resolve?: (value: YAMLMap.Parsed | YAMLSeq.Parsed, onError: (message: string) => void, options: ParseOptions) => unknown; | ||
} | ||
export {}; |
import { ToJSContext } from '../../nodes/toJS.js'; | ||
import { YAMLSeq } from '../../nodes/YAMLSeq.js'; | ||
import { CreateNodeContext } from '../../util.js'; | ||
import type { Schema } from '../Schema.js'; | ||
import { CollectionTag } from '../types.js'; | ||
@@ -24,3 +26,4 @@ export declare class YAMLOMap extends YAMLSeq { | ||
toJSON(_?: unknown, ctx?: ToJSContext): unknown[]; | ||
static from(schema: Schema, iterable: unknown, ctx: CreateNodeContext): YAMLOMap; | ||
} | ||
export declare const omap: CollectionTag; |
@@ -44,2 +44,8 @@ 'use strict'; | ||
} | ||
static from(schema, iterable, ctx) { | ||
const pairs$1 = pairs.createPairs(schema, iterable, ctx); | ||
const omap = new this(); | ||
omap.items = pairs$1.items; | ||
return omap; | ||
} | ||
} | ||
@@ -68,8 +74,3 @@ YAMLOMap.tag = 'tag:yaml.org,2002:omap'; | ||
}, | ||
createNode(schema, iterable, ctx) { | ||
const pairs$1 = pairs.createPairs(schema, iterable, ctx); | ||
const omap = new YAMLOMap(); | ||
omap.items = pairs$1.items; | ||
return omap; | ||
} | ||
createNode: (schema, iterable, ctx) => YAMLOMap.from(schema, iterable, ctx) | ||
}; | ||
@@ -76,0 +77,0 @@ |
@@ -1,2 +0,1 @@ | ||
import type { Schema } from '../../schema/Schema.js'; | ||
import { Pair } from '../../nodes/Pair.js'; | ||
@@ -6,3 +5,5 @@ import { Scalar } from '../../nodes/Scalar.js'; | ||
import { YAMLMap } from '../../nodes/YAMLMap.js'; | ||
import type { Schema } from '../../schema/Schema.js'; | ||
import type { StringifyContext } from '../../stringify/stringify.js'; | ||
import { CreateNodeContext } from '../../util.js'; | ||
import type { CollectionTag } from '../types.js'; | ||
@@ -26,3 +27,4 @@ export declare class YAMLSet<T = unknown> extends YAMLMap<T, Scalar<null> | null> { | ||
toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string; | ||
static from(schema: Schema, iterable: unknown, ctx: CreateNodeContext): YAMLSet<unknown>; | ||
} | ||
export declare const set: CollectionTag; |
@@ -62,2 +62,13 @@ 'use strict'; | ||
} | ||
static from(schema, iterable, ctx) { | ||
const { replacer } = ctx; | ||
const set = new this(schema); | ||
if (iterable && Symbol.iterator in Object(iterable)) | ||
for (let value of iterable) { | ||
if (typeof replacer === 'function') | ||
value = replacer.call(iterable, value, value); | ||
set.items.push(Pair.createPair(value, null, ctx)); | ||
} | ||
return set; | ||
} | ||
} | ||
@@ -71,2 +82,3 @@ YAMLSet.tag = 'tag:yaml.org,2002:set'; | ||
tag: 'tag:yaml.org,2002:set', | ||
createNode: (schema, iterable, ctx) => YAMLSet.from(schema, iterable, ctx), | ||
resolve(map, onError) { | ||
@@ -82,13 +94,2 @@ if (identity.isMap(map)) { | ||
return map; | ||
}, | ||
createNode(schema, iterable, ctx) { | ||
const { replacer } = ctx; | ||
const set = new YAMLSet(schema); | ||
if (iterable && Symbol.iterator in Object(iterable)) | ||
for (let value of iterable) { | ||
if (typeof replacer === 'function') | ||
value = replacer.call(iterable, value, value); | ||
set.items.push(Pair.createPair(value, null, ctx)); | ||
} | ||
return set; | ||
} | ||
@@ -95,0 +96,0 @@ }; |
@@ -48,3 +48,3 @@ 'use strict'; | ||
parts | ||
.map(n => (n < 10 ? '0' + String(n) : String(n))) | ||
.map(n => String(n).padStart(2, '0')) | ||
.join(':') | ||
@@ -51,0 +51,0 @@ .replace(/000000\d*$/, '') // % 60 may introduce error |
{ | ||
"name": "yaml", | ||
"version": "2.3.0-4", | ||
"version": "2.3.0-5", | ||
"license": "ISC", | ||
@@ -5,0 +5,0 @@ "author": "Eemeli Aro <eemeli@gmail.com>", |
663241
17606