Skip to content

Commit c4395db

Browse files
authored
feat(nodes): expose some positional info (#15)
1 parent 59d2698 commit c4395db

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/__tests__/tree.spec.ts

+68
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,74 @@ describe('SchemaTree', () => {
679679
});
680680
});
681681

682+
describe('position', () => {
683+
let schema: JSONSchema4;
684+
685+
beforeEach(() => {
686+
schema = {
687+
type: ['string', 'object'],
688+
properties: {
689+
ids: {
690+
type: 'array',
691+
items: {
692+
type: 'integer',
693+
},
694+
},
695+
tag: {
696+
type: 'string',
697+
},
698+
uuid: {
699+
type: 'string',
700+
},
701+
},
702+
};
703+
});
704+
705+
it('given node being the only child, should have correct position info', () => {
706+
const tree = new SchemaTree(schema);
707+
tree.populate();
708+
709+
const node = tree.root.children[0];
710+
711+
expect(node.isFirst).toBe(true);
712+
expect(node.isLast).toBe(true);
713+
expect(node.pos).toEqual(0);
714+
});
715+
716+
it('given node being the first child among other children, should have correct position info', () => {
717+
const tree = new SchemaTree(schema);
718+
tree.populate();
719+
720+
const node = (tree.root.children[0] as RegularNode).children![0];
721+
722+
expect(node.isFirst).toBe(true);
723+
expect(node.isLast).toBe(false);
724+
expect(node.pos).toEqual(0);
725+
});
726+
727+
it('given node being the last child among other children, should have correct position info', () => {
728+
const tree = new SchemaTree(schema);
729+
tree.populate();
730+
731+
const node = (tree.root.children[0] as RegularNode).children![2];
732+
733+
expect(node.isFirst).toBe(false);
734+
expect(node.isLast).toBe(true);
735+
expect(node.pos).toEqual(2);
736+
});
737+
738+
it('given node not being the first nor the child among other children, should have correct position info', () => {
739+
const tree = new SchemaTree(schema);
740+
tree.populate();
741+
742+
const node = (tree.root.children[0] as RegularNode).children![1];
743+
744+
expect(node.isFirst).toBe(false);
745+
expect(node.isLast).toBe(false);
746+
expect(node.pos).toEqual(1);
747+
});
748+
});
749+
682750
describe('mirroring', () => {
683751
describe('circular references', () => {
684752
it('should self expand', () => {

src/nodes/BaseNode.ts

+16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ export abstract class BaseNode {
1919
return this.parent === null ? 0 : this.parent.depth + 1;
2020
}
2121

22+
private get parentChildren(): BaseNode[] {
23+
return (this.parent?.children ?? []) as BaseNode[];
24+
}
25+
26+
public get pos(): number {
27+
return Math.max(0, this.parentChildren.indexOf(this));
28+
}
29+
30+
public get isFirst(): boolean {
31+
return this.pos === 0;
32+
}
33+
34+
public get isLast(): boolean {
35+
return this.pos === this.parentChildren.length - 1;
36+
}
37+
2238
protected constructor(public readonly fragment: SchemaFragment) {
2339
this.id = String(SEED++);
2440
this.subpath = [];

0 commit comments

Comments
 (0)