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

format-prettify

v1.1.1

Published

A package that allows you to automate formatting text based on tagging its parts and parsing through custom rules.

Downloads

3

Readme

Format-prettify

Get started

const { TextPiece, FormattingMatch, FormattingRule,
	FormattingPass, FormatCollection } = require( 'format-prettify' );

Tagging text

new TextPiece( 'some text', 'tag1', 'tag2', 'and another one' ... );

Matching text

let match = new FormattingMatch() // include any number of the rules below:
	.IncludeAny  ( tags ) // false if no tags from this list
	.IncludeExact( tags ) // false if doesnt exactly match ( no order )
	.ExcludeAny  ( tags ) // false if any tags from this list
	.ExcludeExact( tags ) // false if matches exactly ( no order )
	.regexTextMatch( regex ) // false if text doesnt match
	.functionTextTest( ( text ) => { return text === otherText; } ) // false if returns false
	.functionTagTest ( ( tags ) => { return tags.length === 2; } ); // false if returns false
	
match.test( textPiece ); // otherwise true

Making rules

let rule = new FormattingRule ( formattingMatch1 ) // match a single textPiece
	.EndMatch( formattingMatch2 ) // or a range
	.Nest( numberFrom0To2 ) // change nesting ( more below )
	.callback( ( pieces ) => { pieces.forEach( p => { p.text += '\r\n'; } ); } ); // this is what happens to the text in selected range

You cant run a rule because its just a data storage, that is the job of the FormattingPass class.

Nesting

0 - default
{ ( A begin )
a	{ ( B begin )
a	b	.
a	} ( B end )
} ( A end )

1 - first come first serve
{ ( A begin )
a	{ ( B begin )
b	a	.
b	} ( A end )
} ( B end )

2 - end all
{ ( A begin )
a	{ ( B begin )
b	a	.
} ( A and B end )
} ( nothing )

Running the shenigan

let pass = new FormattingPass( ...formattingRules );

pass.run( [ ...textPieces ] ); // returns the same array, but after running the rules
pass.format( [ ...textPieces ] ); // returns a string after applying rules

Multiple passes

let ruleset = new FormatCollection ( ...formattingPasses );

ruleset.run( [ ...textPieces ] ); // returns a string after running passes in order

Example

let test = [
	new TextPiece( '{', 'object', 'begin' ),
	new TextPiece( '{', 'object', 'begin' ),
	new TextPiece( 'field', 'field' ),
	new TextPiece( 'value', 'value' ),
	new TextPiece( '}', 'object', 'end' ),
	new TextPiece( '}', 'object', 'end' )
];

let rules = new FormatCollection(
	new FormattingPass(
		new FormattingRule( new FormattingMatch().IncludeExact( 'field' ) )
			.callback( ( p ) => { p.forEach( e => { e.text = e.text + ': '; } ); } ),
		new FormattingRule( new FormattingMatch().IncludeExact( 'object', 'begin' ) )
			.EndMatch( new FormattingMatch().IncludeExact( 'object', 'end' ) )
			.callback( ( p ) => { p.forEach( e => { if ( e !== p[ 0 ] && e !== p[ p.length - 1 ] && new FormattingMatch().IncludeAny( 'field', 'object' ).test( e ) ) e.text = '\t' + e.text; } ); } )
	),
	new FormattingPass(
		new FormattingRule( new FormattingMatch().IncludeExact( 'object', 'begin' ) )
			.callback( ( p ) => { p.forEach( e => { e.text = e.text + '\r\n'; } ); } ),
		new FormattingRule( new FormattingMatch().IncludeExact( 'object', 'end' ) )
			.callback( ( p ) => { p.forEach( e => { e.text = '\r\n' + e.text; } ); } ),
	)
);

console.log( rules.run( test ) );

Output:

{
    {
        field: value
    }
}

More functions

FormattingPass.selectNewLines( [ ...textPieces ] ); // returns [ ... { index: number, piece: TextPiece } ... ]

Example:

let pieces = [ 
	new TextPiece( 'blah blah blah\r\n' ),
	new TextPiece( 'this', 'comment', 'begin' ),
	new TextPiece( '\r\nis' ),
	new TextPiece( ' a\r\n' ),
	new TextPiece( 'comment' ),
	new TextPiece( '\r\nI had 1 extra line', 'comment', 'end' ),
	new TextPiece( '\r\nprobably a function here?' )
];

console.log( new FormattingPass( 
	new FormattingRule( new FormattingMatch().IncludeExact( 'comment', 'begin' ) )
		.EndMatch( new FormattingMatch().IncludeExact( 'comment', 'end' ) )
		.callback( pieces => {
			FormattingPass.selectNewLines( pieces ).forEach( e => {
				e.piece.text = e.piece.text.substring( 0, e.index ) + '// ' + e.piece.text.substr( e.index );
			});
		} )
).format( pieces ) );

Output:

blah blah blah
// this
// is a
// comment
// I had 1 extra line
probably a function here?

Thats all

npm i format-prettify