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

@redarrowlabs/modeljx

v0.4.0

Published

Patterns for client-side projections

Downloads

2

Readme

model-jx

A fluent data shape projection library. At this time, the library is designed to be used with typescript, but it may be possible to use it without.

The source to model-jx can be found on github.

Why?

Let's say you have an application that communicates with a server via a RESTful API that returns JSON.

The JSON being returned by the server represents the server's view of the persisted state of the object. However, you app wants to translate that domain model into a view model. Perhaps the server was written in Java or C# and uses different naming conventions that look horrible in Javascript. Let's also say that the server isn't perfectly RESTful: some of the POST actions are more like remote procedure calls. You now need to map your modified view model to the parameters on the RPC-style post.

Or perhaps that's not the case: perhaps you have complete control over your server, and just want to cleanly seperate your server responses from you application's state. And you want to be able to test the mappings from server responses to state, and state to server requests seperately from your domain logic.

Perhaps you just want to ensure that all your server responses become immutable when they are added to your application state.

Examples

For complete examples, see the examples folder.

Let's say your server responds with a model that can be defined with the following typescript interfaces:

interface DomainObject {
    value: string,
    inner: NestedDomainObject,
    anotherInner: NestedDomainObject,
    DifferentCase?: string
}

interface NestedDomainObject {
    value: string,
}

But you want a view model like this:

interface DomainObject {
    value: string,
    isDirty: boolean,
    moreData: NestedDomainObject,
    anotherInner: NestedDomainObject,
    differentcase: string
}

interface NestedDomainObject {
    value: string
}

Then you could define projections like this:

const MapToLowerCase = (propertyName: string) => propertyName.toLowerCase();

// No need to export this, since it's only used by another projection:
const InnerResult_to_InnerModel = ProjectionBuilder
    .defineProjection<ServerModel.NestedDomainObject, ClientModel.NestedDomainObject>(
        ServerFactory.nestedDomainObject,
        ClientFactory.nestedDomainObject
    ).build();

// ...exported, so the rest of the application can use the projection:
export const Response_to_ViewModel = ProjectionBuilder
    //
    // This will define a typed projection from the server's version of
    // a domain object to the client's view model of the same object.
    //
    // Note that these can handle collections like arrays and Immutable.Lists
    // w/out any additional work (see tests in example directory).
    //
    .defineProjection<ServerModel.DomainObject, ClientModel.DomainObject>(
        ServerFactory.domainObject,
        ClientFactory.domainObject,
    )
    //
    // This will map all server objects properties that contain capital letters
    // to properties on the client model that are all lower case:
    //
    .withMapping(MapToLowerCase)
    //
    // This will override the projection for a specific property.
    // Notice how it uses the previously defined projection for the INestedDomainObject type.
    // This form of an override provides both the name of the source and target properties:
    // you'd generally do this is they differ in a way that's not covered by any 'withMappings'.
    //
    .override({
        // You may specify the name of a property with a lambda (if you like code completion), but a string will also work.
        fromProperty: (x: ServerModel.DomainObject) => x.inner,  // Map the 'inner' property on the server response.
        toProperty: (x: ClientModel.DomainObject) => x.moreData, // To the 'moreData' property on the client view model.
        use: InnerResult_to_InnerModel
    })
    //
    // ...but there's another form using 'forProperty' can be used if the target property's name doesn't need to be
    // specified.
    //
    .override({
        forProperty: (x: ServerModel.DomainObject) => x.anotherInner,
        use: InnerResult_to_InnerModel,
        // You can also project based on conditions.
        // In this case, when the value isn't 'magic', we use the default projection...
        when: (x: ServerModel.DomainObject) => x.anotherInner.value != "magic"
    })
    .override({
        forProperty: (x: ServerModel.DomainObject) => x.anotherInner,
        // ...But when it is 'magic', we project it differently.
        use: (x: ServerModel.DomainObject) => "did some magic",
        when: (x: ServerModel.DomainObject) => x.anotherInner.value == "magic"
    })
    .build();

Development

Install rimraf globally. This is needed for some of the build commands.

npm install rimraf -g