2019-02-12 04:00:45 +00:00
|
|
|
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) => {
|
2021-04-29 20:00:59 +00:00
|
|
|
if (typeof val === 'string' || typeof val === 'number') return val;
|
|
|
|
return val.length ? JSON.stringify(val) : '';
|
|
|
|
};
|
2019-02-12 04:00:45 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
export const deserialize = (str) => {
|
2019-02-12 04:00:45 +00:00
|
|
|
try {
|
2021-12-28 14:45:20 +00:00
|
|
|
return JSON.parse(str).compact().without('');
|
2019-02-12 04:00:45 +00:00
|
|
|
} 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 () {
|
2019-02-12 04:00:45 +00:00
|
|
|
return deserialize(this.get(qpKey));
|
|
|
|
});
|