{"_id":"route-sort","_rev":"4-65bb6a7df4ed76dbe7529cdc7bb6a9f5","time":{"0.1.0":"2015-04-06T15:47:41.090Z","created":"2019-08-27T18:31:08.107Z","1.0.0":"2019-08-27T18:31:08.225Z","modified":"2022-05-16T05:35:04.635Z"},"name":"route-sort","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"route-sort","version":"1.0.0","repository":{"type":"git","url":"git+https://github.com/lukeed/route-sort.git"},"description":"A tiny (200B) utility to sort route patterns by specificity","module":"dist/rsort.mjs","unpkg":"dist/rsort.min.js","main":"dist/rsort.js","types":"rsort.d.ts","umd:name":"rsort","license":"MIT","author":{"name":"Luke Edwards","email":"luke.edwards05@gmail.com","url":"https://lukeed.com"},"engines":{"node":">= 6"},"scripts":{"build":"bundt","pretest":"npm run build","test":"tape -r esm test/*.js | tap-spec"},"keywords":["exact","route","pattern","specificity","sort"],"devDependencies":{"bundt":"0.4.0","esm":"3.2.25","tap-spec":"5.0.0","tape":"4.11.0"},"gitHead":"6343996e519015d197b28e5bcb731e5394d91418","bugs":{"url":"https://github.com/lukeed/route-sort/issues"},"homepage":"https://github.com/lukeed/route-sort#readme","_id":"route-sort@1.0.0","_npmVersion":"6.4.1","_nodeVersion":"10.15.3","_npmUser":{"name":"lukeed","email":"luke@lukeed.com"},"dist":{"integrity":"sha512-SFgmvjoIhp5S4iBEDW3XnbT+7PRuZ55oRuNjY+CDB1SGZkyCG9bqQ3/dhaZTctTBYMAvDxd2Uy9dStuaUfgJqQ==","shasum":"078149c8d2d2de8b8563adf139ec58476835c94b","tarball":"https://registry.npmjs.org/route-sort/-/route-sort-1.0.0.tgz","fileCount":7,"unpackedSize":7177,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdZXbsCRA9TVsSAnZWagAA7CMP/0ZmdmTu2ss7OUuiGvuV\nXjtHEUha0lTZtaRwE7ebAN7ZPxVHOcwWm8u38EpXglVIo+Pr+J+5096sTatb\nP42bz42L+SWzHtCV3erYDUlxWABpCHR9NuqCr45KylBLC69xC5kRACXyK21h\nDPbDrcogQo2XB+lNGJm0G6Rg4ARPwVmpQ8yctN2Snrrag7/QeD+zQXnmoRKY\n0OrO41FbC6VkhnV/S77FtopGjhno5+PglQtcXNE8bBaWqfP4obN87UuWU0/J\nBunGvbU0metc7smW6SEnL0DVWZT3iAicMlG6jRHv4ZYREH1uqwCq8evWpJTd\npXWkz5Ql8G4PHxOn9O0ABK+lxvMKey8FA7hOWJxjEcU8v2QQGQGfzen6ew2f\nx2hVfOnIUv0m0dOPh0RT9Fs5N59wqxH/LWRDrl0vo0BnZPY5WnuERo9cHsXa\nQKfnzyoqSeFGfwv9OXKbSC4HSf9339VgsC09pZPuG1qvfbDHf/OVybUxFKU/\noQGUQo6p1j+tJbfJgv3V6Grv8q3qip8KsNfzqpcTq226xnd0B4u5zlXnXvf9\n3eLTfo64iaRpuAIi/J24Ocd11r6MqwG/ksjdgjG2yPM2HXIr4of3sDitZLJ4\nxnloKR+vJnHTy2u3qjTjY+onex3m+IuY/4yFHeUxWC0GaaUPg9T+Cc47A4ni\n5f4t\r\n=XJWL\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCp39tbUTeqbDVzP1A0uHgrgZGetUF8Tn2l56ZvgEvsgQIhANbMVAT/Sm49jP619Mrxlx/5+DZrvdaFqPF3AdrULaOY"}]},"maintainers":[{"name":"lukeed","email":"luke@lukeed.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/route-sort_1.0.0_1566930668107_0.4228789434526772"},"_hasShrinkwrap":false}},"maintainers":[{"name":"lukeed","email":"luke@lukeed.com"}],"description":"A tiny (200B) utility to sort route patterns by specificity","homepage":"https://github.com/lukeed/route-sort#readme","keywords":["exact","route","pattern","specificity","sort"],"repository":{"type":"git","url":"git+https://github.com/lukeed/route-sort.git"},"author":{"name":"Luke Edwards","email":"luke.edwards05@gmail.com","url":"https://lukeed.com"},"bugs":{"url":"https://github.com/lukeed/route-sort/issues"},"license":"MIT","readme":"# route-sort [![codecov](https://badgen.now.sh/codecov/c/github/lukeed/route-sort)](https://codecov.io/gh/lukeed/route-sort)\n\n> A tiny (200B) utility to sort route patterns by [specificity](#specificity).\n\nThis module is available in three formats:\n\n* **ES Module**: `dist/rsort.mjs`\n* **CommonJS**: `dist/rsort.js`\n* **UMD**: `dist/rsort.min.js`\n\n\n## Install\n\n```\n$ npm install --save route-sort\n```\n\n\n## Usage\n\n```js\nimport rsort from 'route-sort';\n\n// We have multiple Author-based routes\n// Note: These are currently an unsorted mess\nconst routes = ['/authors', '/authors/*', '/authors/:username/posts', '/authors/:username'];\n\nconst output = rsort(routes);\n\n// Now, our routes are sorted correctly!\nconsole.log(routes);\n//=> [ '/authors', '/authors/:username', '/authors/:username/posts', '/authors/*' ]\n\n// The original input was mutated, but it's also returned\nconsole.log(routes === output);\n//=> true\n```\n\n\n## API\n\n### rsort(patterns)\nReturns: `Array<String>`\n\nReturns the same `patterns` you provide, sorted by [specificity](#specificity).\n\n> **Important:** Your _original_ array is mutated!\n\n\n#### patterns\nType: `Array<String>`\n\nA list of [route pattern](#route-patterns) strings.\n\n\n## Route Patterns\n\nThe supported route pattern types are:\n\n* static – `/users`\n* named parameters – `/users/:id`\n* nested parameters – `/users/:id/books/:title`\n* optional parameters – `/users/:id?/books/:title?`\n* suffixed parameters – `/movies/:title.mp4`, `/movies/:title.(mp4|mov)`\n* wildcards – `/users/*`\n\n\n## Specificity\n\nWhile this working definition may not apply _completely_ across the board, `route-sort` is meant to sort Express-like routing patterns in a safe manner, such that a serial traversal of the sorted array will always give you the most specific match.\n\nYou may use [`regexparam`](https://github.com/lukeed/regexparam) to convert the patterns into `RegExp` instances, and then use those to test an incoming URL against the patterns. We'll do that in the example below:\n\n```js\nimport rsort from 'route-sort';\nimport toRegExp from 'regexparam';\n\n// We have multiple Author-based routes\n// Note: These are currently an unsorted mess\nconst routes = ['/authors', '/authors/*', '/authors/:username/posts', '/authors/:username'];\n\nrsort(routes);\n// Now, our routes are sorted correctly!\n//=> [ '/authors', '/authors/:username', '/authors/:username/posts', '/authors/*' ]\n\n// Let's make an inefficent DEMO function to:\n// 1) loop thru the `routes` array\n// 2) convert each pattern to a RegExp (repetitive)\n// 3) test the RegExp to see if we had a match\nfunction find(path) {\n  for (let i=0; i < routes.length; i++) {\n    let { pattern } = toRegExp(routes[i]);\n    if (pattern.test(path)) return routes[i];\n  }\n  return false; // no match\n}\n\nfind('/authors'); //=> \"/authors\"\nfind('/authors/lukeed'); //=> \"/authors/:username\"\nfind('/authors/foo/bar/baz'); //=> \"/authors/*\"\nfind('/authors/lukeed/posts'); //=> \"/authors/:username/posts\"\nfind('/hello/moto'); //=> false\n\n// Sorting was important here, but otherwise our\n// original `routes` list would have matched \"/authors/*\"\n// against every path except `/hello/moto` and `/authors`.\n\n// Cya!\n```\n\n## Related\n\n* [regexparam](https://github.com/lukeed/regexparam) – convert route patterns to `RegExp` instances\n* [route-manifest](https://github.com/lukeed/route-manifest) – runtime parser of the Route Manifest format\n* [webpack-route-manifest](https://github.com/lukeed/webpack-route-manifest) – generate a Route Manifest file for your webpack build\n\n\n## License\n\nMIT © [Luke Edwards](https://lukeed.com)\n","readmeFilename":"readme.md"}