GithubHelp home page GithubHelp logo

mikemunsie / js-mapper Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 3 KB

Creates an object based on the matching properties and types of another object

License: The Unlicense

JavaScript 72.85% HTML 27.15%

js-mapper's Introduction

js-mapper

Compares two objects (or arrays) and creates a new object (or array) based on the matching keys and types. If a property does not match up, the property will not be included in the new object. This helps to keep models consistent and remove extra data that isn't being used.

Usage

The mapper routine consists of two parameters, where ```data``` is your custom object and ```objToCheck``` is what the data will be compared against. The ```objToCheck``` is an array or object that is defined using types. These types can be any javascript primitive type (see: https://developer.mozilla.org/en-US/docs/Glossary/Primitive), and can nest as deep as you would like.

Example

var Mapper = require("mapper");

var Oreo = {
  doubleStuff: Boolean,
  hasCreamFilling: Boolean,
  cookiesLeft: Number
};

var Food = {
  name: String,
  calories: Number,
  oreo: Oreo
};

var User = {
  name: String,
  age: Number,
  food: [Food]
};

var data = {
  name: "Mike",
  age: 28,
  // "extraData" will be ignored since it doesn't exist we are comparing to
  extraData: "You will never see me",
  food: [
    {
      name: "Cheesecake",
      calories: 1000
    },
    {
      name: "Oreo",
      calories: 100,
      oreo: {
        // "doubleStuff" will be ignored since this is a string and not a boolean
        doubleStuff: "true", 
        hasCreamFilling: true
      }
    }
  ]
};

console.log(Mapper(data, User));

Will return:

{
  "name": "Mike",
  "age": 28,
  "food": [
    {
      "name": "Cheesecake",
      "calories": 1000
    },
    {
      "name": "Oreo",
      "calories": 100,
      "oreo": {
        "hasCreamFilling": true
      }
    }
  ]
}

Why?

Because we care about making sure the right data gets passed around. This becomes especially useful when you are writing models that can be used on both the front and back end of a NodeJS application.

And honestly it's just fun to write something like this! Why not?!

js-mapper's People

Contributors

mikemunsie avatar

Watchers

 avatar  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.