UNPKG

@react-mvi/http

Version:

Http IO module for React MVI.

77 lines (76 loc) 3.24 kB
"use strict"; /** * The MIT License (MIT) * Copyright (c) Taketoshi Aono * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * @fileoverview * @author Taketoshi Aono */ Object.defineProperty(exports, "__esModule", { value: true }); var TYPE_MATCHER = /\[object ([^\]]+)\]/; var TO_STRING = Object.prototype.toString; /** * Convert object to query string. * @param data Data that need to convert to query string. * @param Query string. */ function qs(data) { var ret = []; serialize(data, ret); return ret.join('&'); } exports.qs = qs; /** * Get constructor type from any object. * @param value Object which want to inspect constructor type. */ function getType(value) { return TO_STRING.call(value).match(TYPE_MATCHER)[1]; } /** * Push query string to resultCollection. * @param data Value that want to convert to query string. * @param resultCollection Serialized object store. * @param parentKey ParentObject key if current data is object. */ function serialize(data, resultCollection, parentKey) { if (parentKey === void 0) { parentKey = ''; } var type = getType(data); if (type === 'Object') { for (var key in data) { var valueType = getType(data[key]); var keyValue = "" + (parentKey ? parentKey + "." : '') + key; if (valueType === 'String' || valueType === 'Number' || valueType === 'RegExp' || valueType === 'Boolean') { resultCollection.push(encodeURIComponent(keyValue) + "=" + encodeURIComponent(data[key])); } else if (valueType === 'Date') { resultCollection.push(encodeURIComponent(keyValue) + "=" + encodeURIComponent(String(+(data[key])))); } else if (valueType === 'Object') { serialize(data[key], resultCollection, key); } else if (valueType === 'Array') { serialize(data[key], resultCollection, key); } } } else if (type === 'Array') { for (var i = 0, len = data.length; i < len; i++) { resultCollection.push(encodeURIComponent(parentKey[i]) + "=" + encodeURIComponent(data[i])); } } }