How to use regexparam - 9 common examples

To help you get started, we’ve selected a few regexparam examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lavyun / better-mock / src / core / mocked.ts View on Github external
private _matchUrl(expected: string | RegExp | undefined, actual: string): boolean {
    if (isString(expected)) {
      if (expected === actual) {
        return true
      }

      // expected: /hello/world
      // actual: /hello/world?type=1
      if (actual.indexOf(expected) === 0 && actual[expected.length] === '?') {
        return true
      }
      
      if (expected.indexOf('/') === 0) {
        return rgx(expected).pattern.test(actual)
      }
    }
    if (isRegExp(expected)) {
      return expected.test(actual)
    }
    return false
  }
github lukeed / navaid / builder.js View on Github external
const fs = require('fs');
const mkdir = require('mk-dirs');
const pretty = require('pretty-bytes');
const { minify } = require('terser');
const sizer = require('gzip-size');
const pkg = require('./package');
const ESM = fs.readFileSync('src/index.js', 'utf8');
const regexparam = require('regexparam').toString();

mkdir('dist').then(() => {
	// Copy as is for ESM
	fs.writeFileSync(pkg.module, ESM);

	// Mutate (im|ex)ports for CJS
	const CJS = regexparam.replace('function ', 'function convert ').concat(
		ESM.replace(`import convert from 'regexparam';`, '')
			.replace(/export default/, 'module.exports =')
	);

	fs.writeFileSync(pkg.main, CJS);

	// Minify & print gzip-size
	const { code } = minify(CJS, { toplevel:true, compress:{ passes:10 } });
	console.log(`> gzip size: ${pretty(sizer.sync(code))}`);
github ItalyPaleAle / svelte-spa-router / active.js View on Github external
path = path.substring(1)
        }
    }

    // Default class name
    if (!className) {
        className = 'active'
    }

    // Path must start with '/' or '*'
    if (!path || path.length < 1 || (path.charAt(0) != '/' && path.charAt(0) != '*')) {
        throw Error('Invalid value for "path" argument')
    }

    // Get the regular expression
    const {pattern} = regexparam(path)

    // Add the node to the list
    const el = {
        node,
        className,
        pattern
    }
    nodes.push(el)

    // Trigger the action right away
    checkActive(el)

    return {
        // When the element is destroyed, remove it from the list
        destroy() {
            nodes.splice(nodes.indexOf(el), 1)
github lukeed / route-manifest / bin / index.js View on Github external
const fs = require('fs');
const mkdir = require('mk-dirs');
const pretty = require('pretty-bytes');
const { minify } = require('terser');
const sizer = require('gzip-size');
const pkg = require('../package');

const ESM = fs.readFileSync('src/index.js', 'utf8');
const regexparam = require('regexparam').toString();

mkdir('dist').then(() => {
	// Copy as is for ESM
	fs.writeFileSync(pkg.module, ESM);

	// Mutate (im|ex)ports for CJS
	const CJS = ESM.replace(
		`import toRegExp from 'regexparam';`,
		`var toRegExp = require('regexparam');`
	).replace(/export default/, 'module.exports =')

	fs.writeFileSync(pkg.main, CJS);

	// Mutate again for UMD prep :: inline dependency
	const INLINED = regexparam.replace('function ', 'function toRegExp').concat(
		CJS.replace(`var toRegExp = require('regexparam');`, '')
github lukeed / route-manifest / src / index.js View on Github external
export default function (manifest, uri, wCommon) {
	var k,
		tmp = wCommon && manifest['*'] || [],
		headers = tmp.headers || [],
		files = tmp.files || tmp;

	for (k in manifest) {
		if (k != '*' && toRegExp(k).pattern.test(uri)) {
			tmp = manifest[k];
			files = files.concat(tmp.files || tmp);
			headers = headers.concat(tmp.headers || []);
			break;
		}
	}

	return {
		files: files,
		headers: headers,
	};
}
github IBM / nicedoc.io / core / github / fetch-repo.js View on Github external
import { join as pathJoin } from 'path'
import regexParam from 'regexparam'

import fetch from 'isomorphic-unfetch'

const githubBlobUrl = regexParam('blob/:ref/*')

const isEmpty = obj =>
  [Object, Array].includes((obj || {}).constructor) && !Object.entries(obj || {}).length

const exec = (path, result) => {
  let i = 0
  let out = {}
  let matches = result.pattern.exec(path) || {}
  while (i < result.keys.length) out[result.keys[i]] = matches[++i] || null
  return out
}

export default ({ isMarkdownPath, GITHUB_TOKEN, ALTERNATIVE_README_NAMES }) => {
  const fetchReadme = ({ owner, repo, path, ref }) => {
    return fetch(`https://api.github.com/repos/${owner}/${repo}/contents/${path}?ref=${ref}`, {
      headers: {
github IBM / nicedoc.io / core / build-readme.js View on Github external
REGEX_HTTP_PROTOCOL,
  SITE_URL,
  REGEX_LOCAL_URL,
  REGEX_START_WITH_LETTER_OR_NUMBER
} = require('../constants')

const { URL } = url

const extension = (str = '') => {
  const urlObj = url.parse(str)
  urlObj.hash = ''
  urlObj.search = ''
  return fileExtension(url.format(urlObj))
}

const { pattern: githubRegexParam } = regexParam('/:owner/:repo')

const loadHTML = html =>
  cheerio.load(html, {
    xmlMode: false,
    lowerCaseTags: true,
    decodeEntities: true,
    lowerCaseAttributeNames: true
  })

const resolveUrl = (from, to) => {
  if (to[0] === '/') to = to.substr(1)
  return url.resolve(from, to)
}

const withAnchorLinks = $ => {
  $('h1, h2, h3, h4, h5, h6').each(function () {
github lukeed / navaid / src / index.js View on Github external
$.on = function (pat, fn) {
		(pat = convert(pat)).fn = fn;
		routes.push(pat);
		return $;
	}
github IBM / nicedoc.io / core / github / build-readme / index.js View on Github external
import url from 'url'
import qsm from 'qsm'

import build from '../../build'
import { extension, resolveUrl, resolveAssetUrl } from './util'

const {
  REGEX_HTTP_PROTOCOL,
  SITE_URL,
  REGEX_LOCAL_URL,
  REGEX_START_WITH_LETTER_OR_NUMBER
} = require('../../../constants')

const { URL } = url

const { pattern: githubRegexParam } = regexParam('/:owner/:repo')

const loadHTML = html =>
  cheerio.load(html, {
    xmlMode: false,
    lowerCaseTags: true,
    decodeEntities: true,
    lowerCaseAttributeNames: true
  })

const withAnchorLinks = $ => {
  $('h1, h2, h3, h4, h5, h6').each(function () {
    const el = $(this)
    const text = el.text()
    const slug = el.attr('id')
    el.removeAttr('id')
    el.html(

regexparam

A tiny (399B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` 🙇‍

MIT
Latest version published 12 months ago

Package Health Score

68 / 100
Full package analysis

Popular regexparam functions