Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Port to TypeScript and expose typings #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules/
.tern-port
.DS_Store
yarn-error.log
stats.js
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
"version": "2.2.0",
"description": "A light statistical package that operates on numeric Arrays.",
"main": "stats.js",
"types": "./stats.d.ts",
"directories": {
"example": "examples",
"test": "test"
},
"scripts": {
"test": "node test/"
"test": "node test/",
"build": "tsc",
"prepare": "run-s build test"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -41,7 +44,9 @@
},
"devDependencies": {
"dice": "0.0.2",
"tape": "~4.6.3"
"npm-run-all": "^4.1.3",
"tape": "~4.6.3",
"typescript": "^3.0.3"
},
"homepage": "https://github.com/brycebaril/node-stats-lite"
}
21 changes: 21 additions & 0 deletions stats.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export declare function isNumber(n: any): boolean;
export declare function numbers(vals: any): number[];
export declare function nsort(vals: number[]): number[];
export declare function sum(vals: any): number;
export declare function mean(vals: any): number;
export declare function median(vals: any): number;
export declare function mode(vals: any): number | Set<number>;
export declare function valuesMinusMeanSquared(vals: any): any[];
export declare function populationVariance(vals: any): number;
export declare function sampleVariance(vals: any): number;
export declare function populationStdev(vals: any): number;
export declare function sampleStdev(vals: any): number;
export declare function percentile(vals: any, ptile: number): number;
export declare function histogram(vals: any, bins: any): {
values: any[];
bins: any;
binWidth: number;
binLimits: number[];
};
export declare const variance: typeof populationVariance;
export declare const stdev: typeof populationStdev;
51 changes: 20 additions & 31 deletions stats.js → stats.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
"use strict";

module.exports.numbers = numbers
module.exports.sum = sum
module.exports.mean = mean
module.exports.median = median
module.exports.mode = mode
module.exports.variance = populationVariance
module.exports.sampleVariance = sampleVariance
module.exports.populationVariance = populationVariance
module.exports.stdev = populationStdev
module.exports.sampleStdev = sampleStdev
module.exports.populationStdev = populationStdev
module.exports.percentile = percentile
module.exports.histogram = histogram

var isNumber = require("isnumber")

function numbers(vals) {
export function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}

export function numbers(vals): number[] {
var nums = []
if (vals == null)
return nums
Expand All @@ -28,11 +14,11 @@ function numbers(vals) {
return nums
}

function nsort(vals) {
export function nsort(vals: number[]) {
return vals.sort(function numericSort(a, b) { return a - b })
}

function sum(vals) {
export function sum(vals): number {
vals = numbers(vals)
var total = 0
for (var i = 0; i < vals.length; i++) {
Expand All @@ -41,13 +27,13 @@ function sum(vals) {
return total
}

function mean(vals) {
export function mean(vals): number {
vals = numbers(vals)
if (vals.length === 0) return NaN
return (sum(vals) / vals.length)
}

function median(vals) {
export function median(vals): number {
vals = numbers(vals)
if (vals.length === 0) return NaN

Expand All @@ -66,7 +52,7 @@ function median(vals) {

// Returns the mode of a unimodal dataset
// If the dataset is multi-modal, returns a Set containing the modes
function mode(vals) {
export function mode(vals): number | Set<number> {
vals = numbers(vals)
if (vals.length === 0) return NaN
var mode = NaN
Expand Down Expand Up @@ -105,7 +91,7 @@ function mode(vals) {
// This helper finds the mean of all the values, then squares the difference
// from the mean for each value and returns the resulting array. This is the
// core of the varience functions - the difference being dividing by N or N-1.
function valuesMinusMeanSquared(vals) {
export function valuesMinusMeanSquared(vals) {
vals = numbers(vals)
var avg = mean(vals)
var diffs = []
Expand All @@ -116,12 +102,12 @@ function valuesMinusMeanSquared(vals) {
}

// Population Variance = average squared deviation from mean
function populationVariance(vals) {
export function populationVariance(vals) {
return mean(valuesMinusMeanSquared(vals))
}

// Sample Variance
function sampleVariance(vals) {
export function sampleVariance(vals) {
var diffs = valuesMinusMeanSquared(vals)
if (diffs.length <= 1) return NaN

Expand All @@ -130,16 +116,16 @@ function sampleVariance(vals) {


// Population Standard Deviation = sqrt of population variance
function populationStdev(vals) {
export function populationStdev(vals) {
return Math.sqrt(populationVariance(vals))
}

// Sample Standard Deviation = sqrt of sample variance
function sampleStdev(vals) {
export function sampleStdev(vals) {
return Math.sqrt(sampleVariance(vals))
}

function percentile(vals, ptile) {
export function percentile(vals, ptile: number): number {
vals = numbers(vals)
if (vals.length === 0 || ptile == null || ptile < 0) return NaN

Expand All @@ -154,7 +140,7 @@ function percentile(vals, ptile) {
return (1 - fract) * vals[int_part] + fract * vals[Math.min(int_part + 1, vals.length - 1)]
}

function histogram (vals, bins) {
export function histogram (vals, bins) {
if (vals == null) {
return null
}
Expand Down Expand Up @@ -208,3 +194,6 @@ function histogram (vals, bins) {

return hist
}

export const variance = populationVariance
export const stdev = populationStdev
10 changes: 10 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"declaration": true,
"outDir": "."
},
"include": ["stats.ts"],
"exclude": ["node_modules"]
}
Loading