-
Notifications
You must be signed in to change notification settings - Fork 12
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
Showing
14 changed files
with
150 additions
and
80 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
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import eq from './eq' | ||
import chunkBy from './chunkBy' | ||
|
||
/** | ||
* Chunks the values in a list. | ||
* | ||
* This is a special case of the `chunkBy` function where the values are compared | ||
* using the strict equality `===` operator. | ||
* | ||
* @function | ||
* @param {Array|String} as The list. | ||
* @returns {Array|String} A list that contains the values in the list of `as` | ||
* chunked into sublists of equal values. | ||
* @example | ||
* | ||
* import { chunk } from 'fkit' | ||
* chunk([1, 2, 2, 3, 3, 3]) // [[1], [2, 2], [3, 3, 3]] | ||
* chunk('Mississippi') // ['M', 'i', 'ss', 'i', 'ss', 'i', 'pp', 'i'] | ||
*/ | ||
export default chunkBy(eq) |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import chunk from './chunk' | ||
|
||
describe('chunk', () => { | ||
it('handles an empty array', () => { | ||
expect(chunk([])).toEqual([]) | ||
}) | ||
|
||
it('handles an empty string', () => { | ||
expect(chunk('')).toEqual([]) | ||
}) | ||
|
||
it('handles an array', () => { | ||
expect(chunk([1, 2, 2, 3, 3, 3])).toEqual([[1], [2, 2], [3, 3, 3]]) | ||
}) | ||
|
||
it('handles a string', () => { | ||
expect(chunk('Mississippi')).toEqual(['M', 'i', 'ss', 'i', 'ss', 'i', 'pp', 'i']) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import curry from './curry' | ||
import chunkBy from './uncurried/chunkBy' | ||
|
||
/** | ||
* Chunks the values in a list using a comparator function. | ||
* | ||
* The comparator function `f` compares two values, `a` and `b`. If the values | ||
* are both considered to be in the same chunk, then the comparator function | ||
* should return `true`. Otherwise it should return `false`. | ||
* | ||
* @function | ||
* @param {Function} c The comparator function. | ||
* @param {Array|String} as The list. | ||
* @returns {Array|String} A list that contains the values in the list of `as` | ||
* chunked into sublists that satisfy the comparator function `c`. | ||
* @example | ||
* | ||
* import { chunkBy } from 'fkit' | ||
* chunkBy((a, b) => a === b, [1, 2, 2, 3, 3, 3]) // [[1], [2, 2], [3, 3, 3]] | ||
* chunkBy((a, b) => a === b, 'Mississippi') // ['M', 'i', 'ss', 'i', 'ss', 'i', 'pp', 'i'] | ||
*/ | ||
export default curry(chunkBy) |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import chunkBy from './chunkBy' | ||
|
||
describe('chunkBy', () => { | ||
const f = (a, b) => a === b | ||
|
||
it('handles an empty array', () => { | ||
expect(chunkBy(f, [])).toEqual([]) | ||
}) | ||
|
||
it('handles an empty string', () => { | ||
expect(chunkBy(f, '')).toEqual([]) | ||
}) | ||
|
||
it('handles an array', () => { | ||
expect(chunkBy(f, [1, 2, 2, 3, 3, 3])).toEqual([[1], [2, 2], [3, 3, 3]]) | ||
}) | ||
|
||
it('handles a string', () => { | ||
expect(chunkBy(f, 'Mississippi')).toEqual(['M', 'i', 'ss', 'i', 'ss', 'i', 'pp', 'i']) | ||
}) | ||
|
||
it('calls the comparator function', () => { | ||
const spy = jest.fn() | ||
chunkBy(spy, [1, 2]) | ||
expect(spy).toHaveBeenCalledWith(2, 1) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,27 +1,26 @@ | ||
import groupBy from './groupBy' | ||
import id from './id' | ||
|
||
describe('groupBy', () => { | ||
const f = (a, b) => a === b | ||
|
||
it('handles an empty array', () => { | ||
expect(groupBy(f, [])).toEqual([]) | ||
expect(groupBy(id, [])).toEqual({}) | ||
}) | ||
|
||
it('handles an empty string', () => { | ||
expect(groupBy(f, '')).toEqual([]) | ||
expect(groupBy(id, '')).toEqual({}) | ||
}) | ||
|
||
it('handles an array', () => { | ||
expect(groupBy(f, [1, 2, 2, 3, 3, 3])).toEqual([[1], [2, 2], [3, 3, 3]]) | ||
expect(groupBy(id, [1, 2, 2, 3, 3, 3])).toEqual({ '1': [1], '2': [2, 2], '3': [3, 3, 3] }) | ||
}) | ||
|
||
it('handles a string', () => { | ||
expect(groupBy(f, 'Mississippi')).toEqual(['M', 'i', 'ss', 'i', 'ss', 'i', 'pp', 'i']) | ||
expect(groupBy(id, 'Mississippi')).toEqual({ 'M': ['M'], 'i': ['i', 'i', 'i', 'i'], p: ['p', 'p'], 's': ['s', 's', 's', 's'] }) | ||
}) | ||
|
||
it('calls the comparator function', () => { | ||
const spy = jest.fn() | ||
groupBy(spy, [1, 2]) | ||
expect(spy).toHaveBeenCalledWith(2, 1) | ||
it('handles an grouping by a key path', () => { | ||
const a = { address: { city: 'Melbourne' } } | ||
const b = { address: { city: 'Sydney' } } | ||
expect(groupBy('address.city', [a, b])).toEqual({ 'Melbourne': [a], 'Sydney': [b] }) | ||
}) | ||
}) |
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
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Returns true if `a` is a function. | ||
* | ||
* @private | ||
*/ | ||
export default function isFunction (a) { | ||
return (a instanceof Function) | ||
} |
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,6 +1,8 @@ | ||
/** | ||
* Returns true if `as` is a string or an array of strings. | ||
* Returns true if `a` is a string. | ||
* | ||
* @private | ||
*/ | ||
export default function isString (as) { return (typeof as === 'string') } | ||
export default function isString (a) { | ||
return (typeof a === 'string') | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import empty from '../empty' | ||
import head from '../head' | ||
import last from '../last' | ||
import span from '../span' | ||
import tail from '../tail' | ||
import prepend from './prepend' | ||
|
||
export default function chunkBy (c, as) { | ||
const b = head(as) | ||
const bs = span(a => c(a, b), tail(as)) | ||
|
||
return empty(as) | ||
? [] | ||
: prepend( | ||
prepend(b, head(bs)), | ||
chunkBy(c, last(bs)) | ||
) | ||
} |
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,18 +1,12 @@ | ||
import empty from '../empty' | ||
import head from '../head' | ||
import last from '../last' | ||
import span from '../span' | ||
import tail from '../tail' | ||
import prepend from './prepend' | ||
import fold from './fold' | ||
import getIn from './getIn' | ||
import isFunction from '../internal/isFunction' | ||
|
||
export default function groupBy (c, as) { | ||
const b = head(as) | ||
const bs = span(a => c(a, b), tail(as)) | ||
|
||
return empty(as) | ||
? [] | ||
: prepend( | ||
prepend(b, head(bs)), | ||
groupBy(c, last(bs)) | ||
) | ||
export default function groupBy (f, as) { | ||
const iteratee = a => isFunction(f) ? f(a) : getIn(f, a) | ||
return fold((memo, a) => { | ||
const key = iteratee(a); | ||
(memo[key] = memo[key] || []).push(a) | ||
return memo | ||
}, {}, as) | ||
} |