forked from dayitv89/react-native-jsmodel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSModel.js
38 lines (32 loc) · 757 Bytes
/
JSModel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// Copyright © 2017-Present, Gaurav D. Sharma
// All rights reserved.
//
'use strict';
export default class JSONModel {
constructor(json = {}) {
if (this.validate(json)) {
Object.assign(this, json);
this.loadExtra(json);
}
}
validate(json) {
return typeof json !== 'undefined';
}
/// this method use in case you want to hack(prototype) your model and want to introduced new keys
loadExtra(json = {}) {}
// id => appID, first_name => firstName
// pass args as `{id: 'appID', first_name: 'firstName'}`
keyMapper(hash) {
for (const key in hash) {
this[hash[key]] = this[key];
delete this[key];
}
}
clone() {
return new this.constructor(JSON.parse(this.toJSON()));
}
toJSON() {
return JSON.stringify(this);
}
}