{"size": 863, "ext": "js", "max_stars_repo_path": "src/index.js", "max_stars_repo_name": "quarterto/jsxt", "max_stars_repo_stars_event_min_datetime": "2016-05-15T08:18:57.000Z", "max_stars_repo_stars_event_max_datetime": "2016-05-15T08:18:57.000Z", "max_issues_repo_path": "src/index.js", "max_issues_repo_name": "quarterto/jsxt", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/index.js", "max_forks_repo_name": "quarterto/jsxt", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.7105263158, "max_line_length": 67, "alphanum_fraction": 0.6813441483}
import cheerio from 'cheerio';
import forEach from 'lodash.foreach';
import jsxCheerio, {cheerioRoot} from '@quarterto/jsx-cheerio';

export function match(selector, replacer) {
	return ($, transform) => {
		$(selector).each((i,matched) => {
			const matched$ = $(matched);
			const replacement = cheerioRoot(replacer({
				node: matched$,
				select: matched$.find.bind(matched$),
				apply: () => transform(matched$.children())
			}));
			matched$.replaceWith(replacement);
		});

		return $;
	};
}

export const element = jsxCheerio;

export default function transform(text, transforms, options = {}) {
	function transform(node) {
		forEach(transforms, t => t(node, transform));
		return node;
	}

	const transformed = transform(cheerio.load(text, options));

	if(options.returnSerialised !== false) {
		return transformed.html();
	}

	return transformed;
};

