npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@transformation/file

v7.0.2

Published

Transformations for working with files

Downloads

8,546

Readme

@transformation/file

A package for using child processes.

readFile

Reads the content of a given file into the pipeline.

Notice this step won't take any input, it only outputs file content.

const { readFile } = require("@transformation/file");

If you have a file count.txt with the following content.

one
two
three

Then you can read that into the pipeline this way:

import { splitIterable } from "@transformation/core";

await expect(
  pipeline(
    readFile("count.txt", "utf8"),
    (data) => data.trim().split("\n"),
    splitIterable()
  ),
  "to yield items",
  ["one", "two", "three"]
);

If we want to read the name of a package, we can combine readFile with fromJSON.

const { pipeline, fromJSON, map } = require("@transformation/core");

await expect(
  pipeline(
    readFile("package.json", "utf8"),
    fromJSON(),
    map(({ name }) => name)
  ),
  "to yield items",
  ["@transformation/file"]
);

readEachFile

Read incoming paths into a file entry that contains the path and the content.

const { readEachFile } = require("@transformation/file");

If you have three files with the following content.

  • 1.txt: one
  • 2.txt: two
  • 3.txt: three

Then you can read them into the pipeline this way:

import { emitItems, pipeline } from "@transformation/core";

await expect(
  pipeline(emitItems("1.txt", "2.txt", "3.txt"), readEachFile("utf8")),
  "to yield items",
  [
    {
      path: "1.txt",
      data: "one",
    },
    {
      path: "2.txt",
      data: "two",
    },
    {
      path: "3.txt",
      data: "three",
    },
  ]
);

writeFile

Write each item to the given path as a side-effect.

import { writeFile } from "@translation/file";
import { emitItems, pipeline } from "@transformation/core";

await expect(
  pipeline(emitItems("Hello", "world"), writeFile("./output.txt")),
  "to yield items",
  ["Hello", "world"]
);

await expect(fs.readFileSync("./output.txt", "utf8"), "to equal", "world");

writeEachFile

Write each item to a file.

import { writeEachFile } from "@translation/file";

If you provide a function as the argument, it will write the content of the item to the path returned by the function.

import { emitItems, pipeline } from "@transformation/core";

await expect(
  pipeline(
    emitItems("hello", "world"),
    writeEachFile((value) => path.join(outputDir, `${value}.txt`))
  ),
  "to yield items",
  ["hello", "world"]
);

await expect(fs.readFileSync("./hello.txt", "utf8"), "to equal", "hello");
await expect(fs.readFileSync("./world.txt", "utf8"), "to equal", "world");

If you don't provide a function, the 'path', 'data' and 'options' will be read from the incoming items.

import { emitItems, program } from "@transformation/core";

await program(
  emitItems(
    { path: "1.txt", data: "one", options: { encoding: "utf8" } },
    { path: "2.txt", data: "two", options: "utf8" },
    { path: "3.txt", data: "three" }
  ),
  writeEachFile()
);

await expect(fs.readFileSync("./1.txt", "utf8"), "to equal", "one");
await expect(fs.readFileSync("./2.txt", "utf8"), "to equal", "two");
await expect(fs.readFileSync("./3.txt", "utf8"), "to equal", "three");

Let's do a bit more advanced example, where we glob for files, upper case their content and write them back to disk.

import { glob } from "@transformation/glob";
import { map, program, transform } from "@transformation/core";
import { readEachFile, writeEachFile } from "@transformation/file";

await program(
  glob("./test/*.txt"),
  readEachFile("utf8"),
  transform({
    data: map((data) => data.toUpperCase()),
  }),
  writeEachFile()
);