Git Product home page Git Product logo

the-first-module's Introduction

params-mapper

⚠️ 请移步到重构后的 params-mapper

将前端页面参数映射到返回给服务端的参数

Install

npm i params-mapper

Code this:

const paramsMapper = require('params-mapper');
const BaseParamObject = paramsMapper.BaseParamObject;
const autoTransfer = paramsMapper.autoTransfer;

// or

import { autoTransfer, BaseParamObject } from 'params-mapper';

const TransformationPayload = {
    emptyObject: {
      paramKey: 'empty_object',
      alwaysKeep: true,
    },
    emptyObjectNotKeep: {
      paramKey: 'empty_object_not_keep',
    },
    notEmptyObject: {
      paramKey: 'not_empty_object',
    },
  
    emptyArray: {
      paramKey: 'empty_array',
      alwaysKeep: true,
    },
    emptyArrayNotKeep: {
      paramKey: 'empty_array_not_keep',
    },
    notEmptyArray: {
      paramKey: 'not_empty_array',
    },
  };
  
  const payload = {
    emptyObject: {},
    emptyObjectNotKeep: {},
    notEmptyObject: { a: 1 },
  
    emptyArray: [],
    emptyArrayNotKeep: [],
    notEmptyArray: [1, 2, 3],
  };
  
  class A extends BaseParamObject {}
  autoTransfer(TransformationPayload)(A);
  
  // 或者
  // or this:
  const A = autoTransfer(TransformationPayload)(class extends BaseParamObject {});
  
  const temp = new A(payload);
  const params = temp.getParams();
  console.log(params);

Result in:

{
    "empty_object": {},
    "not_empty_object": {
        "a": 1
    },
    "empty_array": [],
    "not_empty_array": [
        1,
        2,
        3
    ]
}

the-first-module

the-first-module's People

Contributors

mrbb avatar

Watchers

 avatar

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.