2.0.0 • Published 2 years ago

@evokegroup/transform v2.0.0

Weekly downloads
46
License
ISC
Repository
bitbucket
Last release
2 years ago

@evokegroup/transform

Transform an object to a new format.

Class: Transform

toObject(data, map)

Transform an object.

ParameterTypeDescription
dataobjectThe object to transform
mapArray<object>, objectThe transformation mapping

Example

const Transform = require('@evokegroup/transform');

const data = {
  site: 'website',
  utm: {
    source: 'utm_source_value',
    content: 'utm_content_value'
  },
  contact: {
    firstName: 'John',
    lastName: 'Doe',
    date: 1612392072345,
    question1: 'Yes',
    question2: 'No',
    opts: {
      general: true,
      brandName: true
    }
  }
};

const transformMap = {
  properties: {
    source: { src: 'site' },
    firstName: { src: 'contact.firstName' },
    // Process the value by passing a function
    lastName: { src: 'contact.lastName', value: (val) => { return val ? val.toUpperCase() : val; } },
    // Automatically cast the value to type
    dateCreated: { type: 'date', src: 'contact.date' },
    // Cast number to hexidecimal
    dateCreatedHex: { type: 'string', radix: 16, src: 'contact.date' },
    qa: {
      src: ['contact.question1', 'contact.question2'],
      properties: {
        // data = The flattened data object given to Transform.toObject
        // map = The current property map
        // src = The the current src item
        q: { src: ({ data, map, src }) => { return src.split('.')[1]; } },
        a: { src: ({ data, map, src }) => { return data[src]; } }
      }
    },
    optData: {
      src: ['contact.opts'],
      properties: {
        // data = Because the src item is an object, the flattened src object data
        // map = The current property map
        // src = The the current src item, in this case the object property name being transformed
        brand: { src: ({ data, map, src }) => { return src; } },
        active: { type: 'string', src: ({ data, map, src }) => { return data[src]; } }
      }
    }
  }
};

const transformed = Transform.toObject(data, transformMap);
/*
  transformed = {
    source: 'website',
    firstName: 'John',
    lastName: 'Doe',
    dateCreated: 2021-02-03T22:41:12.345Z,
    dateCreatedHex: '1776a0e8499',
    qa: [
      { q: 'question1', a: 'Yes' },
      { q: 'question2', a: 'No' }
    ],
    optData: [
      { brand: 'general', active: 'true' }
      { brand: 'brandName', active: 'true' }
    ]
  }
*/

toObjectArray(data, map)

Transform an object into an array

ParameterTypeDescription
dataobjectThe object to transform
mapArray<object>, objectThe transformation mapping

Example

const Transform = require('@evokegroup/transform');

const data = {
  utm: {
    source: 'utm_source_value',
    content: 'utm_content_value'
  },
  contact: {
    question1: 'Yes',
    question2: 'No'
  }
};

const transformMap = [{
  src: ['utm'],
  properties: {
    name: { src: ({ data, map, src }) => { return `utm ${src}`; } },
    value: { src: ({ data, map, src }) => { return data[src]; } }
  }
}, {
  src: ['contact.question1', 'contact.question2'],
  properties: {
    name: { src: ({ data, map, src }) => { return src.split('.')[1]; } },
    value: { src: ({ data, map, src }) => { return data[src]; } }
  }
}];

const transformed = Transform.toObjectArray(data, transformMap);
/*
  transformed = [
    { name: 'utm source', value: 'utm_source_value' },
    { name: 'utm content', value: 'utm_content_value' },
    { name: 'question1', value: 'Yes' },
    { name: 'question2', value: 'No' }
  ]
*/
2.0.0

2 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago