kentcdodds / match-sorter

Simple, expected, and deterministic best-match sorting of an array in JavaScript

Home Page:https://npm.im/match-sorter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Filter items in a normalized array

janhesters opened this issue · comments

  • match-sorter version: 2.3.0
  • node version: v9.4.0
  • npm (or yarn) version: 6.4.1

Relevant code or config

const adults = [
  { name: "Jim", house: "house-uuid-one", children: ["children-uuid-one", children-uuid-two"], uuid: "adult-uuid" },
  { name: "Yin", house: "house-uuid-two", children: [], uuid: "adult-uuid-two" }
];
const normalizedHouses = { 
  "house-uuid-one": { address: "Some Street", uuid: "house-uuid-one" },
  "hourse-uuid-two": { address: "Simba Ave", uuid: "house-uuid-two"
};
const normalizedChildren = { "children-uuid-one": { age: 2, name: "John", uuid: "children-uuid-one" }, "children-uuid-two": { age: 8, name: "Jenna", uuid: "children-uuid-two" } };

What you did:
I would like to use match sorter with normalized arrays.

What happened:
I couldn't figure out how to use matchSorter with normalized arrays.

Problem description:
I tried it like this:

matchSorter(adults, "o", {
  keys: [
    "name",
    item => normalizedHouses[item.house].name,
    item => item.children.map(child =>  normalizedChildren[child]).name
  ]
})

Suggested solution:
Let the array be transformed:

matchSorter(items, "o", { keys: ["name", "house", "children"], trafo: .map(<someTransformation>) })

So that the initial array stays the same, but the trafo gets applied during the filtering.

Sorry, I'm not 100% certain I understand the need, and I'm guessing this is no longer needed as it's been a long time 😬 Sorry!