Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statistic Module added #243

Closed
wants to merge 15 commits into from
17 changes: 17 additions & 0 deletions modules/_baseIteratee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import identity from './identity.js';
import isFunction from './isFunction.js';
import isObject from './isObject.js';
import isArray from './isArray.js';
import matcher from './matcher.js';
import property from './property.js';
import optimizeCb from './_optimizeCb.js';

// An internal function to generate callbacks that can be applied to each
// element in a collection, returning the desired result — either `_.identity`,
// an arbitrary callback, a property matcher, or a property accessor.
export default function baseIteratee(value, context, argCount) {
if (value == null) return identity;
if (isFunction(value)) return optimizeCb(value, context, argCount);
if (isObject(value) && !isArray(value)) return matcher(value);
return property(value);
}
10 changes: 10 additions & 0 deletions modules/_cb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import _ from './underscore.js';
import baseIteratee from './_baseIteratee.js';
import iteratee from './iteratee.js';

// The function we call internally to generate a callback. It invokes
// `_.iteratee` if overridden, otherwise `baseIteratee`.
export default function cb(value, context, argCount) {
if (_.iteratee !== iteratee) return _.iteratee(value, context);
return baseIteratee(value, context, argCount);
}
40 changes: 40 additions & 0 deletions modules/_collectNonEnumProps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { nonEnumerableProps, ObjProto } from './_setup.js';
import isFunction from './isFunction.js';
import has from './_has.js';

// Internal helper to create a simple lookup structure.
// `collectNonEnumProps` used to depend on `_.contains`, but this led to
// circular imports. `emulatedSet` is a one-off solution that only works for
// arrays of strings.
function emulatedSet(keys) {
var hash = {};
for (var l = keys.length, i = 0; i < l; ++i) hash[keys[i]] = true;
return {
contains: function(key) { return hash[key]; },
push: function(key) {
hash[key] = true;
return keys.push(key);
}
};
}

// Internal helper. Checks `keys` for the presence of keys in IE < 9 that won't
// be iterated by `for key in ...` and thus missed. Extends `keys` in place if
// needed.
export default function collectNonEnumProps(obj, keys) {
keys = emulatedSet(keys);
var nonEnumIdx = nonEnumerableProps.length;
var constructor = obj.constructor;
var proto = isFunction(constructor) && constructor.prototype || ObjProto;

// Constructor is a special case.
var prop = 'constructor';
if (has(obj, prop) && !keys.contains(prop)) keys.push(prop);

while (nonEnumIdx--) {
prop = nonEnumerableProps[nonEnumIdx];
if (prop in obj && obj[prop] !== proto[prop] && !keys.contains(prop)) {
keys.push(prop);
}
}
}
18 changes: 18 additions & 0 deletions modules/_createAssigner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// An internal function for creating assigner functions.
export default function createAssigner(keysFunc, defaults) {
return function(obj) {
var length = arguments.length;
if (defaults) obj = Object(obj);
if (length < 2 || obj == null) return obj;
for (var index = 1; index < length; index++) {
var source = arguments[index],
keys = keysFunc(source),
l = keys.length;
for (var i = 0; i < l; i++) {
var key = keys[i];
if (!defaults || obj[key] === void 0) obj[key] = source[key];
}
}
return obj;
};
}
15 changes: 15 additions & 0 deletions modules/_createPredicateIndexFinder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import cb from './_cb.js';
import getLength from './_getLength.js';

// Internal function to generate `_.findIndex` and `_.findLastIndex`.
export default function createPredicateIndexFinder(dir) {
return function(array, predicate, context) {
predicate = cb(predicate, context);
var length = getLength(array);
var index = dir > 0 ? 0 : length - 1;
for (; index >= 0 && index < length; index += dir) {
if (predicate(array[index], index, array)) return index;
}
return -1;
};
}
9 changes: 9 additions & 0 deletions modules/_createSizePropertyCheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { MAX_ARRAY_INDEX } from './_setup.js';

// Common internal logic for `isArrayLike` and `isBufferLike`.
export default function createSizePropertyCheck(getSizeProperty) {
return function(collection) {
var sizeProperty = getSizeProperty(collection);
return typeof sizeProperty == 'number' && sizeProperty >= 0 && sizeProperty <= MAX_ARRAY_INDEX;
}
}
9 changes: 9 additions & 0 deletions modules/_deepGet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Internal function to obtain a nested property in `obj` along `path`.
export default function deepGet(obj, path) {
var length = path.length;
for (var i = 0; i < length; i++) {
if (obj == null) return void 0;
obj = obj[path[i]];
}
return length ? obj : void 0;
}
4 changes: 4 additions & 0 deletions modules/_getLength.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import shallowProperty from './_shallowProperty.js';

// Internal helper to obtain the `length` property of an object.
export default shallowProperty('length');
15 changes: 15 additions & 0 deletions modules/_group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import cb from './_cb.js';
import each from './each.js';

// An internal function used for aggregate "group by" operations.
export default function group(behavior, partition) {
return function(obj, iteratee, context) {
var result = partition ? [[], []] : {};
iteratee = cb(iteratee, context);
each(obj, function(value, index) {
var key = iteratee(value, index, obj);
behavior(result, value, key);
});
return result;
};
}
6 changes: 6 additions & 0 deletions modules/_has.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { hasOwnProperty } from './_setup.js';

// Internal function to check whether `key` is an own property name of `obj`.
export default function has(obj, key) {
return obj != null && hasOwnProperty.call(obj, key);
}
8 changes: 8 additions & 0 deletions modules/_isArrayLike.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import createSizePropertyCheck from './_createSizePropertyCheck.js';
import getLength from './_getLength.js';

// Internal helper for collection methods to determine whether a collection
// should be iterated as an array or as an object.
// Related: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength
// Avoids a very nasty iOS 8 JIT bug on ARM-64. #2094
export default createSizePropertyCheck(getLength);
21 changes: 21 additions & 0 deletions modules/_optimizeCb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Internal function that returns an efficient (for current engines) version
// of the passed-in callback, to be repeatedly applied in other Underscore
// functions.
export default function optimizeCb(func, context, argCount) {
if (context === void 0) return func;
switch (argCount == null ? 3 : argCount) {
case 1: return function(value) {
return func.call(context, value);
};
// The 2-argument case is omitted because we’re not using it.
case 3: return function(value, index, collection) {
return func.call(context, value, index, collection);
};
case 4: return function(accumulator, value, index, collection) {
return func.call(context, accumulator, value, index, collection);
};
}
return function() {
return func.apply(context, arguments);
};
}
43 changes: 43 additions & 0 deletions modules/_setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Current version.
export var VERSION = '1.13.0';

// Establish the root object, `window` (`self`) in the browser, `global`
// on the server, or `this` in some virtual machines. We use `self`
// instead of `window` for `WebWorker` support.
export var root = typeof self == 'object' && self.self === self && self ||
typeof global == 'object' && global.global === global && global ||
Function('return this')() ||
{};

// Save bytes in the minified (but not gzipped) version:
export var ArrayProto = Array.prototype, ObjProto = Object.prototype;
export var SymbolProto = typeof Symbol !== 'undefined' ? Symbol.prototype : null;

// Create quick reference variables for speed access to core prototypes.
export var push = ArrayProto.push,
slice = ArrayProto.slice,
toString = ObjProto.toString,
hasOwnProperty = ObjProto.hasOwnProperty;

// Modern feature detection.
export var supportsArrayBuffer = typeof ArrayBuffer !== 'undefined',
supportsDataView = typeof DataView !== 'undefined';

// All **ECMAScript 5+** native function implementations that we hope to use
// are declared here.
export var nativeIsArray = Array.isArray,
nativeKeys = Object.keys,
nativeCreate = Object.create,
nativeIsView = supportsArrayBuffer && ArrayBuffer.isView;

// Create references to these builtin functions because we override them.
export var _isNaN = isNaN,
_isFinite = isFinite;

// Keys in IE < 9 that won't be iterated by `for key in ...` and thus missed.
export var hasEnumBug = !{toString: null}.propertyIsEnumerable('toString');
export var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString',
'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];

// The largest integer that can be represented exactly.
export var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1;
6 changes: 6 additions & 0 deletions modules/_shallowProperty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Internal helper to generate a function to obtain property `key` from `obj`.
export default function shallowProperty(key) {
return function(obj) {
return obj == null ? void 0 : obj[key];
};
}
9 changes: 9 additions & 0 deletions modules/_tagTester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { toString } from './_setup.js';

// Internal function for creating a `toString`-based type tester.
export default function tagTester(name) {
var tag = '[object ' + name + ']';
return function(obj) {
return toString.call(obj) === tag;
};
}
8 changes: 8 additions & 0 deletions modules/_toPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import _ from './underscore.js';
import './toPath.js';

