Skip to content

Instantly share code, notes, and snippets.

@deepakshrma
Last active April 9, 2020 11:19
Show Gist options
  • Save deepakshrma/4b6a0a31b4582d6418ec4f76b7439781 to your computer and use it in GitHub Desktop.
Save deepakshrma/4b6a0a31b4582d6418ec4f76b7439781 to your computer and use it in GitHub Desktop.
Mapper.js - A small design pattern to avoid m x n loop back while finding some object from object array
/*
*
The MIT License (MIT)
Copyright (c) 2014 deepakshrma
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
class Mapper {
constructor(array, key) {
this.map = array.reduce((map, item) => {
const val = item[key];
if (!map[val]) {
map[val] = [];
}
map[val].push(item);
return map;
}, {});
}
find(key) {
return this.map[key] && this.map[key][Mapper.FIRST_INDEX]; //return blank array
}
findAll(key, returnUndefined) {
//return blank array
return this.map[key] ? this.map[key] : returnUndefined ? undefined : [];
}
}
Mapper.FIRST_INDEX = 0;
const users = [
{
_id: "57e88c892727ab7f4828c015",
isActive: true,
balance: "$1,807.06",
picture: "http://placehold.it/32x32",
age: 21,
name: "Schmidt"
},
{
_id: "57e88c897a849644b79d6555",
isActive: false,
balance: "$1,487.94",
picture: "http://placehold.it/32x32",
age: 40,
name: "Griffin"
},
{
_id: "57e88c89075380040077b307",
isActive: false,
balance: "$2,939.40",
picture: "http://placehold.it/32x32",
age: 22,
name: "Nellie"
},
{
_id: "57e88c89cbfe5ec2c41ae27f",
isActive: true,
balance: "$2,392.49",
picture: "http://placehold.it/32x32",
age: 36,
name: "Nellie"
},
{
_id: "57e88c89ace3c83d92a0de7f",
isActive: true,
balance: "$2,161.58",
picture: "http://placehold.it/32x32",
age: 40,
name: "Payne"
},
{
_id: "57e88c895cec7aa54f7409cb",
isActive: false,
balance: "$2,109.69",
picture: "http://placehold.it/32x32",
age: 37,
name: "Sandoval"
}
];
//How to use
var userMapper = new Mapper(users, "name"); // create a map on names
console.log(userMapper.find("Schmidt")); //will return only user with name 'Schmidt'
const userToFind = ["Payne", "Schmidt"];
const reqUsers = userToFind.map(name => userMapper.find(name));
console.log(reqUsers);
var userMapper = new Mapper(users, "isActive"); // create a map on isActive
console.log(userMapper.find(true)); //will return only first active user
console.log(userMapper.findAll(true)); //will return all active user
var userMapper = new Mapper(users, "name"); // create a map on names
console.log(userMapper.find("Nellie")); //will return only first user with name 'Nellie'
console.log(userMapper.findAll("Nellie").map(({ _id }) => _id));
//will return all user _id whose name is 'Nellie'
console.log(userMapper.map); //will return map based on name
if(typeof window !== 'undefined') window.Mapper = Mapper;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment