-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
145 lines (121 loc) · 3.42 KB
/
util.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict';
var _ = require('underscore');
function assert(cond, message) {
if (!cond) {
throw new Error(message);
}
};
function heir(object) {
function Heir() {};
Heir.prototype = object;
return new Heir();
};
function makePrototype(Constructor, SuperConstructor, mixin) {
Constructor.prototype = heir(SuperConstructor.prototype);
Constructor.prototype.constructor = Constructor;
if (mixin) {
_.extend(Constructor.prototype, mixin);
}
return Constructor;
};
function Set() {
this.clear();
};
_.extend(Set.prototype, {
'length': 0,
'add': function(value) {
var key = this.makeKey(value);
if (!this.members.hasOwnProperty(key)) {
this.length++;
}
this.members[key] = value;
return this;
},
'remove': function(value) {
var key = this.makeKey(value);
if (this.members.hasOwnProperty(key)) {
this.length--;
}
delete this.members[key];
return this;
},
'has': function(value) {
var key = this.makeKey(value);
return this.members.hasOwnProperty(key);
},
'clear': function() {
this.members = {};
delete this.length;
return this;
},
'toArray': function() {
return _.values(this.members);
},
'_': function() {
if (arguments.length === 0) {
return _(this.members);
} else {
var args = _.toArray(arguments);
var fname = args[0];
args[0] = this.members;
return _[fname].apply(_, args);
}
}
});
// Create a dictionary-like object that has a default value for any missing
// key. Specify this default with a function that returns an instance or a
// native type.
function DefaultDict(newValue) {
this.newValue = _.isFunction(newValue) ? newValue : function() {
return newValue;
};
this.properties = {};
};
_.extend(DefaultDict.prototype, {
// Get a property. Set a default value if necessary.
'get': function(property) {
return (this.has(property) ?
this.properties[property] :
(this.properties[property] = this.newValue(property)));
},
// Set a value.
'set': function(property, value) {
this.properties[property] = value;
return value;
},
// Remove a property.
'remove': function(property) {
var value = this.properties[property];
delete this.properties[property];
return value;
},
'clear': function() {
this.properties = {};
return this;
},
// Return `true` iff the dictionary already has a property.
// This function should be used carefully since it does not avoid
// default values.
'has': function(property) {
return this.properties.hasOwnProperty(property);
},
// Return a clone of the current state of the properties dictionary.
'toObject': function() {
return _.clone(this.properties);
},
'_': function() {
if (arguments.length === 0) {
return _(this.properties);
} else {
var args = _.toArray(arguments);
var fname = args[0];
args[0] = this.properties;
return _[fname].apply(_, args);
}
}
});
exports.assert = assert;
exports.heir = heir;
exports.makePrototype = makePrototype;
exports.DefaultDict = DefaultDict;
exports.Set = Set;