similar-object
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Similar Object

A utility to compare objects with numeric fields and get back a score of how close they are.

Can then be used together with .sort() to get the most similar objects.

Useful for getting related objects that don't match fully.

Install

npm install similar-object
# or
yarn add similar-object
import { similarObject } from 'similar-object'

// we are looking at this
const current = { price: 800, rooms: 3, area: 80 }

// which one of these is closest to current?
const unit1 = { price: 1000, rooms: 3, area: 100 }
const unit2 = { price: 1000, rooms: 5, area: 50 }
const unit3 = { price: 1500, rooms: 5, area: 50 }
const units = [unit1, unit2, unit3]

const similarConfig = {
  price: 1, // weight 1
  rooms: 2, // it's more important so we boost the score of rooms by 2
  area: 3, // area is most important, so we boost it by 3
}

units.sort(
  (a, b) => similarObject(current, a, similarConfig) - similarObject(current, b, similarConfig),
)

// units are now sorted according to how similar they are

Package Sidebar

Install

npm i similar-object

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

5.46 kB

Total Files

8

Last publish

Collaborators

  • leon