Source RapidContext_Encode.js

1/*
2 * RapidContext <https://www.rapidcontext.com/>
3 * Copyright (c) 2007-2023 Per Cederberg. All rights reserved.
4 *
5 * This program is free software: you can redistribute it and/or
6 * modify it under the terms of the BSD license.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * See the RapidContext LICENSE for more details.
13 */
14
15/**
16 * Provides functions for encoding and decoding data.
17 * @namespace RapidContext.Encode
18 */
19(function (window) {
20
21    /**
22     * Returns the unicode escape sequence for a character.
23     *
24     * @param {string} chr the single character string
25     * @return {string} the unicode escape sequence
26     * @private
27     */
28    function toCharEscape(chr) {
29        return "\\u" + chr.charCodeAt(0).toString(16).padStart(4, "0");
30    }
31
32    /**
33     * Serializes a value to JSON. The value is serialized by
34     * `JSON.stringify`, but unicode escape sequences are inserted
35     * for any non-printable ASCII characters.
36     *
37     * @param {Object} val the value to serialize
38     * @return {string} the JSON string
39     * @memberof RapidContext.Encode
40     */
41    function toJSON(val) {
42        val = (val == null) ? null : val;
43        return JSON.stringify(val).replace(/[\u007F-\uFFFF]/g, toCharEscape);
44    }
45
46    /**
47     * Serializes a value to a URL component. The value is serialized
48     * by `encodeURIComponent`, but any non-String values are first
49     * converted to strings.
50     *
51     * @param {Object} val the value to serialize
52     * @param {Object} isForm the flag for using `+` instead of `%20`
53     * @return {string} the URL-encoded string
54     * @memberof RapidContext.Encode
55     */
56    function toUrlPart(val, isForm) {
57        val = (val == null) ? "" : val;
58        var isObject = typeof(val) === "object";
59        var res = encodeURIComponent(isObject ? toJSON(val) : String(val));
60        return isForm ? res.replace(/%20/g, "+") : res;
61    }
62
63    /**
64     * Serializes an object to a URL query string. If an object value
65     * is an array, each entry will be added to the query separately.
66     *
67     * @param {Object} val the key-value pairs to serialize
68     * @param {Object} isForm the flag for using `+` instead of `%20`
69     * @return {string} the URL-encoded query string
70     * @memberof RapidContext.Encode
71     */
72    function toUrlQuery(data, isForm) {
73        data = data || {};
74        var parts = [];
75        for (var key in data) {
76            var val = data[key];
77            var arr = Array.isArray(val) ? val : [val];
78            arr.length || arr.push("");
79            arr.forEach(function (v) {
80                parts.push(toUrlPart(key, isForm) + "=" + toUrlPart(v, isForm));
81            });
82        }
83        return parts.join("&");
84    }
85
86    // Create namespaces & export symbols
87    var RapidContext = window.RapidContext || (window.RapidContext = {});
88    var Encode = RapidContext.Encode || (RapidContext.Encode = {});
89    Object.assign(Encode, { toJSON, toUrlPart, toUrlQuery });
90
91})(this);
92