math-helper-functions
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -5,6 +5,8 @@ { | ||
"es6": true, | ||
"node": true, | ||
"jquery": true | ||
"node": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:jest/recommended" | ||
], | ||
"parserOptions": { | ||
@@ -14,13 +16,16 @@ "sourceType": "module", | ||
}, | ||
"globals": { | ||
"Vue": true | ||
}, | ||
"rules": { | ||
"accessor-pairs": "error", | ||
"array-bracket-newline": "error", | ||
"array-bracket-spacing": ["error", "never"], | ||
"array-bracket-spacing": [ | ||
"error", | ||
"never" | ||
], | ||
"array-callback-return": "off", | ||
"array-element-newline": "off", | ||
"arrow-body-style": "error", | ||
"arrow-parens": ["error", "always"], | ||
"arrow-parens": [ | ||
"error", | ||
"always" | ||
], | ||
"arrow-spacing": [ | ||
@@ -35,8 +40,17 @@ "error", | ||
"block-spacing": "error", | ||
"brace-style": ["error", "1tbs"], | ||
"brace-style": [ | ||
"error", | ||
"1tbs" | ||
], | ||
"callback-return": "error", | ||
"camelcase": "off", | ||
"capitalized-comments": ["off", "always"], | ||
"capitalized-comments": [ | ||
"off", | ||
"always" | ||
], | ||
"class-methods-use-this": "off", | ||
"comma-dangle": ["error", "always-multiline"], | ||
"comma-dangle": [ | ||
"error", | ||
"always-multiline" | ||
], | ||
"comma-spacing": [ | ||
@@ -49,5 +63,11 @@ "error", | ||
], | ||
"comma-style": ["error", "last"], | ||
"comma-style": [ | ||
"error", | ||
"last" | ||
], | ||
"complexity": "error", | ||
"computed-property-spacing": ["error", "never"], | ||
"computed-property-spacing": [ | ||
"error", | ||
"never" | ||
], | ||
"consistent-return": "off", | ||
@@ -57,3 +77,6 @@ "consistent-this": "off", | ||
"default-case": "error", | ||
"dot-location": ["error", "property"], | ||
"dot-location": [ | ||
"error", | ||
"property" | ||
], | ||
"dot-notation": "off", | ||
@@ -65,4 +88,10 @@ "eol-last": "error", | ||
"func-name-matching": "error", | ||
"func-names": ["error", "never"], | ||
"func-style": ["error", "declaration"], | ||
"func-names": [ | ||
"error", | ||
"never" | ||
], | ||
"func-style": [ | ||
"error", | ||
"declaration" | ||
], | ||
"function-paren-newline": "error", | ||
@@ -77,3 +106,6 @@ "generator-star-spacing": "error", | ||
"id-match": "error", | ||
"indent": ["off", "tab"], | ||
"indent": [ | ||
"off", | ||
"tab" | ||
], | ||
"indent-legacy": "off", | ||
@@ -91,3 +123,6 @@ "init-declarations": "off", | ||
"line-comment-position": "error", | ||
"linebreak-style": ["error", "unix"], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"lines-around-comment": "error", | ||
@@ -103,3 +138,6 @@ "lines-around-directive": "error", | ||
"max-statements-per-line": "error", | ||
"multiline-comment-style": ["error", "separate-lines"], | ||
"multiline-comment-style": [ | ||
"error", | ||
"separate-lines" | ||
], | ||
"new-cap": "off", | ||
@@ -134,3 +172,6 @@ "new-parens": "error", | ||
"no-inline-comments": "error", | ||
"no-inner-declarations": ["error", "functions"], | ||
"no-inner-declarations": [ | ||
"error", | ||
"functions" | ||
], | ||
"no-invalid-this": "off", | ||
@@ -208,6 +249,9 @@ "no-iterator": "error", | ||
"object-property-newline": "error", | ||
"object-shorthand": "off", | ||
"object-shorthand": "error", | ||
"one-var": "off", | ||
"one-var-declaration-per-line": "error", | ||
"operator-assignment": ["error", "always"], | ||
"operator-assignment": [ | ||
"error", | ||
"always" | ||
], | ||
"operator-linebreak": "error", | ||
@@ -239,3 +283,6 @@ "padded-blocks": "off", | ||
], | ||
"semi-style": ["error", "last"], | ||
"semi-style": [ | ||
"error", | ||
"last" | ||
], | ||
"sort-imports": "off", | ||
@@ -246,12 +293,24 @@ "sort-keys": "off", | ||
"space-before-function-paren": "off", | ||
"space-in-parens": ["error", "never"], | ||
"space-in-parens": [ | ||
"error", | ||
"never" | ||
], | ||
"space-infix-ops": "error", | ||
"space-unary-ops": "error", | ||
"spaced-comment": ["error", "always"], | ||
"spaced-comment": [ | ||
"error", | ||
"always" | ||
], | ||
"strict": "error", | ||
"switch-colon-spacing": "error", | ||
"symbol-description": "error", | ||
"template-curly-spacing": ["error", "never"], | ||
"template-curly-spacing": [ | ||
"error", | ||
"never" | ||
], | ||
"template-tag-spacing": "error", | ||
"unicode-bom": ["error", "never"], | ||
"unicode-bom": [ | ||
"error", | ||
"never" | ||
], | ||
"valid-jsdoc": "off", | ||
@@ -262,4 +321,7 @@ "vars-on-top": "off", | ||
"yield-star-spacing": "error", | ||
"yoda": ["error", "never"] | ||
"yoda": [ | ||
"error", | ||
"never" | ||
] | ||
} | ||
} | ||
} |
68
index.js
@@ -1,4 +0,2 @@ | ||
const {max, min, mean, median, histogram} = require('d3-array'); | ||
const sum = require('lodash/sum'); | ||
const sumBy = require('lodash/sumBy'); | ||
const {sum, max, min, mean, median, histogram} = require('d3-array'); | ||
const get = require('lodash/get'); | ||
@@ -23,5 +21,4 @@ | ||
static calcMax(array, property = null) { | ||
return max(array, function(d) { | ||
return property ? d[property] : d; | ||
}); | ||
const args = property ? [array, (d) => get(d, property)] : [array]; | ||
return max(...args); | ||
} | ||
@@ -38,3 +35,4 @@ | ||
static calcSum(array, property = null) { | ||
return property ? sumBy(array, (d) => d[property]) : sum(array); | ||
const args = property ? [array, (d) => get(d, property)] : [array]; | ||
return sum(...args); | ||
} | ||
@@ -50,5 +48,4 @@ | ||
static calcMin(array, property = null) { | ||
return min(array, function(d) { | ||
return property ? d[property] : d; | ||
}); | ||
const args = property ? [array, (d) => get(d, property)] : [array]; | ||
return min(...args); | ||
} | ||
@@ -65,8 +62,4 @@ | ||
return [ | ||
min(array, function(d) { | ||
return property ? d[property] : d; | ||
}), | ||
max(array, function(d) { | ||
return property ? d[property] : d; | ||
}), | ||
MathFunctions.calcMin(array, property), | ||
MathFunctions.calcMax(array, property), | ||
]; | ||
@@ -85,5 +78,4 @@ } | ||
static calcMedian(array, property = null) { | ||
return median(array, function(d) { | ||
return property ? d[property] : d; | ||
}); | ||
const args = property ? [array, (d) => get(d, property)] : [array]; | ||
return median(...args); | ||
} | ||
@@ -101,5 +93,4 @@ | ||
static calcMean(array, property = null) { | ||
return mean(array, function(d) { | ||
return property ? d[property] : d; | ||
}); | ||
const args = property ? [array, (d) => get(d, property)] : [array]; | ||
return mean(...args); | ||
} | ||
@@ -121,5 +112,10 @@ | ||
} | ||
const totalWeight = MathFunctions.calcSum(array, weightProperty); | ||
const weightedValues = array.map((d) => d[valueProperty] * d[weightProperty]); | ||
return MathFunctions.calcSum(weightedValues) / totalWeight; | ||
const result = array | ||
.map((d) => { | ||
const weight = get(d, weightProperty, 0); | ||
const upper = get(d, valueProperty, 0) * weight; | ||
return [upper, weight]; | ||
}) | ||
.reduce((acc, d) => [acc[0] + d[0], acc[1] + d[1]], [0, 0]); | ||
return result[0] / result[1]; | ||
} | ||
@@ -152,11 +148,13 @@ | ||
const dist = hist(array); | ||
const graphData = { | ||
labels: [], | ||
data: [], | ||
}; | ||
for (let bin of dist) { | ||
graphData.labels.push(`${bin.x0} - ${bin.x1}`); | ||
graphData.data.push(bin.length); | ||
} | ||
return graphData; | ||
return dist.reduce( | ||
(acc, {x0, x1, length}) => { | ||
acc.labels.push(`${x0} - ${x1}`); | ||
acc.data.push(length); | ||
return acc; | ||
}, | ||
{ | ||
labels: [], | ||
datasets: [], | ||
} | ||
); | ||
} | ||
@@ -177,3 +175,3 @@ | ||
.map(function(d) { | ||
return property ? d[property] : d; | ||
return property ? get(d, property) : d; | ||
}) | ||
@@ -180,0 +178,0 @@ .sort((a, b) => a - b); |
{ | ||
"name": "math-helper-functions", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Helper with misc. math functions such as sums, averages, max, min, etc", | ||
@@ -27,3 +27,6 @@ "main": "index.js", | ||
"devDependencies": { | ||
"eslint": "^5.16.0" | ||
"coveralls": "^3.1.0", | ||
"eslint": "^5.16.0", | ||
"eslint-plugin-jest": "^24.1.0", | ||
"jest": "^26.6.1" | ||
}, | ||
@@ -40,3 +43,8 @@ "dependencies": { | ||
"eslintIntegration": true | ||
}, | ||
"scripts": { | ||
"test": "jest", | ||
"prepublish": "yarn test", | ||
"coveralls": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js" | ||
} | ||
} |
32072
7
648
4