format-prettify

1.1.1 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i format-prettify

Weekly Downloads

7

Version

1.1.1

License

GPL-3.0-only

Unpacked Size

10.8 kB

Total Files

3

Last publish

Collaborators

  • flutterish