New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

string-sort

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

string-sort

Simple library to sort a string based on character priority

latest
Source
npmnpm
Version
1.2.0
Version published
Maintainers
1
Created
Source

string-sort

Simple library to sort an array based on string character priority.

This module performs a Schwartzian Transform on strings to return a 'transformed' version, sorts by that then resolves back to the original value. The upshot of this is that you can change the default string comparator behaviour so you can bias what characters are worth in the sort order. This is especially useful when wanting to sort by numbers above alpha characters or reorder the behaviour of punctuation.

var ss = require('string-sort');

// For whatever reason we hate the letter 'c', send it to the bottom of the sort order
ss.sort(['a', 'b', 'c', 'd', 'e', 'f'], {charOrder: 'abdef'}); 
// => ['a', 'b', 'd', 'e', 'f', 'c']

// Sort so that numbers sort before alpha characters
ss.sort(['a', 'b', 'c', '1', '5', '9'], {charOrder: '0123456789abcdefghijklmnopqrstuvwxyz'}); 
// => ['1', '5', '9', 'a', 'b', 'c]

API

stringSort.sort(array, [options])

Sort and return an array with the supplied options. This function really just wraps Array.sort(), calculating the transform table beforehand.

stringSort.sortBy(collection, key, [options])

Similar to sort() but works on a collection (an array of objects) using the specified key as the sorter.

stringSort.transformTable(charOrder)

Return a string transformation table. This is mainly used internally by transform() and untransform().

stringSort.transform(str, [options])

Return the translated, sort compatible version of an input string. This function is very slow as it needs to parse the options structure and reconstruct the table each time. Use sort() for larger arrays.

stringSort.untransform(str, [options])

Return the untranslated, version of a translated string. This function is very slow as it needs to parse the options structure and reconstruct the table each time. Use sort() for larger arrays.

stringSort.defaults

An object of the default options to use if unspecified.

Options

The following are the default options used by the functions.

OptionTypeDefaultDescription
charOrderstringabcdefghijklmnopqrstuvwxyz0123456789:/-_The ascending character values when comparing strings. Anything not in this string will get its value via fallback
fallback(char)functionc => 999Function that is expected to return the fallback values if the char does not exist in charOrder

Keywords

string

FAQs

Package last updated on 21 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts