1 |
|
2 | /**
|
3 | A primitive Javascript variable wrapped in an Object so that it can be
|
4 | handled by reference instead of by value.
|
5 |
|
6 | This is useful as an inline variable placeholder where the value may or
|
7 | may not be known when the query is constructed.
|
8 |
|
9 | note: values must be a valid js primitive type (string,numeric,boolean),
|
10 | or an Array. No objects allowed.
|
11 | note: the object prototype contains custom serialization methods
|
12 |
|
13 | warning: using an instance of Variable() in a boolean operation will
|
14 | *always* yield true; regardless of the underlying value; because js.
|
15 | **/
|
16 |
|
17 | var check = require('check-types');
|
18 |
|
19 | function Variable(){
|
20 | this.$ = '';
|
21 | }
|
22 |
|
23 | Variable.prototype.set = function( val ){
|
24 | if( !check.nonEmptyString(val) && !check.number(val) && !check.boolean(val) && !check.array(val) && !check.object(val)){
|
25 | throw new Error( 'invalid value, value must be valid js Variable' );
|
26 | }
|
27 | this.$ = val;
|
28 | };
|
29 |
|
30 | Variable.prototype.valueOf =
|
31 | Variable.prototype.toString =
|
32 | Variable.prototype.toJSON =
|
33 | Variable.prototype.get = function(){
|
34 | return this.$;
|
35 | };
|
36 |
|
37 | module.exports = Variable;
|