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

string-content-sort

v2.3.2

Published

sort the content inside of a string

Downloads

6

Readme

String Content Sort

A feature rich text sorter that takes indentation into account

This npm package is part of a bigger project called scopedsort. Documentation and examples are mainly held there.

Functions and properties will still have descriptions and typing, so you don't need to always refer back to the documentation.

Usage

This package exports two main functions sort(<string>, [options]) and sortComments(<string>). There are other functions that are exported, but they are more low-level and meant for scopedsort providers like the vscode extension or the command line tool.

const { sort } = require('string-content-sort');
// or
import { sort } from 'string-content-sort';

let list = `Pokemon
Dragon Ball Z
Naruto`;

list = sort(list, { reverse: true });

assert.equals(
    list,
    `Pokemon
Naruto
Dragon Ball Z`
);

The first argument is the string to sort, and the second argument is an optional object of options.

This function will throw if you have faulty options. You can turn this off by setting options.reportErrors to false. If turned off, it will always return a string similar to the input, possibly not what you want or expect.

sort('', {
    sorter: 'random'
    // regex with random sort makes no sense
    regexFilter: /\w+/,
    reportErrors: false
});

NOTE: The options object will be mutated, as a lot of the properties are shorthand for other values.

The end result of options can still be re-used and will still produce the same output.


The other function, sortComments(<string>) serves to sort text that contain sort-comments. It does not return string, but instead an object with the schema:

interface {
    commentSections: {
        startLine: number;
        endLine: number | null;
        hasChanged: boolean;
    }[];
    errors: string[];
    result: string;
}

Example:

import { sortComments } from 'string-content-sort';

const list = `Here is my list of my favorite food:

// { sort-start -s }
- ice cream
- pizza
- orange
// { sort-end }

Welp that concludes my list.`;

assert.deepEquals(sortComments(list), {
    errors: [],
    commentSections: {
        startLine: 2,
        endLine: 6,
        hasChanged: true,
    },
    result: `Here is my list of favorite food:

// { sort-start -s }
- pizza
- orange
- ice cream
// { sort-end }

Welp that concludes my list`,
});

Questions & Contribution

This program might have some learning curve, so if you need any help, submit a GitHub issue, and I'll be glad to help. If you find any bugs or want to contribute you should also create a GitHub issue.