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

p90

v1.0.0

Published

A minimalist search and replace tool for preprocessing files.

Downloads

35

Readme

P90

A minimalist search and replace tool for preprocessing files.

P90 scans CSS for P90 tokens which are substituted with user defined values. It's really just an enhanced GREP using string.replace.

This tool is straight up optimised for me and my tastes. The design trade-offs lean towards simplicity and flexibility more than writability.

This tool is also low level, language agnostic, and doesn't handle IO. P69 is usually what you want to use. It provides Node based CSS preprocessing using P90 with out of the box support for Svelte.

The example below is CSS but this tool is language agnostic.

import p90 from 'p90'

// You can create any sort of utility functions you like.
const newSpacingFunc = (sizePx, base = 16) => {
	return (fmt = 'px') => {
		switch (fmt) {
			case 'px':
				return sizePx + 'px'
			case 'em':
				return sizePx / base + 'em'
			case 'rem':
				return sizePx / base + 'rem'
			default:
				throw new Error(`Unknown spacing fmt '${fmt}'`)
		}
	}
}

// You can configure this how you like.
// There's no convention, just do what works for you.
const valueMap = {
	$: (n = 1) => '$'.repeat(n), // Used to escape the prefix
	text: {
		family: {
			helvetica: ['Helvetica', 'Arial', 'Verdana'],
		},
		size: {
			// https://utopia.fyi/
			md: 'clamp(1.06rem, calc(0.98rem + 0.39vw), 1.38rem)',
			xl: 'clamp(2.59rem, calc(2.32rem + 1.34vw), 3.66rem)',
		},
	},
	color: {
		base: 'rgb(255, 255, 255)',
		text: 'rgb(11, 19, 43)',
		link: 'rgb(20, 20, 255)',
		strong: 'Navy',
	},
	space: {
		sm: newSpacingFunc(8),
		md: newSpacingFunc(16),
		lg: newSpacingFunc(32),
	},
}

const before = `
body {
	background: $color.base;
}

p {
	font-family: $text.family.helvetica;
	font-size: $text.size.md;
	color: $color.text;
	margin-top: $space.md(em);
}

h1 {
	font-size: $text.size.xl;
	color: $color.strong;
}

strong {
	color: $color.strong;
}
`

const options = {}
const after = p90(valueMap, before, options)
console.log(after)
/*
`
body {
	background: rgb(255, 255, 255);
}

p {
	font-family: 'Helvetica', 'Arial', 'Verdana';
	font-size: clamp(1.06rem, calc(0.98rem + 0.39vw), 1.38rem);
	color: rgb(11, 19, 43);
	margin-top: 1em;
}

h1 {
	font-size: clamp(2.59rem, calc(2.32rem + 1.34vw), 3.66rem);
	color: Navy;
}

strong {
	color: Navy;
}
`
*/

Options and their defaults

const options = {
	// Prefix character
	prefix: '$',

	// Logger for informational messages.
	stdout: console.log,

	// Logger for error messages.
	stderr: console.error,

	// If true, errors will be thrown rather than ignored.
	// This will immediately end processing.
	// Default is false because I use Svelte and it's good at
	// tell me where the errors are.
	throwOnError: false,

	// Print file name and token information when an error is
	// encountered.
	printErrors: true,

	// A note when printing errors, usually a filename or some
	// identifier that may aid you in debugging.
	errorNote: '¯\\_(ツ)_/¯',
}

Real example

See sveltekit-minimalist-template for an example in a runnable project.

Rules for value maps and usage

Value map rules:

  1. There are no rules, standards, or conventions on how one should organise their value maps. Do what works, not the popular opinion.
  2. Any value type is allowed except undefined, object, promise, and async functions.
  3. Functions are invoked and the result returned as the value.
  4. But a function cannot return undefined, object, promise, or another function of any kind.
  5. Nulls are resolved to empty strings, discarding any suffix.
  6. It's possible to pass an array of value maps p69([...]). Each value map is checked in turn for a valid value.

In-code usage rules:

  1. All variables must be prefixed. The default is '$' but this can be changed via an option.
  2. Functions can have arguments, e.g. $func(1, 2, 3).
  3. A function that has no arguments needs no parenthesis, e.g. $func is the same as $func().
  4. String arguments to functions do not require delimiters but single or double quotes may be applied, e.g. $func(abc) == $func("abc").
  5. String arguments must be quoted if it contains a comma or closing parenthesis.
  6. There is no special escape character, instead add $: (n=1) => '$'.repeat(n) to the value map and use in code, e.g. $$ => $ and $$(3) => $$$.
  7. Interesting useless side effect: you can pass arguments to a non-function; they're just not used in processing.
  8. Don't make changes to the value map in the middle of processing if you value your sanity. I don't cloned it before using it.