Created
May 24, 2023 11:39
-
-
Save Phury/2f15120096283bd20250c494a6898613 to your computer and use it in GitHub Desktop.
This code show how to play with objects and maps to revert the values and filter them
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
import {expect} from "@jest/globals"; | |
import {map, of} from "rxjs"; | |
function matchesAny(tuples: [string, string][], prefix: string) { | |
return tuples.filter(t => matches(t, prefix)); | |
} | |
function matches(tuple: [string, string], prefix: string) { | |
return tuple[0].startsWith(prefix) | |
} | |
describe('Maps', () => { | |
it('should reverse map', () => { | |
let data = { | |
"foobar.cert": ["info.foo", "error.bar", "info.baz"], | |
"foobaz.cert": ["info.foo", "error.bar", "info.baz"], | |
"foobarbaz.cert": ["info.foo", "error.bar", "error.baz"], | |
"foobazbar.cert": ["info.foo", "info.bar"], | |
}; | |
let observable = of(data); | |
let batchResult$ = observable.pipe( | |
map(res => new Map(Object.entries(res))), | |
map( mapOf => { | |
let reverseLookup: [string, string][] = []; | |
mapOf.forEach((messages, cert) => { | |
messages.reduce((acc, msg) => { | |
acc.push([msg, cert]); | |
return acc; | |
}, reverseLookup); | |
}); | |
return reverseLookup; | |
}) | |
); | |
let info$ = batchResult$.pipe( | |
map(tuples => matchesAny(tuples, "info")) | |
); | |
info$.subscribe(tuples => { | |
tuples.forEach(t => { | |
expect(t[0]).toMatch("^info") | |
}) | |
}); | |
let error$ = batchResult$.pipe( | |
map(tuples => matchesAny(tuples, "error")) | |
); | |
error$.subscribe(tuples => { | |
tuples.forEach(t => { | |
expect(t[0]).toMatch("^error") | |
}) | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment