UNPKG

@adamsy/bhai-lang

Version:

<h1 align="center">Bhai Lang</h1> <p align="center"> <a href="https://lgtm.com/projects/g/DulLabs/bhai-lang/alerts/"><img alt="Total alerts" src="https://img.shields.io/lgtm/alerts/g/DulLabs/bhai-lang.svg?logo=lgtm&logoWidth=18"/></a> <a href="https://lgt

139 lines (105 loc) 3.06 kB
import InvalidStateException from "../exceptions/invalidStateException"; import RuntimeException from "../exceptions/runtimeException"; export function checkNumberOperands(operands: { left: unknown; right: unknown; }): operands is { left: number; right: number } { return ( typeof operands.left === "number" && typeof operands.right === "number" ); } export function checkStringOperands(operands: { left: unknown; right: unknown; }): operands is { left: string; right: string } { return ( typeof operands.left === "string" && typeof operands.right === "string" ); } export function getOperationValue( operands: { left: unknown; right: unknown }, operator: string ) { const exception = new RuntimeException( `Ye kya kar raha hai: "${operator}" ke sath "${typeof operands.left}" aur "${typeof operands.right}" nahi jamte.` ); switch (operator) { case "=": return operands.right; case "+=": case "+": if (checkNumberOperands(operands)) { return operands.left + operands.right; } if (checkStringOperands(operands)) { return operands.left + operands.right; } throw exception; case "-=": case "-": if (checkNumberOperands(operands)) { return operands.left - operands.right; } throw exception; case "*=": case "*": if (checkNumberOperands(operands)) { return operands.left * operands.right; } throw exception; case "/=": case "/": if (checkNumberOperands(operands)) { return operands.left / operands.right; } throw exception; case "%=": case "%": if (checkNumberOperands(operands)) { return operands.left % operands.right; } throw exception; case "==": if (checkNumberOperands(operands)) { return operands.left === operands.right; } if (checkStringOperands(operands)) { return operands.left === operands.right; } throw exception; case "!=": if (checkNumberOperands(operands)) { return operands.left !== operands.right; } if (checkStringOperands(operands)) { return operands.left !== operands.right; } throw exception; case ">": if (checkNumberOperands(operands)) { return operands.left > operands.right; } throw exception; case "<": if (checkNumberOperands(operands)) { return operands.left < operands.right; } throw exception; case ">=": if (checkNumberOperands(operands)) { return operands.left >= operands.right; } throw exception; case "<=": if (checkNumberOperands(operands)) { return operands.left <= operands.right; } throw exception; case "&&": return operands.left && operands.right; case "||": return operands.left || operands.right; default: throw new InvalidStateException(`Unsupported operator: ${operator}`); } }