2 * Convert { [key: string]: boolean } to bitmap
3 * @param o ex: { announcements: true, features: false, newsletter: false, beta: false }
6 export const toBitMap = (o: { [key: string]: boolean } = {}): number =>
7 Object.keys(o).reduce((acc, key, index) => acc + (Number(o[key]) << index), 0);
10 * Define an Object from a bitmap value
12 * @param keys ex: ['announcements', 'features', 'newsletter', 'beta']
13 * @returns ex: { announcements: true, features: false, newsletter: false, beta: false }
15 export const fromBitmap = (value: number, keys: string[] = []) =>
16 keys.reduce<{ [key: string]: boolean }>((acc, key, index) => {
17 acc[key] = !!(value & (1 << index));
22 * This method creates an object composed of the own and inherited enumerable property paths of object that are not omitted.
23 * @param model The source object.
24 * @param properties Properties to omit.
25 * @retuns Returns a new object.
27 export const omit = <T extends object, K extends keyof T>(model: T, properties: readonly K[] = []): Omit<T, K> => {
28 const result = { ...model };
29 for (let i = 0; i < properties.length; ++i) {
30 delete result[properties[i]];
36 * Review of omit function
37 * @param model The source object.
38 * @param properties Properties to keep.
39 * @return Returns a new object.
41 export const pick = <T extends object, K extends keyof T>(model: T, properties: readonly K[] = []) => {
42 const result: Pick<T, K> = {} as any;
43 for (let i = 0; i < properties.length; ++i) {
44 const key = properties[i];
46 result[key] = model[key];
53 * Compare two objects but not deeply
55 export const isEquivalent = (a: { [key: string]: any }, b: { [key: string]: any }) => {
56 const aProps = Object.getOwnPropertyNames(a);
57 const bProps = Object.getOwnPropertyNames(b);
59 if (aProps.length !== bProps.length) {
63 for (let i = 0; i < aProps.length; i++) {
64 const propName = aProps[i];
66 if (a[propName] !== b[propName]) {
75 * Create a map from a collection
77 export const toMap = <T extends { [key: string]: any }, K extends keyof T>(
81 const result: { [key in T[K]]: T } = {} as any;
82 for (let i = 0; i < collection.length; i++) {
83 const item = collection[i];
84 result[item[key]] = item;