Just-add-sugar calculator functions.
Using npm:
$ npm i --save calculator-scripts
In your frontend project:
import * as keys from 'calculator-scripts'
Clears the state properties.
keys.clear()
(object): Returns the state object with empty string properties.
keys.clear()
// => {operand1: '', operand2: '', operator: ''}
Converts active operand into a decimal number.
keys.decimal({operand1, operand2, operator})
operand1 (string): First operand property.
operand2 (string): Second operand property.
operator (string): Operation to perform.
(object): Returns a new state object with the converted operand.
keys.decimal({operand1: '1', operand2: '', operator: ''})
// => {operand1: '1.', operand2: '', operator: ''}
keys.decimal({operand1: '1', operand2: '1', operator: 'ADD'})
// => {operand1: '1', operand2: '1.', 'ADD'}
Performs calculation between operands.
keys.equals({operand1, operand2, operator})
operand1 (string): First operand property.
operand2 (string): Second operand property.
operator (string): Operation to perform.
(object): Returns a new state object with the result of the operation performed.
keys.equals({operand1: '1', operand2: '1', operator: 'ADD'})
// => {operand1: '2', operand2: '', operator: ''}
keys.equals({operand1: '100', operand2: '10', operator: 'DIVIDE'})
// => {operand1: '10', operand2: '', ''}
Converts the active operand into negative or positive number.
keys.negation({operand1, operand2, operator})
operand1 (string): First operand property.
operand2 (string): Second operand property.
operator (string): Operation to perform.
(object): Returns a new state object with the converted operand.
keys.negation({operand1: '1', operand2: '', operator: ''})
// => {operand1: '-1', operand2: '', operator: ''}
keys.negation({operand1: '1', operand2: '-1', operator: 'ADD'})
// => {operand1: '1', operand2: '1', 'ADD'}
Appends a number to the state object.
keys.number(number, {operand1, operand2, operator})
number (string): Number to append.
operand1 (string): First operand property.
operand2 (string): Second operand property.
operator (string): Operation to perform.
(object): Returns a new state object.
keys.number('1', {operand1: '1', operand2: '', operator: ''})
// => {operand1: '11', operand2: '', operator: ''}
keys.number('4', {operand1: '1', operand2: '123', operator: 'ADD'})
// => {operand1: '1', operand2: '1234', 'ADD'}
Saves the operation in the state object.
keys.operation(newOperation, {operand1, operand2, operator})
newOperation (string): New operation to perform.
operand1 (string): First operand property.
operand2 (string): Second operand property.
operator (string): Existing operation.
(object): Returns a new state object.
keys.operation('ADD', {operand1: '1', operand2: '', operator: ''})
// => {operand1: '1', operand2: '', operator: 'ADD'}
keys.operation('MULTIPLY', {operand1: '1', operand2: '1', operator: 'ADD'})
// => {operand1: '2', operand2: '', 'MULTIPLY'}
Gets the percentage value of the active operand.
keys.percent({operand1, operand2, operator})
operand1 (string): First operand property.
operand2 (string): Second operand property.
operator (string): Operation to perform.
(object): Returns a new state object with the converted operand.
keys.percent({operand1: '1', operand2: '', operator: ''})
// => {operand1: '0.01', operand2: '', operator: ''}
keys.decimal({operand1: '1', operand2: '1', operator: 'ADD'})
// => {operand1: '1', operand2: '0.01', 'ADD'}