hd-utils
Version:
A handy utils for modern JS developers
113 lines (112 loc) • 4.59 kB
JavaScript
/**
* @description HMap is an extension of Map object, that adds more functionalities to the Map object, such as .from, .filter, .map, .mapFields and others.
* its great utility to deal with Map and normal JS objects.
* @example HMap.from({ a: 1, b: 2 }).mapArray((val) => val); // [ 1, 2 ]
*/
export default class HMap extends Map {
constructor() {
super(...arguments);
/**
* @description similar to [].filter() it takes a callback with key value and reruns the keys and values the condition is applied to them.
* @example HMap.from({a:1}).filter((v, k) => v === 1).toObject() // {a:1}
*/
this.filter = (callback) => {
const filtered = new HMap();
this.forEach((value, key) => {
if (callback(value, key, this)) {
filtered.set(key, value);
}
});
return filtered;
};
/**
* @description will behave like .map in arrays where you can change the keys and the values, will return a new HMap object.
* @example HMap.from({a:1}).mapFields((v, k) => ([v + 1, "b"])).toObject() // {b:2}
*/
this.mapFields = (callback) => {
const mapped = new HMap();
this.forEach((value, key) => {
const [newVal, newKey] = callback(value, key, this);
mapped.set(newKey, newVal);
});
return mapped;
};
/**
* @description will behave like .map in arrays, but you can't change the keys but you can change the values, will return a new HMap object.
* @example HMap.from({a:1}).mapFields((v, k) => ([v + 1, "b"])).toObject() // { a: [ 2, 'b' ] }
*/
this.map = (callback) => {
const mapped = new HMap();
this.forEach((value, key) => {
mapped.set(key, callback(value, key, this));
});
return mapped;
};
/**
* @description will behave like .map in arrays, but you can't change the keys but you can change the values, will return a new HMap object.
* @example HMap.from({a:1, b:2}).mapArray((val) => (val)) // [ 1, 2 ]
*/
this.mapArray = (callback) => {
const arr = [];
this.forEach((v, k) => {
arr.push(callback(v, k, this));
});
return arr;
};
/**
* @description will return the value based on a callback that returns a boolean
* @example HMap.from({a:1}).findValue((val, key) => val > 0) // 1;
*/
this.findValue = (callback) => {
let found;
this.forEach((value, key) => {
if (callback(value, key, this)) {
found = value;
}
});
return found;
};
/**
* @description acts like [].every(), where you provide a function that takes the key and value and return boolean if the condition is applied on every.
* @example HMap.from({a:1}).every((val, key) => val === 1) // true
*/
this.every = (callback) => {
return this.map(callback).getAllValues().every(Boolean);
};
/**
* @description will get all of the HMap object keys in an array.
* @example HMap.from({a:1}).getAllKeys() // ["a"]
*/
this.getAllKeys = () => {
return Array.from(this.keys());
};
/**
* @description acts like [].some(), where you provide a function that takes the key and value and return boolean if the condition is applied on some.
* @example HMap.from({a:1}).some((val, key) => val === 1) // true
*/
this.some = (callback) => {
return this.map(callback).getAllValues().some(Boolean);
};
/**
* @description will get all of the HMap object values in an array.
* @example HMap.from({a:1}).getAllVAlues() // [1]
*/
this.getAllValues = () => {
return Array.from(this.values());
};
/**
* @description will convert the HMap to normal JS object.
* @example HMap.from({a:1}).toObject() // {a:1}
*/
this.toObject = () => {
return Object.fromEntries(this);
};
}
/**
* @description takes a normal js object and reruns HMap instance.
* @example const hmap = HMap.from({a:1}).
*/
static from(obj) {
return new HMap(Object.entries(obj));
}
}