npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

structtransform

v0.0.4

Published

Use a template to change the value of an object.

Downloads

7

Readme

Use a template to change the value of an object.

GitHub package.json version npm version npm type definitions GitHub 7thCode node.js.yml GitHub last commit

README DEMO in detail


Motivation

I have to migrate... Do you change the shape of so many objects?

Features

Transform an object using two templates, pre-transform and post-transform. The two templates are a mechanism to recognize each other's location by "key".

Installation

npm install structtransform

No modules depend on it.

Usage

StructTransformer

Transform the structure.

First, the object before transformation is defined as the original template.

Next, an object representing the transformed structure is prepared as a post-processing template.

By applying inset_data that holds the data to the target data, the value of the target data is converted from the original template to the structure of the post-process template using the value as a key.

Replace the structure of the from template with the structure of the to template. The from and to templates apply the same values to the same key locations. Note that the value must be of built-in type.

Example

const original_template: any = [{a: "Key1"}, {b: "Key2"}, {c: ["Key3", "Key4"]}];
const post_processing_template: any = {x1: "Key1", x2: {y1: "Key2", y2: "Key2", y3: {z1: "Key3", z2: "Key4"}}};

const transformer: StructTransformer = new StructTransformer(before_template, after_template);

const inset_data = [{a: "Data1"}, {b: "Data2"}, {c: ["Data3", "Data4"]}]
const target_and_result = {x1: "", x2: {y1: "", y2: "", y3: {z1: "", z2: ""}}};

transformer.Transform(inset_data, target_and_result);
console.log(target_and_result);
    {
      x1: 'Data1',
      x2: { y1: 'Data2', y2: 'Data2', y3: { z1: 'Data3', z2: 'Data4' } }
    }

The target object may be part of the object.

Example

const original_template: any = [{a: "Key1"}, {b: "Key2"}, {c: ["Key3", "Key4"]}];
const post_processing_template: any = {x1: "Key1", x2: {y1: "Key2", y2: "Key2", y3: {z1: "Key3", z2: "Key4"}}};


// works equally well for container elements.
const transformer: StructTransformer = new StructTransformer(before_template, after_template);

const inset_data = [{a: "Data1"}, {b: "Data2"}, {c: ["Data3", "Data4"]}]
const target_and_result = [
    {x1: "", x2: {y1: "", y2: "", y3: {z1: "", z2: ""}}},
    {x1: "", x2: {y1: "", y2: "", y3: {z1: "", z2: ""}}},
    {x1: "", x2: {y1: "", y2: "", y3: {z1: "", z2: ""}}},
    {x1: "", x2: {y1: "", y2: "", y3: {z1: "", z2: ""}}},
];

transformer.Transform(inset_data, target_and_result[2]);
console.log(target_and_result);
[
    {x1: '',x2: {y1: '', y2: '', y3: {z1: "", z2: ""}}},
    {x1: '',x2: {y1: '', y2: '', y3: {z1: "", z2: ""}}},
    {x1: 'Data1', x2: {y1: 'Data2', y2: 'Data2', y3: {z1: "Data3", z2: "Data4"}}},
    {x1: '',x2: {y1: '', y2: '', y3: {z1: "", z2: ""}}}
]

The various classes that make up StructTransformer can also be used alone. Such classes include:

PathScanner

Returns an enumeration of all object paths keyed by a value. If the same value is duplicated, only one path of the object whose value was found last is returned.

Example

const unique_scanner = new PathScanner();
let result: any = {};

const target = {
    a: {a1: "Unique1"},
    b: {
        b1: {
            b11: {b111: "Unique2"},
            b12: {b121: ["Unique3", "Unique4", {b1211: "Unique5"}]}
        }
    }
}

unique_scanner.Scan(target, result);
console.log(result);
{
    Unique1: '/a/a1',
    Unique2: '/b/b1/b11/b111',
    Unique3: '/b/b1/b12/b121/0',
    Unique4: '/b/b1/b12/b121/1',
    Unique5: '/b/b1/b12/b121/2/b1211'
}

PathDictBuilder

Returns an enumeration of all object paths keyed by a value. If the same value is duplicated, the path is added as an array element.

Example

const many_key_scanner = new PathDictBuilder();
let result: any = {};

const target = {
    a: {a1: "Duplicate1"},
    b: {
        b1: {
            b11: {b111: "Duplicate1"},
            b12: {b121: ["Duplicate2", "Duplicate2", {b1211: "Duplicate1"}]}
        }
    }
}

many_key_scanner.Scan(target, result);
expect(result).toStrictEqual({Duplicate1: ["/a/a1", "/b/b1/b11/b111", "/b/b1/b12/b121/2/b1211"], Duplicate2: ["/b/b1/b12/b121/0", "/b/b1/b12/b121/1"]});
console.log(result);
{
    Duplicate1: ['/a/a1', '/b/b1/b11/b111', '/b/b1/b12/b121/2/b1211'],
    Duplicate2: ['/b/b1/b12/b121/0', '/b/b1/b12/b121/1']
}

ValueCollecter

Returns an object whose key is a path and whose value is the value of that path.

Example


const value_collecter = new ValueCollecter();
let result: any = {};

const target = {
    a: {a1: "Value1"},
    b: {
        b1: {
            b11: {b111: "Value2"},
            b12: {b121: ["Value3", "Value4", {b1211: "Value5"}]}
        }
    }
}

value_collecter.Scan(target, result);
expect(result).toStrictEqual({"/a/a1": "Value1", "/b/b1/b11/b111": "Value2", "/b/b1/b12/b121/0": "Value3", "/b/b1/b12/b121/1": "Value4", "/b/b1/b12/b121/2/b1211": "Value5"});
console.log(result);
{
    '/a/a1': 'Value1',
    '/b/b1/b11/b111': 'Value2',
    '/b/b1/b12/b121/0': 'Value3',
    '/b/b1/b12/b121/1': 'Value4',
    '/b/b1/b12/b121/2/b1211': 'Value5'
}

StructRenderer

Change the value of the target path of the target object by using [Object whose value is the path as the key] such as the result of "ValueCollecter".

Example


const struct_renderer = new StructRenderer();
let dictionary: any = {"/a/a1": "Updated1", "/b/b1/b12/b121/1": "Updated4"};

const target_and_result = {
    a: {a1: "Original1"},
    b: {
        b1: {
            b11: {b111: "Original2"},
            b12: {b121: ["Original3", "Original4", {b1211: "Original5"}]}
        }
    }
}

struct_renderer.Scan(target_and_result, dictionary);
console.log(target_and_result);
{
    a: {a1: "Original1"},
    b: {
        b1: {
            b11: {b111: "Original2"},
            b12: {b121: ["Original3", "Original4", {b1211: "Original5"}]}
        }
    }
}

Note

See demo.md for unclear cases.

Author

[email protected]

License

"structtransform" is under MIT license.