npm install array.prototype.tosorted

An ESnext spec-compliant `Array.prototype.toSorted` shim/polyfill/replacement that works as far down as ES3.

About array.prototype.tosorted

The npm package "array.prototype.tosorted" serves as a robust ESnext spec-compliant shim/polyfill/replacement for JavaScript arrays, adhering closely to the latest ECMAScript proposals. Its primary function is to enhance JavaScript's Array.prototype by adding a `toSorted` method, which allows developers to easily obtain a sorted copy of an array while leaving the original array unmodified. This is particularly useful in functional programming where immutability is a key concern. The package is designed to be backward compatible down to ES3, making it a versatile tool for developers working with various versions of JavaScript. This ensures broad compatibility and utility across different development environments and legacy systems.

To integrate "array.prototype.tosorted" into your project, simply run the command `npm install array.prototype.tosorted`. This installation process is straightforward and allows developers to quickly enhance their array handling capabilities with minimal setup. Once installed, the `toSorted` method can be called on any array instance, providing a new array that is sorted according to the default sort comparator or a custom comparator function provided by the developer. The method does not alter the original array, thereby preserving the integrity of original data and preventing potential side-effects. This npm package is an essential tool for developers looking to add reliable and efficient sorting functionality to their JavaScript applications without compromising on the principles of immutable data structures.

The "array.prototype.tosorted" module is not only easy to implement but also ensures that applications remain clean and performant. By using this package, developers can avoid common bugs associated with mutating arrays directly and can write cleaner, more predictable code that is easier to test and maintain.

More from es-shims

es-shims npm packages

Find the best node modules for your project.

Search npm

regexp.prototype.flags

ES6 spec-compliant RegExp.prototype.flags shim...

Read more
,

string.prototype.trimend

ES2019 spec-compliant String.prototype.trimEnd shim...

Read more
,

string.prototype.trimstart

ES2019 spec-compliant String.prototype.trimStart shim...

Read more
,

arraybuffer.prototype.slice

ES spec-compliant shim for ArrayBuffer.prototype...

Read more
,

string.prototype.matchall

Spec-compliant polyfill for String.prototype...

Read more
,

object.groupby

An ESnext spec-compliant `Object.groupBy` shim/polyfill/replacement that works as far down as ES3...

Read more
,

array.prototype.tosorted

An ESnext spec-compliant `Array.prototype...

Read more
,

es-iterator-helpers

An ESnext spec-compliant iterator helpers shim/polyfill/replacement that works as far down as ES3...

Read more
,

array.prototype.toreversed

An ESnext spec-compliant `Array.prototype...

Read more
,

typedarray.prototype.slice

ES spec-compliant shim for TypedArray.prototype...

Read more
,

string.prototype.replaceall

Spec-compliant polyfill for String.prototype...

Read more
,

error-cause

An ES-spec-compliant shim/polyfill/replacement for the `...

Read more

Dependencies

Core dependencies of this npm package and its dev dependencies.

call-bind, define-properties, es-abstract, es-errors, es-shim-unscopables, @es-shims/api, @ljharb/eslint-config, aud, auto-changelog, eslint, evalmd, functions-have-names, has-strict-mode, hasown, in-publish, npmignore, nyc, safe-publish-latest, tape

Documentation

A README file for the array.prototype.tosorted code repository. View Code

array.prototype.tosorted Version Badge

dependency status dev dependency status License Downloads

npm badge

An ESnext spec-compliant Array.prototype.toSorted shim/polyfill/replacement that works as far down as ES3.

This package implements the es-shim API interface. It works in an ES3-supported environment and complies with the proposed spec.

Because Array.prototype.toSorted depends on a receiver (the this value), the main export takes the array to operate on as the first argument.

Getting started

npm install --save array.prototype.tosorted

Usage/Examples

var toSorted = require('array.prototype.tosorted');
var assert = require('assert');

var input = [5, 4, 3, 2, 1, 0];

var output = toSorted(input);

assert.deepEqual(output, [0, 1, 2, 3, 4, 5]);
assert.notEqual(output, input);
assert.deepEqual(input, [5, 4, 3, 2, 1, 0]);
var toSorted = require('array.prototype.tosorted');
var assert = require('assert');
/* when Array#toSorted is not present */
delete Array.prototype.toSorted;
var shimmed = toSorted.shim();

assert.equal(shimmed, toSorted.getPolyfill());
assert.deepEqual(input.toSorted(), toSorted(input));
var toSorted = require('array.prototype.tosorted');
var assert = require('assert');
/* when Array#toSorted is present */
var shimmed = toSorted.shim();

assert.equal(shimmed, Array.prototype.toSorted);
assert.deepEqual(input.toSorted(), toSorted(input));

Tests

Simply clone the repo, npm install, and run npm test