Just-add-sugar calculator functions.
Installation
Using npm:
$ npm i --save calculator-keys
In your frontend project:
Basic Key Functions
Clear
Clears the state properties.
keysclear
Returns
(object): Returns the state object with empty string properties.
Example
keysclear// => {operand1: '', operand2: '', operator: ''}
Decimal
Converts active operand into a decimal number.
keys
Arguments
operand1 (string): First operand property.
operand2 (string): Second operand property.
operator (string): Operation to perform.
Returns
(object): Returns a new state object with the converted operand.
Example
keys// => {operand1: '1.', operand2: '', operator: ''} keys// => {operand1: '1', operand2: '1.', 'ADD'}
Equals
Performs calculation between operands.
keys
Arguments
operand1 (string): First operand property.
operand2 (string): Second operand property.
operator (string): Operation to perform.
Returns
(object): Returns a new state object with the result of the operation performed.
Example
keys// => {operand1: '2', operand2: '', operator: ''} keys// => {operand1: '10', operand2: '', ''}
Negation
Converts the active operand into negative or positive number.
keys
Arguments
operand1 (string): First operand property.
operand2 (string): Second operand property.
operator (string): Operation to perform.
Returns
(object): Returns a new state object with the converted operand.
Example
keys// => {operand1: '-1', operand2: '', operator: ''} keys// => {operand1: '1', operand2: '1', 'ADD'}
Number
Appends a number to the state object.
keys
Arguments
number (string): Number to append.
operand1 (string): First operand property.
operand2 (string): Second operand property.
operator (string): Operation to perform.
Returns
(object): Returns a new state object.
Example
keys// => {operand1: '11', operand2: '', operator: ''} keys// => {operand1: '1', operand2: '1234', 'ADD'}
Operation
Saves the operation in the state object.
keys
Arguments
newOperation (string): New operation to perform.
operand1 (string): First operand property.
operand2 (string): Second operand property.
operator (string): Existing operation.
Returns
(object): Returns a new state object.
Example
keys// => {operand1: '1', operand2: '', operator: 'ADD'} keys// => {operand1: '2', operand2: '', 'MULTIPLY'}
Percent
Gets the percentage value of the active operand.
keys
Arguments
operand1 (string): First operand property.
operand2 (string): Second operand property.
operator (string): Operation to perform.
Returns
(object): Returns a new state object with the converted operand.
Example
keys// => {operand1: '0.01', operand2: '', operator: ''} keys// => {operand1: '1', operand2: '0.01', 'ADD'}