From e8e0bacd400c760ab59d83a0ce1d2276702533c2 Mon Sep 17 00:00:00 2001 From: Bo Borgerson Date: Mon, 7 Jan 2019 14:46:29 -0800 Subject: [PATCH 1/2] golf: Drive loop with RegExp.prototype.exec Pull the individually used components out of each path segment with a single capturing global RegExp that can be used to drive the loop. This reduces minified, compressed bytes from 249 to 234. --- src/index.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 10f1de7..0159ea2 100644 --- a/src/index.js +++ b/src/index.js @@ -1,18 +1,22 @@ export default function (str) { - var c, o, tmp, keys=[], pattern='', arr=str.split('/'); - arr[0] || arr.shift(); + // This captures: + // 1. Leading slash, if any (unused... non-capturing group adds bytes). + // 2. Key type, if any. + // 3. Key name (or constant path component). + // 4. Optional flag. + var match, keys=[], pattern='', re=/(\/|^)([:*])?([^\/]*?)(\?)?(?=\/|$)/g; - while (tmp = arr.shift()) { - c = tmp[0]; - if (c === '*') { + // An empty input string will match and not update `lastIndex`, leading to + // an infinite loop. The default to '/' avoids this. + while (match = re.exec(str || '/')) { + if (match[2] === '*') { keys.push('wild'); pattern += '/(.*)'; - } else if (c === ':') { - o = tmp[tmp.length - 1] === '?'; // optional? - keys.push( tmp.substring(1, o ? tmp.length - 1 : tmp.length) ); - pattern += o ? '(?:/([^/]+?))?' : '/([^/]+?)'; + } else if (match[2] === ':') { + keys.push( match[3] ); + pattern += match[4] ? '(?:/([^/]+?))?' : '/([^/]+?)'; } else { - pattern += '/' + tmp; + pattern += '/' + match[3]; } } From 7b175cfbb6236cd10488088ad4f8e42459a39acc Mon Sep 17 00:00:00 2001 From: Bo Borgerson Date: Mon, 7 Jan 2019 15:09:26 -0800 Subject: [PATCH 2/2] chore: update sizes --- package.json | 2 +- readme.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a172f36..6042b43 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "regexparam", "version": "1.0.2", "repository": "lukeed/regexparam", - "description": "A tiny (252B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` 🙇‍", + "description": "A tiny (236B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` 🙇‍", "module": "dist/regexparam.mjs", "main": "dist/regexparam.js", "license": "MIT", diff --git a/readme.md b/readme.md index 121885b..b38c18d 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # regexparam [![Build Status](https://travis-ci.org/lukeed/regexparam.svg?branch=master)](https://travis-ci.org/lukeed/regexparam) -> A tiny (252B) utility that converts route patterns into RegExp. Limited alternative to [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp) 🙇 +> A tiny (236B) utility that converts route patterns into RegExp. Limited alternative to [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp) 🙇 With `regexparam`, you may turn a pathing string (eg, `/users/:id`) into a regular expression.