Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Idea: Unique keys #32

Open
jmsv opened this issue Nov 29, 2023 · 0 comments
Open

Idea: Unique keys #32

jmsv opened this issue Nov 29, 2023 · 0 comments

Comments

@jmsv
Copy link
Owner

jmsv commented Nov 29, 2023

At the moment, (where food is equal to the README example)

const oatyFood = new OatyArray(food, { keys: ['id', 'type'] })
const idOne = oatyFood.get("fruit", 1)

idOne has the following type:

const idOne: {
    id: number;
    name: string;
    type: string;
}[] | undefined

which is annoying for two reasons:

  1. Need code to check if idOne is defined
  2. Need to check array length is 1

That second reason is particularly annoying if you know that id is unique the source data.

Would be cool if oaty supported something like:

const oatyFood = new OatyArray(food, {
  keys: [
    { key: "id", unique: true },
    { key: "type" }
  ],
});

and could even make this backwards-compatible, e.g.

const oatyFood = new OatyArray(food, {
  keys: [{ key: "id", unique: true }, "type"],
});

and then the type for oatyFood.get("id", 1) should become

const idOne: {
    id: number;
    name: string;
    type: string;
} | undefined

which still requires a (necessary and not that annoying) check for undefined, but means you don't need to check the array (not necessary and very annoying)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant