Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "func/deep-transform"

Index

Functions

Functions

Const deepTransform

  • Higher-order deep object transformer used by {@link (mapDeep:1)}. Accepts a nested spec array reflecting same key structure as the object to be mapped, but with functions or sub-specs as their values. Returns a new function, which when called, recursively applies nested transformers in post-order traversal (child transformers are run first) and returns the result of the root transformer.

    remarks

    The transform specs are given as arrays in this format:

    [tx-function, { key1: [tx-function, {...}], key2: tx-fn }]

    If a key in the spec has no further sub maps, its transform function can be given directly without having to wrap it into the usual array structure.

    example
    // source object to be transformed
    src = {
       meta: {
         author: { name: "Alice", email: "a@b.com" },
         date: 1041510896000
       },
       type: "post",
       title: "Hello world",
       body: "Ratione necessitatibus doloremque itaque."
    };
    
    // deep transformation spec
    spec = [
       // root transform (called last)
       ({type, meta, title, body}) => ["div", {class: type}, title, meta, body],
       // object of transform sub-specs
       {
         meta: [
           ({author, date}) => ["div.meta", author, `(${date})`],
           {
             author: ({email, name}) => ["a", {href: `mailto:${email}`}, name],
             date: (d) => new Date(d).toLocaleString()
           }
         ],
         title: (title) => ["h1", title]
       }
    ];
    
    // build transformer & apply to src
    deepTransform(spec)(src);
    
    // [ "div",
    //   { class: "article" },
    //   [ "h1", "Hello world" ],
    //   [ "div.meta",
    //     [ "a", { href: "mailto:a@.b.com" }, "Alice" ],
    //     "(1/2/2003, 12:34:56 PM)" ],
    //   "Ratione necessitatibus doloremque itaque." ]

    Parameters

    Returns (x: any) => any

      • (x: any): any
      • Parameters

        • x: any

        Returns any

Generated using TypeDoc