Skip to content

Commit

Permalink
feat: next alpha version
Browse files Browse the repository at this point in the history
  • Loading branch information
belgattitude committed Aug 26, 2024
1 parent 9df4903 commit f0227c3
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 116 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-ghosts-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@httpx/treeu": minor
---

Add toTreeNode in mapper
274 changes: 180 additions & 94 deletions packages/treeu/src/mapper/__tests__/flat-tree-ws-mapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,148 +10,234 @@ type CustomValue =
| { type: 'file'; size: number };

describe('FlatTreeWsMapper', () => {
describe('toTreeNodes4()', () => {
describe('when a valid FlatTreeWs is given', () => {
const paths: FlatTreeWs<CustomValue> = [
{
key: 'file1.ts',
value: { type: 'file', size: 10 },
},
{
key: 'file2.ts',
value: { type: 'file', size: 20 },
const vadidFlatTreeWs: FlatTreeWs<CustomValue> = [
{
key: 'file1.ts',
value: { type: 'file', size: 10 },
},
{
key: 'file2.ts',
value: { type: 'file', size: 20 },
},
{
key: 'folder1',
value: { type: 'folder' },
},
{
key: 'folder1/file1.ts',
value: { type: 'file', size: 30 },
},
{
key: 'folder2',
value: { type: 'folder' },
},
{
key: 'folder2/file1.ts',
value: { type: 'file', size: 40 },
},
{
key: 'folder2/subfolder1',
value: { type: 'folder' },
},
{
key: 'folder2/subfolder1/file1.ts',
value: { type: 'file', size: 50 },
},
{
key: 'folder3',
value: { type: 'folder' },
},
];

const validTreeNodes: TreeNode<CustomValue>[] = [
{
id: 'file1.ts',
parentId: null,
value: {
size: 10,
type: 'file',
},
children: [],
},
{
id: 'file2.ts',
parentId: null,
value: {
size: 20,
type: 'file',
},
children: [],
},
{
id: 'folder1',
parentId: null,
value: {
type: 'folder',
},
children: [
{
id: 'folder1/file1.ts',
parentId: 'folder1',
value: {
size: 30,
type: 'file',
},
children: [],
},
{
key: 'folder1',
value: { type: 'folder' },
],
},
{
id: 'folder2',
parentId: null,
value: {
type: 'folder',
},
children: [
{
id: 'folder2/file1.ts',
parentId: 'folder2',
value: {
size: 40,
type: 'file',
},
children: [],
},
{
key: 'folder1/file1.ts',
value: { type: 'file', size: 30 },
id: 'folder2/subfolder1',
parentId: 'folder2',
value: {
type: 'folder',
},
children: [
{
id: 'folder2/subfolder1/file1.ts',
parentId: 'folder2/subfolder1',
value: {
size: 50,
type: 'file',
},
children: [],
},
],
},
],
},
{
id: 'folder3',
parentId: null,
value: {
type: 'folder',
},
children: [],
},
];

describe('fromTreeNodes', () => {
it('should return a valid flat tree', () => {
const mapper = new FlatTreeWsMapper<CustomValue>();
const treeNodes = mapper.fromTreeNodes(validTreeNodes, {
method: 'breadth-first',
});

const flattenedBreadthFirst: FlatTreeWs<CustomValue> = [
{
key: 'folder2',
value: { type: 'folder' },
key: 'file1.ts',
value: {
size: 10,
type: 'file',
},
},
{
key: 'folder2/file1.ts',
value: { type: 'file', size: 40 },
key: 'file2.ts',
value: {
size: 20,
type: 'file',
},
},
{
key: 'folder2/subfolder1',
value: { type: 'folder' },
key: 'folder1',
value: {
type: 'folder',
},
},
{
key: 'folder2/subfolder1/file1.ts',
value: { type: 'file', size: 50 },
key: 'folder2',
value: {
type: 'folder',
},
},
{
key: 'folder3',
value: { type: 'folder' },
},
];

const expected: TreeNode<CustomValue>[] = [
{
id: 'file1.ts',
parentId: null,
value: {
size: 10,
type: 'file',
type: 'folder',
},
children: [],
},
{
id: 'file2.ts',
parentId: null,
key: 'folder1/file1.ts',
value: {
size: 20,
size: 30,
type: 'file',
},
children: [],
},
{
id: 'folder1',
parentId: null,
key: 'folder2/file1.ts',
value: {
type: 'folder',
size: 40,
type: 'file',
},
children: [
{
id: 'folder1/file1.ts',
parentId: 'folder1',
value: {
size: 30,
type: 'file',
},
children: [],
},
],
},
{
id: 'folder2',
parentId: null,
key: 'folder2/subfolder1',
value: {
type: 'folder',
},
children: [
{
id: 'folder2/file1.ts',
parentId: 'folder2',
value: {
size: 40,
type: 'file',
},
children: [],
},
{
id: 'folder2/subfolder1',
parentId: 'folder2',
value: {
type: 'folder',
},
children: [
{
id: 'folder2/subfolder1/file1.ts',
parentId: 'folder2/subfolder1',
value: {
size: 50,
type: 'file',
},
children: [],
},
],
},
],
},
{
id: 'folder3',
parentId: null,
key: 'folder2/subfolder1/file1.ts',
value: {
type: 'folder',
size: 50,
type: 'file',
},
children: [],
},
];

it('should return a success result with expected treeNodes', () => {
const treeResult = new FlatTreeWsMapper().toTreeNodes(paths, {
separator: '/',
});
expect(treeNodes).toHaveLength(vadidFlatTreeWs.length);
expect(treeNodes).toStrictEqual(flattenedBreadthFirst);

// Back and forth
const treeNodes2 = mapper.toTreeNodesOrThrow(flattenedBreadthFirst, {
separator: '/',
});

const flattened2 = mapper.fromTreeNodes(treeNodes2, {
method: 'breadth-first',
});
expect(flattened2).toStrictEqual(flattenedBreadthFirst);
});
});
describe('toTreeNodes()', () => {
describe('when a valid FlatTreeWs is given', () => {
it('should return a success result with validTreeNodes validTreeNodes', () => {
const treeResult = new FlatTreeWsMapper<CustomValue>().toTreeNodes(
vadidFlatTreeWs,
{
separator: '/',
}
);
expect(treeResult).toStrictEqual({
success: true,
treeNodes: expected,
treeNodes: validTreeNodes,
});
});

it.skip('should be insensitive to path order', () => {
const reversedPaths = paths.slice().reverse();
const reversedPaths = vadidFlatTreeWs.slice().reverse();
const treeResult = new FlatTreeWsMapper().toTreeNodes(reversedPaths, {
separator: '/',
});
expect(treeResult).toStrictEqual({
success: true,
treeNodes: expected,
treeNodes: validTreeNodes,
});
});
});
Expand Down
Loading

0 comments on commit f0227c3

Please sign in to comment.