From d7d95a2c26dcc8aa9fc7d7b4bf409c27aa6511df Mon Sep 17 00:00:00 2001 From: in0068 Date: Thu, 15 Apr 2021 15:39:07 +0530 Subject: [PATCH 01/13] Statistic Module added --- modules/_baseIteratee.js | 17 +++++ modules/_cb.js | 10 +++ modules/_collectNonEnumProps.js | 40 ++++++++++++ modules/_createAssigner.js | 18 +++++ modules/_createSizePropertyCheck.js | 9 +++ modules/_deepGet.js | 9 +++ modules/_getLength.js | 4 ++ modules/_group.js | 15 +++++ modules/_has.js | 6 ++ modules/_isArrayLike.js | 8 +++ modules/_optimizeCb.js | 21 ++++++ modules/_setup.js | 43 ++++++++++++ modules/_shallowProperty.js | 6 ++ modules/_tagTester.js | 9 +++ modules/_toPath.js | 8 +++ modules/each.js | 23 +++++++ modules/extendOwn.js | 7 ++ modules/first.js | 9 +++ modules/groupBy.js | 8 +++ modules/identity.js | 4 ++ modules/index.collections.statistics.js | 10 +++ modules/initial.js | 8 +++ modules/isArguments.js | 16 +++++ modules/isArray.js | 6 ++ modules/isEmpty.js | 18 +++++ modules/isFunction.js | 15 +++++ modules/isMatch.js | 13 ++++ modules/isObject.js | 5 ++ modules/isString.js | 3 + modules/iteratee.js | 10 +++ modules/keys.js | 16 +++++ modules/map.js | 16 +++++ modules/matcher.js | 11 ++++ modules/max.js | 29 +++++++++ modules/mean.js | 11 ++++ modules/median.js | 19 ++++++ modules/min.js | 29 +++++++++ modules/mode.js | 16 +++++ modules/percentile.js | 21 ++++++ modules/pluck.js | 7 ++ modules/property.js | 11 ++++ modules/size.js | 8 +++ modules/sortBy.js | 24 +++++++ modules/standardDeviation.js | 7 ++ modules/standardError.js | 8 +++ modules/statRange.js | 6 ++ modules/toPath.js | 9 +++ modules/underscore.js | 25 +++++++ modules/values.js | 12 ++++ modules/variance.js | 22 +++++++ test/collections.statistics.js | 87 +++++++++++++++++++++++++ 51 files changed, 772 insertions(+) create mode 100644 modules/_baseIteratee.js create mode 100644 modules/_cb.js create mode 100644 modules/_collectNonEnumProps.js create mode 100644 modules/_createAssigner.js create mode 100644 modules/_createSizePropertyCheck.js create mode 100644 modules/_deepGet.js create mode 100644 modules/_getLength.js create mode 100644 modules/_group.js create mode 100644 modules/_has.js create mode 100644 modules/_isArrayLike.js create mode 100644 modules/_optimizeCb.js create mode 100644 modules/_setup.js create mode 100644 modules/_shallowProperty.js create mode 100644 modules/_tagTester.js create mode 100644 modules/_toPath.js create mode 100644 modules/each.js create mode 100644 modules/extendOwn.js create mode 100644 modules/first.js create mode 100644 modules/groupBy.js create mode 100644 modules/identity.js create mode 100644 modules/index.collections.statistics.js create mode 100644 modules/initial.js create mode 100644 modules/isArguments.js create mode 100644 modules/isArray.js create mode 100644 modules/isEmpty.js create mode 100644 modules/isFunction.js create mode 100644 modules/isMatch.js create mode 100644 modules/isObject.js create mode 100644 modules/isString.js create mode 100644 modules/iteratee.js create mode 100644 modules/keys.js create mode 100644 modules/map.js create mode 100644 modules/matcher.js create mode 100644 modules/max.js create mode 100644 modules/mean.js create mode 100644 modules/median.js create mode 100644 modules/min.js create mode 100644 modules/mode.js create mode 100644 modules/percentile.js create mode 100644 modules/pluck.js create mode 100644 modules/property.js create mode 100644 modules/size.js create mode 100644 modules/sortBy.js create mode 100644 modules/standardDeviation.js create mode 100644 modules/standardError.js create mode 100644 modules/statRange.js create mode 100644 modules/toPath.js create mode 100644 modules/underscore.js create mode 100644 modules/values.js create mode 100644 modules/variance.js create mode 100644 test/collections.statistics.js diff --git a/modules/_baseIteratee.js b/modules/_baseIteratee.js new file mode 100644 index 0000000..c276ebe --- /dev/null +++ b/modules/_baseIteratee.js @@ -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); +} diff --git a/modules/_cb.js b/modules/_cb.js new file mode 100644 index 0000000..9b8b555 --- /dev/null +++ b/modules/_cb.js @@ -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); +} diff --git a/modules/_collectNonEnumProps.js b/modules/_collectNonEnumProps.js new file mode 100644 index 0000000..18a2af0 --- /dev/null +++ b/modules/_collectNonEnumProps.js @@ -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); + } + } +} diff --git a/modules/_createAssigner.js b/modules/_createAssigner.js new file mode 100644 index 0000000..b102393 --- /dev/null +++ b/modules/_createAssigner.js @@ -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; + }; +} diff --git a/modules/_createSizePropertyCheck.js b/modules/_createSizePropertyCheck.js new file mode 100644 index 0000000..cc38007 --- /dev/null +++ b/modules/_createSizePropertyCheck.js @@ -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; + } +} diff --git a/modules/_deepGet.js b/modules/_deepGet.js new file mode 100644 index 0000000..42bbec3 --- /dev/null +++ b/modules/_deepGet.js @@ -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; +} diff --git a/modules/_getLength.js b/modules/_getLength.js new file mode 100644 index 0000000..090b156 --- /dev/null +++ b/modules/_getLength.js @@ -0,0 +1,4 @@ +import shallowProperty from './_shallowProperty.js'; + +// Internal helper to obtain the `length` property of an object. +export default shallowProperty('length'); diff --git a/modules/_group.js b/modules/_group.js new file mode 100644 index 0000000..8fdd985 --- /dev/null +++ b/modules/_group.js @@ -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; + }; +} diff --git a/modules/_has.js b/modules/_has.js new file mode 100644 index 0000000..0636181 --- /dev/null +++ b/modules/_has.js @@ -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); +} diff --git a/modules/_isArrayLike.js b/modules/_isArrayLike.js new file mode 100644 index 0000000..a87fe48 --- /dev/null +++ b/modules/_isArrayLike.js @@ -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); diff --git a/modules/_optimizeCb.js b/modules/_optimizeCb.js new file mode 100644 index 0000000..59e40e6 --- /dev/null +++ b/modules/_optimizeCb.js @@ -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); + }; +} diff --git a/modules/_setup.js b/modules/_setup.js new file mode 100644 index 0000000..2c2b9be --- /dev/null +++ b/modules/_setup.js @@ -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; diff --git a/modules/_shallowProperty.js b/modules/_shallowProperty.js new file mode 100644 index 0000000..00bf090 --- /dev/null +++ b/modules/_shallowProperty.js @@ -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]; + }; +} diff --git a/modules/_tagTester.js b/modules/_tagTester.js new file mode 100644 index 0000000..8d417dd --- /dev/null +++ b/modules/_tagTester.js @@ -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; + }; +} diff --git a/modules/_toPath.js b/modules/_toPath.js new file mode 100644 index 0000000..fad5150 --- /dev/null +++ b/modules/_toPath.js @@ -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); +} diff --git a/modules/each.js b/modules/each.js new file mode 100644 index 0000000..d050200 --- /dev/null +++ b/modules/each.js @@ -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; +} diff --git a/modules/extendOwn.js b/modules/extendOwn.js new file mode 100644 index 0000000..5338451 --- /dev/null +++ b/modules/extendOwn.js @@ -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); diff --git a/modules/first.js b/modules/first.js new file mode 100644 index 0000000..3b6685e --- /dev/null +++ b/modules/first.js @@ -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); +} diff --git a/modules/groupBy.js b/modules/groupBy.js new file mode 100644 index 0000000..2670958 --- /dev/null +++ b/modules/groupBy.js @@ -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]; +}); diff --git a/modules/identity.js b/modules/identity.js new file mode 100644 index 0000000..6df631c --- /dev/null +++ b/modules/identity.js @@ -0,0 +1,4 @@ +// Keep the identity function around for default iteratees. +export default function identity(value) { + return value; +} diff --git a/modules/index.collections.statistics.js b/modules/index.collections.statistics.js new file mode 100644 index 0000000..bddf932 --- /dev/null +++ b/modules/index.collections.statistics.js @@ -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'; \ No newline at end of file diff --git a/modules/initial.js b/modules/initial.js new file mode 100644 index 0000000..0b991dc --- /dev/null +++ b/modules/initial.js @@ -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))); +} diff --git a/modules/isArguments.js b/modules/isArguments.js new file mode 100644 index 0000000..61582bf --- /dev/null +++ b/modules/isArguments.js @@ -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; diff --git a/modules/isArray.js b/modules/isArray.js new file mode 100644 index 0000000..7ead47d --- /dev/null +++ b/modules/isArray.js @@ -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'); diff --git a/modules/isEmpty.js b/modules/isEmpty.js new file mode 100644 index 0000000..718ef4a --- /dev/null +++ b/modules/isEmpty.js @@ -0,0 +1,18 @@ +import getLength from './_getLength.js'; +import isArray from './isArray.js'; +import isString from './isString.js'; +import isArguments from './isArguments.js'; +import keys from './keys.js'; + +// Is a given array, string, or object empty? +// An "empty" object has no enumerable own-properties. +export default function isEmpty(obj) { + if (obj == null) return true; + // Skip the more expensive `toString`-based type checks if `obj` has no + // `.length`. + var length = getLength(obj); + if (typeof length == 'number' && ( + isArray(obj) || isString(obj) || isArguments(obj) + )) return length === 0; + return getLength(keys(obj)) === 0; +} diff --git a/modules/isFunction.js b/modules/isFunction.js new file mode 100644 index 0000000..35c41be --- /dev/null +++ b/modules/isFunction.js @@ -0,0 +1,15 @@ +import tagTester from './_tagTester.js'; +import { root } from './_setup.js'; + +var isFunction = tagTester('Function'); + +// Optimize `isFunction` if appropriate. Work around some `typeof` bugs in old +// v8, IE 11 (#1621), Safari 8 (#1929), and PhantomJS (#2236). +var nodelist = root.document && root.document.childNodes; +if (typeof /./ != 'function' && typeof Int8Array != 'object' && typeof nodelist != 'function') { + isFunction = function(obj) { + return typeof obj == 'function' || false; + }; +} + +export default isFunction; diff --git a/modules/isMatch.js b/modules/isMatch.js new file mode 100644 index 0000000..81e43d9 --- /dev/null +++ b/modules/isMatch.js @@ -0,0 +1,13 @@ +import keys from './keys.js'; + +// Returns whether an object has a given set of `key:value` pairs. +export default function isMatch(object, attrs) { + var _keys = keys(attrs), length = _keys.length; + if (object == null) return !length; + var obj = Object(object); + for (var i = 0; i < length; i++) { + var key = _keys[i]; + if (attrs[key] !== obj[key] || !(key in obj)) return false; + } + return true; +} diff --git a/modules/isObject.js b/modules/isObject.js new file mode 100644 index 0000000..73230f0 --- /dev/null +++ b/modules/isObject.js @@ -0,0 +1,5 @@ +// Is a given variable an object? +export default function isObject(obj) { + var type = typeof obj; + return type === 'function' || type === 'object' && !!obj; +} diff --git a/modules/isString.js b/modules/isString.js new file mode 100644 index 0000000..f02707d --- /dev/null +++ b/modules/isString.js @@ -0,0 +1,3 @@ +import tagTester from './_tagTester.js'; + +export default tagTester('String'); diff --git a/modules/iteratee.js b/modules/iteratee.js new file mode 100644 index 0000000..9057701 --- /dev/null +++ b/modules/iteratee.js @@ -0,0 +1,10 @@ +import _ from './underscore.js'; +import baseIteratee from './_baseIteratee.js'; + +// External wrapper for our callback generator. Users may customize +// `_.iteratee` if they want additional predicate/iteratee shorthand styles. +// This abstraction hides the internal-only `argCount` argument. +export default function iteratee(value, context) { + return baseIteratee(value, context, Infinity); +} +_.iteratee = iteratee; diff --git a/modules/keys.js b/modules/keys.js new file mode 100644 index 0000000..f5b596c --- /dev/null +++ b/modules/keys.js @@ -0,0 +1,16 @@ +import isObject from './isObject.js'; +import { nativeKeys, hasEnumBug } from './_setup.js'; +import has from './_has.js'; +import collectNonEnumProps from './_collectNonEnumProps.js'; + +// Retrieve the names of an object's own properties. +// Delegates to **ECMAScript 5**'s native `Object.keys`. +export default function keys(obj) { + if (!isObject(obj)) return []; + if (nativeKeys) return nativeKeys(obj); + var keys = []; + for (var key in obj) if (has(obj, key)) keys.push(key); + // Ahem, IE < 9. + if (hasEnumBug) collectNonEnumProps(obj, keys); + return keys; +} diff --git a/modules/map.js b/modules/map.js new file mode 100644 index 0000000..a2e5121 --- /dev/null +++ b/modules/map.js @@ -0,0 +1,16 @@ +import cb from './_cb.js'; +import isArrayLike from './_isArrayLike.js'; +import keys from './keys.js'; + +// Return the results of applying the iteratee to each element. +export default function map(obj, iteratee, context) { + iteratee = cb(iteratee, context); + var _keys = !isArrayLike(obj) && keys(obj), + length = (_keys || obj).length, + results = Array(length); + for (var index = 0; index < length; index++) { + var currentKey = _keys ? _keys[index] : index; + results[index] = iteratee(obj[currentKey], currentKey, obj); + } + return results; +} diff --git a/modules/matcher.js b/modules/matcher.js new file mode 100644 index 0000000..245fa94 --- /dev/null +++ b/modules/matcher.js @@ -0,0 +1,11 @@ +import extendOwn from './extendOwn.js'; +import isMatch from './isMatch.js'; + +// Returns a predicate for checking whether an object has a given set of +// `key:value` pairs. +export default function matcher(attrs) { + attrs = extendOwn({}, attrs); + return function(obj) { + return isMatch(obj, attrs); + }; +} diff --git a/modules/max.js b/modules/max.js new file mode 100644 index 0000000..9873b35 --- /dev/null +++ b/modules/max.js @@ -0,0 +1,29 @@ +import isArrayLike from './_isArrayLike.js'; +import values from './values.js'; +import cb from './_cb.js'; +import each from './each.js'; + +// Return the maximum element (or element-based computation). +export default function max(obj, iteratee, context) { + var result = -Infinity, lastComputed = -Infinity, + value, computed; + if (iteratee == null || typeof iteratee == 'number' && typeof obj[0] != 'object' && obj != null) { + obj = isArrayLike(obj) ? obj : values(obj); + for (var i = 0, length = obj.length; i < length; i++) { + value = obj[i]; + if (value != null && value > result) { + result = value; + } + } + } else { + iteratee = cb(iteratee, context); + each(obj, function(v, index, list) { + computed = iteratee(v, index, list); + if (computed > lastComputed || computed === -Infinity && result === -Infinity) { + result = v; + lastComputed = computed; + } + }); + } + return result; +} diff --git a/modules/mean.js b/modules/mean.js new file mode 100644 index 0000000..df8209c --- /dev/null +++ b/modules/mean.js @@ -0,0 +1,11 @@ +import size from './size.js'; +import sum from './sum.js'; + +// Compute the average of the numbers obtained from the collection +export default function mean(collection, iteratee, context) { + var length = size(collection); + + if (length < 1) return 0; + + return sum(collection, iteratee, context) / length; +} diff --git a/modules/median.js b/modules/median.js new file mode 100644 index 0000000..921964f --- /dev/null +++ b/modules/median.js @@ -0,0 +1,19 @@ +import map from './map.js' +import isEmpty from './isEmpty'; + +// https://en.wikipedia.org/wiki/Median +// Calulation of median is done using the following method. +// If the array has odd numbers then median is the middle element. +// If the array has even numbers then average of middle two numbers is the median value. +export default function median(collection, iteratee, context) { + if (isEmpty(collection)) return undefined; + + if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') { + iteratee = null; + } + var tmpArr = map(collection, iteratee, context).sort(); + + return tmpArr.length % 2 ? + tmpArr[Math.floor(tmpArr.length / 2)] : + (tmpArr[tmpArr.length / 2 - 1] + tmpArr[tmpArr.length / 2]) / 2 +} diff --git a/modules/min.js b/modules/min.js new file mode 100644 index 0000000..32f92a0 --- /dev/null +++ b/modules/min.js @@ -0,0 +1,29 @@ +import isArrayLike from './_isArrayLike.js'; +import values from './values.js'; +import cb from './_cb.js'; +import each from './each.js'; + +// Return the minimum element (or element-based computation). +export default function min(obj, iteratee, context) { + var result = Infinity, lastComputed = Infinity, + value, computed; + if (iteratee == null || typeof iteratee == 'number' && typeof obj[0] != 'object' && obj != null) { + obj = isArrayLike(obj) ? obj : values(obj); + for (var i = 0, length = obj.length; i < length; i++) { + value = obj[i]; + if (value != null && value < result) { + result = value; + } + } + } else { + iteratee = cb(iteratee, context); + each(obj, function(v, index, list) { + computed = iteratee(v, index, list); + if (computed < lastComputed || computed === Infinity && result === Infinity) { + result = v; + lastComputed = computed; + } + }); + } + return result; +} diff --git a/modules/mode.js b/modules/mode.js new file mode 100644 index 0000000..1786bd0 --- /dev/null +++ b/modules/mode.js @@ -0,0 +1,16 @@ +import isEmpty from './isEmpty'; +import groupBy from './groupBy.js'; +import max from './max.js'; +import first from './first.js'; + +// Return the element (or element-based computation) that appears most frequently in the collection. +// https://en.wikipedia.org/wiki/Mode_(statistics) +export default function mode(collection, iteratee, context) { + if (isEmpty(collection)) return; + + if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') { + iteratee = null; + } + var groups = groupBy(collection, iteratee, context); + return first(max(groups, 'length')); +} diff --git a/modules/percentile.js b/modules/percentile.js new file mode 100644 index 0000000..f597af6 --- /dev/null +++ b/modules/percentile.js @@ -0,0 +1,21 @@ +import isEmpty from './isEmpty'; +import sortBy from './sortBy.js'; + +// Return the percentile value of the numeric elements from the collection +//Ex : 50th,75th,99th etc. +//https://en.wikipedia.org/wiki/Percentile +export default function percentile(collection, percentile) { + if (isEmpty(collection)) return 0; + if (typeof percentile !== 'number') throw new TypeError('Percentile must be a number between 0 - 100'); + if (percentile <= 0) return collection[0]; + if (percentile >= 100) return collection[collection.length - 1]; + + collection = sortBy(collection); + var index = (percentile/100) * (collection.length - 1), + lowerIndex = Math.floor(index), + upperIndex = lowerIndex + 1, + weight = index % 1; + + if (upperIndex >= collection.length) return collection[lowerIndex]; + return collection[lowerIndex] * (1 - weight) + collection[upperIndex] * weight; +} diff --git a/modules/pluck.js b/modules/pluck.js new file mode 100644 index 0000000..45a3533 --- /dev/null +++ b/modules/pluck.js @@ -0,0 +1,7 @@ +import map from './map.js'; +import property from './property.js'; + +// Convenience version of a common use case of `_.map`: fetching a property. +export default function pluck(obj, key) { + return map(obj, property(key)); +} diff --git a/modules/property.js b/modules/property.js new file mode 100644 index 0000000..4853866 --- /dev/null +++ b/modules/property.js @@ -0,0 +1,11 @@ +import deepGet from './_deepGet.js'; +import toPath from './_toPath.js'; + +// Creates a function that, when passed an object, will traverse that object’s +// properties down the given `path`, specified as an array of keys or indices. +export default function property(path) { + path = toPath(path); + return function(obj) { + return deepGet(obj, path); + }; +} diff --git a/modules/size.js b/modules/size.js new file mode 100644 index 0000000..4ce3714 --- /dev/null +++ b/modules/size.js @@ -0,0 +1,8 @@ +import isArrayLike from './_isArrayLike.js'; +import keys from './keys.js'; + +// Return the number of elements in a collection. +export default function size(obj) { + if (obj == null) return 0; + return isArrayLike(obj) ? obj.length : keys(obj).length; +} diff --git a/modules/sortBy.js b/modules/sortBy.js new file mode 100644 index 0000000..bca494b --- /dev/null +++ b/modules/sortBy.js @@ -0,0 +1,24 @@ +import cb from './_cb.js'; +import pluck from './pluck.js'; +import map from './map.js'; + +// Sort the object's values by a criterion produced by an iteratee. +export default function sortBy(obj, iteratee, context) { + var index = 0; + iteratee = cb(iteratee, context); + return pluck(map(obj, function(value, key, list) { + return { + value: value, + index: index++, + criteria: iteratee(value, key, list) + }; + }).sort(function(left, right) { + var a = left.criteria; + var b = right.criteria; + if (a !== b) { + if (a > b || a === void 0) return 1; + if (a < b || b === void 0) return -1; + } + return left.index - right.index; + }), 'value'); +} diff --git a/modules/standardDeviation.js b/modules/standardDeviation.js new file mode 100644 index 0000000..1994ecf --- /dev/null +++ b/modules/standardDeviation.js @@ -0,0 +1,7 @@ +import variance from './variance.js'; + +// Return the standardDeviation based on element-based computation. +// https://en.wikipedia.org/wiki/Standard_deviation +export default function standardDeviation(collection, iteratee, context) { + return Math.sqrt(variance(collection, iteratee, context)); +} diff --git a/modules/standardError.js b/modules/standardError.js new file mode 100644 index 0000000..9c02a30 --- /dev/null +++ b/modules/standardError.js @@ -0,0 +1,8 @@ +import variance from './variance.js'; +import size from './size.js'; + +// Return the standard error of the mean based on element-based computation. +// https://en.wikipedia.org/wiki/Standard_error +export default function standardError(collection, iteratee, context) { + return Math.sqrt(variance(collection, iteratee, context)/(size(collection) - 1)); +} diff --git a/modules/statRange.js b/modules/statRange.js new file mode 100644 index 0000000..cb90f7a --- /dev/null +++ b/modules/statRange.js @@ -0,0 +1,6 @@ +import max from './max.js'; +import min from './min.js'; + +export default function statRange(collection, iteratee, context){ + return max(collection, iteratee, context) - min(collection, iteratee, context); +} diff --git a/modules/toPath.js b/modules/toPath.js new file mode 100644 index 0000000..7d72d1f --- /dev/null +++ b/modules/toPath.js @@ -0,0 +1,9 @@ +import _ from './underscore.js'; +import isArray from './isArray.js'; + +// Normalize a (deep) property `path` to array. +// Like `_.iteratee`, this function can be customized. +export default function toPath(path) { + return isArray(path) ? path : [path]; +} +_.toPath = toPath; diff --git a/modules/underscore.js b/modules/underscore.js new file mode 100644 index 0000000..6029e2a --- /dev/null +++ b/modules/underscore.js @@ -0,0 +1,25 @@ +import { VERSION } from './_setup.js'; + +// If Underscore is called as a function, it returns a wrapped object that can +// be used OO-style. This wrapper holds altered versions of all functions added +// through `_.mixin`. Wrapped objects may be chained. +export default function _(obj) { + if (obj instanceof _) return obj; + if (!(this instanceof _)) return new _(obj); + this._wrapped = obj; +} + +_.VERSION = VERSION; + +// Extracts the result from a wrapped and chained object. +_.prototype.value = function() { + return this._wrapped; +}; + +// Provide unwrapping proxies for some methods used in engine operations +// such as arithmetic and JSON stringification. +_.prototype.valueOf = _.prototype.toJSON = _.prototype.value; + +_.prototype.toString = function() { + return String(this._wrapped); +}; diff --git a/modules/values.js b/modules/values.js new file mode 100644 index 0000000..9591de3 --- /dev/null +++ b/modules/values.js @@ -0,0 +1,12 @@ +import keys from './keys.js'; + +// Retrieve the values of an object's properties. +export default function values(obj) { + var _keys = keys(obj); + var length = _keys.length; + var values = Array(length); + for (var i = 0; i < length; i++) { + values[i] = obj[_keys[i]]; + } + return values; +} diff --git a/modules/variance.js b/modules/variance.js new file mode 100644 index 0000000..2a1098f --- /dev/null +++ b/modules/variance.js @@ -0,0 +1,22 @@ +import cb from './_cb.js'; +import mean from './mean.js'; + +// Return the variance of the numeric elements of the collection, +// optionally after transforming them through `iteratee`. +// https://en.wikipedia.org/wiki/Variance +export default function variance(collection, iteratee, context) { + if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') iteratee = null; + + iteratee = cb(iteratee, context); + + var computed = []; + var avg = mean(collection, function(value, key, collection) { + var result = iteratee(value, key, collection); + computed.push(result); + return result; + }); + return mean(computed, function(value) { + var difference = value - avg; + return difference * difference; + }); +} diff --git a/test/collections.statistics.js b/test/collections.statistics.js new file mode 100644 index 0000000..1d1fe5b --- /dev/null +++ b/test/collections.statistics.js @@ -0,0 +1,87 @@ +(function() { + var _ = typeof require == 'function' ? require('..') : window._; + QUnit.module('Statistics'); + QUnit.test('mean', function(assert) { + assert.strictEqual(_.mean(null), 0, 'can handle null/undefined'); + assert.strictEqual(_.mean(void 0), 0, 'can handle undefined'); + assert.strictEqual(_.mean([1, 2, 3]), 2, "Avearge of the numbers in the collection"); + assert.strictEqual(_.mean({}), 0, 'Avearge value of an empty object'); + assert.strictEqual(_.mean([]), 0, 'Avearge value of an empty array'); + }); + + QUnit.test('median', function(assert) { + assert.strictEqual(_.median(null), undefined, 'can handle null/undefined'); + assert.strictEqual(_.median(void 0), undefined, 'can handle undefined'); + assert.strictEqual(_.median([1, 2, 3]), 2, "Median of the numbers in the collection"); + assert.strictEqual(_.median([1, 2, 3, 4]), 2.5, "Median of the numbers in the collection"); + assert.strictEqual(_.median({}), undefined, 'Median value of an empty object'); + assert.strictEqual(_.median([]), undefined, 'Median value of an empty array'); + }); + + QUnit.test('sum', function(assert) { + assert.strictEqual(_.sum(null), 0, 'can handle null/undefined'); + assert.strictEqual(_.sum(void 0), 0, 'can handle undefined'); + assert.strictEqual(_.sum([1, 2, 3]), 6, "SUM of the numbers in the collection"); + assert.strictEqual(_.sum([1, 2, 3, 4]), 10, "SUM of the numbers in the collection"); + assert.strictEqual(_.sum({}), 0, 'sum value of an empty object'); + assert.strictEqual(_.sum([]), 0, 'sum value of an empty array'); + }); + + QUnit.test('variance', function(assert) { + assert.strictEqual(_.variance(null), 0, 'can handle null/undefined'); + assert.strictEqual(_.variance(void 0), 0, 'can handle undefined'); + assert.strictEqual(_.variance([0, 1, 2, 3, 4]), 2, "variance of the numbers in the collection"); + assert.strictEqual(_.variance([1, 2, 3, 4]), 1.25, "variance of the numbers in the collection"); + assert.strictEqual(_.variance({}), 0, 'variance value of an empty object'); + assert.strictEqual(_.variance([]), 0, 'variance value of an empty array'); + }); + + QUnit.test('standardDeviation', function(assert) { + assert.strictEqual(_.standardDeviation(null), 0, 'can handle null/undefined'); + assert.strictEqual(_.standardDeviation(void 0), 0, 'can handle undefined'); + assert.strictEqual(_.standardDeviation([0, 1, 2, 3, 4]), 1.4142135623730951, "Standard Deviation of the numbers in the collection"); + assert.strictEqual(_.standardDeviation([1, 2, 3, 4]), 1.118033988749895, "Standard Deviation of the numbers in the collection"); + assert.strictEqual(_.standardDeviation({}), 0, 'Standard Deviation value of an empty object'); + assert.strictEqual(_.standardDeviation([]), 0, 'Standard Deviation value of an empty array'); + }); + + QUnit.test('standardError', function(assert) { + assert.strictEqual(_.standardError(null), 0, 'can handle null/undefined'); + assert.strictEqual(_.standardError(void 0), 0, 'can handle undefined'); + assert.strictEqual(_.standardError([0, 1, 2, 3, 4]), 0.7071067811865476, "Standard Error of the numbers in the collection"); + assert.strictEqual(_.standardError([1, 2, 3, 4]), 0.6454972243679028, "Standard Error of the numbers in the collection"); + assert.strictEqual(_.standardError({}), 0, 'Standard Error value of an empty object'); + assert.strictEqual(_.standardError([]), 0, 'Standard Error value of an empty array'); + }); + + QUnit.test('mode', function(assert) { + assert.strictEqual(_.mode(null), undefined, 'can handle null/undefined'); + assert.strictEqual(_.mode(void 0), undefined, 'can handle undefined'); + assert.strictEqual(_.mode([0, 1, 2, 3, 4]), 0, "Mode of the numbers in the collection"); + assert.strictEqual(_.mode([1, 1, 3, 4]), 1, "Mode of the numbers in the collection"); + assert.strictEqual(_.mode({}), undefined, 'Mode value of an empty object'); + assert.strictEqual(_.mode([]), undefined, 'Mode value of an empty array'); + }); + + QUnit.test('statRange', function(assert) { + assert.strictEqual(_.statRange(null), -Infinity, 'can handle null/undefined'); + assert.strictEqual(_.statRange(void 0), -Infinity, 'can handle undefined'); + assert.strictEqual(_.statRange([0, 1, 2, 3, 4]), 4, "Stat Range of the numbers in the collection"); + assert.strictEqual(_.statRange([1, 1, 3, 4]), 3, "Stat Range of the numbers in the collection"); + assert.strictEqual(_.statRange({}), -Infinity, 'Stat Range value of an empty object'); + assert.strictEqual(_.statRange([]), -Infinity, 'Stat Range value of an empty array'); + }); + + QUnit.test('percentile', function(assert) { + assert.strictEqual(_.percentile(null, 25), 0, 'can handle null/undefined'); + assert.strictEqual(_.percentile(void 0, 50), 0, 'can handle undefined'); + assert.strictEqual(_.percentile([0, 1, 2, 3, 4], 75), 3, "75th percentile of the numbers in the collection"); + assert.strictEqual(_.percentile([1, 1, 3, 4], 50), 2, "50th of the numbers in the collection"); + assert.strictEqual(_.percentile({}, 10), 0, 'Percentile value of an empty object'); + assert.strictEqual(_.percentile([], 50), 0, 'Percentile value of an empty array'); + assert.raises(function() { + _.percentile([1, 1, 3, 4], "50") + }, TypeError, 'Percentile must be a number between 0 - 100'); + }); + }()); + \ No newline at end of file From adcd925bcc49fd35d2698967855e2688eed058a7 Mon Sep 17 00:00:00 2001 From: in0068 Date: Thu, 15 Apr 2021 15:45:39 +0530 Subject: [PATCH 02/13] Error Fixed --- test/collections.statistics.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/collections.statistics.js b/test/collections.statistics.js index 1d1fe5b..a2efd6e 100644 --- a/test/collections.statistics.js +++ b/test/collections.statistics.js @@ -80,7 +80,7 @@ assert.strictEqual(_.percentile({}, 10), 0, 'Percentile value of an empty object'); assert.strictEqual(_.percentile([], 50), 0, 'Percentile value of an empty array'); assert.raises(function() { - _.percentile([1, 1, 3, 4], "50") + _.percentile([1, 1, 3, 4], "50"); }, TypeError, 'Percentile must be a number between 0 - 100'); }); }()); From 063e7bd18fdca8799d1c2254e14e094a86d81602 Mon Sep 17 00:00:00 2001 From: in0068 Date: Fri, 16 Apr 2021 10:11:54 +0530 Subject: [PATCH 03/13] Sum function dependency added and whitespaces issuse resolved --- modules/_createPredicateIndexFinder.js | 15 +++ modules/find.js | 10 ++ modules/findIndex.js | 4 + modules/findKey.js | 12 ++ modules/index.collections.statistics.js | 2 +- modules/percentile.js | 22 ++-- modules/standardError.js | 2 +- modules/sum.js | 21 ++++ modules/variance.js | 5 +- test/collections.statistics.js | 155 ++++++++++++------------ 10 files changed, 155 insertions(+), 93 deletions(-) create mode 100644 modules/_createPredicateIndexFinder.js create mode 100644 modules/find.js create mode 100644 modules/findIndex.js create mode 100644 modules/findKey.js create mode 100644 modules/sum.js diff --git a/modules/_createPredicateIndexFinder.js b/modules/_createPredicateIndexFinder.js new file mode 100644 index 0000000..c065948 --- /dev/null +++ b/modules/_createPredicateIndexFinder.js @@ -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; + }; +} diff --git a/modules/find.js b/modules/find.js new file mode 100644 index 0000000..d1f4d28 --- /dev/null +++ b/modules/find.js @@ -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]; +} diff --git a/modules/findIndex.js b/modules/findIndex.js new file mode 100644 index 0000000..b2c87f5 --- /dev/null +++ b/modules/findIndex.js @@ -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); diff --git a/modules/findKey.js b/modules/findKey.js new file mode 100644 index 0000000..e80f1c1 --- /dev/null +++ b/modules/findKey.js @@ -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; + } +} diff --git a/modules/index.collections.statistics.js b/modules/index.collections.statistics.js index bddf932..41c88de 100644 --- a/modules/index.collections.statistics.js +++ b/modules/index.collections.statistics.js @@ -7,4 +7,4 @@ 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'; \ No newline at end of file +export { default as percentile } from './percentile.js'; diff --git a/modules/percentile.js b/modules/percentile.js index f597af6..e0e36c5 100644 --- a/modules/percentile.js +++ b/modules/percentile.js @@ -5,17 +5,17 @@ import sortBy from './sortBy.js'; //Ex : 50th,75th,99th etc. //https://en.wikipedia.org/wiki/Percentile export default function percentile(collection, percentile) { - if (isEmpty(collection)) return 0; - if (typeof percentile !== 'number') throw new TypeError('Percentile must be a number between 0 - 100'); - if (percentile <= 0) return collection[0]; - if (percentile >= 100) return collection[collection.length - 1]; + if (isEmpty(collection)) return 0; + if (typeof percentile !== 'number') throw new TypeError('Percentile must be a number between 0 - 100'); + if (percentile <= 0) return collection[0]; + if (percentile >= 100) return collection[collection.length - 1]; - collection = sortBy(collection); - var index = (percentile/100) * (collection.length - 1), - lowerIndex = Math.floor(index), - upperIndex = lowerIndex + 1, - weight = index % 1; + collection = sortBy(collection); + var index = (percentile/100) * (collection.length - 1), + lowerIndex = Math.floor(index), + upperIndex = lowerIndex + 1, + weight = index % 1; - if (upperIndex >= collection.length) return collection[lowerIndex]; - return collection[lowerIndex] * (1 - weight) + collection[upperIndex] * weight; + if (upperIndex >= collection.length) return collection[lowerIndex]; + return collection[lowerIndex] * (1 - weight) + collection[upperIndex] * weight; } diff --git a/modules/standardError.js b/modules/standardError.js index 9c02a30..c0b24bc 100644 --- a/modules/standardError.js +++ b/modules/standardError.js @@ -4,5 +4,5 @@ import size from './size.js'; // Return the standard error of the mean based on element-based computation. // https://en.wikipedia.org/wiki/Standard_error export default function standardError(collection, iteratee, context) { - return Math.sqrt(variance(collection, iteratee, context)/(size(collection) - 1)); + return Math.sqrt(variance(collection, iteratee, context) / (size(collection) - 1)); } diff --git a/modules/sum.js b/modules/sum.js new file mode 100644 index 0000000..bc5a214 --- /dev/null +++ b/modules/sum.js @@ -0,0 +1,21 @@ +import isArrayLike from './_isArrayLike.js'; +import values from './values.js'; +import cb from './_cb.js'; +import find from './find.js'; + +// Return the sum of elements (or element-based computation). +export default function sum(collection, iteratee, context) { + var result = 0; + if (iteratee == null || typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') { + collection = isArrayLike(collection) ? collection : values(collection); + for (var i = 0, length = collection.length; i < length; i++) { + result += collection[i]; + } + } else { + iteratee = cb(iteratee, context); + find(collection, function(v, index, list) { + result += iteratee(v, index, list);; + }); + } + return result; +} diff --git a/modules/variance.js b/modules/variance.js index 2a1098f..63057bb 100644 --- a/modules/variance.js +++ b/modules/variance.js @@ -5,8 +5,9 @@ import mean from './mean.js'; // optionally after transforming them through `iteratee`. // https://en.wikipedia.org/wiki/Variance export default function variance(collection, iteratee, context) { - if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') iteratee = null; - + if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') { + iteratee = null; + } iteratee = cb(iteratee, context); var computed = []; diff --git a/test/collections.statistics.js b/test/collections.statistics.js index a2efd6e..70d993a 100644 --- a/test/collections.statistics.js +++ b/test/collections.statistics.js @@ -1,87 +1,86 @@ (function() { - var _ = typeof require == 'function' ? require('..') : window._; - QUnit.module('Statistics'); - QUnit.test('mean', function(assert) { - assert.strictEqual(_.mean(null), 0, 'can handle null/undefined'); - assert.strictEqual(_.mean(void 0), 0, 'can handle undefined'); - assert.strictEqual(_.mean([1, 2, 3]), 2, "Avearge of the numbers in the collection"); - assert.strictEqual(_.mean({}), 0, 'Avearge value of an empty object'); - assert.strictEqual(_.mean([]), 0, 'Avearge value of an empty array'); - }); + var _ = typeof require == 'function' ? require('..') : window._; + QUnit.module('Statistics'); + QUnit.test('mean', function(assert) { + assert.strictEqual(_.mean(null), 0, 'can handle null/undefined'); + assert.strictEqual(_.mean(void 0), 0, 'can handle undefined'); + assert.strictEqual(_.mean([1, 2, 3]), 2, "Avearge of the numbers in the collection"); + assert.strictEqual(_.mean({}), 0, 'Avearge value of an empty object'); + assert.strictEqual(_.mean([]), 0, 'Avearge value of an empty array'); + }); - QUnit.test('median', function(assert) { - assert.strictEqual(_.median(null), undefined, 'can handle null/undefined'); - assert.strictEqual(_.median(void 0), undefined, 'can handle undefined'); - assert.strictEqual(_.median([1, 2, 3]), 2, "Median of the numbers in the collection"); - assert.strictEqual(_.median([1, 2, 3, 4]), 2.5, "Median of the numbers in the collection"); - assert.strictEqual(_.median({}), undefined, 'Median value of an empty object'); - assert.strictEqual(_.median([]), undefined, 'Median value of an empty array'); - }); + QUnit.test('median', function(assert) { + assert.strictEqual(_.median(null), undefined, 'can handle null/undefined'); + assert.strictEqual(_.median(void 0), undefined, 'can handle undefined'); + assert.strictEqual(_.median([1, 2, 3]), 2, "Median of the numbers in the collection"); + assert.strictEqual(_.median([1, 2, 3, 4]), 2.5, "Median of the numbers in the collection"); + assert.strictEqual(_.median({}), undefined, 'Median value of an empty object'); + assert.strictEqual(_.median([]), undefined, 'Median value of an empty array'); + }); - QUnit.test('sum', function(assert) { - assert.strictEqual(_.sum(null), 0, 'can handle null/undefined'); - assert.strictEqual(_.sum(void 0), 0, 'can handle undefined'); - assert.strictEqual(_.sum([1, 2, 3]), 6, "SUM of the numbers in the collection"); - assert.strictEqual(_.sum([1, 2, 3, 4]), 10, "SUM of the numbers in the collection"); - assert.strictEqual(_.sum({}), 0, 'sum value of an empty object'); - assert.strictEqual(_.sum([]), 0, 'sum value of an empty array'); - }); + QUnit.test('sum', function(assert) { + assert.strictEqual(_.sum(null), 0, 'can handle null/undefined'); + assert.strictEqual(_.sum(void 0), 0, 'can handle undefined'); + assert.strictEqual(_.sum([1, 2, 3]), 6, "SUM of the numbers in the collection"); + assert.strictEqual(_.sum([1, 2, 3, 4]), 10, "SUM of the numbers in the collection"); + assert.strictEqual(_.sum({}), 0, 'sum value of an empty object'); + assert.strictEqual(_.sum([]), 0, 'sum value of an empty array'); + }); - QUnit.test('variance', function(assert) { - assert.strictEqual(_.variance(null), 0, 'can handle null/undefined'); - assert.strictEqual(_.variance(void 0), 0, 'can handle undefined'); - assert.strictEqual(_.variance([0, 1, 2, 3, 4]), 2, "variance of the numbers in the collection"); - assert.strictEqual(_.variance([1, 2, 3, 4]), 1.25, "variance of the numbers in the collection"); - assert.strictEqual(_.variance({}), 0, 'variance value of an empty object'); - assert.strictEqual(_.variance([]), 0, 'variance value of an empty array'); - }); + QUnit.test('variance', function(assert) { + assert.strictEqual(_.variance(null), 0, 'can handle null/undefined'); + assert.strictEqual(_.variance(void 0), 0, 'can handle undefined'); + assert.strictEqual(_.variance([0, 1, 2, 3, 4]), 2, "variance of the numbers in the collection"); + assert.strictEqual(_.variance([1, 2, 3, 4]), 1.25, "variance of the numbers in the collection"); + assert.strictEqual(_.variance({}), 0, 'variance value of an empty object'); + assert.strictEqual(_.variance([]), 0, 'variance value of an empty array'); + }); - QUnit.test('standardDeviation', function(assert) { - assert.strictEqual(_.standardDeviation(null), 0, 'can handle null/undefined'); - assert.strictEqual(_.standardDeviation(void 0), 0, 'can handle undefined'); - assert.strictEqual(_.standardDeviation([0, 1, 2, 3, 4]), 1.4142135623730951, "Standard Deviation of the numbers in the collection"); - assert.strictEqual(_.standardDeviation([1, 2, 3, 4]), 1.118033988749895, "Standard Deviation of the numbers in the collection"); - assert.strictEqual(_.standardDeviation({}), 0, 'Standard Deviation value of an empty object'); - assert.strictEqual(_.standardDeviation([]), 0, 'Standard Deviation value of an empty array'); - }); + QUnit.test('standardDeviation', function(assert) { + assert.strictEqual(_.standardDeviation(null), 0, 'can handle null/undefined'); + assert.strictEqual(_.standardDeviation(void 0), 0, 'can handle undefined'); + assert.strictEqual(_.standardDeviation([0, 1, 2, 3, 4]), 1.4142135623730951, "Standard Deviation of the numbers in the collection"); + assert.strictEqual(_.standardDeviation([1, 2, 3, 4]), 1.118033988749895, "Standard Deviation of the numbers in the collection"); + assert.strictEqual(_.standardDeviation({}), 0, 'Standard Deviation value of an empty object'); + assert.strictEqual(_.standardDeviation([]), 0, 'Standard Deviation value of an empty array'); + }); - QUnit.test('standardError', function(assert) { - assert.strictEqual(_.standardError(null), 0, 'can handle null/undefined'); - assert.strictEqual(_.standardError(void 0), 0, 'can handle undefined'); - assert.strictEqual(_.standardError([0, 1, 2, 3, 4]), 0.7071067811865476, "Standard Error of the numbers in the collection"); - assert.strictEqual(_.standardError([1, 2, 3, 4]), 0.6454972243679028, "Standard Error of the numbers in the collection"); - assert.strictEqual(_.standardError({}), 0, 'Standard Error value of an empty object'); - assert.strictEqual(_.standardError([]), 0, 'Standard Error value of an empty array'); - }); + QUnit.test('standardError', function(assert) { + assert.strictEqual(_.standardError(null), 0, 'can handle null/undefined'); + assert.strictEqual(_.standardError(void 0), 0, 'can handle undefined'); + assert.strictEqual(_.standardError([0, 1, 2, 3, 4]), 0.7071067811865476, "Standard Error of the numbers in the collection"); + assert.strictEqual(_.standardError([1, 2, 3, 4]), 0.6454972243679028, "Standard Error of the numbers in the collection"); + assert.strictEqual(_.standardError({}), 0, 'Standard Error value of an empty object'); + assert.strictEqual(_.standardError([]), 0, 'Standard Error value of an empty array'); + }); - QUnit.test('mode', function(assert) { - assert.strictEqual(_.mode(null), undefined, 'can handle null/undefined'); - assert.strictEqual(_.mode(void 0), undefined, 'can handle undefined'); - assert.strictEqual(_.mode([0, 1, 2, 3, 4]), 0, "Mode of the numbers in the collection"); - assert.strictEqual(_.mode([1, 1, 3, 4]), 1, "Mode of the numbers in the collection"); - assert.strictEqual(_.mode({}), undefined, 'Mode value of an empty object'); - assert.strictEqual(_.mode([]), undefined, 'Mode value of an empty array'); - }); + QUnit.test('mode', function(assert) { + assert.strictEqual(_.mode(null), undefined, 'can handle null/undefined'); + assert.strictEqual(_.mode(void 0), undefined, 'can handle undefined'); + assert.strictEqual(_.mode([0, 1, 2, 3, 4]), 0, "Mode of the numbers in the collection"); + assert.strictEqual(_.mode([1, 1, 3, 4]), 1, "Mode of the numbers in the collection"); + assert.strictEqual(_.mode({}), undefined, 'Mode value of an empty object'); + assert.strictEqual(_.mode([]), undefined, 'Mode value of an empty array'); + }); - QUnit.test('statRange', function(assert) { - assert.strictEqual(_.statRange(null), -Infinity, 'can handle null/undefined'); - assert.strictEqual(_.statRange(void 0), -Infinity, 'can handle undefined'); - assert.strictEqual(_.statRange([0, 1, 2, 3, 4]), 4, "Stat Range of the numbers in the collection"); - assert.strictEqual(_.statRange([1, 1, 3, 4]), 3, "Stat Range of the numbers in the collection"); - assert.strictEqual(_.statRange({}), -Infinity, 'Stat Range value of an empty object'); - assert.strictEqual(_.statRange([]), -Infinity, 'Stat Range value of an empty array'); - }); + QUnit.test('statRange', function(assert) { + assert.strictEqual(_.statRange(null), -Infinity, 'can handle null/undefined'); + assert.strictEqual(_.statRange(void 0), -Infinity, 'can handle undefined'); + assert.strictEqual(_.statRange([0, 1, 2, 3, 4]), 4, "Stat Range of the numbers in the collection"); + assert.strictEqual(_.statRange([1, 1, 3, 4]), 3, "Stat Range of the numbers in the collection"); + assert.strictEqual(_.statRange({}), -Infinity, 'Stat Range value of an empty object'); + assert.strictEqual(_.statRange([]), -Infinity, 'Stat Range value of an empty array'); + }); - QUnit.test('percentile', function(assert) { - assert.strictEqual(_.percentile(null, 25), 0, 'can handle null/undefined'); - assert.strictEqual(_.percentile(void 0, 50), 0, 'can handle undefined'); - assert.strictEqual(_.percentile([0, 1, 2, 3, 4], 75), 3, "75th percentile of the numbers in the collection"); - assert.strictEqual(_.percentile([1, 1, 3, 4], 50), 2, "50th of the numbers in the collection"); - assert.strictEqual(_.percentile({}, 10), 0, 'Percentile value of an empty object'); - assert.strictEqual(_.percentile([], 50), 0, 'Percentile value of an empty array'); - assert.raises(function() { - _.percentile([1, 1, 3, 4], "50"); - }, TypeError, 'Percentile must be a number between 0 - 100'); - }); - }()); - \ No newline at end of file + QUnit.test('percentile', function(assert) { + assert.strictEqual(_.percentile(null, 25), 0, 'can handle null/undefined'); + assert.strictEqual(_.percentile(void 0, 50), 0, 'can handle undefined'); + assert.strictEqual(_.percentile([0, 1, 2, 3, 4], 75), 3, "75th percentile of the numbers in the collection"); + assert.strictEqual(_.percentile([1, 1, 3, 4], 50), 2, "50th of the numbers in the collection"); + assert.strictEqual(_.percentile({}, 10), 0, 'Percentile value of an empty object'); + assert.strictEqual(_.percentile([], 50), 0, 'Percentile value of an empty array'); + assert.raises(function() { + _.percentile([1, 1, 3, 4], "50") + }, TypeError, 'Percentile must be a number between 0 - 100'); + }); +}()); From f637c0507e9eaf98e1bb7562f58c56aa907a01ee Mon Sep 17 00:00:00 2001 From: in0068 Date: Fri, 16 Apr 2021 10:13:36 +0530 Subject: [PATCH 04/13] Issue in statistics test js fixed --- test/collections.statistics.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/collections.statistics.js b/test/collections.statistics.js index 70d993a..bc2fc99 100644 --- a/test/collections.statistics.js +++ b/test/collections.statistics.js @@ -80,7 +80,7 @@ assert.strictEqual(_.percentile({}, 10), 0, 'Percentile value of an empty object'); assert.strictEqual(_.percentile([], 50), 0, 'Percentile value of an empty array'); assert.raises(function() { - _.percentile([1, 1, 3, 4], "50") + _.percentile([1, 1, 3, 4], "50"); }, TypeError, 'Percentile must be a number between 0 - 100'); }); }()); From bdd4339cdf785d48b47f95979db38e92ad933857 Mon Sep 17 00:00:00 2001 From: in0068 Date: Mon, 19 Apr 2021 10:38:51 +0530 Subject: [PATCH 05/13] Underscore base package chnages --- modules/_baseIteratee.js | 17 ---------- modules/_cb.js | 10 ------ modules/_collectNonEnumProps.js | 40 ------------------------ modules/_createAssigner.js | 18 ----------- modules/_createPredicateIndexFinder.js | 15 --------- modules/_createSizePropertyCheck.js | 9 ------ modules/_deepGet.js | 9 ------ modules/_getLength.js | 4 --- modules/_group.js | 15 --------- modules/_has.js | 6 ---- modules/_isArrayLike.js | 8 ----- modules/_optimizeCb.js | 21 ------------- modules/_setup.js | 43 -------------------------- modules/_shallowProperty.js | 6 ---- modules/_tagTester.js | 9 ------ modules/_toPath.js | 8 ----- modules/each.js | 23 -------------- modules/extendOwn.js | 7 ----- modules/find.js | 10 ------ modules/findIndex.js | 4 --- modules/findKey.js | 12 ------- modules/first.js | 9 ------ modules/groupBy.js | 8 ----- modules/identity.js | 4 --- modules/initial.js | 8 ----- modules/isArguments.js | 16 ---------- modules/isArray.js | 6 ---- modules/isEmpty.js | 18 ----------- modules/isFunction.js | 15 --------- modules/isMatch.js | 13 -------- modules/isObject.js | 5 --- modules/isString.js | 3 -- modules/iteratee.js | 10 ------ modules/keys.js | 16 ---------- modules/map.js | 16 ---------- modules/matcher.js | 11 ------- modules/max.js | 29 ----------------- modules/mean.js | 2 +- modules/median.js | 4 +-- modules/min.js | 29 ----------------- modules/mode.js | 8 ++--- modules/percentile.js | 4 +-- modules/pluck.js | 7 ----- modules/property.js | 11 ------- modules/size.js | 8 ----- modules/sortBy.js | 24 -------------- modules/standardError.js | 2 +- modules/statRange.js | 4 +-- modules/sum.js | 11 ++++--- modules/toPath.js | 9 ------ modules/underscore.js | 25 --------------- modules/values.js | 12 ------- modules/variance.js | 3 +- 53 files changed, 20 insertions(+), 624 deletions(-) delete mode 100644 modules/_baseIteratee.js delete mode 100644 modules/_cb.js delete mode 100644 modules/_collectNonEnumProps.js delete mode 100644 modules/_createAssigner.js delete mode 100644 modules/_createPredicateIndexFinder.js delete mode 100644 modules/_createSizePropertyCheck.js delete mode 100644 modules/_deepGet.js delete mode 100644 modules/_getLength.js delete mode 100644 modules/_group.js delete mode 100644 modules/_has.js delete mode 100644 modules/_isArrayLike.js delete mode 100644 modules/_optimizeCb.js delete mode 100644 modules/_setup.js delete mode 100644 modules/_shallowProperty.js delete mode 100644 modules/_tagTester.js delete mode 100644 modules/_toPath.js delete mode 100644 modules/each.js delete mode 100644 modules/extendOwn.js delete mode 100644 modules/find.js delete mode 100644 modules/findIndex.js delete mode 100644 modules/findKey.js delete mode 100644 modules/first.js delete mode 100644 modules/groupBy.js delete mode 100644 modules/identity.js delete mode 100644 modules/initial.js delete mode 100644 modules/isArguments.js delete mode 100644 modules/isArray.js delete mode 100644 modules/isEmpty.js delete mode 100644 modules/isFunction.js delete mode 100644 modules/isMatch.js delete mode 100644 modules/isObject.js delete mode 100644 modules/isString.js delete mode 100644 modules/iteratee.js delete mode 100644 modules/keys.js delete mode 100644 modules/map.js delete mode 100644 modules/matcher.js delete mode 100644 modules/max.js delete mode 100644 modules/min.js delete mode 100644 modules/pluck.js delete mode 100644 modules/property.js delete mode 100644 modules/size.js delete mode 100644 modules/sortBy.js delete mode 100644 modules/toPath.js delete mode 100644 modules/underscore.js delete mode 100644 modules/values.js diff --git a/modules/_baseIteratee.js b/modules/_baseIteratee.js deleted file mode 100644 index c276ebe..0000000 --- a/modules/_baseIteratee.js +++ /dev/null @@ -1,17 +0,0 @@ -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); -} diff --git a/modules/_cb.js b/modules/_cb.js deleted file mode 100644 index 9b8b555..0000000 --- a/modules/_cb.js +++ /dev/null @@ -1,10 +0,0 @@ -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); -} diff --git a/modules/_collectNonEnumProps.js b/modules/_collectNonEnumProps.js deleted file mode 100644 index 18a2af0..0000000 --- a/modules/_collectNonEnumProps.js +++ /dev/null @@ -1,40 +0,0 @@ -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); - } - } -} diff --git a/modules/_createAssigner.js b/modules/_createAssigner.js deleted file mode 100644 index b102393..0000000 --- a/modules/_createAssigner.js +++ /dev/null @@ -1,18 +0,0 @@ -// 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; - }; -} diff --git a/modules/_createPredicateIndexFinder.js b/modules/_createPredicateIndexFinder.js deleted file mode 100644 index c065948..0000000 --- a/modules/_createPredicateIndexFinder.js +++ /dev/null @@ -1,15 +0,0 @@ -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; - }; -} diff --git a/modules/_createSizePropertyCheck.js b/modules/_createSizePropertyCheck.js deleted file mode 100644 index cc38007..0000000 --- a/modules/_createSizePropertyCheck.js +++ /dev/null @@ -1,9 +0,0 @@ -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; - } -} diff --git a/modules/_deepGet.js b/modules/_deepGet.js deleted file mode 100644 index 42bbec3..0000000 --- a/modules/_deepGet.js +++ /dev/null @@ -1,9 +0,0 @@ -// 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; -} diff --git a/modules/_getLength.js b/modules/_getLength.js deleted file mode 100644 index 090b156..0000000 --- a/modules/_getLength.js +++ /dev/null @@ -1,4 +0,0 @@ -import shallowProperty from './_shallowProperty.js'; - -// Internal helper to obtain the `length` property of an object. -export default shallowProperty('length'); diff --git a/modules/_group.js b/modules/_group.js deleted file mode 100644 index 8fdd985..0000000 --- a/modules/_group.js +++ /dev/null @@ -1,15 +0,0 @@ -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; - }; -} diff --git a/modules/_has.js b/modules/_has.js deleted file mode 100644 index 0636181..0000000 --- a/modules/_has.js +++ /dev/null @@ -1,6 +0,0 @@ -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); -} diff --git a/modules/_isArrayLike.js b/modules/_isArrayLike.js deleted file mode 100644 index a87fe48..0000000 --- a/modules/_isArrayLike.js +++ /dev/null @@ -1,8 +0,0 @@ -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); diff --git a/modules/_optimizeCb.js b/modules/_optimizeCb.js deleted file mode 100644 index 59e40e6..0000000 --- a/modules/_optimizeCb.js +++ /dev/null @@ -1,21 +0,0 @@ -// 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); - }; -} diff --git a/modules/_setup.js b/modules/_setup.js deleted file mode 100644 index 2c2b9be..0000000 --- a/modules/_setup.js +++ /dev/null @@ -1,43 +0,0 @@ -// 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; diff --git a/modules/_shallowProperty.js b/modules/_shallowProperty.js deleted file mode 100644 index 00bf090..0000000 --- a/modules/_shallowProperty.js +++ /dev/null @@ -1,6 +0,0 @@ -// 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]; - }; -} diff --git a/modules/_tagTester.js b/modules/_tagTester.js deleted file mode 100644 index 8d417dd..0000000 --- a/modules/_tagTester.js +++ /dev/null @@ -1,9 +0,0 @@ -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; - }; -} diff --git a/modules/_toPath.js b/modules/_toPath.js deleted file mode 100644 index fad5150..0000000 --- a/modules/_toPath.js +++ /dev/null @@ -1,8 +0,0 @@ -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); -} diff --git a/modules/each.js b/modules/each.js deleted file mode 100644 index d050200..0000000 --- a/modules/each.js +++ /dev/null @@ -1,23 +0,0 @@ -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; -} diff --git a/modules/extendOwn.js b/modules/extendOwn.js deleted file mode 100644 index 5338451..0000000 --- a/modules/extendOwn.js +++ /dev/null @@ -1,7 +0,0 @@ -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); diff --git a/modules/find.js b/modules/find.js deleted file mode 100644 index d1f4d28..0000000 --- a/modules/find.js +++ /dev/null @@ -1,10 +0,0 @@ -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]; -} diff --git a/modules/findIndex.js b/modules/findIndex.js deleted file mode 100644 index b2c87f5..0000000 --- a/modules/findIndex.js +++ /dev/null @@ -1,4 +0,0 @@ -import createPredicateIndexFinder from './_createPredicateIndexFinder.js'; - -// Returns the first index on an array-like that passes a truth test. -export default createPredicateIndexFinder(1); diff --git a/modules/findKey.js b/modules/findKey.js deleted file mode 100644 index e80f1c1..0000000 --- a/modules/findKey.js +++ /dev/null @@ -1,12 +0,0 @@ -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; - } -} diff --git a/modules/first.js b/modules/first.js deleted file mode 100644 index 3b6685e..0000000 --- a/modules/first.js +++ /dev/null @@ -1,9 +0,0 @@ -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); -} diff --git a/modules/groupBy.js b/modules/groupBy.js deleted file mode 100644 index 2670958..0000000 --- a/modules/groupBy.js +++ /dev/null @@ -1,8 +0,0 @@ -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]; -}); diff --git a/modules/identity.js b/modules/identity.js deleted file mode 100644 index 6df631c..0000000 --- a/modules/identity.js +++ /dev/null @@ -1,4 +0,0 @@ -// Keep the identity function around for default iteratees. -export default function identity(value) { - return value; -} diff --git a/modules/initial.js b/modules/initial.js deleted file mode 100644 index 0b991dc..0000000 --- a/modules/initial.js +++ /dev/null @@ -1,8 +0,0 @@ -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))); -} diff --git a/modules/isArguments.js b/modules/isArguments.js deleted file mode 100644 index 61582bf..0000000 --- a/modules/isArguments.js +++ /dev/null @@ -1,16 +0,0 @@ -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; diff --git a/modules/isArray.js b/modules/isArray.js deleted file mode 100644 index 7ead47d..0000000 --- a/modules/isArray.js +++ /dev/null @@ -1,6 +0,0 @@ -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'); diff --git a/modules/isEmpty.js b/modules/isEmpty.js deleted file mode 100644 index 718ef4a..0000000 --- a/modules/isEmpty.js +++ /dev/null @@ -1,18 +0,0 @@ -import getLength from './_getLength.js'; -import isArray from './isArray.js'; -import isString from './isString.js'; -import isArguments from './isArguments.js'; -import keys from './keys.js'; - -// Is a given array, string, or object empty? -// An "empty" object has no enumerable own-properties. -export default function isEmpty(obj) { - if (obj == null) return true; - // Skip the more expensive `toString`-based type checks if `obj` has no - // `.length`. - var length = getLength(obj); - if (typeof length == 'number' && ( - isArray(obj) || isString(obj) || isArguments(obj) - )) return length === 0; - return getLength(keys(obj)) === 0; -} diff --git a/modules/isFunction.js b/modules/isFunction.js deleted file mode 100644 index 35c41be..0000000 --- a/modules/isFunction.js +++ /dev/null @@ -1,15 +0,0 @@ -import tagTester from './_tagTester.js'; -import { root } from './_setup.js'; - -var isFunction = tagTester('Function'); - -// Optimize `isFunction` if appropriate. Work around some `typeof` bugs in old -// v8, IE 11 (#1621), Safari 8 (#1929), and PhantomJS (#2236). -var nodelist = root.document && root.document.childNodes; -if (typeof /./ != 'function' && typeof Int8Array != 'object' && typeof nodelist != 'function') { - isFunction = function(obj) { - return typeof obj == 'function' || false; - }; -} - -export default isFunction; diff --git a/modules/isMatch.js b/modules/isMatch.js deleted file mode 100644 index 81e43d9..0000000 --- a/modules/isMatch.js +++ /dev/null @@ -1,13 +0,0 @@ -import keys from './keys.js'; - -// Returns whether an object has a given set of `key:value` pairs. -export default function isMatch(object, attrs) { - var _keys = keys(attrs), length = _keys.length; - if (object == null) return !length; - var obj = Object(object); - for (var i = 0; i < length; i++) { - var key = _keys[i]; - if (attrs[key] !== obj[key] || !(key in obj)) return false; - } - return true; -} diff --git a/modules/isObject.js b/modules/isObject.js deleted file mode 100644 index 73230f0..0000000 --- a/modules/isObject.js +++ /dev/null @@ -1,5 +0,0 @@ -// Is a given variable an object? -export default function isObject(obj) { - var type = typeof obj; - return type === 'function' || type === 'object' && !!obj; -} diff --git a/modules/isString.js b/modules/isString.js deleted file mode 100644 index f02707d..0000000 --- a/modules/isString.js +++ /dev/null @@ -1,3 +0,0 @@ -import tagTester from './_tagTester.js'; - -export default tagTester('String'); diff --git a/modules/iteratee.js b/modules/iteratee.js deleted file mode 100644 index 9057701..0000000 --- a/modules/iteratee.js +++ /dev/null @@ -1,10 +0,0 @@ -import _ from './underscore.js'; -import baseIteratee from './_baseIteratee.js'; - -// External wrapper for our callback generator. Users may customize -// `_.iteratee` if they want additional predicate/iteratee shorthand styles. -// This abstraction hides the internal-only `argCount` argument. -export default function iteratee(value, context) { - return baseIteratee(value, context, Infinity); -} -_.iteratee = iteratee; diff --git a/modules/keys.js b/modules/keys.js deleted file mode 100644 index f5b596c..0000000 --- a/modules/keys.js +++ /dev/null @@ -1,16 +0,0 @@ -import isObject from './isObject.js'; -import { nativeKeys, hasEnumBug } from './_setup.js'; -import has from './_has.js'; -import collectNonEnumProps from './_collectNonEnumProps.js'; - -// Retrieve the names of an object's own properties. -// Delegates to **ECMAScript 5**'s native `Object.keys`. -export default function keys(obj) { - if (!isObject(obj)) return []; - if (nativeKeys) return nativeKeys(obj); - var keys = []; - for (var key in obj) if (has(obj, key)) keys.push(key); - // Ahem, IE < 9. - if (hasEnumBug) collectNonEnumProps(obj, keys); - return keys; -} diff --git a/modules/map.js b/modules/map.js deleted file mode 100644 index a2e5121..0000000 --- a/modules/map.js +++ /dev/null @@ -1,16 +0,0 @@ -import cb from './_cb.js'; -import isArrayLike from './_isArrayLike.js'; -import keys from './keys.js'; - -// Return the results of applying the iteratee to each element. -export default function map(obj, iteratee, context) { - iteratee = cb(iteratee, context); - var _keys = !isArrayLike(obj) && keys(obj), - length = (_keys || obj).length, - results = Array(length); - for (var index = 0; index < length; index++) { - var currentKey = _keys ? _keys[index] : index; - results[index] = iteratee(obj[currentKey], currentKey, obj); - } - return results; -} diff --git a/modules/matcher.js b/modules/matcher.js deleted file mode 100644 index 245fa94..0000000 --- a/modules/matcher.js +++ /dev/null @@ -1,11 +0,0 @@ -import extendOwn from './extendOwn.js'; -import isMatch from './isMatch.js'; - -// Returns a predicate for checking whether an object has a given set of -// `key:value` pairs. -export default function matcher(attrs) { - attrs = extendOwn({}, attrs); - return function(obj) { - return isMatch(obj, attrs); - }; -} diff --git a/modules/max.js b/modules/max.js deleted file mode 100644 index 9873b35..0000000 --- a/modules/max.js +++ /dev/null @@ -1,29 +0,0 @@ -import isArrayLike from './_isArrayLike.js'; -import values from './values.js'; -import cb from './_cb.js'; -import each from './each.js'; - -// Return the maximum element (or element-based computation). -export default function max(obj, iteratee, context) { - var result = -Infinity, lastComputed = -Infinity, - value, computed; - if (iteratee == null || typeof iteratee == 'number' && typeof obj[0] != 'object' && obj != null) { - obj = isArrayLike(obj) ? obj : values(obj); - for (var i = 0, length = obj.length; i < length; i++) { - value = obj[i]; - if (value != null && value > result) { - result = value; - } - } - } else { - iteratee = cb(iteratee, context); - each(obj, function(v, index, list) { - computed = iteratee(v, index, list); - if (computed > lastComputed || computed === -Infinity && result === -Infinity) { - result = v; - lastComputed = computed; - } - }); - } - return result; -} diff --git a/modules/mean.js b/modules/mean.js index df8209c..9d3acbc 100644 --- a/modules/mean.js +++ b/modules/mean.js @@ -1,4 +1,4 @@ -import size from './size.js'; +import size from 'underscore/modules/size.js'; import sum from './sum.js'; // Compute the average of the numbers obtained from the collection diff --git a/modules/median.js b/modules/median.js index 921964f..2dffc34 100644 --- a/modules/median.js +++ b/modules/median.js @@ -1,5 +1,5 @@ -import map from './map.js' -import isEmpty from './isEmpty'; +import map from 'underscore/modules/map.js' +import isEmpty from 'underscore/modules/isEmpty'; // https://en.wikipedia.org/wiki/Median // Calulation of median is done using the following method. diff --git a/modules/min.js b/modules/min.js deleted file mode 100644 index 32f92a0..0000000 --- a/modules/min.js +++ /dev/null @@ -1,29 +0,0 @@ -import isArrayLike from './_isArrayLike.js'; -import values from './values.js'; -import cb from './_cb.js'; -import each from './each.js'; - -// Return the minimum element (or element-based computation). -export default function min(obj, iteratee, context) { - var result = Infinity, lastComputed = Infinity, - value, computed; - if (iteratee == null || typeof iteratee == 'number' && typeof obj[0] != 'object' && obj != null) { - obj = isArrayLike(obj) ? obj : values(obj); - for (var i = 0, length = obj.length; i < length; i++) { - value = obj[i]; - if (value != null && value < result) { - result = value; - } - } - } else { - iteratee = cb(iteratee, context); - each(obj, function(v, index, list) { - computed = iteratee(v, index, list); - if (computed < lastComputed || computed === Infinity && result === Infinity) { - result = v; - lastComputed = computed; - } - }); - } - return result; -} diff --git a/modules/mode.js b/modules/mode.js index 1786bd0..fd77981 100644 --- a/modules/mode.js +++ b/modules/mode.js @@ -1,7 +1,7 @@ -import isEmpty from './isEmpty'; -import groupBy from './groupBy.js'; -import max from './max.js'; -import first from './first.js'; +import isEmpty from 'underscore/modules/isEmpty'; +import groupBy from 'underscore/modules/groupBy.js'; +import max from 'underscore/modules/max.js'; +import first from 'underscore/modules/first.js'; // Return the element (or element-based computation) that appears most frequently in the collection. // https://en.wikipedia.org/wiki/Mode_(statistics) diff --git a/modules/percentile.js b/modules/percentile.js index e0e36c5..07531bd 100644 --- a/modules/percentile.js +++ b/modules/percentile.js @@ -1,5 +1,5 @@ -import isEmpty from './isEmpty'; -import sortBy from './sortBy.js'; +import isEmpty from 'underscore/modules/isEmpty'; +import sortBy from 'underscore/modules/sortBy.js'; // Return the percentile value of the numeric elements from the collection //Ex : 50th,75th,99th etc. diff --git a/modules/pluck.js b/modules/pluck.js deleted file mode 100644 index 45a3533..0000000 --- a/modules/pluck.js +++ /dev/null @@ -1,7 +0,0 @@ -import map from './map.js'; -import property from './property.js'; - -// Convenience version of a common use case of `_.map`: fetching a property. -export default function pluck(obj, key) { - return map(obj, property(key)); -} diff --git a/modules/property.js b/modules/property.js deleted file mode 100644 index 4853866..0000000 --- a/modules/property.js +++ /dev/null @@ -1,11 +0,0 @@ -import deepGet from './_deepGet.js'; -import toPath from './_toPath.js'; - -// Creates a function that, when passed an object, will traverse that object’s -// properties down the given `path`, specified as an array of keys or indices. -export default function property(path) { - path = toPath(path); - return function(obj) { - return deepGet(obj, path); - }; -} diff --git a/modules/size.js b/modules/size.js deleted file mode 100644 index 4ce3714..0000000 --- a/modules/size.js +++ /dev/null @@ -1,8 +0,0 @@ -import isArrayLike from './_isArrayLike.js'; -import keys from './keys.js'; - -// Return the number of elements in a collection. -export default function size(obj) { - if (obj == null) return 0; - return isArrayLike(obj) ? obj.length : keys(obj).length; -} diff --git a/modules/sortBy.js b/modules/sortBy.js deleted file mode 100644 index bca494b..0000000 --- a/modules/sortBy.js +++ /dev/null @@ -1,24 +0,0 @@ -import cb from './_cb.js'; -import pluck from './pluck.js'; -import map from './map.js'; - -// Sort the object's values by a criterion produced by an iteratee. -export default function sortBy(obj, iteratee, context) { - var index = 0; - iteratee = cb(iteratee, context); - return pluck(map(obj, function(value, key, list) { - return { - value: value, - index: index++, - criteria: iteratee(value, key, list) - }; - }).sort(function(left, right) { - var a = left.criteria; - var b = right.criteria; - if (a !== b) { - if (a > b || a === void 0) return 1; - if (a < b || b === void 0) return -1; - } - return left.index - right.index; - }), 'value'); -} diff --git a/modules/standardError.js b/modules/standardError.js index c0b24bc..1b61c0f 100644 --- a/modules/standardError.js +++ b/modules/standardError.js @@ -1,5 +1,5 @@ import variance from './variance.js'; -import size from './size.js'; +import size from 'underscore/modules/size.js'; // Return the standard error of the mean based on element-based computation. // https://en.wikipedia.org/wiki/Standard_error diff --git a/modules/statRange.js b/modules/statRange.js index cb90f7a..5c77d42 100644 --- a/modules/statRange.js +++ b/modules/statRange.js @@ -1,5 +1,5 @@ -import max from './max.js'; -import min from './min.js'; +import max from 'underscore/modules/max.js'; +import min from 'underscore/modules/min.js'; export default function statRange(collection, iteratee, context){ return max(collection, iteratee, context) - min(collection, iteratee, context); diff --git a/modules/sum.js b/modules/sum.js index bc5a214..3577d30 100644 --- a/modules/sum.js +++ b/modules/sum.js @@ -1,7 +1,8 @@ -import isArrayLike from './_isArrayLike.js'; -import values from './values.js'; -import cb from './_cb.js'; -import find from './find.js'; +import isArrayLike from 'underscore/modules/_isArrayLike.js'; +import values from 'underscore/modules/values.js'; +import 'underscore/modules/iteratee.js'; +import _ from 'underscore/modules/underscore.js'; +import find from 'underscore/modules/find.js'; // Return the sum of elements (or element-based computation). export default function sum(collection, iteratee, context) { @@ -12,7 +13,7 @@ export default function sum(collection, iteratee, context) { result += collection[i]; } } else { - iteratee = cb(iteratee, context); + iteratee = _.iteratee(iteratee, context); find(collection, function(v, index, list) { result += iteratee(v, index, list);; }); diff --git a/modules/toPath.js b/modules/toPath.js deleted file mode 100644 index 7d72d1f..0000000 --- a/modules/toPath.js +++ /dev/null @@ -1,9 +0,0 @@ -import _ from './underscore.js'; -import isArray from './isArray.js'; - -// Normalize a (deep) property `path` to array. -// Like `_.iteratee`, this function can be customized. -export default function toPath(path) { - return isArray(path) ? path : [path]; -} -_.toPath = toPath; diff --git a/modules/underscore.js b/modules/underscore.js deleted file mode 100644 index 6029e2a..0000000 --- a/modules/underscore.js +++ /dev/null @@ -1,25 +0,0 @@ -import { VERSION } from './_setup.js'; - -// If Underscore is called as a function, it returns a wrapped object that can -// be used OO-style. This wrapper holds altered versions of all functions added -// through `_.mixin`. Wrapped objects may be chained. -export default function _(obj) { - if (obj instanceof _) return obj; - if (!(this instanceof _)) return new _(obj); - this._wrapped = obj; -} - -_.VERSION = VERSION; - -// Extracts the result from a wrapped and chained object. -_.prototype.value = function() { - return this._wrapped; -}; - -// Provide unwrapping proxies for some methods used in engine operations -// such as arithmetic and JSON stringification. -_.prototype.valueOf = _.prototype.toJSON = _.prototype.value; - -_.prototype.toString = function() { - return String(this._wrapped); -}; diff --git a/modules/values.js b/modules/values.js deleted file mode 100644 index 9591de3..0000000 --- a/modules/values.js +++ /dev/null @@ -1,12 +0,0 @@ -import keys from './keys.js'; - -// Retrieve the values of an object's properties. -export default function values(obj) { - var _keys = keys(obj); - var length = _keys.length; - var values = Array(length); - for (var i = 0; i < length; i++) { - values[i] = obj[_keys[i]]; - } - return values; -} diff --git a/modules/variance.js b/modules/variance.js index 63057bb..6601d9f 100644 --- a/modules/variance.js +++ b/modules/variance.js @@ -1,4 +1,5 @@ -import cb from './_cb.js'; +import 'underscore/modules/iteratee.js'; +import _ from 'underscore/modules/underscore.js'; import mean from './mean.js'; // Return the variance of the numeric elements of the collection, From 2a76ba21f91b25889ce985de18c5e3eef673d74c Mon Sep 17 00:00:00 2001 From: in0068 Date: Thu, 21 Oct 2021 11:22:02 +0530 Subject: [PATCH 06/13] Docs added --- docs/underscore.collections.statistics.md | 151 ++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 docs/underscore.collections.statistics.md diff --git a/docs/underscore.collections.statistics.md b/docs/underscore.collections.statistics.md new file mode 100644 index 0000000..1631f42 --- /dev/null +++ b/docs/underscore.collections.statistics.md @@ -0,0 +1,151 @@ +### statistics + +> Statisctics Functions. View Annotated Source + +#### mean + +Signature: `_.mean(... arrays:Array ...)` + +The `_.mean` function finds out the average value from the array of numbers. + +```javascript + +_.mean([]); +//=> 0 + +_.mean([0, 1, 2, 3, 4]); +//=> 2 + +_.mean(null) +//=> 0 + +``` + +#### median + +Signature: `_.median(... arrays:Array ...)` + +The `_.median` function finds out the middle value from the array of numbers. + +Calulation of median is done using the following method. + +If the array has odd numbers then median is the middle element. + +If the array has even numbers then average of middle two numbers is the median value. +```javascript + +_.median([]); +//=> undefined + +_.median([1, 2, 3]); +//=> 2 + +_.median([1, 2, 3, 4]) +// => 2.5 + +``` + +#### sum + +Signature: `_.sum(... arrays:Array ...)` + +The `_.sum` function calculates the sum of the given arrays. + +```javascript + +_.sum([]); +//=> 0 + +_.sum([0, 1, 2, 3, 4]); +//=> 10 +``` + +#### variance + +Signature: `_.variance(... arrays:Array ...)` + +The `_.variance` function return the variance of the numeric elements of the collection. + +```javascript + +_.variance([]); +//=> 0 + +_.variance([0, 1, 2, 3, 4]); +//=> 1.25 +``` + +#### standardDeviation + +Signature: `_.standardDeviation(... arrays:Array ...)` + +The `_.standardDeviation` function return the standard deviation of the numeric elements of the collection. + +```javascript + +_.standardDeviation([]); +//=> 0 + +_.standardDeviation([1, 2, 3, 4]); +//=> 1.118 +``` + +#### standardError + +Signature: `_.standardError(... arrays:Array ...)` + +The `_.standardError` function return the standard error of the numeric elements of the collection. + +```javascript + +_.standardError([]); +//=> 0 + +_.standardError([1, 2, 3, 4]); +//=> 0.64 +``` + +#### mode + +Signature: `_.mode(... arrays:Array ...)` + +The `_.mode` function return the element (or element-based computation) that appears most frequently in the collection. + +```javascript + +_.mode([]); +//=> undefined + +_.mode([1, 1, 3, 4]); +//=> 1 +``` + +#### statRange + +Signature: `_.statRange(... arrays:Array ...)` + +The `_.statRange` function return the difference of the max and min value of the elements in the array. + +```javascript + +_.statRange([]); +//=> -Infinity + +_.statRange([1, 1, 3, 4]); +//=> 3 +``` + +#### percentile + +Signature: `_.percentile(... arrays:Array ...,percentileval:number)` + +The `_.percentile` function return the percentile value of the numeric elements from the collection like 50th,75th,99th etc. + +```javascript + +_.percentile([], 10); +//=> 0 + +_.percentile([1, 1, 3, 4], 50); +//=> 2 +``` \ No newline at end of file From c3aea80758c314f8d4ddfb294a718cae5b6aabf7 Mon Sep 17 00:00:00 2001 From: in0068 Date: Thu, 21 Oct 2021 11:37:20 +0530 Subject: [PATCH 07/13] Docs added and Changes made as suggested --- modules/variance.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/variance.js b/modules/variance.js index 6601d9f..1d2e85f 100644 --- a/modules/variance.js +++ b/modules/variance.js @@ -9,7 +9,7 @@ export default function variance(collection, iteratee, context) { if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') { iteratee = null; } - iteratee = cb(iteratee, context); + iteratee = iteratee(iteratee, context); var computed = []; var avg = mean(collection, function(value, key, collection) { From 72a910b688da14d6451a1f2a55bb636fdf5f1f8d Mon Sep 17 00:00:00 2001 From: in0068 Date: Fri, 22 Oct 2021 09:45:55 +0530 Subject: [PATCH 08/13] Changes made as suggested --- docs/underscore.collections.statistics.md | 42 ++++++++++++++++------- modules/variance.js | 2 +- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/docs/underscore.collections.statistics.md b/docs/underscore.collections.statistics.md index 1631f42..a9d3a85 100644 --- a/docs/underscore.collections.statistics.md +++ b/docs/underscore.collections.statistics.md @@ -4,10 +4,12 @@ #### mean -Signature: `_.mean(... arrays:Array ...)` +Signature: `_.mean(... array:Array ...)` The `_.mean` function finds out the average value from the array of numbers. +Link for reference Mean + ```javascript _.mean([]); @@ -23,15 +25,18 @@ _.mean(null) #### median -Signature: `_.median(... arrays:Array ...)` +Signature: `_.median(... array:Array ...)` The `_.median` function finds out the middle value from the array of numbers. Calulation of median is done using the following method. -If the array has odd numbers then median is the middle element. +If the array is odd length then median is the middle element. + +If the array is even numbers then average of middle two numbers is the median value. + +Link for reference Median -If the array has even numbers then average of middle two numbers is the median value. ```javascript _.median([]); @@ -47,9 +52,9 @@ _.median([1, 2, 3, 4]) #### sum -Signature: `_.sum(... arrays:Array ...)` +Signature: `_.sum(... array:Array ...)` -The `_.sum` function calculates the sum of the given arrays. +The `_.sum` function calculates the sum of the given array. ```javascript @@ -62,10 +67,12 @@ _.sum([0, 1, 2, 3, 4]); #### variance -Signature: `_.variance(... arrays:Array ...)` +Signature: `_.variance(... array:Array ...)` The `_.variance` function return the variance of the numeric elements of the collection. +Link for reference Variance + ```javascript _.variance([]); @@ -77,10 +84,12 @@ _.variance([0, 1, 2, 3, 4]); #### standardDeviation -Signature: `_.standardDeviation(... arrays:Array ...)` +Signature: `_.standardDeviation(... array:Array ...)` The `_.standardDeviation` function return the standard deviation of the numeric elements of the collection. +Link for reference Standard Deviation + ```javascript _.standardDeviation([]); @@ -92,10 +101,12 @@ _.standardDeviation([1, 2, 3, 4]); #### standardError -Signature: `_.standardError(... arrays:Array ...)` +Signature: `_.standardError(... array:Array ...)` The `_.standardError` function return the standard error of the numeric elements of the collection. +Link for reference Standard Error + ```javascript _.standardError([]); @@ -107,10 +118,12 @@ _.standardError([1, 2, 3, 4]); #### mode -Signature: `_.mode(... arrays:Array ...)` +Signature: `_.mode(... array:Array ...)` The `_.mode` function return the element (or element-based computation) that appears most frequently in the collection. +Link for reference Mode + ```javascript _.mode([]); @@ -122,10 +135,12 @@ _.mode([1, 1, 3, 4]); #### statRange -Signature: `_.statRange(... arrays:Array ...)` +Signature: `_.statRange(... array:Array ...)` The `_.statRange` function return the difference of the max and min value of the elements in the array. +Link for reference Range + ```javascript _.statRange([]); @@ -137,10 +152,13 @@ _.statRange([1, 1, 3, 4]); #### percentile -Signature: `_.percentile(... arrays:Array ...,percentileval:number)` +Signature: `_.percentile(... array:Array ...,percentileVal:number)` The `_.percentile` function return the percentile value of the numeric elements from the collection like 50th,75th,99th etc. +Link for reference Percentile + + ```javascript _.percentile([], 10); diff --git a/modules/variance.js b/modules/variance.js index 1d2e85f..ddce9f9 100644 --- a/modules/variance.js +++ b/modules/variance.js @@ -9,7 +9,7 @@ export default function variance(collection, iteratee, context) { if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') { iteratee = null; } - iteratee = iteratee(iteratee, context); + iteratee = _.iteratee(iteratee, context); var computed = []; var avg = mean(collection, function(value, key, collection) { From acc73da7ccefc00414c1d1da151ea98fc1f29f76 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 30 May 2021 19:18:54 +0000 Subject: [PATCH 09/13] Bump ws from 7.3.1 to 7.4.6 Bumps [ws](https://github.com/websockets/ws) from 7.3.1 to 7.4.6. - [Release notes](https://github.com/websockets/ws/releases) - [Commits](https://github.com/websockets/ws/compare/7.3.1...7.4.6) Signed-off-by: dependabot[bot] --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index 7fca598..91ff88b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2037,9 +2037,9 @@ wrappy@1: integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8= ws@^7.2.3: - version "7.3.1" - resolved "https://registry.yarnpkg.com/ws/-/ws-7.3.1.tgz#d0547bf67f7ce4f12a72dfe31262c68d7dc551c8" - integrity sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA== + version "7.4.6" + resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.6.tgz#5654ca8ecdeee47c33a9a4bf6d28e2be2980377c" + integrity sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A== yauzl@^2.10.0: version "2.10.0" From a2c97bf537a7bf24167fabca473461ea19063ac6 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 12 Aug 2021 20:54:48 +0000 Subject: [PATCH 10/13] Bump path-parse from 1.0.6 to 1.0.7 Bumps [path-parse](https://github.com/jbgutierrez/path-parse) from 1.0.6 to 1.0.7. - [Release notes](https://github.com/jbgutierrez/path-parse/releases) - [Commits](https://github.com/jbgutierrez/path-parse/commits/v1.0.7) --- updated-dependencies: - dependency-name: path-parse dependency-type: indirect ... Signed-off-by: dependabot[bot] --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index 91ff88b..d9c381d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1517,9 +1517,9 @@ path-is-absolute@^1.0.0: integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18= path-parse@^1.0.6: - version "1.0.6" - resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c" - integrity sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw== + version "1.0.7" + resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735" + integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw== path-root-regex@^0.1.0: version "0.1.2" From d83d48ba8af57c051a2e9a3deb2e69c89fa4b693 Mon Sep 17 00:00:00 2001 From: in0068 Date: Thu, 21 Oct 2021 11:22:02 +0530 Subject: [PATCH 11/13] Docs added --- docs/underscore.collections.statistics.md | 151 ++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 docs/underscore.collections.statistics.md diff --git a/docs/underscore.collections.statistics.md b/docs/underscore.collections.statistics.md new file mode 100644 index 0000000..1631f42 --- /dev/null +++ b/docs/underscore.collections.statistics.md @@ -0,0 +1,151 @@ +### statistics + +> Statisctics Functions. View Annotated Source + +#### mean + +Signature: `_.mean(... arrays:Array ...)` + +The `_.mean` function finds out the average value from the array of numbers. + +```javascript + +_.mean([]); +//=> 0 + +_.mean([0, 1, 2, 3, 4]); +//=> 2 + +_.mean(null) +//=> 0 + +``` + +#### median + +Signature: `_.median(... arrays:Array ...)` + +The `_.median` function finds out the middle value from the array of numbers. + +Calulation of median is done using the following method. + +If the array has odd numbers then median is the middle element. + +If the array has even numbers then average of middle two numbers is the median value. +```javascript + +_.median([]); +//=> undefined + +_.median([1, 2, 3]); +//=> 2 + +_.median([1, 2, 3, 4]) +// => 2.5 + +``` + +#### sum + +Signature: `_.sum(... arrays:Array ...)` + +The `_.sum` function calculates the sum of the given arrays. + +```javascript + +_.sum([]); +//=> 0 + +_.sum([0, 1, 2, 3, 4]); +//=> 10 +``` + +#### variance + +Signature: `_.variance(... arrays:Array ...)` + +The `_.variance` function return the variance of the numeric elements of the collection. + +```javascript + +_.variance([]); +//=> 0 + +_.variance([0, 1, 2, 3, 4]); +//=> 1.25 +``` + +#### standardDeviation + +Signature: `_.standardDeviation(... arrays:Array ...)` + +The `_.standardDeviation` function return the standard deviation of the numeric elements of the collection. + +```javascript + +_.standardDeviation([]); +//=> 0 + +_.standardDeviation([1, 2, 3, 4]); +//=> 1.118 +``` + +#### standardError + +Signature: `_.standardError(... arrays:Array ...)` + +The `_.standardError` function return the standard error of the numeric elements of the collection. + +```javascript + +_.standardError([]); +//=> 0 + +_.standardError([1, 2, 3, 4]); +//=> 0.64 +``` + +#### mode + +Signature: `_.mode(... arrays:Array ...)` + +The `_.mode` function return the element (or element-based computation) that appears most frequently in the collection. + +```javascript + +_.mode([]); +//=> undefined + +_.mode([1, 1, 3, 4]); +//=> 1 +``` + +#### statRange + +Signature: `_.statRange(... arrays:Array ...)` + +The `_.statRange` function return the difference of the max and min value of the elements in the array. + +```javascript + +_.statRange([]); +//=> -Infinity + +_.statRange([1, 1, 3, 4]); +//=> 3 +``` + +#### percentile + +Signature: `_.percentile(... arrays:Array ...,percentileval:number)` + +The `_.percentile` function return the percentile value of the numeric elements from the collection like 50th,75th,99th etc. + +```javascript + +_.percentile([], 10); +//=> 0 + +_.percentile([1, 1, 3, 4], 50); +//=> 2 +``` \ No newline at end of file From 009a8cda52b9a16a8d28af826484875a70ea93a4 Mon Sep 17 00:00:00 2001 From: in0068 Date: Thu, 21 Oct 2021 11:37:20 +0530 Subject: [PATCH 12/13] Docs added and Changes made as suggested --- modules/variance.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/variance.js b/modules/variance.js index 6601d9f..1d2e85f 100644 --- a/modules/variance.js +++ b/modules/variance.js @@ -9,7 +9,7 @@ export default function variance(collection, iteratee, context) { if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') { iteratee = null; } - iteratee = cb(iteratee, context); + iteratee = iteratee(iteratee, context); var computed = []; var avg = mean(collection, function(value, key, collection) { From b130a0432be7d698fb31d800a0c43d78ff8631f2 Mon Sep 17 00:00:00 2001 From: in0068 Date: Fri, 22 Oct 2021 09:45:55 +0530 Subject: [PATCH 13/13] Changes made as suggested --- docs/underscore.collections.statistics.md | 42 ++++++++++++++++------- modules/variance.js | 2 +- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/docs/underscore.collections.statistics.md b/docs/underscore.collections.statistics.md index 1631f42..a9d3a85 100644 --- a/docs/underscore.collections.statistics.md +++ b/docs/underscore.collections.statistics.md @@ -4,10 +4,12 @@ #### mean -Signature: `_.mean(... arrays:Array ...)` +Signature: `_.mean(... array:Array ...)` The `_.mean` function finds out the average value from the array of numbers. +Link for reference Mean + ```javascript _.mean([]); @@ -23,15 +25,18 @@ _.mean(null) #### median -Signature: `_.median(... arrays:Array ...)` +Signature: `_.median(... array:Array ...)` The `_.median` function finds out the middle value from the array of numbers. Calulation of median is done using the following method. -If the array has odd numbers then median is the middle element. +If the array is odd length then median is the middle element. + +If the array is even numbers then average of middle two numbers is the median value. + +Link for reference Median -If the array has even numbers then average of middle two numbers is the median value. ```javascript _.median([]); @@ -47,9 +52,9 @@ _.median([1, 2, 3, 4]) #### sum -Signature: `_.sum(... arrays:Array ...)` +Signature: `_.sum(... array:Array ...)` -The `_.sum` function calculates the sum of the given arrays. +The `_.sum` function calculates the sum of the given array. ```javascript @@ -62,10 +67,12 @@ _.sum([0, 1, 2, 3, 4]); #### variance -Signature: `_.variance(... arrays:Array ...)` +Signature: `_.variance(... array:Array ...)` The `_.variance` function return the variance of the numeric elements of the collection. +Link for reference Variance + ```javascript _.variance([]); @@ -77,10 +84,12 @@ _.variance([0, 1, 2, 3, 4]); #### standardDeviation -Signature: `_.standardDeviation(... arrays:Array ...)` +Signature: `_.standardDeviation(... array:Array ...)` The `_.standardDeviation` function return the standard deviation of the numeric elements of the collection. +Link for reference Standard Deviation + ```javascript _.standardDeviation([]); @@ -92,10 +101,12 @@ _.standardDeviation([1, 2, 3, 4]); #### standardError -Signature: `_.standardError(... arrays:Array ...)` +Signature: `_.standardError(... array:Array ...)` The `_.standardError` function return the standard error of the numeric elements of the collection. +Link for reference Standard Error + ```javascript _.standardError([]); @@ -107,10 +118,12 @@ _.standardError([1, 2, 3, 4]); #### mode -Signature: `_.mode(... arrays:Array ...)` +Signature: `_.mode(... array:Array ...)` The `_.mode` function return the element (or element-based computation) that appears most frequently in the collection. +Link for reference Mode + ```javascript _.mode([]); @@ -122,10 +135,12 @@ _.mode([1, 1, 3, 4]); #### statRange -Signature: `_.statRange(... arrays:Array ...)` +Signature: `_.statRange(... array:Array ...)` The `_.statRange` function return the difference of the max and min value of the elements in the array. +Link for reference Range + ```javascript _.statRange([]); @@ -137,10 +152,13 @@ _.statRange([1, 1, 3, 4]); #### percentile -Signature: `_.percentile(... arrays:Array ...,percentileval:number)` +Signature: `_.percentile(... array:Array ...,percentileVal:number)` The `_.percentile` function return the percentile value of the numeric elements from the collection like 50th,75th,99th etc. +Link for reference Percentile + + ```javascript _.percentile([], 10); diff --git a/modules/variance.js b/modules/variance.js index 1d2e85f..ddce9f9 100644 --- a/modules/variance.js +++ b/modules/variance.js @@ -9,7 +9,7 @@ export default function variance(collection, iteratee, context) { if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') { iteratee = null; } - iteratee = iteratee(iteratee, context); + iteratee = _.iteratee(iteratee, context); var computed = []; var avg = mean(collection, function(value, key, collection) {