github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/utils/qp-serialize.js (about)

     1  import { computed } from '@ember/object';
     2  
     3  // An unattractive but robust way to encode query params
     4  export const serialize = (val) => {
     5    if (typeof val === 'string' || typeof val === 'number') return val;
     6    return val.length ? JSON.stringify(val) : '';
     7  };
     8  
     9  export const deserialize = (str) => {
    10    try {
    11      return JSON.parse(str).compact().without('');
    12    } catch (e) {
    13      return [];
    14    }
    15  };
    16  
    17  // A computed property macro for deserializing a query param
    18  export const deserializedQueryParam = (qpKey) =>
    19    computed(qpKey, function () {
    20      return deserialize(this.get(qpKey));
    21    });