Package Exports
- supergeneric
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (supergeneric) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
supergeneric
Just a collection of my favorite bespoke functions that I manage to work into nearly every project. Added to NPM for my convenience (and yours, if you happen to use it)!
Importing
import { sum } from 'supergeneric'
// or for the tree-shaking minimalists...
import { sum } from 'supergeneric/sum'Migrating from v1.x to v2.x
Previously, functions were grouped into collections, such as import { sum } from 'supergeneric/math'. This is no longer the case. All functions are named exports from the base module, or may be referenced directly by name (e.g. "supergeneric/sum").
// version 1.x
import { sum } from 'supergeneric/math'
// version 2.x
import { sum } from 'supergeneric'API
- ascending - ascending sort function
[7, 1, 4, 2].sort(ascending) // [1, 2, 4, 7]
- average(values: number[]): number - returns the average of an array of numbers
average([7, 1, 4, 2]) // 3.5
- binarySearch
- console - a color-injected version of
window.console. Only the first argument (string) will be colored... the rest will be left alone.console.magenta('foo', 'bar') // foo bar (foo in magenta text) // available colors console.blue console.cyan console.green console.grey console.magenta console.orange console.purple console.red console.teal
- convert
- dates
- descending - descending sort function
[7, 1, 4, 2].sort(descending) // [7, 4, 2, 1]
- first - first element in an array
first([7, 1, 4, 2]) // 7
- generateHash(length:number = 6): string - generates an alpha-numeric (alpha on first letter) key of
lengthcharactersgenerateHash() // RUaLy4 generateHash(4) // w9Y7
- getMilliseconds
- last - last element in an array
last([7, 1, 4, 2]) // 2
- mad
- makePath
- max(values: number[]): number - returns the largest number in values, shorthand for
Math.max(...values)max([7, 1, 4, 2]) // 7
- median
- merge(...items: object[]): object - merges all items, shorthand for
Object.merge(...items)merge({ age: 1 }, { name: 'Mittens' }) // { age: 1, name: 'Mittens' }
- mergeClean(...items: object[]): object - same as merge, but removes empty properties
merge({ age: 1, pets: undefined }, { name: 'Mittens' }) // { age: 1, name: 'Mittens' }
- min(values: number[]): number - returns the smallest number in values shorthand for
Math.min(...values)min([7, 1, 4, 2]) // 1
- numbers(item: any): any - returns a number if the item can be parsed as a number, otherwise leaves it unchanged. Used in mapping functions.
['12', 4, '3.14', 'foo'].map(numbers) // [12, 4, 3.14, 'foo']
- onlyNumbers(items: any[]): number[] - returns only the numeric elements in items
onlyNumbers([1, 'foo', undefined, 5, NaN]) // [1, 5]
- random(min: number, max: number): number - returns a random number between min and max, inclusive.
random(1,9) // 7
- randomArray(length: number, filler?: function = () => Math.random()): any[] - returns an Array of length, filled by the filler function (Math.random by default, returning float values from 0 to 1).
randomArray(4) // [0.1231, 0.9999, 0.4612, 0] randomArray(8, () => randomItem('ABC')) // ACCABACA
- randomItem(items: any[]): any - returns a randomly-selected item from array (or string) items
randomItem('foobarbaz') // a randomItem([8, 7, 4, 1]) // 7
- required(message: string): Error - throws an error with message if called. Useful for assigning default values to this to force entry.
const foo(bar = required('bar is a required option of foo(bar)')) => `foo:${bar}:baz` foo('cat') // foocatbaz foo() // throws Error('bar is a required option of foo(bar)')
- round(value: number, precision: number = 0): number - rounds value by precision digits (default 0 for integer-rounding).
round(3.1415926) // 3 round(3.1415926, 2) // 3.14
- rounder(precision: number = 0) => round(value) - curried round function for mapping, etc.
const roundTo1Decimal = rounder(1) [67.14, 16.88, 1.16].map(roundTo1Decimal) // [67.1, 16.9, 1.2]
- sortBy
- stddev
- sum(values: number[]): number - returns the sum of values
sum([7, 1, 4, 2]) // 14