generated from custom-cards/boilerplate-card
-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e84c9e1
commit 71354bd
Showing
6 changed files
with
130 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import type { Config } from '../src/types'; | ||
import { filterConfigByZoomEntity } from '../src/zoom'; | ||
|
||
const config = { | ||
type: '', | ||
sections: [ | ||
{ | ||
entities: [ | ||
{ | ||
entity_id: 'ent1', | ||
children: ['ent2', 'ent3'], | ||
}, | ||
], | ||
}, | ||
{ | ||
entities: [ | ||
{ | ||
entity_id: 'ent2', | ||
children: ['ent4'], | ||
}, | ||
{ | ||
entity_id: 'ent3', | ||
children: ['ent5'], | ||
}, | ||
], | ||
}, | ||
{ | ||
entities: [ | ||
{ | ||
entity_id: 'ent4', | ||
children: [], | ||
}, | ||
{ | ||
entity_id: 'ent5', | ||
children: [], | ||
}, | ||
], | ||
}, | ||
], | ||
} as Config; | ||
|
||
describe('zoom action', () => { | ||
it('filters a config based on zoom entity', async () => { | ||
expect(filterConfigByZoomEntity(config, config.sections[1].entities[0])).toEqual({ | ||
type: '', | ||
sections: [ | ||
{ | ||
entities: [ | ||
{ | ||
entity_id: 'ent2', | ||
children: ['ent4'], | ||
}, | ||
], | ||
}, | ||
{ | ||
entities: [ | ||
{ | ||
entity_id: 'ent4', | ||
children: [], | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
it('returns the same config when there is no zoom entity', async () => { | ||
expect(filterConfigByZoomEntity(config, undefined)).toEqual(config); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Config, EntityConfigInternal } from "./types"; | ||
|
||
export function filterConfigByZoomEntity(config: Config, zoomEntity?: EntityConfigInternal) { | ||
if (!zoomEntity) { | ||
return config; | ||
} | ||
let children: string[] = []; | ||
const newSections = config.sections.map(section => { | ||
const newEntities = section.entities.filter(entity => entity === zoomEntity || children.includes(entity.entity_id)); | ||
children = newEntities.flatMap(entity => entity.children); | ||
return { | ||
...section, | ||
entities: newEntities, | ||
}; | ||
}).filter(section => section.entities.length > 0); | ||
|
||
return { | ||
...config, | ||
sections: newSections, | ||
}; | ||
} |