open-nomad/ui/app/utils/qp-serialize.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

22 lines
582 B
JavaScript
Raw Normal View History

import { computed } from '@ember/object';
// An unattractive but robust way to encode query params
2021-12-28 14:45:20 +00:00
export const serialize = (val) => {
if (typeof val === 'string' || typeof val === 'number') return val;
return val.length ? JSON.stringify(val) : '';
};
2021-12-28 14:45:20 +00:00
export const deserialize = (str) => {
try {
2021-12-28 14:45:20 +00:00
return JSON.parse(str).compact().without('');
} catch (e) {
return [];
}
};
// A computed property macro for deserializing a query param
2021-12-28 14:45:20 +00:00
export const deserializedQueryParam = (qpKey) =>
computed(qpKey, function () {
return deserialize(this.get(qpKey));
});