File size: 1,812 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { isObject } from "../utils"

export type Operand = string | number | { reference: string }
type Operator = "+" | "-" | "*" | "/"

function resolveReference(operand: Operand): string {
  if (isObject(operand) && operand.reference) {
    return operand.reference
  }
  return String(operand)
}

const toExpression = (operator: Operator, ...operands: Array<Operand>) =>
  operands.map(resolveReference).join(` ${operator} `).replace(/calc/g, "")

const add = (...operands: Array<Operand>) =>
  `calc(${toExpression("+", ...operands)})`

const subtract = (...operands: Array<Operand>) =>
  `calc(${toExpression("-", ...operands)})`

const multiply = (...operands: Array<Operand>) =>
  `calc(${toExpression("*", ...operands)})`

const divide = (...operands: Array<Operand>) =>
  `calc(${toExpression("/", ...operands)})`

const negate = (x: Operand) => {
  const value = resolveReference(x)

  if (value != null && !Number.isNaN(parseFloat(value))) {
    return String(value).startsWith("-") ? String(value).slice(1) : `-${value}`
  }

  return multiply(value, -1)
}

interface CalcChain {
  add: (...operands: Array<Operand>) => CalcChain
  subtract: (...operands: Array<Operand>) => CalcChain
  multiply: (...operands: Array<Operand>) => CalcChain
  divide: (...operands: Array<Operand>) => CalcChain
  negate: () => CalcChain
  toString: () => string
}

export const calc = Object.assign(
  (x: Operand): CalcChain => ({
    add: (...operands) => calc(add(x, ...operands)),
    subtract: (...operands) => calc(subtract(x, ...operands)),
    multiply: (...operands) => calc(multiply(x, ...operands)),
    divide: (...operands) => calc(divide(x, ...operands)),
    negate: () => calc(negate(x)),
    toString: () => x.toString(),
  }),
  {
    add,
    subtract,
    multiply,
    divide,
    negate,
  },
)