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

swapem

v1.0.1

Published

Text processor that replaces directives by values defined in JSON

Downloads

4

Readme

Swapem

Swapem is a text processor that can find swap directives inside a text stream and replace them by the values they ask for.

You can use swapem as a Node module or via CLI with npx.

Usage

From Node via transform

You can import the transform and use it in a pipeline like this:

import { SwapDirectiveTemplate, SwapTransform } from "swapem";
import { Readable } from "node:stream";
import { pipeline } from "node:stream/promises";

await pipeline(
  Readable.from("Hello, #package.name#!"),
  new SwapTransform({
    template: SwapDirectiveTemplate.fromString("# . #"),
    swapData: {
      package: {
        name: "Swapem",
      },
    },
  }),
  process.stdout,
);

This will output Hello, Swapem!.

CLI

Help command

Use npx swapem --help to view the available options.

CLI example

Input files
/* example.swapem.css */
p {
  color: buildvar(--colors-red);
  font-size: buildvar(--sizes-text-medium);
}
// data.json
{
  "colors": {
    "red": "#ff0000"
  },
  "sizes": {
    "text": {
      "medium": "1rem"
    }
  }
}
Running the command
npx swapem                        \
  --input-file example.swapem.css \
  --output example.css            \
  --data-file data.json           \
  --template "buildvar(-- - )"`
Generated file
/* example.css */
p {
  color: #ff0000;
  font-size: 1rem;
}

How does this work?

General definitions

Swapem analyzes a text stream and looks for swap directives. A swap directive consists of 3 parts: a start token, a swap path and an end token.

A start token is a sequence of characters that mark the start of a swap directive. After the start token there is the swap path, which is a sequence of strings joined by a path separator token. Then there is the end token.

You can choose the start token, the path separator token and the end token by providing a swap directive template such as <! -> !>, where <! is the start token, !> is the end token and -> is the path separator. This example template will match directives like <!colors->red!> or <! layout->main->width !>.

Accessing data

Swap paths are called paths for a reason: they are used to traverse a tree of data. This tree is called swap data and can be provided through Node directly, or in the CLI as a JSON file or as inline JSON.

Each swap data node can be either a string (leaf node) or an object containing child data nodes.

The directive <colors.red> (template < . >) will access the colors node in the swap data and then reach the red leaf node inside it. The whole directive will be replaced by #ff0000 per the following JSON:

{
  "colors": {
    "red": "#ff0000"
  }
}

If the provided swap path is invalid or ends on a non-leaf node, an error will be thrown.