1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var check = require('check-types'),
|
9 | Variable = require('./Variable');
|
10 |
|
11 | function VariableStore( vars ){
|
12 | this._vars = {};
|
13 | if( check.assigned( vars ) ){
|
14 | this.set( vars );
|
15 | }
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | VariableStore.prototype.var = function( key, val ){
|
34 | if( !check.nonEmptyString( key ) ){
|
35 | throw new Error( 'invalid query variable, key must be valid string' );
|
36 | }
|
37 |
|
38 | if( !this._vars.hasOwnProperty( key ) ){
|
39 | this._vars[ key ] = new Variable();
|
40 | }
|
41 |
|
42 | if( check.assigned( val ) ){
|
43 | this._vars[ key ].set( val );
|
44 | }
|
45 |
|
46 | return this._vars[ key ];
|
47 | };
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | VariableStore.prototype.isset = function( key ){
|
53 | if( !check.nonEmptyString( key ) ){
|
54 | throw new Error( 'invalid key, must be valid string' );
|
55 | }
|
56 |
|
57 | if( !this._vars.hasOwnProperty( key ) ){
|
58 | return false;
|
59 | }
|
60 |
|
61 | return this._vars[ key ].get() !== '';
|
62 | };
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | VariableStore.prototype.unset = function( key ){
|
68 | if( !check.nonEmptyString( key ) ){
|
69 | throw new Error( 'invalid key, must be valid string' );
|
70 | }
|
71 |
|
72 | if( !this._vars.hasOwnProperty( key ) ){
|
73 | return false;
|
74 | }
|
75 |
|
76 | delete this._vars[ key ];
|
77 | return true;
|
78 | };
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | VariableStore.prototype.set = function( pojo ){
|
84 | if( !check.object( pojo ) ){
|
85 | throw new Error( 'invalid object' );
|
86 | }
|
87 | for( var attr in pojo ){
|
88 | this.var( attr ).set( pojo[attr] );
|
89 | }
|
90 | };
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | VariableStore.prototype.export = function(){
|
96 | var out = {};
|
97 | for( var v in this._vars ){
|
98 | out[ v ] = this._vars[v].get();
|
99 | }
|
100 | return out;
|
101 | };
|
102 |
|
103 | module.exports = VariableStore;
|