Skip to content

Commit

Permalink
Merge pull request #19 from ZIMkaRU/feature/add-min-max-utils
Browse files Browse the repository at this point in the history
Add max and min utils
  • Loading branch information
prdn authored Dec 11, 2023
2 parents 770e396 + 701ecc6 commit bff7d38
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.8.0
- feat: max
- feat: min

# 1.7.0
- feat: freezeDeep

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ Currently supported utils:
- `isNil` - checks whenever value is null or undefined
- `isObject` - checks if the input is not a nullable object instance
- `isPlainObject` - checks if input is object, not null object and not array object
- `max` - computes the maximum value of array. If array is empty or falsey, undefined is returned
- `merge` - deep merge functionality for objects
- `min` - computes the minimum value of array. If array is empty or falsey, undefined is returned
- `omit` - provides new object that omits only specific fields of source object
- `omitBy` - provides new object that omits only specific fields of source object depending on predicate function filter
- `pick` - provides new object that picks only specific fields of source object
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export function isFunction (val: any): boolean
export function isNil (val: any): boolean
export function isObject (verifiable: any): Boolean
export function isPlainObject (val: any): boolean
export function max (array: Array<any>): any
export function merge (obj: Object, ...sources: Object[]): Object
export function min (array: Array<any>): any
export function omit (obj: Object, keys: Array<string>): Object
export function omitBy (obj: Object, predicate: (val: any, key: string) => boolean): Object
export function pick (obj: Object, keys: Array<string>): Object
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const isFunction = require('./src/isFunction')
const isNil = require('./src/isNil')
const isObject = require('./src/isObject')
const isPlainObject = require('./src/isPlainObject')
const max = require('./src/max')
const merge = require('./src/merge')
const min = require('./src/min')
const omit = require('./src/omit')
const omitBy = require('./src/omitBy')
const pick = require('./src/pick')
Expand All @@ -32,7 +34,9 @@ module.exports = {
isNil,
isObject,
isPlainObject,
max,
merge,
min,
omit,
omitBy,
pick,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bitfinexcom/lib-js-util-base",
"version": "1.7.0",
"version": "1.8.0",
"description": "general utils",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions src/max.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const determineExtremumValue = require('./util/determineExtremumValue')

const max = (array) => {
return determineExtremumValue(
array,
(value, other) => value > other
)
}

module.exports = max
12 changes: 12 additions & 0 deletions src/min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const determineExtremumValue = require('./util/determineExtremumValue')

const min = (array) => {
return determineExtremumValue(
array,
(value, other) => value < other
)
}

module.exports = min
29 changes: 29 additions & 0 deletions src/util/determineExtremumValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const isNil = require('../isNil')

const determineExtremumValue = (array, comparator) => {
if (
!Array.isArray(array) ||
array.length === 0
) {
return
}

return array.reduce((prev, curr) => {
if (isNil(curr)) {
return prev
}
if (
prev === undefined &&
!Number.isNaN(curr) &&
typeof curr !== 'symbol'
) {
return curr
}

return comparator(curr, prev) ? curr : prev
}, undefined)
}

module.exports = determineExtremumValue
36 changes: 36 additions & 0 deletions test/max.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

/* eslint-env mocha */

const assert = require('assert')
const max = require('../src/max')

describe('max', () => {
it('should return the largest value from a collection', () => {
const actual = max([4, null, 2, 8, 6])

assert.strictEqual(actual, 8)
})

it('should return `undefined` without arguments', () => {
const actual = max()

assert.strictEqual(actual, undefined)
})

it('should return `undefined` for empty collections', () => {
const sources = [null, undefined, false, 0, NaN, '', Symbol('a'), []]

for (const source of sources) {
const actual = max(source)

assert.strictEqual(actual, undefined)
}
})

it('should return the largest value from a string collection', () => {
const actual = max(['cd', null, 'ab', 'gh', 'ef'])

assert.strictEqual(actual, 'gh')
})
})
36 changes: 36 additions & 0 deletions test/min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

/* eslint-env mocha */

const assert = require('assert')
const min = require('../src/min')

describe('min', () => {
it('should return the smallest value from a collection', () => {
const actual = min([4, null, 2, 8, 6])

assert.strictEqual(actual, 2)
})

it('should return `undefined` without arguments', () => {
const actual = min()

assert.strictEqual(actual, undefined)
})

it('should return `undefined` for empty collections', () => {
const sources = [null, undefined, false, 0, NaN, '', Symbol('a'), []]

for (const source of sources) {
const actual = min(source)

assert.strictEqual(actual, undefined)
}
})

it('should return the smallest value from a string collection', () => {
const actual = min(['cd', null, 'ab', 'gh', 'ef'])

assert.strictEqual(actual, 'ab')
})
})

0 comments on commit bff7d38

Please sign in to comment.