Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve list tags performances #57

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/List.context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import test from 'ava';
import { List } from './List';

test('context › get contexts', (t) => {
const list = new List('first item @context-1\nsecond item @context-2');
t.deepEqual(list.contexts(), ['context-1', 'context-2']);
});

test('context › deduplicate', (t) => {
const list = new List('first item @context-1\nsecond item @context-1');
t.deepEqual(list.contexts(), ['context-1']);
});
17 changes: 17 additions & 0 deletions src/List.extensions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import test from 'ava';
import { List } from './List';

test('extensions › get extensions', (t) => {
const list = new List('first item ext-1:value-1\nsecond item ext-2:value-2');
t.deepEqual(list.extensions(), {
'ext-1': ['value-1'],
'ext-2': ['value-2'],
});
});

test('extensions › deduplicate', (t) => {
const list = new List('first item ext-1:value-1\nsecond item ext-1:value-1 ext-1:value-2');
t.deepEqual(list.extensions(), {
'ext-1': ['value-1', 'value-2'],
});
});
12 changes: 12 additions & 0 deletions src/List.projects.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import test from 'ava';
import { List } from './List';

test('projects › get projects', (t) => {
const list = new List('first item +project-1\nsecond item +project-2');
t.deepEqual(list.projects(), ['project-1', 'project-2']);
});

test('projects › deduplicate', (t) => {
const list = new List('first item +project-1\nsecond item +project-1');
t.deepEqual(list.projects(), ['project-1']);
});
44 changes: 28 additions & 16 deletions src/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,41 @@ export class List {
}

projects(): string[] {
return [
...new Set(this.#items.map((item) => item.projects()).reduce((p, n) => [...p, ...n], [])),
];
const projects = new Set<string>();

for (const item of this.#items) {
item.projects().forEach((p) => projects.add(p));
}

return [...projects];
}

contexts(): string[] {
return [
...new Set(this.#items.map((item) => item.contexts()).reduce((p, n) => [...p, ...n], [])),
];
const context = new Set<string>();

for (const item of this.#items) {
item.contexts().forEach((c) => context.add(c));
}

return [...context];
}

extensions(): KeysForExtensions {
const ret: KeysForExtensions = {};

this.#items.forEach((item) => {
item.extensions().forEach((ext) => {
const values = ret[ext.key] || [];
values.push(ext.value);
ret[ext.key] = [...new Set(values)];
});
});
const extensionsSets = this.#items
.flatMap((i) => i.extensions())
.reduce<Record<string, Set<string>>>((acc, { key, value }) => {
if (acc[key] === undefined) {
acc[key] = new Set([value]);
} else {
acc[key].add(value);
}
return acc;
}, {});

return ret;
return Object.entries(extensionsSets).reduce<KeysForExtensions>((acc, [key, value]) => {
acc[key] = [...value];
return acc;
}, {});
}

filter(input: ListFilter): ListItem[] {
Expand Down
Loading