{"id":17912394,"url":"https://github.com/tolking/tagged-operator","last_synced_at":"2025-03-23T22:34:30.175Z","repository":{"id":40355720,"uuid":"483596742","full_name":"tolking/tagged-operator","owner":"tolking","description":"Simulate operator overloading with Tagged templates","archived":false,"fork":false,"pushed_at":"2022-05-13T06:33:37.000Z","size":45,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-18T12:14:46.884Z","etag":null,"topics":["operator","operator-overloading","rewrite-operator","simulate-operator","tagged-templates","template-literals","template-strings"],"latest_commit_sha":null,"homepage":"","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/tolking.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":"2022-04-20T09:49:52.000Z","updated_at":"2024-06-01T21:21:16.000Z","dependencies_parsed_at":"2022-08-09T18:01:45.602Z","dependency_job_id":null,"html_url":"https://github.com/tolking/tagged-operator","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tolking%2Ftagged-operator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tolking%2Ftagged-operator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tolking%2Ftagged-operator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tolking%2Ftagged-operator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tolking","download_url":"https://codeload.github.com/tolking/tagged-operator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245180132,"owners_count":20573608,"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":["operator","operator-overloading","rewrite-operator","simulate-operator","tagged-templates","template-literals","template-strings"],"created_at":"2024-10-28T19:44:51.348Z","updated_at":"2025-03-23T22:34:29.912Z","avatar_url":"https://github.com/tolking.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tagged-operator [alpha]\n\n\u003e Simulate operator overloading with [Tagged templates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates)\n\n## Installation\n\n```sh\nnpm i tagged-operator\n```\n\n## Usage\n\n- simple to use\n\n```js\nimport { createPipeTag } from 'tagged-operator'\n\nconst operator = (type, val1, val2) =\u003e {\n  if (type === '~') {\n    return (val1 + val2) * (val2 + 1 - val1) / 2\n  }\n}\nconst calc = createPipeTag({ operator })\n\nconsole.log(calc`${1} ~ ${100}`) // 5050\nconsole.log(calc`${23} ~ ${86}`) // 3488\n```\n\n- use by class, eg [Coordinate](./src/core/class.test.ts)\n\n```js\nconst a = new Coordinate(100, 100)\nconst b = new Coordinate(0, 200)\n\n// a * 3 - b\nconsole.log(a.calc`${a} * ${3} - ${b}`) // Coordinate { x: 300, y: 100 }\n```\n\n- use with TypeScript\n\n```ts\nimport { createTag, Precedence, Operator } from 'tagged-operator'\n\nconst precedence: Precedence = { 1: ['*', '/'] }\nconst operator: Operator\u003cstring | number, string\u003e = (type, val1, val2) =\u003e {\n  switch (type) {\n    case '+':\n      return String(val1) + String(val2)\n    case '-':\n      return String(val1).replace(String(val2), '')\n    case '*':\n      if (typeof val1 === 'number' \u0026\u0026 typeof val2 === 'string') {\n        return val2.repeat(val1)\n      } else {\n        return String(val1).repeat(+val2)\n      }\n    case '/':\n      return String(val1).replaceAll(String(val2), '')\n    default:\n      console.warn(`no operator configured: ${type}`)\n      return String(val1)\n  }\n}\nconst calc = createTag({ operator, precedence })\n\nconsole.log(calc`${'Hello'} + ${' World!'}`) // Hello World!\nconsole.log(calc`${'Hello'} * ${3}`) // HelloHelloHello\nconsole.log(calc`(${'Hello'} + ${' World!'}) / ${'l'} - ${'!'}`) // Heo Word\n```\n\n## config\n\n### type\n\n- Type: `default` | `precedence` | `pipe`\n- Default: `default`\n\nhow to calculate (for the class `TaggedOperator`)\n\n- `default` - the config operator precedence information and grouping operators `()`.\n- `precedence` - just used the config operator precedence information.\n- `pipe` - executes sequentially.\n\n### operator\n\n- Required: `true`\n- Type: `Function(type, val1, val2)`\n\nHow the match operator is evaluated\n\n- `type` - current operator (Note: prohibited to use `(` and `)` when use function `createTag`)\n- `val1` - the value to the left of the current operator\n- `val2` - the value to the right of the current operator\n\n### precedence\n\n- Type: `object`\n\nConfigure operator precedence information.\n\nThe larger the key, the higher the precedence. For performance reasons, you should only config operators that need to be evaluated first.\n\neg: \n\n```js\nconst precedence = {\n  2: ['^'],\n  1: ['*', '/'],\n}\n```\n\n## Function\n\n### TaggedOperator\n\nCreate a tagged calculator through class\n\n### createTag\n\nCreate a tagged calculator that executes with precedence and grouping operators `()`.\n\n### createPrecedenceTag\n\nCreate a tagged calculator that only includes precedence information, *without grouping operators `()`*\n\n### createPipeTag\n\nCreate a tagged calculator that executes sequentially\n\n## License\n\n[MIT](http://opensource.org/licenses/MIT)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftolking%2Ftagged-operator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftolking%2Ftagged-operator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftolking%2Ftagged-operator/lists"}