UNPKG

@ngha/transform-object

Version:

New object is transformed by object pattern

124 lines (122 loc) 3.91 kB
import { Injectable } from '@angular/core'; export class TransformObjectService { constructor() { this.get = (object, path, defaultValue) => { const travel = regexp => String.prototype.split .call(path, regexp) .filter(Boolean) .reduce((res, key) => (res !== null && res !== undefined ? res[key] : res), object); const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/); return result === undefined || result === object ? defaultValue : result; }; this.mapValue = (object, iteratee) => { object = Object(object); const result = {}; Object.keys(object).forEach((key) => { result[key] = iteratee(object[key], key, object); }); return result; }; // private has = (obj, key) => { // const keyParts = key.split('.'); // // return !!obj && ( // keyParts.length > 1 // ? this.has(obj[key.split('.')[0]], keyParts.slice(1).join('.')) // : hasOwnProperty.call(obj, key) // ); // }; } /** * transformObject map object to new object base on object pattern * * @param value * @param objPattern * @param objDefaultValue * @example * dataProvider = [ { 'id': '005564a8-2b3d-4f87-9e19-290526ac5e26', 'username': 'a@mail.com', 'emailAddress': 'a@mail.com', 'fullName': 'John', 'status': 'ACTIVE', 'roleId': '09323760-ba53-42e2-a961-840c75726f93', 'role': { 'id': '09323760-ba53-42e2-a961-840c75726f93', 'code': 'USER' }, 'createdDate': '2020-09-07T03:01:15.587Z' }, { 'id': '91458c99-271b-408d-b0bc-9e23384e5c5f', 'username': 'b@mail.com', 'emailAddress': 'b@mail.com', 'fullName': 'test2', 'status': 'PENDING', 'roleId': 'f34ebf72-f55d-4ad4-b7ea-72f2711bf7f8', 'role': { 'id': 'f34ebf72-f55d-4ad4-b7ea-72f2711bf7f8', 'code': 'PARTNER' }, 'createdDate': '2020-09-01T08:27:47.237Z' }, { 'id': '13d8b6a7-d7da-49b0-8a98-0bad8e6117cc', 'username': 'c@mail.com', 'emailAddress': 'c@mail.com', 'fullName': 'test111', 'status': 'PENDING', 'roleId': '09323760-ba53-42e2-a961-840c75726f93', 'role': { 'id': '09323760-ba53-42e2-a961-840c75726f93', 'code': 'USER' }, 'createdDate': '2020-09-01T08:27:04.553Z' } ]; objectPattern = { index: 'id', name: 'fullName', title: 'role.code' } transformObject(dataProvider[0], objectPattern) => { "index": "005564a8-2b3d-4f87-9e19-290526ac5e26", "name": "John", "title": "USER" } transformObject(dataProvider, objectPattern) => [ { "index": "005564a8-2b3d-4f87-9e19-290526ac5e26", "name": "John", "title": "USER" }, { "index": "91458c99-271b-408d-b0bc-9e23384e5c5f", "name": "test2", "title": "PARTNER" }, { "index": "13d8b6a7-d7da-49b0-8a98-0bad8e6117cc", "name": "test111", "title": "USER" } ] * */ transformObject(value, objPattern, objDefaultValue) { return Array.isArray(value) ? value === null || value === void 0 ? void 0 : value.map(val => this.mapValue(objPattern, (v, k) => this.get(val, v, objDefaultValue && this.get(objDefaultValue, k, null)))) : this.mapValue(objPattern, (v, k) => this.get(value, v, objDefaultValue && this.get(objDefaultValue, k, null))); } } TransformObjectService.decorators = [ { type: Injectable } ]; //# sourceMappingURL=transform-object.service.js.map