js-web-tools
Version:
Tools for Javascript develpers
84 lines (65 loc) • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isString = isString;
exports.trim = trim;
exports.toNumber = toNumber;
exports.isObject = isObject;
exports.isWindows = isWindows;
exports.parseQuery = void 0;
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function isString(src) {
return src && (typeof src === 'string' || src instanceof String);
}
function trim(src) {
if (isString(src)) {
/* istanbul ignore else */
if (src.trim) {
return src.trim();
}
return src.replace(/(^\s*)|(\s*$)/g, '');
}
return src;
}
function toNumber(val) {
if (typeof val === 'number' || val instanceof Number) {
return val;
}
if (typeof val === 'string') {
return parseInt(val, 10) || 0;
}
return 0;
}
function isObject(value) {
var type = _typeof(value);
return type === 'function' || type === 'object' && !!value;
}
function isWindows() {
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return process.platform !== 'win32' || opts.windows === true;
}
var parseQuery = function parseQuery() {
var search = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
if (search === '') {
return null;
}
var decodeSearch = decodeURIComponent(search);
var s = decodeSearch.substring(decodeSearch.indexOf('?'));
var query = s.replace(/^\?/, '');
return query.split('&').reduce(function (obj, v) {
var pair = v.split('=');
/* istanbul ignore else */
if (pair.length === 2) {
var name = pair[0].replace(/[[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp("([\\\\?&]*)".concat(name, "=([^&#]*)"));
var results = regex.exec(s);
/* istanbul ignore else */
if (results && results.length >= 3) {
obj[name] = results[2].replace(/\+/g, ' ');
}
}
return obj;
}, {});
};
exports.parseQuery = parseQuery;