Skip to content

Commit

Permalink
collectBy
Browse files Browse the repository at this point in the history
  • Loading branch information
Deyan Totev committed Jul 23, 2023
1 parent 4f552ba commit fb0f6b6
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 69 deletions.
2 changes: 1 addition & 1 deletion NEXT_VERSION_CHECKLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ fix https://github.com/selfrefactor/rambdax/issues/93
release X

- binary
- call

---
- call
- collectBy
- comparator
- composeWith
Expand Down
3 changes: 2 additions & 1 deletion files/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5503,7 +5503,8 @@ Notes:
*/
// @SINGLE_MARKER
export function collectBy<T>(x: T): T;
export function collectBy<T, K extends PropertyKey>(keyFn: (value: T) => K, list: T[]): T[][];
export function collectBy<T, K extends PropertyKey>(keyFn: (value: T) => K): (list: T[]) => T[][];

/*
Method: comparator
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"combinate": "1.1.11",
"cross-env": "7.0.3",
"dtslint": "4.2.1",
"fast-check": "^3.11.0",
"helpers-fn": "1.8.1",
"is-ci": "3.0.1",
"jest": "29.5.0",
Expand Down
28 changes: 24 additions & 4 deletions source/collectBy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
export function collectBy(foo, bar) {
import { reduce } from './reduce.js'

export function collectBy(fn, list){
if (arguments.length === 1){
return (_bar) => collectBy(foo, _bar);
return _list => collectBy(fn, _list)
}

const group = reduce(
(o, x) => {
const tag = fn(x)
if (o[ tag ] === undefined){
o[ tag ] = []
}
o[ tag ].push(x)

return o
},
{},
list
)
const newList = []
for (const tag in group){
newList.push(group[ tag ])
}

return
}
return newList
}
120 changes: 65 additions & 55 deletions source/collectBy.spec.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,68 @@
import { collectBy } from './collectBy'
import { collectBy as collectByRamda } from 'ramda'
import fc from 'fast-check'
import {
all,
compose,
difference,
equals,
head,
identity,
is,
isEmpty,
length,
uniq,
unnest,
} from 'rambdax'

test('happy', () => {
const result = collectBy()
console.log(result)
import { collectBy } from './collectBy.js'

test('returns a list of lists', () => {
fc.assert(fc.property(fc.array(fc.nat()), xs => {
const check = all(is(Array))
const ys = collectBy(identity)(xs)

return check(ys)
}))
})

/*
var {all, compose , difference , equals , head , identity , is , isEmpty , length , uniq , unnest , collectBy} = require('../source/index.js');
var fc = require('fast-check');
var {spy} = require('sinon');
describe('collectBy', function() {
it('returns a list of lists', function() {
fc.assert(fc.property(fc.array(fc.nat()), function(xs) {
var check = all(is(Array));
var ys = collectBy(identity)(xs);
return check(ys);
}));
});
it('groups items but neither adds new ones nor removes any', function() {
fc.assert(fc.property(fc.array(fc.nat()), function(xs) {
var check = compose(isEmpty, difference(xs), unnest);
var ys = collectBy(identity)(xs);
return check(ys);
}));
});
it('groups related items together', function() {
fc.assert(fc.property(fc.array(fc.boolean()), function(xs) {
var ys = collectBy(identity)(xs);
var check = all(compose(equals(1), length, uniq));
return check(ys);
}));
});
it('invokes the tag function for each item in the list', function() {
fc.assert(fc.property(fc.array(fc.nat()), function(xs) {
var id = spy(x => 42);
collectBy(id)(xs);
var check = compose(isEmpty, difference(xs));
return check(id.getCalls().map(call => call.args[0]));
}));
});
it('groups items according to the tag value', function() {
fc.assert(fc.property(fc.array(fc.nat()), function(xs) {
var ys = collectBy(x => 42)(xs);
var check = compose(isEmpty, difference(xs), head);
return isEmpty(xs) && isEmpty(ys) ? true : check(ys);
}));
});
});
*/
test('groups items but neither adds new ones nor removes any', () => {
fc.assert(fc.property(fc.array(fc.nat()), xs => {
const check = compose(
isEmpty, difference(xs), unnest
)
const ys = collectBy(identity)(xs)

return check(ys)
}))
})

test('groups related items together', () => {
fc.assert(fc.property(fc.array(fc.boolean()), xs => {
const ys = collectBy(identity)(xs)
const check = all(compose(
equals(1), length, uniq
))

return check(ys)
}))
})

test('invokes the tag function for each item in the list', () => {
fc.assert(fc.property(fc.array(fc.nat()), xs => {
const id = jest.fn(x => 42)
collectBy(id)(xs)
const check = compose(isEmpty, difference(xs))

return check(id.mock.calls.map(call => call[ 0 ]))
}))
})

test('groups items according to the tag value', () => {
fc.assert(fc.property(fc.array(fc.nat()), xs => {
const ys = collectBy(x => 42)(xs)
const check = compose(
isEmpty, difference(xs), head
)

return isEmpty(xs) && isEmpty(ys) ? true : check(ys)
}))
})
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1438,13 +1438,6 @@
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.7.2.tgz#6c2324641cc4ba050a8c710b2b251b377581fbf0"
integrity sha512-KufADq8uQqo1pYKVIYzfKbJfBAc0sOeXqGbFaSpv8MRmC/zXgowNZmFcbngndGk922QDmOASEXUZCaY48gs4cg==

"@types/[email protected]":
version "0.29.1"
resolved "https://registry.yarnpkg.com/@types/ramda/-/ramda-0.29.1.tgz#c3d3842b9bc4aa5fbe03e90faa0c3eff9ba0f9f4"
integrity sha512-Ff5RRG9YRqMgWOqZVVavSjGEvYHUnXnGF0YPGbzIWhB3o8qiccSJZlFX2z8qm3G1H/IC5w0ozHmlezUeQCtGfQ==
dependencies:
types-ramda "^0.29.2"

"@types/[email protected]":
version "1.20.2"
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-1.20.2.tgz#97d26e00cd4a0423b4af620abecf3e6f442b7975"
Expand Down Expand Up @@ -2312,6 +2305,13 @@ extsprintf@^1.2.0:
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.4.1.tgz#8d172c064867f235c0c84a596806d279bf4bcc07"
integrity sha512-Wrk35e8ydCKDj/ArClo1VrPVmN8zph5V4AtHwIuHhvMXsKf73UT3BOD+azBIW+3wOJ4FhEH7zyaJCFvChjYvMA==

fast-check@^3.11.0:
version "3.11.0"
resolved "https://registry.yarnpkg.com/fast-check/-/fast-check-3.11.0.tgz#bb3f7877c5ba7940b17f983afc412e4241b5aaee"
integrity sha512-H2tctb7AGfFQfz+DEr3UWhJ3s47LXsGp5g3jeJr5tHjnf4xUvpArIqiwcDmL2EXiv+auLHIpF5MqaIpIKvpxiA==
dependencies:
pure-rand "^6.0.0"

fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
Expand Down Expand Up @@ -4434,7 +4434,7 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==

types-ramda@^0.29.2:
[email protected]:
version "0.29.2"
resolved "https://registry.yarnpkg.com/types-ramda/-/types-ramda-0.29.2.tgz#2bf33863a51498b43f616cd8cd98227efe9109eb"
integrity sha512-HpLcR0ly2EfXQwG8VSI5ov6ml7PvtT+u+cp+7lZLu7q4nhnPDVW+rUTC1uy/SNs4aAyTUXri5M/LyhgvjEXJDg==
Expand Down

0 comments on commit fb0f6b6

Please sign in to comment.