Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80713 views
1
/**
2
* class Namespace
3
*
4
* Simple object for storing attributes. Implements equality by attribute names
5
* and values, and provides a simple string representation.
6
*
7
* See also [original guide][1]
8
*
9
* [1]:http://docs.python.org/dev/library/argparse.html#the-namespace-object
10
**/
11
'use strict';
12
13
var _ = require('lodash');
14
15
/**
16
* new Namespace(options)
17
* - options(object): predefined propertis for result object
18
*
19
**/
20
var Namespace = module.exports = function Namespace(options) {
21
_.extend(this, options);
22
};
23
24
/**
25
* Namespace#isset(key) -> Boolean
26
* - key (string|number): property name
27
*
28
* Tells whenever `namespace` contains given `key` or not.
29
**/
30
Namespace.prototype.isset = function (key) {
31
return _.has(this, key);
32
};
33
34
/**
35
* Namespace#set(key, value) -> self
36
* -key (string|number|object): propery name
37
* -value (mixed): new property value
38
*
39
* Set the property named key with value.
40
* If key object then set all key properties to namespace object
41
**/
42
Namespace.prototype.set = function (key, value) {
43
if (typeof (key) === 'object') {
44
_.extend(this, key);
45
} else {
46
this[key] = value;
47
}
48
return this;
49
};
50
51
/**
52
* Namespace#get(key, defaultValue) -> mixed
53
* - key (string|number): property name
54
* - defaultValue (mixed): default value
55
*
56
* Return the property key or defaulValue if not set
57
**/
58
Namespace.prototype.get = function (key, defaultValue) {
59
return !this[key] ? defaultValue: this[key];
60
};
61
62
/**
63
* Namespace#unset(key, defaultValue) -> mixed
64
* - key (string|number): property name
65
* - defaultValue (mixed): default value
66
*
67
* Return data[key](and delete it) or defaultValue
68
**/
69
Namespace.prototype.unset = function (key, defaultValue) {
70
var value = this[key];
71
if (value !== null) {
72
delete this[key];
73
return value;
74
} else {
75
return defaultValue;
76
}
77
};
78
79