stevendesu / jsindex

1 stars 0 forks source link

No matches returns `undefined` #8

Closed stevendesu closed 5 years ago

stevendesu commented 5 years ago

Currently because I'm using JavaScript Objects for the index, if a value does not exist then undefined is returned. For example:

const collection = [
    {name: "Alice", gender: "female"},
    {name: "Bob", gender: "male"},
    {name: "Charlie", gender: "male"},
    {name: "Dana", gender: "female"}
];
collection.index();

console.log(collection.idx.gender["neither"]);
// Outputs: undefined

Desired output would be:

console.log(collection.idx.gender["neither"]);
// Outputs: []

This could be done fairly easily using a Proxy for the index:

this.idx.gender = new Proxy({}, {
    get: function(obj, prop) {
        if (prop in obj) return obj[prop];
        else return [];
    }
});

This technically changes the client-facing API, and so it should be a bump to 0.2.0 instead of 0.1.1. Arguably it could be said that the previous behavior was a bug, though, justifying 0.1.1... I'll probably just bump to 0.2.0 anyway, though.