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

@putout/compare

v15.0.2

Published

compare AST-nodes according to 🦎 PutoutScript

Downloads

84,496

Readme

@putout/compare NPM version

Compare AST-nodes according to 🦎PutoutScript.

Install

npm i @putout/compare

API

getTemplateValues(node, template)

Get template values from node according to 🦎PutouScript template.

  • node - AST-node or code that will be generated;
  • template - 🦎PutouScript;
const {operator} = require('putout');
const {template} = operator;
const node = template.ast('const [] = a');

getTemplateValues(node, 'const __array = array');
// returns
({
    __array: {
        type: 'ArrayPattern',
    },
});

compare(node: string | Node, template: string | Node [, options: Options])

  • node - AST-node or code that will be generated;
  • template - AST-node with support of template variables.
  • options - (optional) - object with properties:
    • findUp (default: true) - find up template node;

compareAll(node: string | Node, templates: string[] | Node|Nodes[], [, options: Options])

Compare nodes feats templates.

compareAny(node: string | Node, templates: string[] | Node|Nodes[], [, options: Options])

Compare any nodes that feats one of templates

compareAny(path, 'const __a = __b', {
    findUp: false,
});

Supported template variables:

__

Any node.

compare('const x = data', 'const __ = __');
compare('const {x} = data', 'const __ = __');
compare('const x = {data}', 'const __ = __');
compare('<h1>hello</h1>', '<h1>__</h1>');
// returns
true;
__object

ObjectPattern or ObjectExpression with any count of properties.

compare('const {} = data', 'const __object = __');
compare('const {hello} = data', 'const __object = __');
// returns
true;
__array

ArrayPattern or ArrayExpression with any count of elements.

compare('const [] = data', 'const __array = __');
compare('const [hello] = data', 'const __array = __');
compare('const hello = [data]', 'const __ = __array');
// returns
true;
__args, __args__a

Any count of arguments:

compare('(a, b, c) => {}', '(__args) => {}');
compare('(a, b) => {}', '(__args) => {}');
compare('() => {}', '(__args) => {}');
// returns
true;

Or linked arguments:

compare('((a) => fn(a))(value)', '((__args__a) => __c(__args__a))(__args__b)');
// returns
true;

compare('((a) => fn(42))(value)', '((__args__a) => __c(__args__a))(__args__b)');
// returns
false;
__imports

Any count of import specifiers:

compare('import React, {Component} from "react"', 'import __imports from "react"');
// returns
true;
__exports

Any count of export specifiers:

compare('export {scan, fix, report}', 'export {__exports}');
// returns
true;
"__"

Any string literal.

compare('const a = "hello"', 'const __ = "__"');
__a

Linked node.

compare('const __a = "hello"', 'const __a = "hello"');
"__a"

Linked string literal.

compare('const a = "hello"', 'const __a = "__b"');
__a

Linked template literal.

compare('const a = `hello`', 'const __a = `__b`');
// returns
true;
__body

Any BlockStatement.

compare('const a = () => 0', 'const a = () => __body');
// returns
false;

compare('const a = () => {}', 'const a = () => __body');
// returns
true;

compare('function a(b) {return b;}', 'function __(__args) {__body}');
// returns
true;

compare(`class a {hello: 'world'}`, 'class __a {__body}');
// returns
true;
__jsx_children

Any count of children of JSXElement:

compare('<div hello="world"></div>', '<div hello="world">__jsx_children</div>');
// returns
true;

compare('<div hello="world"><span>hi</span></div>', '<div hello="world">__jsx_children</div>');
// returns
true;
__jsx_attributes

Any count of attributes of JSXElement:

compare('<div hello="world"></div>', '<__a __jsx_attributes/>');
// returns
true;
__nop

Any Function with no arguments and empty body;

compare('const a = () => {}', 'const __a = __nop');
// returns
true;

compare('const a = async () => {}', 'const a = __nop');
// returns
true;
__identifier

Any Identifier

compare('const a = 5', 'const __identifier = 5');
// returns
true;
__bool

Any Boolean

compare('const a = true', 'const a = __bool');
// returns
true;
/__a/

Any regexp

compare('const a = /hello/g', 'const a = /__a/');
// returns
true;
__type_params

Any count of TSTypeParameter's.

compare('function clear<es, ax, di>() {}', 'function __a<__type_params>(): __c {__body}');
// returns
true;

License

MIT