GithubHelp home page GithubHelp logo

codeandcats / compare-lists Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 1.82 MB

Super efficiently compares two sorted lists (arrays, strings, anything that is iterable actually).

License: MIT License

JavaScript 40.53% TypeScript 59.47%

compare-lists's Introduction

compare-lists

Super efficiently compares two sorted lists (arrays, strings, anything that is iterable actually).

npm version Build Status Coverage Status

Install

npm install compare-lists --save

Usage

import { compareLists } from "compare-lists";

Say you have two sorted lists of filenames and you need to know which files exist in the left list, which exist in the right list, and which exist in both lists.

const leftList = [
  "documents/apples.txt",
  "documents/funny-cats.mp4",
  "documents/funny-dogs.avi",
  "trash/linux.iso",
  "trash/zebras.doc"
];

const rightList = [
  "documents/apples.txt",
  "documents/funny-cats.mp4",
  "documents/taxes.doc",
  "trash/linux.iso",
  "trash/zebras.doc"
];

Just call compareLists passing both the lists, a function to compare items in each list and handlers for any events that you're interested in.

compareLists({
  left: leftList,
  right: rightList,
  compare: (left, right) => left.localeCompare(right),

  onMatch: value => console.log(`"${value}" is found in both lists`),
  onMissingInLeft: right =>
    console.log(`"${right}" is missing in the left list`),
  onMissingInRight: left =>
    console.log(`"${left}" is missing in the right list`)
});

The output will be:

"documents/apples.txt" is found in both lists
"documents/funny-cats.mp4" is found in both lists
"documents/funny-dogs.avi" is missing in the right list
"documents/taxes.doc" is missing in the left list
"trash/linux.iso" is found in both lists
"trash/zebras.doc" is found in both lists

That's the basics!

More...

Simple API

Alternatively, you can ask for a "report" instead of handling events. Just keep in mind this will use more memory than simply handling events.

const report = compareLists({
  left: leftList,
  right: rightList,
  compare: (left, right) => left.localeCompare(right),

  returnReport: true
});

console.log(JSON.stringify(report, null, "  "));
{
  "missingInLeft": ["documents/taxes.doc"],
  "missingInRight": ["documents/funny-dogs.avi"],
  "matches": [
    ["documents/apples.txt", "documents/apples.txt"],
    ["documents/funny-cats.mp4", "documents/funny-cats.mp4"],
    ["trash/linux.iso", "trash/linux.iso"],
    ["trash/zebras.doc", "trash/zebras.doc"]
  ]
}

The two lists can be different types

The left and right lists do not need to be the same type as each other. Just remember the items still need to by sorted by the field you are comparing on.

const leftList = [{ name: "Morty Smith" }, { name: "Rick Sanchez" }];
const rightList = ["Morty Smith", "Mr. Poopy Butthole"];

const report = compareLists({
  left: leftList,
  right: rightList,
  compare: (left, right) => left.name.localeCompare(right),
  returnReport: true
});

console.log(JSON.stringify(report, null, "  "));

Will output:

{
  "missingInLeft": ["Mr. Poopy Butthole"],
  "missingInRight": [{ "name": "Rick Sanchez" }],
  "matches": [[{ "name": "Morty Smith" }, "Morty Smith"]]
}

Comparing lists other than arrays

The library works with any objects that implement the iterable protocol, including arrays, strings, maps, etc.

Here is an example of comparing characters in two strings:

const left = "abcdef";
const right = "abcxyz";

const report = compareLists({
  left,
  right,
  compare: (left, right) => left.localeCompare(right)
});

console.log(JSON.stringify(report, null, "  "));

Will output:

{
  "missingInLeft": ["x", "y", "z"],
  "missingInRight": ["d", "e", "f"],
  "matches": [
    ["a", "a"],
    ["b", "b"],
    ["c", "c"]
  ]
}

Comparing two iterators

There is also a handy compareIterators function if you need it. Don't forget the values need to be in ascending order!

import { compareIterators } from "compare-lists";

const leftIterator = function*() {
  yield "a";
  yield "b";
  yield "c";
};

const rightIterator = function*() {
  yield "a";
  yield "c";
  yield "d";
};

const report = compareIterators({
  left: leftIterator(),
  right: rightIterator(),
  compare: (left, right) => left.localeCompare(right),
  returnReport: true
});

console.log(JSON.stringify(report, null, "  "));

Will output:

{
  "missingInLeft": ["d"],
  "missingInRight": ["b"],
  "matches": [
    ["a", "a"],
    ["c", "c"]
  ]
}

Contributing

Got an issue or a feature request? Log it.

Pull-requests are also welcome. 😸

compare-lists's People

Contributors

codeandcats avatar dependabot-preview[bot] avatar dependabot[bot] avatar greenkeeper[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

compare-lists's Issues

An in-range update of ts-jest is breaking the build 🚨

The devDependency ts-jest was updated from 24.0.0 to 24.0.1.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

ts-jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Commits

The new version differs by 23 commits.

  • b43b3c1 chore(release): 24.0.1
  • 2d91a37 chore: update package-lock
  • 485d3f7 build(deps): bump semver from 5.6.0 to 5.7.0 (#1043)
  • 2bd2534 build(deps-dev): bump @types/node from 10.14.3 to 10.14.4 (#1041)
  • bdba560 build(deps-dev): bump @types/node from 10.14.2 to 10.14.3 (#1038)
  • 08766bf build(deps-dev): bump @types/node from 10.14.1 to 10.14.2 (#1036)
  • 5f92fd2 build(deps-dev): bump js-yaml from 3.12.2 to 3.13.0 (#1034)
  • a9c79e9 build(deps-dev): bump @types/yargs from 12.0.9 to 12.0.10 (#1032)
  • 245ab29 build(deps-dev): bump eslint from 5.15.2 to 5.15.3 (#1031)
  • 4e72e59 build(deps-dev): bump eslint from 5.15.1 to 5.15.2 (#1030)
  • fb7dd55 feat(config): specify package.json location (#823) (#1013)
  • 279edcd build(deps-dev): bump tslint from 5.13.1 to 5.14.0 (#1028)
  • 8b93228 build(deps-dev): bump @types/node from 10.12.30 to 10.14.1 (#1027)
  • b825c7f build(deps-dev): bump @types/lodash.memoize from 4.1.4 to 4.1.6 (#1014)
  • 6f0ab80 build(deps-dev): bump @types/lodash.merge from 4.6.5 to 4.6.6 (#1015)

There are 23 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of @types/node is breaking the build 🚨

The devDependency @types/node was updated from 11.11.3 to 11.11.4.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/node is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.