node-balanced
allows you to replace the contents inside of your own balanced constraints.
Last updated 10 years ago by icodeforlove .
ISC · Original npm · Tarball · package.json
$ npm install node-balanced 
SYNC missed versions from official npm registry.

Balanced Build Status

balanced string matching, and replacing.

install

npm install node-balanced

example time

lets say you have

{
	@hello 1 {
		a {
		}
	}
	@hello 2 {
		a {
		}
	}
	@hello 3 {
		a {
		}
	}
}

and you would like to replace the @hello block easily, balanced allows you to do this

var balanced = require('node-balanced');

balanced.replacements({
	source: source,
	head: /@hello \d \{/, // optional (defalut: open)
	open: '{',
	close: '}',
	balance: false, // optional (default: false)
	exceptions: false, // optional (default: false)
	replace: function (source, head, tail) {
		return head + source + tail;
	}
});

this is a simple and efficient way to make balanced replacements, without a parser.

matching

you can get balanced matches by doing the following

var balanced = require('node-balanced');

balanced.matches({
	source: source,
	head: /@hello \d \{/, // optional (defalut: open)
	open: '{',
	close: '}',
	balance: false, // optional (default: false) when set to true it will return `null` when there is an error
	exceptions: false // optional (default: false),
	ignore: [] // array of ignore ranges/matches
});

multiple head/open/close

you can match multiple head/open/close efficiently by doing this

var isBalanced = balanced.matches({
	source: '{[({)]}}',
	open: ['{', '[', '('],
	close: ['}', ']', ')'],
	balance: true
});

ignore

ignore is supported by the matches and replacements methods, this is very useful for something like not matching inside of comments

var blockComments = balanced.matches({source: source, open: '/*', close: '*/'}),
	singleLineComments = balanced.getRangesForMatch(source, /^\s*\/\/.+$/gim);

balanced.matches({
	source: source,
	head: /@hello \d \{/,
	open: '{',
	close: '}',
	ignore: Array.prototype.concat.call([], blockComments, singleLineComments),
	replace: function (source, head, tail) {
		return head + source + tail;
	}
});

advanced

in this example we have code and we want to avoid replacing text thats inside of the multiline/singleline comments, and quotes

{
	@hello 1 {
		a {
		}
	}
/*
	@hello 2 {
		a {
		}
	}
*/
	@hello 3 {
		a {
		}
	}
// @hello 4 {}
}

var hello = "@hello 5 {}";

with balanced you can do this

	// returns quote ranges with option ignore filter
	function getQuoteRanges (string, ignore) {
		var quotes = balanced.getRangesForMatch(string, new RegExp('\'|"', 'g'));
		
		// filter out ingored ranges
		if (ignore) {
			quotes = balanced.rangesWithout(quotes, ignore);
		}

		var currect = null,
			ranges = [];
	
		quotes.forEach(function (quote) {
			if (currect && currect.match === quote.match) {
					ranges.push({
						index: currect.index,
						length: quote.index - currect.index + 1
					});
					currect = null;
			} else if (!currect) {
				currect = quote;
			}
		});

		return ranges;
	}

	var blockComments = balanced.matches({source: string, open: '/*', close: '*/'}),
		singleLineComments = balanced.getRangesForMatch(string, /^\s*\/\/.+$/gim),
		ignores = Array.prototype.concat.call([], blockComments, singleLineComments),
		quotes = getQuoteRanges(string, ignores);

	// remove ignores inside of quotes
	ignores = balanced.rangesWithout(ignores, quotes);

	// optional ignore code inside of quotes
	ignores = ignores.concat(quotes);
	
	// run your matches or replacements method
	balanced.matches({
		source: string,
		head: /@hello \d \{/,
		open: '{',
		close: '}',
		ignore: ignores
	});

as you can see by using these principles you can accomplish this kind of stuff easily

Current Tags

  • 0.0.14                                ...           latest (10 years ago)

13 Versions

  • 0.0.14                                ...           10 years ago
  • 0.0.13                                ...           10 years ago
  • 0.0.12                                ...           10 years ago
  • 0.0.11                                ...           10 years ago
  • 0.0.10                                ...           10 years ago
  • 0.0.9                                ...           10 years ago
  • 0.0.8                                ...           10 years ago
  • 0.0.7                                ...           10 years ago
  • 0.0.6                                ...           10 years ago
  • 0.0.3                                ...           10 years ago
  • 0.0.2                                ...           10 years ago
  • 0.0.1                                ...           10 years ago
  • 0.0.0                                ...           10 years ago
Maintainers (1)
Downloads
Total 1
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE