@stoplight/yaml
Advanced tools
Comparing version 3.3.2 to 3.4.0
{ | ||
"name": "@stoplight/yaml", | ||
"version": "3.3.2", | ||
"version": "3.4.0", | ||
"description": "Useful functions when working with YAML.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -37,2 +37,3 @@ "use strict"; | ||
}; | ||
const KEYS = Symbol('object_keys'); | ||
exports.walkAST = (node, options, duplicatedMappingKeys) => { | ||
@@ -42,3 +43,4 @@ if (node) { | ||
case types_2.Kind.MAP: { | ||
const container = {}; | ||
const preserveKeyOrder = options !== void 0 && options.preserveKeyOrder === true; | ||
const container = createMapContainer(preserveKeyOrder); | ||
const seenKeys = []; | ||
@@ -63,8 +65,20 @@ const handleMergeKeys = options !== void 0 && options.mergeKeys === true; | ||
if (handleMergeKeys && key === "<<") { | ||
Object.assign(container, reduceMergeKeys(exports.walkAST(mapping.value, options, duplicatedMappingKeys))); | ||
const reduced = reduceMergeKeys(exports.walkAST(mapping.value, options, duplicatedMappingKeys), preserveKeyOrder); | ||
if (preserveKeyOrder && reduced !== null) { | ||
for (const reducedKey of Object.keys(reduced)) { | ||
pushKey(container, reducedKey); | ||
} | ||
} | ||
Object.assign(container, reduced); | ||
} | ||
else { | ||
container[mapping.key.value] = exports.walkAST(mapping.value, options, duplicatedMappingKeys); | ||
container[key] = exports.walkAST(mapping.value, options, duplicatedMappingKeys); | ||
if (preserveKeyOrder) { | ||
pushKey(container, key); | ||
} | ||
} | ||
} | ||
if (KEYS in container) { | ||
container[KEYS].push(KEYS); | ||
} | ||
return container; | ||
@@ -198,8 +212,49 @@ } | ||
}; | ||
const reduceMergeKeys = (items) => { | ||
const reduceMergeKeys = (items, preserveKeyOrder) => { | ||
if (Array.isArray(items)) { | ||
return items.reduceRight((merged, item) => Object.assign(merged, item), {}); | ||
const reduced = items.reduceRight(preserveKeyOrder | ||
? (merged, item) => { | ||
const keys = Object.keys(item); | ||
for (let i = keys.length - 1; i >= 0; i--) { | ||
unshiftKey(merged, keys[i]); | ||
} | ||
return Object.assign(merged, item); | ||
} | ||
: (merged, item) => Object.assign(merged, item), createMapContainer(preserveKeyOrder)); | ||
if (preserveKeyOrder) { | ||
reduced[KEYS].push(KEYS); | ||
} | ||
return reduced; | ||
} | ||
return typeof items !== 'object' || items === null ? null : Object(items); | ||
}; | ||
const traps = { | ||
ownKeys(target) { | ||
return target[KEYS]; | ||
}, | ||
}; | ||
function createMapContainer(preserveKeyOrder) { | ||
if (preserveKeyOrder) { | ||
const container = new Proxy({}, traps); | ||
Reflect.defineProperty(container, KEYS, { | ||
value: [], | ||
}); | ||
return container; | ||
} | ||
return {}; | ||
} | ||
function deleteKey(container, key) { | ||
const index = key in container ? container[KEYS].indexOf(key) : -1; | ||
if (index !== -1) { | ||
container[KEYS].splice(index, 1); | ||
} | ||
} | ||
function unshiftKey(container, key) { | ||
deleteKey(container, key); | ||
container[KEYS].unshift(key); | ||
} | ||
function pushKey(container, key) { | ||
deleteKey(container, key); | ||
container[KEYS].push(key); | ||
} | ||
//# sourceMappingURL=parseWithPointers.js.map |
@@ -7,2 +7,3 @@ import { IParserResult, Optional } from '@stoplight/types'; | ||
mergeKeys?: boolean; | ||
preserveKeyOrder?: boolean; | ||
} | ||
@@ -9,0 +10,0 @@ export declare type YAMLBaseNode<K extends Kind> = Omit<YAMLAstParser.YAMLNode, 'kind' | 'parent'> & { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60997
671