{"id":24614219,"url":"https://github.com/parzh/re-scaled","last_synced_at":"2025-03-18T17:45:48.533Z","repository":{"id":103434897,"uuid":"406940927","full_name":"parzh/re-scaled","owner":"parzh","description":null,"archived":false,"fork":false,"pushed_at":"2021-10-01T16:12:04.000Z","size":106,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-04-24T19:22:40.528Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/parzh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-09-15T22:26:12.000Z","updated_at":"2023-10-08T02:51:31.000Z","dependencies_parsed_at":"2023-04-23T11:33:34.969Z","dependency_job_id":null,"html_url":"https://github.com/parzh/re-scaled","commit_stats":null,"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parzh%2Fre-scaled","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parzh%2Fre-scaled/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parzh%2Fre-scaled/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parzh%2Fre-scaled/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/parzh","download_url":"https://codeload.github.com/parzh/re-scaled/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244227400,"owners_count":20419239,"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":"2025-01-24T21:15:38.764Z","updated_at":"2025-03-18T17:45:48.528Z","avatar_url":"https://github.com/parzh.png","language":"TypeScript","readme":"# `reScaled`\nHelpers and utility functions for creating readable, scalable and reusable regular expressions.\n\n## Tiny guide\nWhen dealing with real-world regular expressions, it is usually super hard to manage them. This is because they are implemented (at least in JavaScript) in such a way that makes them intrinsicly unreadable and unscalable.\n\nConsider the following use case:\n\n```ts\nconst input = prompt();\nconst regex = /^(?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)$/;\n\nregex.test(input);\n```\n\n\u003e Just for fun, try to understand what's happening in the code before reading the next paragraph\n\nWithout the knowledge that the regular expression has something to do with IP addresses, you would probably struggle a lot to understand the meaning of the given regex. This happens because regular expressions heavily utilize special characters, which always decrease readability; in most cases, plain words are much easier to read than special characters.\n\n`reScaled` provides handy functions for the most frequently used features, otherwise achieved by special syntax. Examples of such functions are: `eitherOf()`, `optional()`, `separatedBy()` etc.\n\nAnother problem with the regex in the example above is that it is highly repetitive (it is often the case with regular expressions). There is no ability to refer to the regular expression in the body of another one. Would it be the case, it would dramatically improve scalability and usability of regular expressions. Fortunately, with `reScaled` this is possible.\n\nFirst, create smaller building blocks, _atoms_, from which our main regex will be comprised later:\n\n```ts\nconst $Dot = /\\./;\nconst $Digit = /\\d/;\nconst $Bit = eitherOf(0, 1);\n\nconst $OctetHigh = /25[0-5]/; // 250 to 255\nconst $OctetMiddle = /2[0-4]\\d/; // 200 to 249\nconst $OctetLow = combined(optional($Bit), optional($Digit), $Digit); // 0 to 199\n// same as /[01]?\\d?\\d/\n\nconst $Octet = eitherOf($OctetHigh, $OctetMiddle, $OctetLow); // order matters\n// same as /(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)/\n```\n\nNow, with the help of `$Octet` and `$Dot` atoms we can create regular expression for IP addresses, that is easy to read and modify:\n\n```ts\nconst $IPAddress = detached(repeated.times(3)($Octet, $Dot), $Octet);\n// three octets each followed by dot, then single octet without a dot\n// same as /^(?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)$/\n```\n\n```ts\nconst $IPAddress = detached(separatedBy($Dot)($Octet, $Octet, $Octet, $Octet));\n// four octets with dots in between\n```\n\nWhat is great here, is that we can use `$IPAddress` as an atom to some other regex, if needed:\n\n```ts\nimport $IPAddress from \"./ip-address-regex\";\nimport $Port from \"./port-regex\";\nimport $HumanFriendlyURL from \"./human-friendly-url-regex\";\n\nconst $MachineFriendlyURL = combined($IPAddress, optional(\":\", $Port));\nconst $URL = eitherOf($MachineFriendlyURL, $HumanFriendlyURL);\n\nexport default $URL;\n```\n\nWe can build regular expressions on top of each other!\n\n## API\n\nFollow the [docs].\n\n_Try [direct link] if the previous didn't work._\n\n## Legacy notes\n\nIn order to see versions of the package prior to `re-scaled@1.1.2`, refer to history of the package [`regex-utils`].\n\n  [`regex-utils`]: https://npmjs.org/package/regex-utils\n  [docs]: /docs/api/README.md\n  [direct link]: https://github.com/parzh/re-scaled/blob/main/docs/api/README.md","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparzh%2Fre-scaled","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fparzh%2Fre-scaled","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparzh%2Fre-scaled/lists"}