Have ideas to improve npm?Join in the discussion! »

@pelevesque/remove-duplicates

1.0.1 • Public • Published

Build Status Coverage Status JavaScript Style Guide

remove-duplicates

Removes and returns duplicates from an array.

Node Repository

https://www.npmjs.com/package/@pelevesque/remove-duplicates

Installation

npm install @pelevesque/remove-duplicates

Tests

Standard Style & Unit Tests

npm test

Unit Tests & Coverage

npm run cover

Usage

Requiring

const removeDuplicates = require('@pelevesque/remove-duplicates')

Basic

const arr = [
  'd131dd02c5e6eec4', // a
  '55ad340609f4b302',
  'd8823e3156348f5b', // b
  'd131dd02c5e6eec4', // a
  'd8823e3156348f5b', // b
  '551111111114b302', // c
  '551111111114b302'  // c
]
const duplicates = removeDuplicates(arr)
/*
arr = [
  'd131dd02c5e6eec4',
  '55ad340609f4b302',
  'd8823e3156348f5b',
  '551111111114b302'
]
duplicates = [
  'd131dd02c5e6eec4',
  'd8823e3156348f5b',
  '551111111114b302'
]
*/

OnlyKeepElementsWithoutDuplicates Option

When setting the onlyKeepElementsWithoutDuplicates to true, only elements that have no duplicates are kept.

const arr = [
  'd131dd02c5e6eec4', // a
  '55ad340609f4b302',
  'd8823e3156348f5b', // b
  'd131dd02c5e6eec4', // a
  'd8823e3156348f5b', // b
  '551111111114b302', // c
  '551111111114b302'  // c
]
const opts = { onlyKeepElementsWithoutDuplicates: true }
const duplicates = removeDuplicates(arr, opts)
/*
arr = [
  '55ad340609f4b302'
]
duplicates = [
  'd131dd02c5e6eec4',
  'd8823e3156348f5b',
  'd131dd02c5e6eec4',
  'd8823e3156348f5b',
  '551111111114b302',
  '551111111114b302'
]
*/

Remove option

With the remove option set to false, duplicates are still returned, but nothing is removed from the original array.

const arr = [
  'd131dd02c5e6eec4', // a
  '55ad340609f4b302',
  'd8823e3156348f5b', // b
  'd131dd02c5e6eec4', // a
  'd8823e3156348f5b', // b
  '551111111114b302', // c
  '551111111114b302'  // c
]
const opts = { remove: false }
const duplicates = removeDuplicates(arr, opts)
/*
arr = [
  'd131dd02c5e6eec4',
  '55ad340609f4b302',
  'd8823e3156348f5b',
  'd131dd02c5e6eec4',
  'd8823e3156348f5b',
  '551111111114b302',
  '551111111114b302'
]
duplicates = [
  'd131dd02c5e6eec4',
  'd8823e3156348f5b',
  '551111111114b302'
]
*/

Install

npm i @pelevesque/remove-duplicates

DownloadsWeekly Downloads

8

Version

1.0.1

License

MIT

Unpacked Size

8.79 kB

Total Files

6

Last publish

Collaborators

  • avatar