-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support entities[].attribute for displaying an attribute instead of s…
…tate. Refactorings
- Loading branch information
1 parent
01a6c11
commit 037ae5b
Showing
6 changed files
with
1,445 additions
and
62 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
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,26 @@ | ||
// Helper to return an attribute if specified and present, | ||
// if not the value of state | ||
export function getAttributeOrState({ state, attributes }, attribute = false) { | ||
if (typeof attribute === "string" && attributes.hasOwnProperty(attribute)) { | ||
return attributes[attribute]; | ||
} | ||
return state; | ||
} | ||
|
||
export function mapObject(data, fn) { | ||
return Object.entries(data).reduce((result, [key, value]) => { | ||
return { | ||
...result, | ||
[key]: fn(value, key) | ||
}; | ||
}, {}); | ||
} | ||
|
||
export function parseEntity(entity) { | ||
if (typeof entity === "object") { | ||
return mapObject(entity, value => { | ||
return value === false ? null : value; | ||
}); | ||
} | ||
return { entity }; | ||
} |
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,38 @@ | ||
import test from "ava"; | ||
import { parseEntity, mapObject, getAttributeOrState } from "./utils"; | ||
|
||
// Test that entities both can be specified as a string or as an object | ||
test("parseEntity supports string + object", t => { | ||
const entity = "my.entity"; | ||
t.deepEqual(parseEntity(entity), { entity }); | ||
t.deepEqual(parseEntity({ entity }), { entity }); | ||
}); | ||
|
||
test("parseEntity nulls false values", t => { | ||
const fixture = { | ||
entity: "a", | ||
name: false | ||
}; | ||
t.is(parseEntity(fixture).name, null); | ||
}); | ||
|
||
test("parseEntity passes through all truthy values", t => { | ||
const fixture = { | ||
entity: "a", | ||
name: "B", | ||
foo: true | ||
}; | ||
t.deepEqual(parseEntity(fixture), fixture); | ||
}); | ||
|
||
test("getAttributeOrState returns only valid attribute", t => { | ||
const fixture = { | ||
state: "on", | ||
attributes: { | ||
active: true | ||
} | ||
}; | ||
|
||
t.is(getAttributeOrState(fixture, "active"), true); | ||
t.is(getAttributeOrState(fixture, "nope"), "on"); | ||
}); |
Oops, something went wrong.