UNPKG

app-decorators

Version:

Collection of useful ES7 Decorators, writtin in ES6, that can be used for building webapps

141 lines (115 loc) 5.57 kB
System.register(['app-decorators/src/libs/element-to-function'], function (_export, _context) { "use strict"; var _elementToFunc, queryString; return { setters: [function (_appDecoratorsSrcLibsElementToFunction) { _elementToFunc = _appDecoratorsSrcLibsElementToFunction.default; }], execute: function () { _export('queryString', queryString = { parse: function parse() { var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; // cleanup query = query.trim().replace(/^(\?|#|&)/g, ''); if (!query || query === ' ') { return {}; } // init var searchParams = new URLSearchParams(decodeURIComponent(query)); var queryObject = Object.create(null); // build var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = searchParams.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var param = _step.value; var key = param[0]; var value = param[1] || null; var propertyValue = queryObject[key]; // foo=bar&key=val, foo&key if (propertyValue === undefined && propertyValue !== null) { queryObject[key] = value; } // foo=bar&foo=baz else { if (this.classof(propertyValue) !== 'Array') { queryObject[key] = [propertyValue]; } queryObject[key].push(value); } } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } return queryObject; }, stringify: function stringify() { var _this = this; var queryObject = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var encode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; // '', 0, 1, [], etc if (this.classof(queryObject) !== 'Object') { return ''; } // {} var queryObjectKeys = Object.keys(queryObject); if (!queryObjectKeys.length) { return ''; } var raw_query = []; queryObjectKeys.forEach(function (key) { if (_this.classof(queryObject[key]) === 'Array') { queryObject[key].forEach(function (propValue) { // { bar: [undefined, 'baz'] } if (propValue === undefined) return; // bar: [null, 'baz'] if (propValue === null) { raw_query.push(key); } // { foo: ['bar', 'baz'] } else { raw_query.push(key + '=' + propValue); } }); return; } // { abc: undefined } if (queryObject[key] === undefined) return; // { 'def': null } if (queryObject[key] === null) { raw_query.push(key); } // { foo: 'baz' } else { raw_query.push(key + '=' + queryObject[key]); } }); var queryString = raw_query.join('&'); if (encode) { var _ref = new URL('http://localhost?' + queryString), search = _ref.search; queryString = search.replace(/^\?/g, ''); } return queryString; }, classof: function classof(value) { return Object.prototype.toString.call(value).slice(8, -1); } }); _export('queryString', queryString); } }; }); //# sourceMappingURL=queryString.js.map