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

compute-issorted

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compute-issorted

Returns a boolean indicating if an input array is sorted.

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

issorted

NPM version Build Status Coverage Status Dependencies

Returns a boolean indicating if an input array is sorted.

Installation

$ npm install compute-issorted

For use in the browser, use browserify.

Usage

To use the module,

var issorted = require( 'compute-issorted' );

issorted( arr[, comparator] )

Returns a boolean indicating if an input array is sorted.

var bool = issorted( [ 2, 6, 13, 5 ] );
// returns false

By default, the function checks if the input array is sorted in ascending order. To impose a different order, provide a comparator function.

// Descending order...
function comparator( a, b ) {
	return b - a;
}

var bool = issorted( [ 13, 6, 5, 2 ], comparator );
// returns true

The comparator function should behave the same as a comparator provided to arr.sort().

The comparator should take two arguments: a and b, where a and b are consecutive array elements. If the comparator returns a numeric value less than or equal to 0, consecutive elements are considered sorted; otherwise, issorted returns false.

Examples

var issorted = require( 'compute-issorted' ),
	shuffle = require( 'compute-shuffle' );

// Simulate some data...
var data = new Array( 5 );

for ( var i = 0; i < data.length; i++ ) {
	data[ i ] = i;
}

// Randomly shuffle the data and check if sorted...
var bool;
console.log( 'Data\t\tSorted?' );
for ( var j = 0; j < 100; j++ ) {
	shuffle( data );
	bool = issorted( data );
	console.log( data.join( ',' )+'\t'+bool );
}

To run the example code from the top-level application directory,

$ node ./examples/index.js

Notes

This function runs in linear time: O(N), where N is the input array length.

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

License

MIT license.

Copyright © 2014. Athan Reines.

Keywords

compute.io

FAQs

Package last updated on 12 Nov 2014

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