{"id":18409278,"url":"https://github.com/sdtelectronics/dart-interpolator","last_synced_at":"2026-04-29T10:04:51.405Z","repository":{"id":56833066,"uuid":"280878534","full_name":"SdtElectronics/dart-interpolator","owner":"SdtElectronics","description":"Yet another Dart package to handle dynamic String interpolation with lighter implementation and error check.","archived":false,"fork":false,"pushed_at":"2021-03-05T02:20:30.000Z","size":26,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-12T20:04:02.975Z","etag":null,"topics":["dart"],"latest_commit_sha":null,"homepage":"","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SdtElectronics.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":"2020-07-19T14:07:28.000Z","updated_at":"2021-03-05T02:20:32.000Z","dependencies_parsed_at":"2022-09-08T22:30:23.860Z","dependency_job_id":null,"html_url":"https://github.com/SdtElectronics/dart-interpolator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/SdtElectronics/dart-interpolator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SdtElectronics%2Fdart-interpolator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SdtElectronics%2Fdart-interpolator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SdtElectronics%2Fdart-interpolator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SdtElectronics%2Fdart-interpolator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SdtElectronics","download_url":"https://codeload.github.com/SdtElectronics/dart-interpolator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SdtElectronics%2Fdart-interpolator/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259521514,"owners_count":22870446,"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":["dart"],"created_at":"2024-11-06T03:24:26.969Z","updated_at":"2026-04-29T10:04:51.349Z","avatar_url":"https://github.com/SdtElectronics.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Interpolator\r\n\r\n[Interpolator](https://pub.dev/packages/interpolator) is yet another Dart package to handle dynamic String interpolation with better performance and error check.\r\n\r\n## Usage\r\n\r\nCreate an interpolator instance form a format String:\r\n\r\n```dart\r\nconst format = \"{Name} is a chemical element with the symbol {Symbol} and atomic number {No.}.\";\r\nfinal element = Interpolator(format);\r\n```\r\n\r\nGet result string by call the instance as a method with substitution Map:\r\n\r\n```dart\r\nconst map = {\r\n    \"Name\":     \"Chromium\",\r\n    \"Symbol\":   \"Cr\",\r\n    \"No.\":      24\r\n};\r\nprint(element(map));\r\n\r\n//Chromium is a chemical element with the symbol Cr and atomic number 24.\r\n```\r\n\r\nUse {pre} and {suf} to escape braces in the format string:\r\n\r\n```dart\r\nconst format = \"Need format string includes '{pre}' and '{suf}'? No problem!\";\r\nfinal escaped = Interpolator(format);\r\nprint(escaped(const {}));\r\n\r\n//Need format string includes '{' and '}'? No problem!\r\n```\r\n\r\nNote: this only works out of braces contain the keys. Names of keys containing braces are not allowed.\r\n\r\n## Features \r\nDefault values can be set when initializing an Interpolator. When format string contains keys not provided by the map during interpolation, corresponding default values will be filled. Set the value of key null as a placeholder to fill all unspecified values.\r\n```dart\r\nconst format = \"Default value: {default}, placeholder: {unspecified}\";\r\nconst defaultVal = {\"default\": 0, null: '_'};\r\nfinal fillDefault = Interpolator(format, defaultVal);\r\nprint(fillDefault(const {}));\r\n\r\n//Default value: 0, placeholder: _\r\n```\r\n\r\nInterpolator can detect and locate syntax errors in format strings, i.e. unclosed brace or a key not provided by the substitution map while no placeholder is specified. This feature makes it preferable when handling format strings you have less control on them, such as user inputs or a configuration file.\r\n\r\n```dart\r\nInterpolator(\"Unmatched '{pre}' at 2:2\\n { \");\r\n\r\n//FormatException: Expected '}' to match '{' at 2:2\r\n```\r\n\r\n```dart\r\nfinal nullMatch = Interpolator(\"{nullMatch}\");\r\nnullMatch(const{});\r\n\r\n///FormatException: No match with key \"nullMatch\" at 1:2 and no placeholder specified\r\n```\r\n\r\nProviding richer functionality, interpolator also has a better performance than the simple function implemented with the [splitMapJoin](https://api.dart.dev/stable/2.8.4/dart-core/String/splitMapJoin.html) method provided by the core library when running as a pre-compiled binary(Tested with dart SDK 2.8.4, WSL 4.4.0-18362 and [GNU time](https://www.gnu.org/software/time/)). \r\n\r\n```dart\r\nString inter(String format, Map\u003cString, dynamic\u003e sub ,{String placeholder = \"\"}){\r\n\treturn format.splitMapJoin(RegExp(r'{.*?}'),\r\n    onMatch:    (m) =\u003e sub[m.group(0).substring(1, m.group(0).length - 1)].toString() ?? placeholder,\r\n    onNonMatch: (n) =\u003e n);\r\n}\r\n```\r\n\r\nSince the parsed format string is cached during initialization, this performance improvement will become more significant when performing interpolation on the same format string several times.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsdtelectronics%2Fdart-interpolator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsdtelectronics%2Fdart-interpolator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsdtelectronics%2Fdart-interpolator/lists"}