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

sort-predicate

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sort-predicate

A simple utility for creating flexible sorting predicates for arrays of objects in JavaScript and TypeScript.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
0
Weekly downloads
 
Created
Source

sort-predicate

A simple utility for creating flexible sorting predicates for arrays of objects in JavaScript and TypeScript.

Features

  • Lightweight and easy to use
  • Supports ascending and descending sorting
  • Handles null and undefined values gracefully
  • Compatible with JavaScript and TypeScript

Installation

Install the package via npm:

npm install sort-predicate

Usage

The createSortPredicate function generates a sorting predicate that can be used to sort arrays of objects by a specified field.

Example

import { createSortPredicate } from 'sort-predicate';

interface IModel {
    name: string;
}

const byName = createSortPredicate<IModel>("name");
const byNameDesc = createSortPredicate<IModel>("name", "desc");

const arr = [{ name: "002" }, { name: "001" }, { name: "003" }];

console.log(arr.sort(byName));       // Output: [{ name: "001" }, { name: "002" }, { name: "003" }]
console.log(arr.sort(byNameDesc));    // Output: [{ name: "003" }, { name: "002" }, { name: "001" }]

API

createSortPredicate<T>(field: keyof T, order: 'asc' | 'desc' = 'asc')

  • field: The field in the object to sort by.
  • order: The order of sorting: "asc" for ascending, "desc" for descending. Default is "asc".

Returns a predicate function that can be used with Array.sort().

Testing

This package includes tests written in Jest. To run the tests, simply execute:

npm test

License

This project is licensed under the MIT License.

Keywords

FAQs

Package last updated on 25 Oct 2024

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