npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

mu-unique

v0.0.5

Published

Uniqueify an array

Downloads

10

Readme

Version Version Build Status Coverage Status

mu-unique

Uniqueify an array of elements.

Runtime complexity: O(n*log(n)) for sortable arrays, O(n^2) for non-sortable arrays.

unique(arr, sortable, comparator)

  1. arr {Array} - The source array.
  2. sortable {Boolean} - Optional. Does the array contain sortable elements? defaults to false.
  3. comparator {Function} - Optional.
    • If the array is sortable, it defines the order of the elements in the array. It is called with 2 elements from the array and should return 0 if they are equal, a positive number if a is bigger and a negative number if b is bigger. If not provided, the array will be sorted with a natural order (as defined by the runtime).
    • If the array is not sortable, it defines the equality between elements in the array. It is called with 2 elements from the array and should return true if they are equal, and false otherwise. If not provided, the elements will be tested for equality with ===.

Notes:

  1. The function modifies the array and returns its new length.
  2. The original order of items may not be preserved.
  3. If the this value of the function is defined, it will be used as the array (see examples). Thus it is possible to use this function to extend the Array prototype: Array.prototype.unique = unique.

Installation

  • Node: 0. npm install mu-unique 0. var unique = require('mu-unique');
  • AMD (install with bower): 0. bower install mu-unique 0. require(['mu-unique'], function(unique){ /* ... */ });

Run tests with make test.

Run coverage analysis with make coverage (coverage report is saved to ./coverage).

Examples

var arr = [1, 2, 1, 2, 3],
    len = unique(arr);
console.log(arr, true); // [1, 2, 3]
console.log(len); // 3
var o1 = {},
    o2 = function(){},
    o3 = "foo",
    arr = [o1,o1,o2,o3,o2,o3],
    len = unique(arr);
console.log(arr); // [o1, o2, o3]
console.log(len); // 3

With a custom order:

var arr1 = ['hello', 'hi', 'world'],
    arr2 = unique(arr1, function(a, b){
        // compare only first character
        return a[0] > b[0] ? 1 : a[0] < b[0] ? -1 : 0; 
    });
console.log(arr1); // ['hello', 'hi', 'world']
console.log(arr2); // ['hello', 'world']

By setting the this value:

var arr = [1, 2, 1, 2, 3],
    len = unique.call(arr);
console.log(arr); // [1, 2, 3]
console.log(len); // 3