main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { morphism } from "morphism";

// Source data coming from an API.
const source = {
  foo: "baz",
  bar: ["bar", "foo"],
  baz: {
    qux: "bazqux"
  }
};

// Target Class in which to morph the source data.
class Destination {
  foo = null;
  bar = null;
  bazqux = null;
}

// A structure-preserving object from a source data towards a target data.
const schema = {
  foo: "bar[1]", // Grab the property value by his path
  bar: (iteratee, source, destination) => {
    // Apply a Function on the current element
    return iteratee.bar[0];
  },
  bazqux: {
    // Apply a function on property value
    path: "baz.qux",
    fn: (propertyValue, source) => {
      return propertyValue;
    }
  }
};

const classObjects = morphism(schema, source, Destination);
// Destination {foo: "foo", bar: "bar", bazqux: "bazqux"}

const jsObjects = morphism(schema, source);
// Object {foo: "foo", bar: "bar", bazqux: "bazqux"}
console.log(classObjects);
console.log(jsObjects);
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie