Skip to content

Commit

Permalink
feat: 🎸 add identifier parameter to [graph]
Browse files Browse the repository at this point in the history
  • Loading branch information
touv committed Sep 8, 2023
1 parent 3699fdb commit 961e201
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
13 changes: 6 additions & 7 deletions packages/analytics/src/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,21 @@ export default function graph(data, feed) {
feed.close();
return;
}
let fields = this.getParam('path', []);
if (!Array.isArray(fields)) {
fields = [fields];
}

const values = fields
const path = this.getParam('path', []);
const idt = this.getParam('identifier', false);
const weight = idt === false ? 1 : get(data, idt, 1);
const values = [].concat(path)
.map((key) => get(data, key))
.filter((x) => x)
.map((item) => (item instanceof Array ? item : [item]))
.reduce((pre, cur) => pre.concat(cur), [])
.filter(Boolean)
.sort();

values.forEach(
(v, i) => values
.slice(i + 1)
.forEach((w) => feed.write(core([v, w], 1))),
.forEach((w) => feed.write(core([v, w], weight))),
);
feed.end();
}
26 changes: 25 additions & 1 deletion packages/analytics/test/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import statements from '../src';
ezs.addPath(__dirname);

describe('network', () => {
it('graph', (done) => {
it('graph #1', (done) => {
ezs.use(statements);
const res = [];
from([
Expand All @@ -30,6 +30,30 @@ describe('network', () => {
});
});

it('graph #2', (done) => {
ezs.use(statements);
const res = [];
from([
{ i: 'doc#1', a: ['x', 'b', 'z'] },
{ i: 'doc#2', a: ['t', 'b', 'z'] },
{ i: 'doc#3', a: ['t', 'c', 'z'] },
{ i: 'doc#4', a: ['y', 'd', 'z'] },
{ i: 'doc#5', a: ['x', 'b', 'z'] },
])
.pipe(ezs('graph', { path: 'a', identifier: 'i' }))
.pipe(ezs('reducing'))
.on('data', (chunk) => {
assert(typeof chunk === 'object');
res.push(chunk);
})
.on('end', () => {
assert.equal(10, res.length);
assert.equal(2, res[1].value.length);
done();
});
});


it('segment #3', (done) => {
ezs.use(statements);
const res = [];
Expand Down

0 comments on commit 961e201

Please sign in to comment.