UNPKG

@coolgk/utils

Version:

javascript, typescript utility and wrapper functions and classes: array, string, base64, ampq, bcrypt, cache, captcha, csv, email, jwt, number, pdf, tmp, token, unit conversion, url params, session, form data, google sign in, facebook sign in

44 lines (42 loc) 1.24 kB
/*! * @package @coolgk/utils * @version 3.1.4 * @link https://github.com/coolgk/node-utils * @license MIT * @author Daniel Gong <daniel.k.gong@gmail.com> * * Copyright (c) 2017 Daniel Gong <daniel.k.gong@gmail.com>. All rights reserved. * Licensed under the MIT License. */ "use strict"; /*! * Copyright (c) 2017 Daniel Gong <daniel.k.gong@gmail.com>. All rights reserved. * Licensed under the MIT License. */ Object.defineProperty(exports, "__esModule", { value: true }); function getParams(url, pattern) { const params = {}; if (pattern[0] !== '/') { pattern = '/' + pattern; } (pattern.match(/:\w+/g) || []).forEach((name, index) => { name = name.substr(1); params[name] = ''; Object.defineProperty(params, index + 1, { set: (value) => { params[name] = value; } }); }); const match = (new RegExp(pattern.replace(/\/:\w+/g, '/?([^\/#\?]*)'), 'gi')).exec(url); if (match) { match.forEach((value, index) => { if (index) { params[index] = decodeURIComponent(value); } }); } return params; } exports.getParams = getParams; exports.default = getParams;