Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

batchloader

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

batchloader - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

2

package.json
{
"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]]);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc