1.1.3 • Published 2 months ago

adaptr v1.1.3

Weekly downloads
5
License
MIT
Repository
github
Last release
2 months ago

Adaptr

Adaptr is a small helper that allows the transformation of data received from a server to a more JS friendly format. It works in both direction..

Usage

import Adaptr from 'adaptr';

// user object received from server
const user = {
    'id': 133,
    'is_logged_in': true
};

// we define an adapter with the desired schema
// on the left it's the expected server format
// on the right it's the desired local format
const userAdapter = new Adaptr('user', {
    'id': 'userId',
    'is_logged_in': 'isLoggedIn'
});

const localFormat = userAdapter.unserialize(user);
const serverFormat = userAdapter.serialize(localFormat);

Output

"localFormat": {
    "user": {
       "userId": 133,
       "isLoggedIn": true
    }
}

"serverFormat": {
    "user": {
       "id": 133,
       "is_logged_in": true
    }
}

Nested objects

import Adaptr from 'adaptr';

// user object received from server
const ;

const comment = {
    'comment_id': 23,
    'text': 'this is a comment',
    'user'= {
        'id': 133,
        'is_logged_in': true
    }
};

// we define an adapter with the desired schema
// on the left it's the expected server format
// on the right it's the desired local format
const userAdapter = new Adaptr('user', {
    'id': 'userId',
    'is_logged_in': 'isLoggedIn'
});

const commentAdapter = new Adaptr('comment', {
    'comment_id': 'id',
    'text': 'text',
    'user': userAdapter
});

const localFormat = commentAdapter.unserialize(user);
const serverFormat = commentAdapter.serialize(localFormat);

Output

"localFormat": {
    "comment": {
        "id": 23,
        "text": "this is a comment",
        "user": {
           "userId": 133,
           "isLoggedIn": true
        }
    }
}

"serverFormat": {
    "comment": {
        "comment_id": 23,
        "text": "this is a comment",
        "user": {
           "id": 133,
           "is_logged_in": true
        }
    }
}

Installation

Adaptr is dependencie free.

$ npm install adaptr

License

MIT

Free Software, Hell Yeah!

1.1.3

2 months ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.8

4 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago