-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea2f040
commit e425780
Showing
2 changed files
with
13 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import { maxBy, path, split, apply, compose } from 'ramda'; | ||
import { split, last, sortBy, path } from 'ramda'; | ||
import camelcase from 'camelcase'; | ||
|
||
const maxByProp = compose(apply, maxBy, path, split('.')); | ||
const splitByDot = split('.'); | ||
const maxCamel = camelcase.bind(null, 'max'); | ||
|
||
export default function maxValues(arr, props) { | ||
if (!arr || !props) return; | ||
props.forEach(prop => { maxByProp(prop)(arr)[camelcase('max', prop)] = true; }); | ||
props.forEach(prop => { | ||
const sortByProp = sortBy(path(splitByDot(prop))); | ||
last(sortByProp(arr))[maxCamel(prop)] = true; | ||
}); | ||
return arr; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import { equal, deepEqual } from 'assert'; | ||
import maxValues from './index'; | ||
|
||
const input = [{ name: 'Bob', age: 25, skills: { crypto: 5 }}, | ||
{ name: 'Alice', age: 14, skills: { crypto: 10 }}] | ||
const input = [{ name: 'Bob', age: 25, skills: { crypto: 1 }}, | ||
{ name: 'Some', age: 14, skills: { crypto: 5 }}, | ||
{ name: 'Alice', age: 7, skills: { crypto: 10 }}] | ||
|
||
it('should maxValues', () => | ||
deepEqual(maxValues(input, ['age', 'skills.crypto']), | ||
[{ name: 'Bob', age: 25, skills: { crypto: 5 }, maxAge: true }, | ||
{ name: 'Alice', age: 14, skills: { crypto: 10 }, maxSkillsCrypto: true }])); | ||
[{ name: 'Bob', age: 25, skills: { crypto: 1 }, maxAge: true }, | ||
{ name: 'Some', age: 14, skills: { crypto: 5 }}, | ||
{ name: 'Alice', age: 7, skills: { crypto: 10 }, maxSkillsCrypto: true }])); | ||
|
||
it('should maxValues invalid input', () => | ||
equal(maxValues(), undefined)); |