{"id":15468047,"url":"https://github.com/rangermauve/mqtt-pattern","last_synced_at":"2025-04-15T06:04:38.316Z","repository":{"id":57303722,"uuid":"89000958","full_name":"RangerMauve/mqtt-pattern","owner":"RangerMauve","description":"Fast library for matching MQTT patterns with named wildcards","archived":false,"fork":false,"pushed_at":"2023-05-02T21:08:29.000Z","size":24,"stargazers_count":30,"open_issues_count":1,"forks_count":4,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-15T06:04:31.727Z","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/RangerMauve.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":"2017-04-21T15:59:33.000Z","updated_at":"2025-04-08T18:40:31.000Z","dependencies_parsed_at":"2024-06-18T15:36:50.802Z","dependency_job_id":"cc0c04f6-1645-447f-aecd-9b01a2bb7070","html_url":"https://github.com/RangerMauve/mqtt-pattern","commit_stats":{"total_commits":22,"total_committers":7,"mean_commits":3.142857142857143,"dds":0.5,"last_synced_commit":"9e04908adebc59e9a1703aa75c2b204ac9081acc"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RangerMauve%2Fmqtt-pattern","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RangerMauve%2Fmqtt-pattern/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RangerMauve%2Fmqtt-pattern/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RangerMauve%2Fmqtt-pattern/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RangerMauve","download_url":"https://codeload.github.com/RangerMauve/mqtt-pattern/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249016622,"owners_count":21198832,"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-10-02T01:38:45.664Z","updated_at":"2025-04-15T06:04:38.281Z","avatar_url":"https://github.com/RangerMauve.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mqtt-pattern\nFast library for matching MQTT patterns with named wildcards to extract data from topics\n\nSuccessor to [mqtt-regex](./mqtt-regex)\n\n## Example:\n\n``` javascript\nvar MQTTPattern = require(\"mqtt-pattern\");\n\n// Wildcards in patterns don't need names\nvar pattern = \"device/+id/+/#data\";\n\nvar topic = \"device/fitbit/heartrate/rate/bpm\";\n\nvar params = MQTTPattern.exec(pattern, topic);\n\n// params will be\n{\n\tid: \"fitbit\",\n\tdata: [\"rate\", \"bmp\"]\n}\n\nvar filled = MQTTPattern.fill(pattern, params);\n// filled will be\n\"device/fitbit/undefined/rate/bmp\"\n\nMQTTPattern.clean(\"hello/+param1/world/#param2\");\n// hello/+/world/#\n\n```\n\n## Installing\n\nWith NPM:\n\n```bash\nnpm install --save mqtt-pattern\n```\n\n## API\n\n### `exec(pattern : String, topic : String) : Object | null`\nValidates that `topic` fits the `pattern` and parses out any parameters.\nIf the topic doesn't match, it returns `null`\n\n### `matches(pattern : String, topic : String) : Boolean`\nValidates whether `topic` fits the `pattern`. Ignores parameters.\n\n### `extract(pattern : String, topic : String) : Object`\nTraverses the `pattern` and attempts to fetch parameters from the `topic`.\nUseful if you know in advance that your `topic` will be valid and want to extract data.\nIf the `topic` doesn't match, or the `pattern` doesn't contain named wildcards, returns an empty object.\nDo not use this for validation.\n\n### `fill(pattern : String, params: Object) : String`\nReverse of `extract`, traverse the `pattern` and fill in params with keys in an object. Missing keys for `+` params are set to `undefined`. Missing keys for `#` params yeid empty strings.\n\n### `clean(pattern : String) : String`\nRemoves the parameter names from a pattern.\n\n## How params work\n\nMQTT defines two types of \"wildcards\", one for matching a single section of the path (`+`), and one for zero or more sections of the path (`#`).\nNote that the `#` wildcard must only be used if it's at the end of the topic.\nThis library was inspired by the syntax in the routers for web frameworks.\n\n### Examples of topic patterns:\n\n#### user/+id/#path\nThis would match paths that start with `user/`, and then extract the next section as the user `id`.\nThen it would get the following paths and turn them into an array for the `path` param.\nHere is some input/output that you can expect:\n\n\tuser/bob/status/mood: {id: \"bob\", path:[\"status\",\"mood\"]\n\tuser/bob: {id:\"bob\", path: []}\n\tuser/bob/ishungry: {id: \"bob\", path: [\"ishungry\"]\n\n#### device/+/+/component/+type/#path\nNot all wildcards need to be associated with a parameter, and it could be useful to use plain MQTT topics.\nIn this example you might only care about the status of some part of a device, and are willing to ignore a part of the path.\nHere are some examples of what this might be used with:\n\n\tdevice/deviceversion/deviceidhere/component/infrared/status/active: {type:\"infrared\",path: [\"status\",\"active\"]}\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frangermauve%2Fmqtt-pattern","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frangermauve%2Fmqtt-pattern","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frangermauve%2Fmqtt-pattern/lists"}