New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

total-sorta

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

total-sorta

Abstracted helper functions, based on total order. Inspired by Sedgewick.

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Total Sorta

This is a small set of helper functions, designed to compare array elements. Usually, you'd use them in implementing a sorting algorithm.

I got the idea of wrapping total order into functions from Sedgewick & Wayne's book on Algorithms.

They're reliable, and hopefully the JS community will find them useful too.

Installation

$ npm install total-sorta

Usage

var ts = require('total-sorta');

API

compare(v, w)

Compares two items & returns a result. It's assumed that they have some common characteristic.

Like the <=> (spaceship) operator in Ruby, Perl, PHP or Groovy, it returns:

  • -1 if the 1st item is < the 2nd item
  • 1 if the 1st item is > the 2nd item
  • 0 otherwise
less(v, w)
  • Returns true if & only if item v is less than w
  • Returns false if the item v is greater than w
  • Returns false if the two items are equal
more(v, w)
  • Returns true if & only if item v is greater than w
  • Returns false if the item v is less than w
  • Returns false if the two items are equal
sorted(arr, [option])

Returns true if your array, arr, is sorted. By default, it will check for ascending order. If you want descending order, pass in the option, { descending: true }, as 2nd parameter.

For what it's worth, !condition

This may seem obvious, but it wasn't to me at first. Flip the boolean result with ! to check for 'less than or equal to' or 'greater than or equal to'.

  • !more(v, w) is true if v <= w
  • !less(v, w) is true if v >= w

Keywords

FAQs

Package last updated on 07 Oct 2015

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc