batchloader
Advanced tools
Comparing version 0.0.4 to 0.0.5
{ | ||
"name": "batchloader", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "BatchLoader is a utility for data fetching layer to reduce requests via batching written in TypeScript. Inspired by Facebook's DataLoader", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -104,7 +104,8 @@ # BatchLoader | ||
const usernameLoader = new MappedBatchLoader( | ||
userLoader, // previously defined loader | ||
(user) => user && user.username // mapping function | ||
); | ||
const getUsername = (user) => user && user.username; | ||
const usernameLoader = userLoader.mapLoader(getUsername); | ||
// or | ||
const usernameLoader = new MappedBatchLoader(userLoader, getUsername); | ||
// same APIs as BatchLoader | ||
@@ -118,6 +119,5 @@ const username = await usernameLoader.load(userId); | ||
const anotherMappedLoader = new MappedBatchLoader( | ||
usernameLoader, // MappedBatchLoader can be mapped, too. | ||
... | ||
); | ||
const anotherMappedLoader = usernameLoader.mapLoader(mapFn); | ||
// or | ||
const anotherMappedLoader = new MappedBatchLoader(usernameLoader, mapFn); | ||
``` | ||
@@ -124,0 +124,0 @@ |
@@ -86,2 +86,42 @@ import { BatchLoader } from './batchloader'; | ||
}); | ||
test('mapLoader()', async () => { | ||
const idss = [] as number[][]; | ||
const loader = new BatchLoader( | ||
(ids: number[]): Promise<number[]> => | ||
new Promise( | ||
(resolve): void => { | ||
idss.push(ids); | ||
setTimeout(() => resolve(ids.map((i) => i * 2)), 10); | ||
} | ||
), | ||
String | ||
) | ||
.mapLoader((x): Promise<string> => Promise.resolve(String(x))) | ||
.mapLoader((x) => `${x}${x}`); | ||
expect(await loader.load(3)).toBe('66'); | ||
expect(await loader.load(4)).toBe('88'); | ||
expect(await loader.load(5)).toBe('1010'); | ||
expect(await loader.loadMany([])).toEqual([]); | ||
expect(await loader.loadMany([1, 2, 3])).toEqual(['22', '44', '66']); | ||
expect(await loader.loadMany([1, 2, 3, 2, 3, 2, 1])).toEqual( | ||
[2, 4, 6, 4, 6, 4, 2].map((x) => `${x}${x}`) | ||
); | ||
expect( | ||
await Promise.all([ | ||
loader.load(1), | ||
loader.load(2), | ||
loader.load(3), | ||
loader.load(2), | ||
loader.load(1), | ||
loader.load(2), | ||
loader.load(3), | ||
]) | ||
).toEqual([2, 4, 6, 4, 2, 4, 6].map((x) => `${x}${x}`)); | ||
expect(idss).toEqual([[3], [4], [5], [1, 2, 3], [1, 2, 3], [1, 2, 3]]); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30150
532