Skip to content

Commit

Permalink
maxBy → sortBy
Browse files Browse the repository at this point in the history
  • Loading branch information
iamstarkov committed Aug 20, 2015
1 parent ea2f040 commit e425780
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
10 changes: 7 additions & 3 deletions index.js
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;
};
10 changes: 6 additions & 4 deletions test.js
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));

0 comments on commit e425780

Please sign in to comment.