calculator-scripts
v1.0.0
Published
Calculator Javascript Library.
Downloads
1
Maintainers
Readme
Installation
Using npm:
$ npm i --save calculator-keys
In your frontend project:
import * as keys from 'calculator-keys'
Basic Key Functions
Clear
Clears the state properties.
keys.clear()
Returns
(object): Returns the state object with empty string properties.
Example
keys.clear()
// => {operand1: '', operand2: '', operator: ''}
Decimal
Converts active operand into a decimal number.
keys.decimal({operand1, operand2, operator})
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.decimal({operand1: '1', operand2: '', operator: ''})
// => {operand1: '1.', operand2: '', operator: ''}
keys.decimal({operand1: '1', operand2: '1', operator: 'ADD'})
// => {operand1: '1', operand2: '1.', 'ADD'}
Equals
Performs calculation between operands.
keys.equals({operand1, operand2, operator})
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.equals({operand1: '1', operand2: '1', operator: 'ADD'})
// => {operand1: '2', operand2: '', operator: ''}
keys.equals({operand1: '100', operand2: '10', operator: 'DIVIDE'})
// => {operand1: '10', operand2: '', ''}
Negation
Converts the active operand into negative or positive number.
keys.negation({operand1, operand2, operator})
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.negation({operand1: '1', operand2: '', operator: ''})
// => {operand1: '-1', operand2: '', operator: ''}
keys.negation({operand1: '1', operand2: '-1', operator: 'ADD'})
// => {operand1: '1', operand2: '1', 'ADD'}
Number
Appends a number to the state object.
keys.number(number, {operand1, operand2, operator})
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.number('1', {operand1: '1', operand2: '', operator: ''})
// => {operand1: '11', operand2: '', operator: ''}
keys.number('4', {operand1: '1', operand2: '123', operator: 'ADD'})
// => {operand1: '1', operand2: '1234', 'ADD'}
Operation
Saves the operation in the state object.
keys.operation(newOperation, {operand1, operand2, operator})
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.operation('ADD', {operand1: '1', operand2: '', operator: ''})
// => {operand1: '1', operand2: '', operator: 'ADD'}
keys.operation('MULTIPLY', {operand1: '1', operand2: '1', operator: 'ADD'})
// => {operand1: '2', operand2: '', 'MULTIPLY'}
Percent
Gets the percentage value of the active operand.
keys.percent({operand1, operand2, operator})
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.percent({operand1: '1', operand2: '', operator: ''})
// => {operand1: '0.01', operand2: '', operator: ''}
keys.decimal({operand1: '1', operand2: '1', operator: 'ADD'})
// => {operand1: '1', operand2: '0.01', 'ADD'}