
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
tagged-operator
Advanced tools
Simulate operator overloading with Tagged templates
npm i tagged-operator
import { createPipeTag } from 'tagged-operator'
const operator = (type, val1, val2) => {
if (type === '~') {
return (val1 + val2) * (val2 + 1 - val1) / 2
}
}
const calc = createPipeTag({ operator })
console.log(calc`${1} ~ ${100}`) // 5050
console.log(calc`${23} ~ ${86}`) // 3488
const a = new Coordinate(100, 100)
const b = new Coordinate(0, 200)
// a * 3 - b
console.log(a.calc`${a} * ${3} - ${b}`) // Coordinate { x: 300, y: 100 }
import { createTag, Precedence, Operator } from 'tagged-operator'
const precedence: Precedence = { 1: ['*', '/'] }
const operator: Operator<string | number, string> = (type, val1, val2) => {
switch (type) {
case '+':
return String(val1) + String(val2)
case '-':
return String(val1).replace(String(val2), '')
case '*':
if (typeof val1 === 'number' && typeof val2 === 'string') {
return val2.repeat(val1)
} else {
return String(val1).repeat(+val2)
}
case '/':
return String(val1).replaceAll(String(val2), '')
default:
console.warn(`no operator configured: ${type}`)
return String(val1)
}
}
const calc = createTag({ operator, precedence })
console.log(calc`${'Hello'} + ${' World!'}`) // Hello World!
console.log(calc`${'Hello'} * ${3}`) // HelloHelloHello
console.log(calc`(${'Hello'} + ${' World!'}) / ${'l'} - ${'!'}`) // Heo Word
default
| precedence
| pipe
default
how to calculate (for the class TaggedOperator
)
default
- the config operator precedence information and grouping operators ()
.precedence
- just used the config operator precedence information.pipe
- executes sequentially.true
Function(type, val1, val2)
How the match operator is evaluated
type
- current operator (Note: prohibited to use (
and )
when use function createTag
)val1
- the value to the left of the current operatorval2
- the value to the right of the current operatorobject
Configure operator precedence information.
The larger the key, the higher the precedence. For performance reasons, you should only config operators that need to be evaluated first.
eg:
const precedence = {
2: ['^'],
1: ['*', '/'],
}
Create a tagged calculator through class
Create a tagged calculator that executes with precedence and grouping operators ()
.
Create a tagged calculator that only includes precedence information, without grouping operators ()
Create a tagged calculator that executes sequentially
0.0.3
perf
fix
FAQs
Simulate operator overloading with Tagged templates
The npm package tagged-operator receives a total of 7 weekly downloads. As such, tagged-operator popularity was classified as not popular.
We found that tagged-operator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.