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

snippet-composer

v0.1.4

Published

A library is used to compose your snippets.

Downloads

4

Readme

snippet-composer

A library is used to compose your snippets.

See this article for details.

📦Installation

npm i snippet-composer -g

​ or

npm i snippet-composer -D

🤔Why use it

In vscode or any other editor, snippets are not related to snippets, they are relatively isolated, which makes it difficult to write more complex snippets that are closer to the real business. To solve this problem, I designed and implemented this library for composing snippets.

Before, we can't compose snippets. ☹️

before.drawio

When we use this library, we can compose any snippets if we want.😊

after.drawio

✨This library has the following features.

  1. Compose multiple code snippets
  2. Renumber tabstops
  3. Custom variable

🔨Usage

  1. Body recompose

    The syntax of body recompose is ${SnippetName[BodyIndex]},support for slice syntax.

// snippet.json
{
    foo: {
        prefix: ['foo'],
        body: [
           'line0',
           'line1',
           'line2',
        ],
    },
}
// body replace result

'${foo[0]}' -> 'line0'
'${foo[0,1]}' -> 'line0\tline1'
'${foo[1,]}' -> 'line1\tline2'
  1. Renumber tabstops

    The syntax of renumber tabstops is ${TheNewNumber:[SnippetName$BodyIndex]}, it depends on the field extra.tabstops.

// snippet.json
{
    foo: {
        prefix: ['foo'],
        body: [
          'foo code $1',
          'foo code $2',
        ],
    },
    bar: {
        prefix: ['bar'],
        body: [
          'bar code $1',
          'bar code $2',
        ],
    }
}
// tabstops demo
{
	....
    body:['${foo[0]}','${foo[1]}','${bar[0]}','${bar[1]}'],
    extra: {
        tabstops:{
            2:['foo$2','bar$1'],
            3:['bar$2'],
        }
    }
}
// Compose bodies, and then the new bodies are as follows after replacing tabstops.
body: [
    'foo code $1',
    'foo code $2',
-   'bar code $1',
+   'bar code $2',
-   'bar code $2',
+   'bar code $3'
]
  1. Custom variable

    The syntax of custom variable is $CustomVarName, it depends on the field extra.customVar.

/************* snippet.json *************/
{
	mysql: {
        prefix: ['ms'],
        body: [
            'import mysql from mysql',
            'mysql.connect($USER_NAME,$USER_PASSWORD,$1);',
            ],
        },
}
// customVar demo
{
    .....
    extra: {
        customVar: {
            USER_NAME: 'admin',
            USER_PASSWORD: () => '123',
        },
    }
}

'mysql.connect($USER_NAME,$USER_PASSWORD,$1);' -> 'mysql.connect(admin,123,$1);'

A simple demo

/************* snippet1.json *************/
{
      mysql: {
        prefix: ['ms'],
        body: [
          'import mysql from mysql',
          'mysql.connect($USER_NAME,$USER_PASSWORD,$1);',
        ],
      },
      assert: {
        prefix: ['as'],
        body: [
          'import assert from assert',
          'assert(a,1);\rassert(b,$1);\rassert(c,$2);',
          '$0',
        ],
      },
}
/************* snippet2.json *************/
{
      utils: {
        prefix: ['utils'],
        body: [
          'function delay() {\r\tsleep($1);\r}',
          'function getPort() {\r\treturn ${2};\r}',
        ],
      },
}

/************* index.js *************/
import snippet1 from "./snippet1.json";
import snippet2 from "./snippet2.json";

const newSnippet = defineSnippet(
    // Parameter 1 is the extended snippet, which is responsible for composing the snippet.
    {
        'Test Case': {
            prefix: ['tc'],
            description: 'Test Case',
            scope: 'javascript',
            // recompose snippet1 and snippet2
            body: [
                '${mysql[0]}',
                '${assert[0]}',
                '${mysql[1]}\r${assert[1,3]}',
                '${utils[0,]}',
            ],
            // extra field
            extra: {
                tabstops: {
                    1: ['mysql$1', 'utils$2'],
                    2: ['assert$1'],
                    3: ['assert$2'],
                    4: ['utils$1'],
                },
                customVar: {
                    USER_NAME: 'admin',
                    USER_PASSWORD: () => '123',
                },
            },
        },
    },
    // Parameter 2, pass in an array of snippets to be composed.
    [snippet1, snippet2],
);

// Expect result of new snippet
expect(newSnippet).toEqual({
    'Test Case': {
        prefix: ['tc'],
        description: 'Test Case',
        body: [
            'import mysql from mysql',
            'import assert from assert',
            'mysql.connect(admin,123,$1);\rassert(a,1);\rassert(b,$2);\rassert(c,$3);\r$0',
            'function delay() {\r\tsleep($4);\r}\rfunction getPort() {\r\treturn ${1};\r}',
        ],
    },
});

💻Examples

  • Edit on CodeSandbox.

    Edit example

  • Clone repo and run the example.(For vscode & nodejs)

    git clone https://github.com/ascodelife/snippet-composer
    cd snippet-composer && pnpm install
    cd example && pnpm install && pnpm run start