purrjs v1.0.0
Purr.js
Simple JavaScript utility library with respect to FP
Installation
Library can be installed via downloading a git repo:
git clone https://github.com/happyCoda/purrjs.git
or from npm:
npm i purrjs
Functions
_checkType(thing) ⇒ Boolean
Checks entity's type
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
thing | Any | Entity to check type for |
_throwIfNotObjectOrArrayOrString(thing)
Throws if entity is not Object, Array or String. Wrapper method over _throwIfNotOneOfTypes
Kind: global function
Param | Type | Description |
---|---|---|
thing | Any | Entity to check type for |
_throwIfNotObjectOrArray(thing)
Throws if entity is not Object or Array. Wrapper method over _throwIfNotOneOfTypes
Kind: global function
Param | Type | Description |
---|---|---|
thing | Any | Entity to check type for |
_throwIfNotOneOfTypes(thing, types)
Throws if entity is not of one of types provided
Kind: global function
Param | Type | Description |
---|---|---|
thing | Any | Entity to check type for |
types | Array | Array of types to check against |
_throwWrongType(expected, given)
Throws exception with given explanations
Kind: global function
Param | Type | Description |
---|---|---|
expected | String | Expected type(s) |
given | String | Type provided |
_checkArity(args, arity) ⇒ Boolean
Checks if args given fits arity provided
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
args | Array | Arguments array |
arity | Number | Arity of a function |
_expose(global, item, name)
Makes any object available in global scope
Kind: global function
Param | Type | Description |
---|---|---|
global | Object | Any global object to include exposable |
item | Any | Entity for expose |
name | String | The name of the exposed entity |
_inspect(collection, deeper) ⇒ String
Inspects objects
Kind: global function
Returns: String - stringified Stringified collection representation
Param | Type | Description |
---|---|---|
collection | Object | Can be array or object |
deeper | Boolean | Wether we need trailing coma |
_setRecursionMaxDepth(value)
Sets maximum recursion depth (useful for walk method)
Kind: global function
Param | Type | Description |
---|---|---|
value | Number | New recursion depth |
isObject(thing) ⇒ Boolean
Checks if entity is an Object
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
thing | Any | Entity to check type for |
isArray(thing) ⇒ Boolean
Checks if entity is an Array
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
thing | Any | Entity to check type for |
isFunction(thing) ⇒ Boolean
Checks if entity is an Function
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
thing | Any | Entity to check type for |
isBoolean(thing) ⇒ Boolean
Checks if entity is an Boolean
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
thing | Any | Entity to check type for |
isNumber(thing) ⇒ Boolean
Checks if entity is an Number
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
thing | Any | Entity to check type for |
isString(thing) ⇒ Boolean
Checks if entity is an String
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
thing | Any | Entity to check type for |
isNull(thing) ⇒ Boolean
Checks if entity is an Null
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
thing | Any | Entity to check type for |
isUndefined(thing) ⇒ Boolean
Checks if entity is an Undefined
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
thing | Any | Entity to check type for |
isSymbol(thing) ⇒ Boolean
Checks if entity is an Symbol
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
thing | Any | Entity to check type for |
is(thing, type) ⇒ Boolean
Checks if entity has a given type
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
thing | Any | Entity to check type for |
type | String | Entity's type to check against |
isOneOfTypes(thing, types) ⇒ Boolean
Checks if entity has one of the given types
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
thing | Any | Entity to check type for |
types | Array | Entity's types to check against |
isTruthy(thing) ⇒ Boolean
Checks if entity has a truthy value
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
thing | Any | Entity to check |
isEmpty(thing) ⇒ Boolean
Checks if entity has an empty value ('', [], {})
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
thing | Any | Entity to check |
curry(func, arity, context) ⇒ function | Any
Performs function currying
Kind: global function
Returns: function | Any - Returns curried function
Param | Type | Default | Description |
---|---|---|---|
func | function | Function to curry | |
arity | Number | 2 | Number of arguments curried function expected |
context | Object | Context to apply curried function on |
compose(...args) ⇒ function
Composed functions
Kind: global function
Returns: function - Curried function
Param | Type | Description |
---|---|---|
...args | * | Functions to compose |
noop(thing) ⇒ Any
Function placeholder in case you need some callback. Simply returns given value
Kind: global function
Returns: Any - thing Some argument
Param | Type | Description |
---|---|---|
thing | Any | Some argument |
each(collection, func)
Iterates over collection and calls callback on every iteration
Kind: global function
Param | Type | Description |
---|---|---|
collection | Arrray | Object | String | Collection to iterate over |
func | function | Callback function to to call |
map(collection, func) ⇒ Arrray | Object | String
Iterates over collection and calls callback on every iteration
Kind: global function
Returns: Arrray | Object | String - collectionMapped New mapped collection
Param | Type | Description |
---|---|---|
collection | Arrray | Object | String | Collection to iterate over |
func | function | Callback function to call |
reduce(collection, func, acc) ⇒ Any
Reduces collection to single value
Kind: global function
Returns: Any - acc Reduced value
Param | Type | Description |
---|---|---|
collection | Arrray | Object | String | Collection to reduce |
func | function | Callback function to call |
acc | Any | Some initial accumulator value |
filter(collection, func, reverse) ⇒ Arrray | Object | String
Filter collection by criteria
Kind: global function
Returns: Arrray | Object | String - result Filtered collection
Param | Type | Default | Description |
---|---|---|---|
collection | Arrray | Object | String | Collection to filter | |
func | function | Array | Object | Callback function to call | |
reverse | Boolean | false | Determines whether filter should invert condition |
reject(collection, func) ⇒ Arrray | Object | String
Reject collection's items by criteria
Kind: global function
Returns: Arrray | Object | String - Rejected collection
Param | Type | Description |
---|---|---|
collection | Arrray | Object | String | Collection to reject |
func | function | Array | Object | Callback function to call |
all(collection, func) ⇒ Arrray | Object | String
Checks if all collection's items fit criteria
Kind: global function
Returns: Arrray | Object | String - Checked collection
Param | Type | Description |
---|---|---|
collection | Arrray | Object | String | Collection to check |
func | function | Callback function to call |
any(collection, func) ⇒ Arrray | Object | String
Checks if any collection's items fit criteria
Kind: global function
Returns: Arrray | Object | String - Checked collection
Param | Type | Description |
---|---|---|
collection | Arrray | Object | String | Collection to check |
func | function | Callback function to call |
times(num, func)
Calls the function a specified number of times
Kind: global function
Param | Type | Description |
---|---|---|
num | Number | Number of times to call the function |
func | function | Callback function to call |
find(collection, func) ⇒ Arrray | Object | Undefined
Searches element in collection
Kind: global function
Returns: Arrray | Object | Undefined - Value found or undefined
Param | Type | Description |
---|---|---|
collection | Arrray | Object | String | Collection to check |
func | function | Array | Object | Iteratee for checking |
contains(collection, valOrKey) ⇒ Boolean
Checks if element is in collection
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
collection | Arrray | Object | String | Collection to check |
valOrKey | Number | String | Value for checking |
destroy(collection, key) ⇒ Boolean
Deletes property from collection
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
collection | Arrray | Object | String | Collection to operate |
key | String | Property to delete |
groupBy(collection, criteria) ⇒ Object
Group collection's elements by given criteria
Kind: global function
Returns: Object - result Groupped collection
Param | Type | Description |
---|---|---|
collection | Object | Collection to operate |
criteria | String | Property to order collection by |
orderBy(collection, criteria, direction) ⇒ Array
Order collection's elements by given criteria and direction
Kind: global function
Returns: Array - result Ordered collection
Param | Type | Description |
---|---|---|
collection | Array | Collection to operate |
criteria | String | Property to order collection by |
direction | String | Ascendant or Descendant |
uniq(collection) ⇒ Object
Drops all collection's duplicates
Kind: global function
Returns: Object - result Unique collection
Param | Type | Description |
---|---|---|
collection | Array | Collection to operate |
toArray(arrayLike) ⇒ Array
Converts array like structures to array
Kind: global function
Returns: Array - result Converted value
Param | Type | Description |
---|---|---|
arrayLike | Object | Array like to convert |
debounce(fn, wait, asap) ⇒ function
Creates debounced version of the function given
Kind: global function
Returns: function - Debounced function
Param | Type | Default | Description |
---|---|---|---|
fn | function | The function to debounce | |
wait | Number | 200 | Timeout value |
asap | Boolean | false | Whether function shuld be called immediately |
pluck(collection, key) ⇒ Array
Creates new collection from the old one by the given key
Kind: global function
Returns: Array - result New collection of plucked values
Param | Type | Description |
---|---|---|
collection | Array | Object | The collection to pluck |
key | String | The key to pluck collection by |
omit(collection, key) ⇒ Object
Drops values from collection
Kind: global function
Returns: Object - obj Mutated collection
Param | Type | Description |
---|---|---|
collection | Object | The collection to omit |
key | String | The property to delete |
extend(collection, source) ⇒ Object
Extends collection
Kind: global function
Returns: Object - collection Extended collection
Param | Type | Description |
---|---|---|
collection | Object | The collection to extend |
source | Object | The extension source |
clone(collection) ⇒ Object
Clones collection
Kind: global function
Returns: Object - clone Clone of the collection
Param | Type | Description |
---|---|---|
collection | Object | The collection to clone |
mixin(collection, ...sources) ⇒ Object
Ads mixin's props to the collection
Kind: global function
Returns: Object - collection Mutated collection
Param | Type | Description |
---|---|---|
collection | Object | The collection to mutate |
...sources | * | The mixin sources |
chunk(collection, size, start) ⇒ Object
Gets collection chunk
Kind: global function
Returns: Object - Collection chunk
Param | Type | Default | Description |
---|---|---|---|
collection | Array | String | The collection to operate | |
size | Number | Chunk size | |
start | Number | 0 | Chunk start position |
size(collection) ⇒ Number
Calculates collection size
Kind: global function
Returns: Number - Collection size
Param | Type | Description |
---|---|---|
collection | Array | String | The collection to operate |
walk(collection, func, parent, depth)
Walks collection recursively
Kind: global function
Param | Type | Default | Description |
---|---|---|---|
collection | Array | Object | The collection to operate | |
func | function | Callback to call | |
parent | Array | Object | Parent entity | |
depth | Number | 0 | Walk's depth |
flatten(collection, flatArr)
Makes multidimensional arrays flat
Kind: global function
Param | Type | Description |
---|---|---|
collection | Array | The Array to flat |
flatArr | Array | New flattened array |
inject(obj, stuff) ⇒ Object
Injects logic from purr to given object
Kind: global function
Returns: Object - obj Mutated object
Param | Type | Description |
---|---|---|
obj | Object | Object to inject logic |
stuff | Array | String | Properties to inject |
namespace(parent, nsstring) ⇒ Object
Creates namespace on the given object or new object
Kind: global function
Returns: Object - parent Mutated object
Param | Type | Description |
---|---|---|
parent | Object | Object create namespace |
nsstring | String | String representation of namespace |
ensureImplements(obj, methods) ⇒ Boolean
Ensures than object implements given interface
Kind: global function
Returns: Boolean - true or false
Param | Type | Description |
---|---|---|
obj | Object | Object to operate |
methods | Array | String | Interface to implement |
randomNum(min, max) ⇒ Number
Generates random number
Kind: global function
Returns: Number - randNum Random number generated.
Param | Type | Description |
---|---|---|
min | Number | Minimum number boundary. |
max | Number | Maximum number boundary. |
Release History
- 2017-12-08 v1.0.0 v1.0.0 release
- 2013-08-07 v0.1.0 First release
6 years ago