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

camelscore-models

v1.0.0

Published

Create models for fetch with converting under_score style strings to camelCase and the reverse, as well as on objects.

Downloads

12

Readme

camelscore-models

CamelScore-Models is a way to (un)serialize server Data.

It helps you to convert weird data fields from server to something more friendly.

Npm Version Month Downloads Npm Licence

NPM

Usage

This library was created for using in React application uses Redux as store. In actions when data is fetching serialize method passed to convert income data and unserialize method passed to convert outcome data. There is converted data in store and components.

Code Example

Below you can see response body example. There are fields that should have other names in App.

var responseBody = {
  user_id: 1,
  user_name: "Pavel",
  user_avatar: {
    preview_url: "http://site.com/avatar.jpg",
    size: {
      min_width: 100,
      min_height: 100
    }
  },
  user_posts: [{
      post_id: 1, // id
      user_id: 1, // userId
      text: "Hey, how are u?",
      created: new Date(),
    }, {
      post_id: 2,
      user_id: 1,
      text: "Hello! I'm fine",
      created: new Date(),
  }],
};

To change the name of these fields need to create models and use models serialize / unserialize methods

Create Models

User.js

import CSM from 'camelscore-models'
import Avatar from './Avatar'
import Post from './Post'

const User = new CSM({
  fields: {
    'user_id': {
      to: 'id'
    },
    'user_name': {
      to: 'name'
    },
    'user_avatar': {
      to: 'avatar',
      model: Avatar
    },
    'user_posts': {
      to: 'posts',
      model: Post
    },
  }
});

export default User;

Avatar.js

import CSM from 'camelscore-models'

const Avatar = new CSM({
  fields: {
    size: {
      model: {
        'min_width': {
          to: 'width'
        },
        'min_height': {
          to: 'height'
        },
      }
    },
  }
});


export default Avatar;

Post.js

import CSM from 'camelscore-models'

const Post = new CSM({
  fields: {
    'post_id': {
      to: 'id'
    },
    'user_id': {
      to: 'userId'
    },
  }
});


export default Post;

Serialize

To convert data fields to friendly way use model's serialize method

var serialized = User.serialize(responseBody);

returns:

{
  id: 1,
  name: 'Pavel',
  avatar: {
    previewUrl: 'http://site.com/avatar.jpg',
    size: {
      minWidth: 100,
      minHeight: 100
    }
  },
  posts: [{
    id: 1,
    userId: 1,
    text: 'Hey, how are u?'
  }, {
    id: 2,
    userId: 1,
    text: 'Hello! I\'m fine'
  }]
}

Unserialize

to revert data back for passing as body in request use model's unserialize method

var unserialized = User.unserialize(serialized)

returns:

{
  user_id: 1,
  user_name: 'Pavel',
  user_avatar: {
    preview_url: 'http://site.com/avatar.jpg',
    size: {
      min_width: 100,
      min_height: 100
    }
  },
  user_posts: [{
    post_id: 1,
    user_id: 1,
    text: 'Hey, how are u?'
  }, {
    post_id: 2,
    user_id: 1,
    text: 'Hello! I\'m fine'
  }]
}