apphouse
Version:
Component library for React that uses observable state management and theme-able components.
30 lines (27 loc) • 872 B
text/typescript
import { getUniqueId } from '../string/getUniqueId';
/**
* Get the values of any object with string keys.
* @param obj any
* @returns array of the values in the object passed as parameter
*/
export function values<T>(data: { [key: string]: T } | T[]): T[] {
if (Array.isArray(data)) {
return data;
} else if (typeof data === 'object') {
return Object.values(data as object);
} else {
throw new TypeError('Invalid input type');
}
}
export function valuesWithIds<T>(data: { [key: string]: T } | T[]) {
const _data = data as any;
if (Array.isArray(_data)) {
return _data.map((item) => ({ id: item.id || getUniqueId(), ...item }));
} else if (typeof _data === 'object') {
return Object.keys(_data as object).map((key) => {
return { ..._data[key], id: key };
});
} else {
throw new TypeError('Invalid input type');
}
}