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

csv-async-generator

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

csv-async-generator

A tiny CSV library to serialize and deserialize in a performant way. You can take a look at the kind of memory problems it can solve at [serialize-async-generator.test.ts](src/__tests__/serialize-async-generator.test.ts).

  • 1.0.8
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-83.33%
Maintainers
1
Weekly downloads
 
Created
Source

CSV Async Generator

A tiny CSV library to serialize and deserialize in a performant way. You can take a look at the kind of memory problems it can solve at serialize-async-generator.test.ts.

Usage

Basic

expect(
  serialize(
    [
      { propA: "A1", propB: "B1" },
      { propA: "A2", propB: "B2" },
    ],
    [
      { header: "A", cell: (item) => item.propA },
      { header: "B", cell: (item) => item.propB },
    ],
  ),
).toBe("A;B\r\n" + "A1;B1\r\n" + "A2;B2\r\n");

Advanced

const getPositiveNumbers = async function* () {
  let number = 1;
  while (true) {
    yield Promise.resolve(number);
    number++;
  }
};

const limit = async function* <Item>(items: AsyncIterable<Item>, max: number) {
  let count = 0;
  for await (const item of items) {
    if (count >= max) {
      return;
    }

    yield item;
    count++;
  }
};

const filter = async function* <Item>(
  items: AsyncIterable<Item>,
  predicate: (item: Item) => any,
) {
  for await (const item of items) {
    if (predicate(item)) {
      yield item;
    }
  }
};

const map = async function* <Input, Output>(
  items: AsyncIterable<Input>,
  mapper: (item: Input) => Output,
) {
  for await (const item of items) {
    yield mapper(item);
  }
};

const getFirstFiveEvenPositiveNumbersAsItems = () =>
  map(
    limit(
      filter(getPositiveNumbers(), (it) => it % 2),
      5,
    ),
    (it) => ({ value: String(it) }),
  );

const lines: Array<string> = [];
for await (const line of serializeAsyncGenerator(
  getFirstFiveEvenPositiveNumbersAsItems(),
  [{ header: "Value", cell: (item) => item.value }],
)) {
  lines.push(line);
}

expect(lines).toStrictEqual([
  "Value\r\n",
  "1\r\n",
  "3\r\n",
  "5\r\n",
  "7\r\n",
  "9\r\n",
]);

FAQs

Package last updated on 23 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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