github.com/hernad/nomad@v1.6.112/ui/app/utils/qp-serialize.js (about)

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