Merge pull request #812 from hashicorp/refactor-filter-mocks
refactor: 💡 simplify/improve evaluator record serialization
This commit is contained in:
1 file changed
+3
-15
@@ -1,19 +1,7 @@
|
||||
import { makeEvaluator } from 'js-bexpr';
|
||||
import { underscore } from '@ember/string';
|
||||
import SessionSerializer from '../serializers/session';
|
||||
|
||||
/**
|
||||
* Because Mirage, a record cannot be serialized outside of a route handler
|
||||
* context. Here is a quick and dirty attempt to serialize attribute names
|
||||
* to underscore format. This is not entirely realistic, but may suffice.
|
||||
*/
|
||||
const quickSerialize = (item) => {
|
||||
const json = item.toJSON();
|
||||
Object.keys(json).forEach((key) => {
|
||||
const newKey = underscore(key);
|
||||
json[newKey] = json[key];
|
||||
});
|
||||
return json;
|
||||
};
|
||||
const serializer = new SessionSerializer();
|
||||
|
||||
/**
|
||||
* Creates a filter function that operates on a single object, which itself
|
||||
@@ -24,7 +12,7 @@ export default (expression) => {
|
||||
const evaluator = makeEvaluator(expression);
|
||||
// Since API query selectors begin with `/item`, the instance is wrapped.
|
||||
return (item) => {
|
||||
const json = quickSerialize(item);
|
||||
const json = serializer.serialize(item);
|
||||
return evaluator({ item: json });
|
||||
};
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user