/usr/lib/nodejs/class-utils/index.js is in node-class-utils 0.3.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | 'use strict';
var util = require('util');
var utils = require('./utils');
/**
* Expose class utils
*/
var cu = module.exports;
/**
* Expose class utils: `cu`
*/
cu.isObject = function isObject(val) {
return utils.isObj(val) || typeof val === 'function';
};
/**
* Returns true if an array has any of the given elements, or an
* object has any of the give keys.
*
* ```js
* cu.has(['a', 'b', 'c'], 'c');
* //=> true
*
* cu.has(['a', 'b', 'c'], ['c', 'z']);
* //=> true
*
* cu.has({a: 'b', c: 'd'}, ['c', 'z']);
* //=> true
* ```
* @param {Object} `obj`
* @param {String|Array} `val`
* @return {Boolean}
* @api public
*/
cu.has = function has(obj, val) {
val = cu.arrayify(val);
var len = val.length;
if (cu.isObject(obj)) {
for (var key in obj) {
if (val.indexOf(key) > -1) {
return true;
}
}
var keys = cu.nativeKeys(obj);
return cu.has(keys, val);
}
if (Array.isArray(obj)) {
var arr = obj;
while (len--) {
if (arr.indexOf(val[len]) > -1) {
return true;
}
}
return false;
}
throw new TypeError('expected an array or object.');
};
/**
* Returns true if an array or object has all of the given values.
*
* ```js
* cu.hasAll(['a', 'b', 'c'], 'c');
* //=> true
*
* cu.hasAll(['a', 'b', 'c'], ['c', 'z']);
* //=> false
*
* cu.hasAll({a: 'b', c: 'd'}, ['c', 'z']);
* //=> false
* ```
* @param {Object|Array} `val`
* @param {String|Array} `values`
* @return {Boolean}
* @api public
*/
cu.hasAll = function hasAll(val, values) {
values = cu.arrayify(values);
var len = values.length;
while (len--) {
if (!cu.has(val, values[len])) {
return false;
}
}
return true;
};
/**
* Cast the given value to an array.
*
* ```js
* cu.arrayify('foo');
* //=> ['foo']
*
* cu.arrayify(['foo']);
* //=> ['foo']
* ```
*
* @param {String|Array} `val`
* @return {Array}
* @api public
*/
cu.arrayify = function arrayify(val) {
return val ? (Array.isArray(val) ? val : [val]) : [];
};
/**
* Noop
*/
cu.noop = function noop() {
return;
};
/**
* Returns the first argument passed to the function.
*/
cu.identity = function identity(val) {
return val;
};
/**
* Returns true if a value has a `contructor`
*
* ```js
* cu.hasConstructor({});
* //=> true
*
* cu.hasConstructor(Object.create(null));
* //=> false
* ```
* @param {Object} `value`
* @return {Boolean}
* @api public
*/
cu.hasConstructor = function hasConstructor(val) {
return cu.isObject(val) && typeof val.constructor !== 'undefined';
};
/**
* Get the native `ownPropertyNames` from the constructor of the
* given `object`. An empty array is returned if the object does
* not have a constructor.
*
* ```js
* cu.nativeKeys({a: 'b', b: 'c', c: 'd'})
* //=> ['a', 'b', 'c']
*
* cu.nativeKeys(function(){})
* //=> ['length', 'caller']
* ```
*
* @param {Object} `obj` Object that has a `constructor`.
* @return {Array} Array of keys.
* @api public
*/
cu.nativeKeys = function nativeKeys(val) {
if (!cu.hasConstructor(val)) return [];
return Object.getOwnPropertyNames(val);
};
/**
* Returns property descriptor `key` if it's an "own" property
* of the given object.
*
* ```js
* function App() {}
* Object.defineProperty(App.prototype, 'count', {
* get: function() {
* return Object.keys(this).length;
* }
* });
* cu.getDescriptor(App.prototype, 'count');
* // returns:
* // {
* // get: [Function],
* // set: undefined,
* // enumerable: false,
* // configurable: false
* // }
* ```
*
* @param {Object} `obj`
* @param {String} `key`
* @return {Object} Returns descriptor `key`
* @api public
*/
cu.getDescriptor = function getDescriptor(obj, key) {
if (!cu.isObject(obj)) {
throw new TypeError('expected an object.');
}
if (typeof key !== 'string') {
throw new TypeError('expected key to be a string.');
}
return Object.getOwnPropertyDescriptor(obj, key);
};
/**
* Copy a descriptor from one object to another.
*
* ```js
* function App() {}
* Object.defineProperty(App.prototype, 'count', {
* get: function() {
* return Object.keys(this).length;
* }
* });
* var obj = {};
* cu.copyDescriptor(obj, App.prototype, 'count');
* ```
* @param {Object} `receiver`
* @param {Object} `provider`
* @param {String} `name`
* @return {Object}
* @api public
*/
cu.copyDescriptor = function copyDescriptor(receiver, provider, name) {
if (!cu.isObject(receiver)) {
throw new TypeError('expected receiving object to be an object.');
}
if (!cu.isObject(provider)) {
throw new TypeError('expected providing object to be an object.');
}
if (typeof name !== 'string') {
throw new TypeError('expected name to be a string.');
}
var val = cu.getDescriptor(provider, name);
if (val) Object.defineProperty(receiver, name, val);
};
/**
* Copy static properties, prototype properties, and descriptors
* from one object to another.
*
* @param {Object} `receiver`
* @param {Object} `provider`
* @param {String|Array} `omit` One or more properties to omit
* @return {Object}
* @api public
*/
cu.copy = function copy(receiver, provider, omit) {
if (!cu.isObject(receiver)) {
throw new TypeError('expected receiving object to be an object.');
}
if (!cu.isObject(provider)) {
throw new TypeError('expected providing object to be an object.');
}
var props = Object.getOwnPropertyNames(provider);
var keys = Object.keys(provider);
var len = props.length,
key;
omit = cu.arrayify(omit);
while (len--) {
key = props[len];
if (cu.has(keys, key)) {
utils.define(receiver, key, provider[key]);
} else if (!(key in receiver) && !cu.has(omit, key)) {
cu.copyDescriptor(receiver, provider, key);
}
}
};
/**
* Inherit the static properties, prototype properties, and descriptors
* from of an object.
*
* @param {Object} `receiver`
* @param {Object} `provider`
* @param {String|Array} `omit` One or more properties to omit
* @return {Object}
* @api public
*/
cu.inherit = function inherit(receiver, provider, omit) {
if (!cu.isObject(receiver)) {
throw new TypeError('expected receiving object to be an object.');
}
if (!cu.isObject(provider)) {
throw new TypeError('expected providing object to be an object.');
}
var keys = [];
for (var key in provider) {
keys.push(key);
receiver[key] = provider[key];
}
keys = keys.concat(cu.arrayify(omit));
var a = provider.prototype || provider;
var b = receiver.prototype || receiver;
cu.copy(b, a, keys);
};
/**
* Returns a function for extending the static properties,
* prototype properties, and descriptors from the `Parent`
* constructor onto `Child` constructors.
*
* ```js
* var extend = cu.extend(Parent);
* Parent.extend(Child);
*
* // optional methods
* Parent.extend(Child, {
* foo: function() {},
* bar: function() {}
* });
* ```
* @param {Function} `Parent` Parent ctor
* @param {Function} `extend` Optional extend function to handle custom extensions. Useful when updating methods that require a specific prototype.
* @param {Function} `Child` Child ctor
* @param {Object} `proto` Optionally pass additional prototype properties to inherit.
* @return {Object}
* @api public
*/
cu.extend = function() {
// keep it lazy, instead of assigning to `cu.extend`
return utils.staticExtend.apply(null, arguments);
};
/**
* Bubble up events emitted from static methods on the Parent ctor.
*
* @param {Object} `Parent`
* @param {Array} `events` Event names to bubble up
* @api public
*/
cu.bubble = function(Parent, events) {
events = events || [];
Parent.bubble = function(Child, arr) {
if (Array.isArray(arr)) {
events = utils.union([], events, arr);
}
var len = events.length;
var idx = -1;
while (++idx < len) {
var name = events[idx];
Parent.on(name, Child.emit.bind(Child, name));
}
cu.bubble(Child, events);
};
};
|