forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
183 lines (159 loc) · 4.9 KB
/
Copy pathutil.js
File metadata and controls
183 lines (159 loc) · 4.9 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var object = this.object = {
mixin: function(){
// copy reference to target object
var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, source;
// Handle a deep copy situation
if ( typeof target === "boolean" ) {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
// Handle case when target is a string or something (possible in deep copy)
if ( typeof target !== "object" && !(typeof target === 'function') )
target = {};
// mixin process itself if only one argument is passed
if ( length == i ) {
target = GLOBAL;
--i;
}
for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
if ( (source = arguments[i]) != null ) {
// Extend the base object
Object.getOwnPropertyNames(source).forEach(function(k){
var d = Object.getOwnPropertyDescriptor(source, k) || {value: source[k]};
if (d.get) {
target.__defineGetter__(k, d.get);
if (d.set) {
target.__defineSetter__(k, d.set);
}
}
else {
// Prevent never-ending loop
if (target === d.value) {
continue;
}
if (deep && d.value && typeof d.value === "object") {
target[k] = object.mixin(deep,
// Never move original objects, clone them
source[k] || (d.value.length != null ? [] : {})
, d.value);
}
else {
target[k] = d.value;
}
}
});
}
}
// Return the modified object
return target;
}
},
_define = function(proto, key, value){
Object.defineProperty(proto, key, {value: value, enumerable: false});
};
// OO - Class - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
// Based on http://ejohn.org/blog/simple-javascript-inheritance/
// which is based on implementations by Prototype / base2
exports.Class = (function(){
var global = this, initialize = true
var referencesSuper = /xyz/.test(function(){ xyz }) ? /\b__super__\b/ : /.*/
/**
* Shortcut for Class.extend()
*
* @param {hash} props
* @return {function}
* @api public
*/
var Class = function(props){
if (this == global)
return Class.extend(props)
},
SuperClass = Class;
// --- Version
Class.version = '1.2.0'
/**
* Create a new class.
*
* User = Class({
* init: function(name){
* this.name = name
* }
* })
*
* Classes may be subclassed using the .extend() method, and
* the associated superclass method via this.__super__().
*
* Admin = User.extend({
* init: function(name, password) {
* this.__super__(name)
* // or this.__super__.apply(this, arguments)
* this.password = password
* }
* })
*
* @param {hash} props
* @return {function}
* @api public
*/
Class.extend = function(props) {
var __super__ = this.prototype, __superself__ = this
initialize = false
var prototype = new this
initialize = true
function Class() {
if (initialize && this.init)
this.init.apply(this, arguments)
}
function extend(props) {
for (var key in props)
if (props.hasOwnProperty(key))
Class[key] = props[key]
}
Class.include = function(props) {
for (var name in props)
if (name == 'include')
if (Array.isArray(props[name]))
for (var i = 0, len = props[name].length; i < len; ++i)
Class.include(props[name][i])
else
Class.include(props[name])
else if (name == 'extend'){
if (__superself__){
var older = {};
for (var i in __superself__){
if (!(i in SuperClass) && !(i in Class)){
older[i] = __superself__[i];
}
}
props[name] = [].concat(older).concat(props[name])
}
if (Array.isArray(props[name]))
for (var i = 0, len = props[name].length; i < len; ++i)
extend(props[name][i])
else
extend(props[name])
} else if (props.hasOwnProperty(name))
_define(prototype,name,
typeof props[name] == 'function' &&
typeof __super__[name] == 'function' &&
referencesSuper.test(props[name]) ?
(function(name, fn){
return function() {
this.__super__ = __super__[name]
return fn.apply(this, arguments)
}
})(name, props[name])
: props[name]
);
}
Class.include(props)
Class.prototype = prototype
Class.constructor = Class
Class.extend = arguments.callee
return Class
}
return Class;
})();