Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/muktihari/expr

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/muktihari/expr

  • v1.4.2
  • Source
  • Go
  • Socket score

Version published
Created
Source

expr

GitHub Workflow Status Go Dev Reference CodeCov Go Report Card

Expr is a simple, lightweight and performant programming toolkit for evaluating basic mathematical expression and boolean expression. The resulting value is one of these following primitive types: string, boolean, numerical (complex, float, integer).

Supported Numerical Notations

- Binary (base-2)       : 0b1011
- Octal (base-8)        : 0o13 or 013
- Decimal (base-10)     : 11
- Hexadecimal (base-16) : 0xB
- Scientific            : 11e0

Expression Examples

"1 + 1"             -> 2
"1.0 / 2"           -> 0.5
"2 < 2 + 2"         -> true
"true && !false"    -> true
"4 << 10"           -> 4096
"0b0100 << 0b1010"  -> 4096 (0b1000000000000)
"0x4 << 0xA"        -> 4096 (0x1000)
"0o4 << 0o12"       -> 4096 (0o10000)
"0b1000 | 0b1001"   -> 9 (0b1001)
"(2+1i) + (2+2i)"   -> (4+3i)
"0x4 << 0xA > 1024" -> true

Usage

Bind

For binding variables into expr string, see Bind

Explain (Experimental)

For explaining step-by-step operations, see Explain

Any

  • Any parses the given expr string into any type it returns as a result. e.g:
    • "1 < 2" -> true
    • "true || false" -> true
    • "2 + 2" -> 4
    • "4 << 10" -> 4906
    • "2.2 + 2" -> 4.2
    • "(2+1i) + (2+2i)" -> (4+3i)
    • ""abc" == "abc"" -> true
    • ""abc"" -> "abc"
  • Supported operators:
    • Comparison: [==, !=, <, <=, >, >=]
    • Logical: [&&, ||, !]
    • Arithmetic: [+, -, *, /, %] (% operator does not work for complex number)
    • Bitwise: [&, |, ^, &^, <<, >>] (only work for integer values)
    str := "(2+1i) + (2+2i)"
    v, err := expr.Any(str)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%v", v) // (4+3i)

Boolean

  • Bool parses the given expr string into boolean as a result. e.g:
    • "1 < 2" -> true
    • "1 > 2" -> false
    • "true || false" -> true
    • "true && !false" -> true
  • Arithmetic operation are supported. e.g:
    • "1 + 2 > 1" -> true
    • "(1 * 10) > -2" -> true
  • Supported operators:
    • Comparison: [==, !=, <, <=, >, >=]
    • Logical: [&&, ||, !]
    • Arithmetic: [+, -, *, /, %] (% operator does not work for complex number)
    • Bitwise: [&, |, ^, &^, <<, >>] (only work for integer values)
    str := "((1 < 2 && 3 > 4) || 1 == 1) && 4 < 5"
    v, err := expr.Bool(str)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%t", v) // true

Complex128

  • Complex128 parses the given expr string into complex128 as a result. e.g:
    • "(2+1i) + (2+2i)" -> (4+3i)
    • "(2.2+1i) + 2" -> (4.2+1i)
    • "2 + 2" -> (4+0i)
  • Supported operators:
    • Arithmetic: [+, -, *, /]
    str := "(2+1i) + (2+2i)"
    v, err := expr.Complex128(str)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%f", v) // (4+3i)

Float64

  • Float64 parses the given expr string into float64 as a result. e.g:
    • "2 + 2" -> 4
    • "2.2 + 2" -> 4.2
    • "10 * -5 + (-5.5)" -> -55.5
    • "10.0 % 2.6" -> 2.2
  • Supported operators:
    • Arithmetic: [+, -, *, /, %]
    str := "((2 * 2) * (8 + 2) * 2) + 2.56789"
    v, err := expr.Float64(str)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%f", v) // 82.56789

Int64

  • Int64 parses the given expr string into int64 as a result. e.g:
    • "2 + 2" -> 4
    • "2.2 + 2" -> 4
    • "10 + ((-5 * -10) / -10) - 2" -> 3
  • Supported operators:
    • Arithmetic: [+, -, *, /, %]
    • Bitwise: [&, |, ^, &^, <<, >>]
    str := "((2 * 2) * (8 + 2) * 2) + 2.56789"
    v, err := expr.Int64(str)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%d", v) // 82

Int64Strict

  • Int64Strict is shorthand for Int64(str) but when x / y and y == 0, it will return ErrIntegerDividedByZero
    str := "12 + 24 - 10/0"
    v, err := expr.Int64Strict(str)
    if err != nil {
        // err == ErrIntegerDividedByZero
    }
    fmt.Printf("%d", v) // 0

Int

  • Int is shorthand for Int64(str) with its result will be converted into int.
    str := "1 + 10"
    v, err := expr.Int(str)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%d", v) // 11

License

Expr is released under Apache Licence 2.0

FAQs

Package last updated on 02 Jul 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc