dotfiles/vscode/.vscode/extensions/randomfractalsinc.vscode-data-preview-2.3.0/node_modules/object-iterators/index.js
Errol Sancaktar ff17c17e23 vscode
2024-06-14 09:31:58 -06:00

207 lines
10 KiB
JavaScript

/* object-iterators.js - Mini Underscore library
* by Jonathan Eiten
*
* The methods below operate on objects (but not arrays) similarly
* to Underscore (http://underscorejs.org/#collections).
*
* For more information:
* https://github.com/joneit/object-iterators
*/
'use strict';
/**
* @constructor
* @summary Wrap an object for one method call.
* @Desc Note that the `new` keyword is not necessary.
* @param {object|null|undefined} object - `null` or `undefined` is treated as an empty plain object.
* @return {Wrapper} The wrapped object.
*/
function Wrapper(object) {
if (object instanceof Wrapper) {
return object;
}
if (!(this instanceof Wrapper)) {
return new Wrapper(object);
}
this.originalValue = object;
this.o = object || {};
}
/**
* @name Wrapper.chain
* @summary Wrap an object for a chain of method calls.
* @Desc Calls the constructor `Wrapper()` and modifies the wrapper for chaining.
* @param {object} object
* @return {Wrapper} The wrapped object.
*/
Wrapper.chain = function (object) {
var wrapped = Wrapper(object); // eslint-disable-line new-cap
wrapped.chaining = true;
return wrapped;
};
Wrapper.prototype = {
/**
* Unwrap an object wrapped with {@link Wrapper.chain|Wrapper.chain()}.
* @return {object|null|undefined} The value originally wrapped by the constructor.
* @memberOf Wrapper.prototype
*/
value: function () {
return this.originalValue;
},
/**
* @desc Mimics Underscore's [each](http://underscorejs.org/#each) method: Iterate over the members of the wrapped object, calling `iteratee()` with each.
* @param {function} iteratee - For each member of the wrapped object, this function is called with three arguments: `(value, key, object)`. The return value of this function is undefined; an `.each` loop cannot be broken out of (use {@link Wrapper#find|.find} instead).
* @param {object} [context] - If given, `iteratee` is bound to this object. In other words, this object becomes the `this` value in the calls to `iteratee`. (Otherwise, the `this` value will be the unwrapped object.)
* @return {Wrapper} The wrapped object for chaining.
* @memberOf Wrapper.prototype
*/
each: function (iteratee, context) {
var o = this.o;
Object.keys(o).forEach(function (key) {
iteratee.call(this, o[key], key, o);
}, context || o);
return this;
},
/**
* @desc Mimics Underscore's [find](http://underscorejs.org/#find) method: Look through each member of the wrapped object, returning the first one that passes a truth test (`predicate`), or `undefined` if no value passes the test. The function returns the value of the first acceptable member, and doesn't necessarily traverse the entire object.
* @param {function} predicate - For each member of the wrapped object, this function is called with three arguments: `(value, key, object)`. The return value of this function should be truthy if the member passes the test and falsy otherwise.
* @param {object} [context] - If given, `predicate` is bound to this object. In other words, this object becomes the `this` value in the calls to `predicate`. (Otherwise, the `this` value will be the unwrapped object.)
* @return {*} The found property's value, or undefined if not found.
* @memberOf Wrapper.prototype
*/
find: function (predicate, context) {
var o = this.o;
var result;
if (o) {
result = Object.keys(o).find(function (key) {
return predicate.call(this, o[key], key, o);
}, context || o);
if (result !== undefined) {
result = o[result];
}
}
return result;
},
/**
* @desc Mimics Underscore's [filter](http://underscorejs.org/#filter) method: Look through each member of the wrapped object, returning the values of all members that pass a truth test (`predicate`), or empty array if no value passes the test. The function always traverses the entire object.
* @param {function} predicate - For each member of the wrapped object, this function is called with three arguments: `(value, key, object)`. The return value of this function should be truthy if the member passes the test and falsy otherwise.
* @param {object} [context] - If given, `predicate` is bound to this object. In other words, this object becomes the `this` value in the calls to `predicate`. (Otherwise, the `this` value will be the unwrapped object.)
* @return {*} An array containing the filtered values.
* @memberOf Wrapper.prototype
*/
filter: function (predicate, context) {
var o = this.o;
var result = [];
if (o) {
Object.keys(o).forEach(function (key) {
if (predicate.call(this, o[key], key, o)) {
result.push(o[key]);
}
}, context || o);
}
return result;
},
/**
* @desc Mimics Underscore's [map](http://underscorejs.org/#map) method: Produces a new array of values by mapping each value in list through a transformation function (`iteratee`). The function always traverses the entire object.
* @param {function} iteratee - For each member of the wrapped object, this function is called with three arguments: `(value, key, object)`. The return value of this function is concatenated to the end of the new array.
* @param {object} [context] - If given, `iteratee` is bound to this object. In other words, this object becomes the `this` value in the calls to `predicate`. (Otherwise, the `this` value will be the unwrapped object.)
* @return {*} An array containing the filtered values.
* @memberOf Wrapper.prototype
*/
map: function (iteratee, context) {
var o = this.o;
var result = [];
if (o) {
Object.keys(o).forEach(function (key) {
result.push(iteratee.call(this, o[key], key, o));
}, context || o);
}
return result;
},
/**
* @desc Mimics Underscore's [reduce](http://underscorejs.org/#reduce) method: Boil down the values of all the members of the wrapped object into a single value. `memo` is the initial state of the reduction, and each successive step of it should be returned by `iteratee()`.
* @param {function} iteratee - For each member of the wrapped object, this function is called with four arguments: `(memo, value, key, object)`. The return value of this function becomes the new value of `memo` for the next iteration.
* @param {*} [memo] - If no memo is passed to the initial invocation of reduce, the iteratee is not invoked on the first element of the list. The first element is instead passed as the memo in the invocation of the iteratee on the next element in the list.
* @param {object} [context] - If given, `iteratee` is bound to this object. In other words, this object becomes the `this` value in the calls to `iteratee`. (Otherwise, the `this` value will be the unwrapped object.)
* @return {*} The value of `memo` "reduced" as per `iteratee`.
* @memberOf Wrapper.prototype
*/
reduce: function (iteratee, memo, context) {
var o = this.o;
if (o) {
Object.keys(o).forEach(function (key, idx) {
memo = (!idx && memo === undefined) ? o[key] : iteratee(memo, o[key], key, o);
}, context || o);
}
return memo;
},
/**
* @desc Mimics Underscore's [extend](http://underscorejs.org/#extend) method: Copy all of the properties in each of the `source` object parameter(s) over to the (wrapped) destination object (thus mutating it). It's in-order, so the properties of the last `source` object will override properties with the same name in previous arguments or in the destination object.
* > This method copies own members as well as members inherited from prototype chain.
* @param {...object|null|undefined} source - Values of `null` or `undefined` are treated as empty plain objects.
* @return {Wrapper|object} The wrapped destination object if chaining is in effect; otherwise the unwrapped destination object.
* @memberOf Wrapper.prototype
*/
extend: function (source) {
var o = this.o;
Array.prototype.slice.call(arguments).forEach(function (object) {
if (object) {
for (var key in object) {
o[key] = object[key];
}
}
});
return this.chaining ? this : o;
},
/**
* @desc Mimics Underscore's [extendOwn](http://underscorejs.org/#extendOwn) method: Like {@link Wrapper#extend|extend}, but only copies its "own" properties over to the destination object.
* @param {...object|null|undefined} source - Values of `null` or `undefined` are treated as empty plain objects.
* @return {Wrapper|object} The wrapped destination object if chaining is in effect; otherwise the unwrapped destination object.
* @memberOf Wrapper.prototype
*/
extendOwn: function (source) {
var o = this.o;
Array.prototype.slice.call(arguments).forEach(function (object) {
Wrapper(object).each(function (val, key) { // eslint-disable-line new-cap
o[key] = val;
});
});
return this.chaining ? this : o;
}
};
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
if (!Array.prototype.find) {
Array.prototype.find = function (predicate) { // eslint-disable-line no-extend-native
if (this === null) {
throw new TypeError('Array.prototype.find called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return value;
}
}
return undefined;
};
}
module.exports = Wrapper;