Skip to content

Commit

Permalink
✨ Add lodash math functions (#82)
Browse files Browse the repository at this point in the history
* ✨ Add math.divide
* ✨ Add math.multiply
* ✨ Add math.subtract
  • Loading branch information
nlepage committed Sep 1, 2017
1 parent a2448ae commit 037b643
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/math/divide.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import _divide from 'lodash/divide'
import { convert } from 'util/convert'

/**
* Replaces by the division of the former number and the given number.
* @function
* @memberof math
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {number} divisor The second number in the division.
* @return {Object} Returns the updated object.
* @example divide({ nested: { prop: 1332 } }, 'nested.prop', 2) // => { nested: { prop: 666 } }
* @see {@link https://lodash.com/docs#divide|lodash.divide} for more information.
* @since 0.3.0
*/
const divide = convert(_divide)
export { divide, divide as default }
20 changes: 20 additions & 0 deletions src/math/divide.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-env jest */
import { divide } from './divide'
import { immutaTest } from 'test.utils'

describe('Divide', () => {

it('should divide two numbers', () => {
immutaTest((input, path) => {
const output = divide(input, path, 2)
expect(output).toEqual({
nested: { prop: 666 }, // 😈
other: {},
})
return output
}, {
nested: { prop: 1332 },
other: {},
}, 'nested.prop')
})
})
6 changes: 6 additions & 0 deletions src/math/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { add } from './add'
import { divide } from './divide'
import { multiply } from './multiply'
import { subtract } from './subtract'

/**
* Math functions.
Expand All @@ -7,4 +10,7 @@ import { add } from './add'
*/
export {
add,
divide,
multiply,
subtract,
}
17 changes: 17 additions & 0 deletions src/math/multiply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import _multiply from 'lodash/multiply'
import { convert } from 'util/convert'

/**
* Replaces by the multiplication of the former number and the given number.
* @function
* @memberof math
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {number} multiplicand The second number in the multiplication.
* @return {Object} Returns the updated object.
* @example multiply({ nested: { prop: 333 } }, 'nested.prop', 2) // => { nested: { prop: 666 } }
* @see {@link https://lodash.com/docs#multiply|lodash.multiply} for more information.
* @since 0.3.0
*/
const multiply = convert(_multiply)
export { multiply, multiply as default }
20 changes: 20 additions & 0 deletions src/math/multiply.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-env jest */
import { immutaTest } from 'test.utils'
import { multiply } from './multiply'

describe('Multiply', () => {

it('should multiply two numbers', () => {
immutaTest((input, path) => {
const output = multiply(input, path, 2)
expect(output).toEqual({
nested: { prop: 666 }, // 😈
other: {},
})
return output
}, {
nested: { prop: 333 },
other: {},
}, 'nested.prop')
})
})
17 changes: 17 additions & 0 deletions src/math/subtract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import _subtract from 'lodash/subtract'
import { convert } from 'util/convert'

/**
* Replaces by the subtraction of the former number by the given number.
* @function
* @memberof math
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {number} subtrahend The number to subtract.
* @return {Object} Returns the updated object.
* @example subtract({ nested: { prop: 2000 } }, 'nested.prop', 336) // => { nested: { prop: 1664 } }
* @see {@link https://lodash.com/docs#subtract|lodash.subtract} for more information.
* @since 0.3.0
*/
const subtract = convert(_subtract)
export { subtract, subtract as default }
20 changes: 20 additions & 0 deletions src/math/subtract.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-env jest */
import { immutaTest } from 'test.utils'
import { subtract } from './subtract'

describe('Subtract', () => {

it('should subtract two numbers', () => {
immutaTest((input, path) => {
const output = subtract(input, path, 336)
expect(output).toEqual({
nested: { prop: 1664 }, // 🍺
other: {},
})
return output
}, {
nested: { prop: 2000 },
other: {},
}, 'nested.prop')
})
})

0 comments on commit 037b643

Please sign in to comment.