https://github.com/timnew/mutator
A function to convert plain javascript object(usually deserialized from JSON) into some class instance
https://github.com/timnew/mutator
Last synced: 24 days ago
JSON representation
A function to convert plain javascript object(usually deserialized from JSON) into some class instance
- Host: GitHub
- URL: https://github.com/timnew/mutator
- Owner: timnew
- License: mit
- Created: 2014-07-29T06:29:29.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2015-09-08T14:11:36.000Z (over 10 years ago)
- Last Synced: 2025-02-11T06:52:07.478Z (over 1 year ago)
- Language: CoffeeScript
- Size: 176 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
mutator [![Bower version][bower-image]][homepage] [![NPM version][npm-image]][npm-url]
=======
> A function to convert plain javascript object(usually deserialized from JSON) into some class instance
Install
-------
Install using [bower][bower-url]:
$ bower install mutator --save
Install using [npm][npm-homepage]:
$ npm install mutator --save
Quick Start
------------
Signature:
```
function mutate(instance, class, [argument passed to constructor])
```
In Node.js:
```javascript
const mutate = require('mutator');
mutate(instance, klass);
```
In Browser:
```javascript
mutate(instance, klass);
```
Environment Support
---------------
`Mutator` works fine in most browsers and `Node.js`.
For `Internet Explorer`, prior than `IE 11`, because of lack of `object.__proto__`, a hard-copy work around is applied.
Quick Sample
------------
A simple `Rect` class definition
```coffeescript
class Rect
constructor: (width, height) ->
@width = width if width?
@height = height if height?
area: ->
@width * @height
rect = new Rect(100, 100) # A rect instance
console.log rect.area() # should print 10000
rectJSON = JSON.stringify(rect) # Serialize the object into JSON
###
{
"width": 100,
"height": 100
}
###
```
`rectJSON` is usually stored into file or transmitted via ajax call.
When consuming the JSON
```coffeescript
mutate = require('mutator')
rectCopy = JSON.parse(rectJSON)
console.log rectCopy.area() # Got error: undefined isn't a function!
rectClone = mutate(rectCopy, Rect)
console.log rectClone == rectCopy # true, they are the same instance
console.log rectCopy.area() # got 10000
```
## License
MIT
[![NPM download][npm-download]][npm-url]
[homepage]: https://github.com/timnew/mutator
[bower-image]: https://badge.fury.io/bo/widget.coffee.svg
[bower-url]: http://bower.io/
[npm-image]: http://img.shields.io/npm/v/mutator.svg
[npm-download]: http://img.shields.io/npm/dm/mutator.svg?style=flat
[npm-url]: https://www.npmjs.org/package/mutator
[npm-homepage]: https://www.npmjs.org/