// Internal wrapper for `_.toPath` to enable minification.
// Similar to `cb` for `_.iteratee`.
export default function toPath(path) {
return _.toPath(path);
}
23 changes: 23 additions & 0 deletions modules/each.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import optimizeCb from './_optimizeCb.js';
import isArrayLike from './_isArrayLike.js';
import keys from './keys.js';

// The cornerstone for collection functions, an `each`
// implementation, aka `forEach`.
// Handles raw objects in addition to array-likes. Treats all
// sparse array-likes as if they were dense.
export default function each(obj, iteratee, context) {
iteratee = optimizeCb(iteratee, context);
var i, length;
if (isArrayLike(obj)) {
for (i = 0, length = obj.length; i < length; i++) {
iteratee(obj[i], i, obj);
}
} else {
var _keys = keys(obj);
for (i = 0, length = _keys.length; i < length; i++) {
iteratee(obj[_keys[i]], _keys[i], obj);
}
}
return obj;
}
7 changes: 7 additions & 0 deletions modules/extendOwn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import createAssigner from './_createAssigner.js';
import keys from './keys.js';

// Assigns a given object with all the own properties in the passed-in
// object(s).
// (https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
export default createAssigner(keys);
10 changes: 10 additions & 0 deletions modules/find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import isArrayLike from './_isArrayLike.js';
import findIndex from './findIndex.js';
import findKey from './findKey.js';

// Return the first value which passes a truth test.
export default function find(obj, predicate, context) {
var keyFinder = isArrayLike(obj) ? findIndex : findKey;
var key = keyFinder(obj, predicate, context);
if (key !== void 0 && key !== -1) return obj[key];
}
4 changes: 4 additions & 0 deletions modules/findIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import createPredicateIndexFinder from './_createPredicateIndexFinder.js';

// Returns the first index on an array-like that passes a truth test.
export default createPredicateIndexFinder(1);
12 changes: 12 additions & 0 deletions modules/findKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import cb from './_cb.js';
import keys from './keys.js';

// Returns the first key on an object that passes a truth test.
export default function findKey(obj, predicate, context) {
predicate = cb(predicate, context);
var _keys = keys(obj), key;
for (var i = 0, length = _keys.length; i < length; i++) {
key = _keys[i];
if (predicate(obj[key], key, obj)) return key;
}
}
9 changes: 9 additions & 0 deletions modules/first.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import initial from './initial.js';

// Get the first element of an array. Passing **n** will return the first N
// values in the array. The **guard** check allows it to work with `_.map`.
export default function first(array, n, guard) {
if (array == null || array.length < 1) return n == null || guard ? void 0 : [];
if (n == null || guard) return array[0];
return initial(array, array.length - n);
}
8 changes: 8 additions & 0 deletions modules/groupBy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import group from './_group.js';
import has from './_has.js';

// Groups the object's values by a criterion. Pass either a string attribute
// to group by, or a function that returns the criterion.
export default group(function(result, value, key) {
if (has(result, key)) result[key].push(value); else result[key] = [value];
});
4 changes: 4 additions & 0 deletions modules/identity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Keep the identity function around for default iteratees.
export default function identity(value) {
return value;
}
10 changes: 10 additions & 0 deletions modules/index.collections.statistics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//Statistical Function
export { default as sum } from './sum.js';
export { default as mean } from './mean.js';
export { default as median } from './median.js';
export { default as standardDeviation } from './standardDeviation.js';
export { default as variance } from './variance.js';
export { default as mode } from './mode.js';
export { default as standardError } from './standardError.js';
export { default as statRange } from './statRange.js';
export { default as percentile } from './percentile.js';
8 changes: 8 additions & 0 deletions modules/initial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { slice } from './_setup.js';

// Returns everything but the last entry of the array. Especially useful on
// the arguments object. Passing **n** will return all the values in
// the array, excluding the last N.
export default function initial(array, n, guard) {
return slice.call(array, 0, Math.max(0, array.length - (n == null || guard ? 1 : n)));
}
16 changes: 16 additions & 0 deletions modules/isArguments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import tagTester from './_tagTester.js';
import has from './_has.js';

var isArguments = tagTester('Arguments');

// Define a fallback version of the method in browsers (ahem, IE < 9), where
// there isn't any inspectable "Arguments" type.
(function() {
if (!isArguments(arguments)) {
isArguments = function(obj) {
return has(obj, 'callee');
};
}
}());

export default isArguments;
6 changes: 6 additions & 0 deletions modules/isArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { nativeIsArray } from './_setup.js';
import tagTester from './_tagTester.js';

// Is a given value an array?
// Delegates to ECMA5's native `Array.isArray`.
export default nativeIsArray || tagTester('Array');
Loading