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

sievery

v0.0.1

Published

Removes unwanted lines from files

Downloads

3

Readme

sievery

Removes unwanted lines from files

npx sievery modern legacy -- index.js

index.modern.js index.legacy.js


What it is

What it does

sievery turns this:

const submitButtons = document.querySelectorAll( 'button[type=submit]:not([data-ajax])' ) // MODERN

// LEGACY/
var submitButtons = []
var buttons = document.getElementsByTagName( 'button' )

for ( var i = 0; i < buttons.length; i++ ) {
	var button = buttons[ i ]

	if ( button.type === 'submit' && !button.hasAttribute( 'data-ajax' ) ) {
		submitButtons.push(button)
	}
}
// /LEGACY

return submitButtons

into these:

const submitButtons = document.querySelectorAll( 'button[type=submit]:not([data-ajax])' )

return submitButtons
var submitButtons = []
var buttons = document.getElementsByTagName( 'button' )

for ( var i = 0; i < buttons.length; i++ ) {
	var button = buttons[ i ]

	if ( button.type === 'submit' && !button.hasAttribute( 'data-ajax' ) ) {
		submitButtons.push(button)
	}
}

return submitButtons

NOTE: See Markers if you need clarification on the semantics of the markers shown above.

What it does not

sievery does not actually care about the contents of your file. You can use it for JS, CSS, HTML, Markdown, C#, CSV, and any other text file.

When to use

sievery's primary intended use-case is splitting a single source file into multiple distributable files of largely the same content, hence the modern/legacy sample above.

When not to use

If all you need is a dual export to CJS/ESM, take a look at sievery. It's probably the a better fit. Depending on your needs, you may even combine them.

If your project is very large, you're probably using webpack or something like that. Take a look at webpack's DefinePlugin – together with dead code removal, it will cover most JS-based use-cases of sievery. If you're using another sophisticated build environment, look into their solution to your problem first.

If you have a large project without such a build environment, think seriously about adopting webpack, it's popular for a reason: it's awesome!

How to use it

node API

You can import either sievery or sievery/fs, depending on how you will use it.

sievery takes two mandatory arguments:

  • sourceData: a string containing the text you want to sift
  • targets: an array of strings use to mark your different output targets

sievery/fs is a thin wrapper around sievery that reads and writes files. It takes two mandatory arguments:

  • sourcePath: the path to the file containing the text you want to sift
  • targets: an array of string pairs:
    • the first string in each pair is the token used to mark the target in your source
    • the second string is the path to the file where the variant is to be written

And that is it.

Examples

Using sievery

const sievery = require('sievery')
const sourceData = getSourceData()
const [modernVariant, legacyVariant] = sievery(sourceData, ['MODERN', 'LEGACY'])

This is how you achieve the result from What it does

Using sievery/fs

const sieveryFS = require('sievery/fs')
sieveryFS(
	'index.js',
	[
		['MODERN', 'modern.js'],
		['LEGACY', 'legacy.js'],
	]
)

Same result, but in files, not in memory.

NOTE: if the target paths include directories, those will be created if they don't exist.

CLI

npx sievery <targetNames> -- <sourcePath> [<outDir>]

The CLI passes the given arguments through to the underlying node API, and works through sievery/fs.

IMPORTANT: The given target names are upper-cased for the tokens in your source, and lower-cased for the file names. That makes the command-line input case-insensitive, but the source file content very much case-sensitive.

Consequently, if you want to have full control over letter-case, you need to use the node API!

Examples

The basic form

npx sievery modern legacy -- index.js

This reads index.js and writes variants index.modern.js and index.legacy.js, based on the tokens MODERN and LEGACY.

Giving a target directory

npx sievery modern legacy -- index.js dist/

The same result, but index.modern.js and index.legacy.js are written into dist/.

NOTE: if the target directory doesn't exist, it will be created.

Markers

sievery takes string tokens to look for (see below), and turns them into markers that will trigger specific behaviour when found in source data.

For example, given the token "TOKEN", it will react to // TOKEN, // TOKEN/ (note the trailing slash), and // /TOKEN (note the slash before the token). What each means will explained next.

For better understanding, the following will assume using the tokens MODERN and LEGACY. Where relevant, the current iteration will be assumed to be MODERN.

When sievery encounters a line ending in // MODERN, it will include that line in its output, but remove the marker.

When sievery encounters a line ending in // LEGACY, it will drop that line.

When sievery encounters a line ending in // MODERN/, it will drop that line but include every line until it encounters a line ending in // /MODERN. That line will be dropped.

When sievery encounters a line ending in // LEGACY/, it will drop that line and every line until it encounters a line ending in // /LEGACY. That line will be dropped.