Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: merge keys #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/yamlDocQuery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,36 @@ describe('yamlDocQuery', () => {
});
});

it('handles aliases merge keys', () => {
expect($(parseDocument('[ &x { X: 42 }, Y, <<: *x ]', {merge: true}))[2].X()).toEqual({
range: [10, 12, 13],
value: 42,
});
});

it('handles aliases merge keys properties', () => {
expect($(parseDocument(`foo: &BAR
bar: 42
yo:
<<: *BAR
mama: 4`, {merge: true})).yo.mama()).toEqual({
range: [43, 44, 44],
value: 4,
});
});

it('handles aliases merge keys override properties', () => {
expect($(parseDocument(`foo: &BAR
bar: 42
yo:
<<: *BAR
bar: 69
mama: 4`, {merge: true})).yo.bar()).toEqual({
range: [42, 44, 45],
value: 69,
});
});

it('gets position when source is made available', () => {
const source = `
one:
Expand Down
53 changes: 32 additions & 21 deletions src/yamlDocQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,7 @@ export function process(
{
get(_, prop): any {
if (isMap<Node, Node>(node)) {
return process(
node.items.find(({ key }) => {
if (!isScalar(key)) {
throw new RangeError(
`Unexpected ${key.constructor.name}`,
key.range || undefined,
);
}
if (!isValidIndexType(key.value)) {
throw new RangeError(
`Unexpected ${typeof key.value}`,
key.range || undefined,
);
}

return (
(typeof key.value === 'number'
? String(key.value)
: key.value) === prop
);
})?.value,
return process(findNode(prop, node, document)?.value,
document,
offset,
source,
Expand All @@ -127,6 +107,37 @@ export function process(
);
}

function findNode(prop: string | symbol, node: any, document: Document) : any {
const foundItem = node.items.find(({ key, value }: {key:any, value:any}) => {
if (!isScalar(key)) {
throw new RangeError(
`Unexpected ${key.constructor.name}`,
key.range || undefined,
);
}
if (!isValidIndexType(key.value)) {
throw new RangeError(
`Unexpected ${typeof key.value}`,
key.range || undefined,
);
}

return (
(typeof key.value === 'number'
? String(key.value)
: key.value) === prop
);
});

if (foundItem) {
return foundItem;
}

if (document.schema.merge && isAlias(node.items[0]?.value) && node.items[0]?.key.value === '<<') {
return findNode(prop, node.items[0].value.resolve(document), document);
}
}

export function toPosition(
range: Range,
source: string,
Expand Down