Git Product home page Git Product logo

transform-object's Introduction

Issues Tweets

TransformObject

A utility class that help transform Object data to array of pairs to support React UI loop.

Rationale

When using React, sometimes you will face some UI challenges. Take this as an example:

  // The problem
  class ReactComponent {
    render() {
      const objectData = { /* ... */ }
      return (
        <>
          <row>
            <col>{objectData.data1}</col>
            <col>{objectData.data2}</col>
          </row>
          <row>
            <col>{objectData.data3}</col>
            <col>{objectData.data4}</col>
          </row>
          <row>
            <col>{objectData.data5}</col>
            <col>{objectData.data6}</col>
          </row>
        </>
      );
    }
  }
  // How can you achieve this:
  class ReactComponent {
    render() {
      const objectData = { /* ... */ }
      return (
        <>
          {toArrayOf2Pairs(objectData).map(([data1, data2]) => (
            <row>
              <col>{data1}</col>
              <col>{data2}</col>
            </row>
          ))}
        </>
      );
    }
  }

That is why I create TransformObject to solve above issue. Creating a class to achieve the abstraction in coding, make a clear goal of the transformation of data before and after through TransformObject.

Usage

  /*
    class TransformObject()
    @param[Object]: objectData - object data
    @param[Array]: struct - favor struct
    @param[Number]: pair - Data in each element of array
  */
  const transformer = new TransformObject(objectData, ['key', 'value'], 2);
  transformer.valueOf() // Output: [ [data1, data2], [data3, data4], [data5, data6] ]

  const transformer = new TransformObject();
  transformer.STRUCT = ['label', 'childObj'];
  transformer.PAIR = 3;
  transformer.transform(objectData);
  
  const transformer = new TransformObject(objectData);
  for (const [key, value] of transformer) {
    // ...
  }

Installation

# yarn
yarn add transform-object

# npm
npm install transform-object --save

Module usage

ES6 module

import TransformObject from 'transform-object';

CommonJS

If you are in a CommonJS environment (eg Node), then you will need to add .default to your import:

const TransformObject = require('transform-object').default;

transform-object's People

Watchers

James Cloos avatar qtp avatar

Forkers

p79n6a

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.