{"id":20170168,"url":"https://github.com/fabrix-app/regexdot","last_synced_at":"2025-03-03T04:15:45.706Z","repository":{"id":37924396,"uuid":"217117787","full_name":"fabrix-app/regexdot","owner":"fabrix-app","description":"Small utility to param match dot syntax strings","archived":false,"fork":false,"pushed_at":"2023-01-24T00:43:11.000Z","size":224,"stargazers_count":0,"open_issues_count":12,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-02-19T06:04:11.080Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fabrix-app.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-23T17:42:03.000Z","updated_at":"2019-10-25T15:26:20.000Z","dependencies_parsed_at":"2023-02-13T06:00:33.695Z","dependency_job_id":null,"html_url":"https://github.com/fabrix-app/regexdot","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrix-app%2Fregexdot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrix-app%2Fregexdot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrix-app%2Fregexdot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrix-app%2Fregexdot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fabrix-app","download_url":"https://codeload.github.com/fabrix-app/regexdot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241439723,"owners_count":19963100,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T01:17:31.755Z","updated_at":"2025-03-03T04:15:45.688Z","avatar_url":"https://github.com/fabrix-app.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# regexdot\n\n[![Gitter][gitter-image]][gitter-url]\n[![NPM version][npm-image]][npm-url]\n[![Build Status][ci-image]][ci-url]\n[![Test Coverage][coverage-image]][coverage-url]\n[![Dependency Status][daviddm-image]][daviddm-url]\n[![Follow @FabrixApp on Twitter][twitter-image]][twitter-url]\n\n[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)\n\n\u003e A tiny utility that converts dot object access patterns into RegExp.\n\nWith `regexdot`, you may turn a path string (eg, `.users.:id`) into a regular expression.\n\nAn object with shape of `{ keys, pattern }` is returned, where `pattern` is the `RegExp` and `keys` is an array of your parameter name(s) in the order that they appeared.\n\nThis module does not create a `keys` dictionary, nor mutate an existing variable. Also, this only ships a parser, which only accept strings. Similarly, and most importantly, `regexdot` **only** handles basic path operators:\n\n* Static (`.foo`, `.foo.bar`)\n* Parameter (`.:title`, `.books.:title`, `.books.:genre.:title`)\n* Parameter w. Suffix (`.movies.:title.mp4`, `.movies.:title.(mp4|mov)`)\n* Optional Parameters (`.:title?`, `.books.:title?`, `.books.:genre.:title?`)\n* Wildcards (`*`, `.books.*`, `.books.:genre.*`)\n\nThis module exposes two module definitions:\n\n* **CommonJS**: `dist/index.js`\n\n## Install\n\n```\n$ npm install --save regexdot\n```\n\n## Usage\n\n```js\nconst { regexdot } = require('@fabrix/regexdot')\n\n// Example param-assignment\nfunction exec(path, result) {\n  let i=0, out={}\n  let matches = result.pattern.exec(path)\n  while (i \u003c result.keys.length) {\n    out[ result.keys[i] ] = matches[++i] || null\n  }\n  return out\n}\n\n\n// Parameter, with Optional Parameter\n// ---\nlet foo = regexdot('.books.:genre.:title?')\n// foo.pattern =\u003e /^\\.books\\.([^\\.]+?)(?:\\.([^\\.]+?))?\\.?$/i\n// foo.keys =\u003e ['genre', 'title']\n\nfoo.pattern.test('.books.horror') // =\u003e true\nfoo.pattern.test('.books.horror.goosebumps') // =\u003e true\n\nexec('.books.horror', foo)\n//=\u003e { genre: 'horror', title: null }\n\nexec('.books.horror.goosebumps', foo)\n//=\u003e { genre: 'horror', title: 'goosebumps' }\n\n\n// Parameter, with suffix\n// ---\nlet bar = regexdot('.movies.:title.(mp4|mov)')\n// bar.pattern =\u003e /^\\/movies\\/([^\\/]+?)\\.(mp4|mov)\\/?$/i\n// bar.keys =\u003e ['title']\n\nbar.pattern.test('.movies.narnia') //=\u003e false\nbar.pattern.test('.movies.narnia.mp3') //=\u003e false\nbar.pattern.test('.movies.narnia.mp4') //=\u003e true\n\nexec('.movies.narnia.mp4', bar)\n//=\u003e { title: 'narnia' }\n\n\n// Wildcard\n// ---\nlet baz = regexdot('users.*')\n// baz.pattern =\u003e /^\\.users\\.(.*)\\.?$/i\n// baz.keys =\u003e ['wild']\n\nbaz.pattern.test('.users') //=\u003e false\nbaz.pattern.test('.users.fabrix') //=\u003e true\n\nexec('.users.fabrix.repos.new', baz)\n//=\u003e { wild: 'fabrix/repos/new' }\n```\n\n\u003e **Importnat:** Using `::` will assume that it is not a param but a message header. Eg. `messege::commplete` does not contain any parameters.\n\n\u003e **Important:** When matching/testing against a generated RegExp, your path **must** begin with a leading dot (`\".\"`)!\n\n## Regular Expressions\n\nFor fine-tuned control, you may pass a `RegExp` value directly to `regexdot` as its only parameter.\n\nIn these situations, `regexdot` **does not** parse nor manipulate your pattern in any way! Because of this, `regexdot` has no \"insight\" on your route, and instead trusts your input fully. In code, this means that the return value's `keys` is always equal to `false` and the `pattern` is identical to your input value.\n\nThis also means that you must manage and parse your own `keys`~!\u003cbr\u003e\nYou may use [named capture groups](https://javascript.info/regexp-groups#named-groups) or traverse the matched segments manually the \"old-fashioned\" way:\n\n```js\n// Named capture group\nconst named = regexdot(/^\\/posts[\\.](?\u003cyear\u003e[0-9]{4})[\\.](?\u003cmonth\u003e[0-9]{2})[\\.](?\u003ctitle\u003e[^\\.]+)/i);\nconst { groups } = named.pattern.exec('.posts.2019.05.hello-world');\nconsole.log(groups);\n//=\u003e { year: '2019', month: '05', title: 'hello-world' }\n\n// Widely supported / \"Old-fashioned\"\nconst named = regexdot(/^\\.posts[\\.]([0-9]{4})[\\.]([0-9]{2})[\\.]([^\\.]+)/i);\nconst [url, year, month, title] = named.pattern.exec('.posts.2019.05.hello-world');\nconsole.log(year, month, title);\n//=\u003e 2019 05 hello-world\n```\n\n\n## API\n\nThere are two API variants:\n\n1) When passing a `String` input, the `loose` parameter is able to affect the output. [View API](#regexdotstr-loose)\n\n2) When passing a `RegExp` value, that must be `regexdot`'s _only_ argument.\u003cbr\u003e\nYour pattern is saved as written, so `loose` is ignored entirely. [View API](#regexdotrgx)\n\n### regexdot(str, loose)\nReturns: `Object`\n\nReturns a `{ keys, pattern }` object, where `pattern` is a generated `RegExp` instance and `keys` is a list of extracted parameter names.\n\n#### str\nType: `String`\n\nThe path string to convert.\n\n\u003e **Note:** It does not matter if your `str` begins with a `/` \u0026mdash; it will be added if missing.\n\n#### loose\nType: `Boolean`\u003cbr\u003e\nDefault: `false`\n\nShould the `RegExp` match URLs that are longer than the [`str`](#str) pattern itself?\u003cbr\u003e\nBy default, the generated `RegExp` will test that the URL begins and _ends with_ the pattern.\n\n```js\nconst { regexdot } = require('@fabrix/regexdot');\n\nregexdot('.users').pattern.test('.users.fabrix'); //=\u003e false\nregexdot('.users', true).pattern.test('.users.fabrix'); //=\u003e true\n\nregexdot('.users.:name').pattern.test('.users.fabrix.repos'); //=\u003e false\nregexdot('.users.:name', true).pattern.test('.users.fabrix.repos'); //=\u003e true\n```\n\n### regexdot(rgx)\nReturns: `Object`\n\nReturns a `{ keys, pattern }` object, where pattern is _identical_ to your `rgx` and `keys` is `false`, always.\n\n#### rgx\nType: `RegExp`\n\nYour RegExp pattern.\n\n\u003e **Important:** This pattern is used _as is_! No parsing or interpreting is done on your behalf.\n\n\n### Release Instructions\nWhen the master is tagged with a release, it will automatically publish to npm, updates the Changelog and bumps the version. Fabrix uses the [standard-version library](https://www.npmjs.com/package/standard-version) to manage it all.\n\nTo run a patch release: \n```bash\nnpm run release -- --release-as patch\n``` \nand then commit to master. `git push --follow-tags origin master`\n\nYou can also test the release by running\n```bash\nnpm run release -- --dry-run --release-as patch\n``` \n\n\n\n[npm-image]: https://img.shields.io/npm/v/@fabrix/regexdot.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/@fabrix/regexdot\n[ci-image]: https://img.shields.io/circleci/project/github/fabrix-app/regexdot/master.svg\n[ci-url]: https://circleci.com/gh/fabrix-app/regexdot/tree/master\n[daviddm-image]: http://img.shields.io/david/fabrix-app/regexdot.svg?style=flat-square\n[daviddm-url]: https://david-dm.org/fabrix-app/regexdot\n[gitter-image]: http://img.shields.io/badge/+%20GITTER-JOIN%20CHAT%20%E2%86%92-1DCE73.svg?style=flat-square\n[gitter-url]: https://gitter.im/fabrix-app/fabrix\n[twitter-image]: https://img.shields.io/twitter/follow/FabrixApp.svg?style=social\n[twitter-url]: https://twitter.com/FabrixApp\n[coverage-image]: https://img.shields.io/codeclimate/coverage/github/fabrix-app/regexdot.svg?style=flat-square\n[coverage-url]: https://codeclimate.com/github/fabrix-app/regexdot/coverage\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabrix-app%2Fregexdot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffabrix-app%2Fregexdot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabrix-app%2Fregexdot/lists"}