npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tagged-operator

v0.0.3

Published

Simulate operator overloading with Tagged templates

Downloads

2

Readme

tagged-operator [alpha]

Simulate operator overloading with Tagged templates

Installation

npm i tagged-operator

Usage

  • simple to use
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 }
  • use with TypeScript
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

config

type

  • Type: default | precedence | pipe
  • Default: 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.

operator

  • Required: true
  • Type: 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 operator
  • val2 - the value to the right of the current operator

precedence

  • Type: object

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: ['*', '/'],
}

Function

TaggedOperator

Create a tagged calculator through class

createTag

Create a tagged calculator that executes with precedence and grouping operators ().

createPrecedenceTag

Create a tagged calculator that only includes precedence information, without grouping operators ()

createPipeTag

Create a tagged calculator that executes sequentially

License

MIT