Minutils

Minimal utility methods for node and the browser

Download .zip Download .tar.gz View on GitHub

minutils

Build Status

Minimal utility methods for node and the browser.

How does one acquire this library?

Node: npm install minutils. require 'minutils'

Browser: Copy either minutils.js or minutils.min.js (in the root of this repo) into your project. Inlcude on page. Then access it at window.mu.

Why not use underscore?

Because I don't necessarily need to bring in something the size of underscore in small projects. Because there may be methods in this library that aren't in underscore (or other libraries). Because it's fun. Because.

API

Objects

Strings

Arrays

Functions

Collections

Objects

extend(destination, objects*)

Copies all properties from all objects into destination, replacing any existing properties of the same name.

var person = {name: 'Richard'}
extend(person, {name: 'Lisa'}, {age: 124}, {alive: false})

console.log(person) // {name: 'Lisa', age: 124, alive: false}

defaults(destination, objects*)

Takes all properties from objects and copies them in to destination only if the properties are undefined in destination

person = {name: 'Richard'}
defaults(person, {name: 'Lisa'}, {age: 124}, {alive: false})

console.log(person) // {name: 'Richard', age: 124, alive: false}

clone(object)

Returns a shallow copy of object.

clone({attr: true}) // {attr: true}

isObject(object)

Determines whether object is an Object.

isObject([]) // true
isObject({}) // true
isObject('') // false

isArray(object)

Is the native isArray if exists. Determines whether an object is a real array.

isArray(['first element']) // true
isArray({'0': 'first element', length: 1}) // false
isArray(arguments) // false

isString(object)

Determines whether object is a String.

isString('s') // true
isString({'0': 's'}) // false

isNumber(object)

Determines whether object is a Number.

isNumber(10) // true
isNumber('10') // false

isFunction(object)

Determines whether object is a Function.

isFunction(function(){}) // true
isFunction({}) // false

isArguments(object)

Determines whether object is the arguments object.

(function(){
  isArguments(arguments) // true
  isArguments({}) //false
})()

isBoolean

Determines whether object is a Boolean.

isBoolean(true) // true
isBoolean(false) // true
isBoolean(true) // false

isRegExp(object)

Determines whether object is a RegExp.

isRegExp(//) // true
isRegExp(10) // false

isDate(object)

Determines whether object is a Date.

isDate(new Date) // true
isDate(10) // false

isEmpty(object)

Determines whether object has any values.

isEmpty(null) // true
isEmpty(undefined) // true
isEmpty({}) // true
isEmpty([]) // true
isEmpty('') // true

isEmpty({key: 'val'}) // false
isEmpty([1]) // false
isEmpty('string') // false

has(object, key)

Determines whether object has own key.

has({toString: ''}, 'toString') // true
has({}, 'toString') // false

Strings

isBlank(string)

Is true if a string contains only whitespace characters.

isBlank('') // true
isBlank('         ') // true
isBlank('\n\t\r \t \t \n \n  \r') // true

isBlank('      c   ') // false

Arrays

first(array, n = 1)

Alias: head

Returns the first n elements of array or the first element if n is 1 (the default).

first([1, 2, 3, 4, 5]) // 1
first([1, 2, 3, 4, 5], 3) // [1, 2, 3]

last(array, n = 1)

Returns the last n elements of the array or the last element of the array if n is 1 (the default).

last([1, 2, 3, 4, 5]) // 5
last([1, 2, 3, 4, 5], 3) // [3, 4, 5]

rest(array, n = 1)

Alias: tail

Returns the rest of the array starting at index n (default is 1).

rest([1, 2, 3, 4, 5]) // [2, 3, 4, 5]
rest([1, 2, 3, 4, 5], 3) // [4, 5]

Functions

bind(fn, object[, defaults*])

Returns a function which will always call fn with a this value of object. Optionally accepts default arguments and will return a partially applied function.

var obj = {name: 'the dude!'}

fn = function(greeting, greeting2){
  return greeting + ' ' + greeting2 + ' ' + this.name
}

fn('hello', 'hi') // 'hello hi '

bind(fn, obj)('hello', 'hi') // 'hello hi the dude!'
bind(fn, obj, 'hello')('hi') // 'hello hi the dude!'
bind(fn, obj, 'hello', 'hi')() // 'hello hi the dude!'

Collections

each(coll, fn[, context])

Applies fn to each element in coll. Returns undefined. context is fn's this value.

If coll is an array, fn will be called with (element, index, coll). If coll is a JavaScript object, then fn will be called with (value, key, coll).

each([1, 2, 3], console.log, console)
/*
  1 0 [1, 2, 3]
  2 1 [1, 2, 3]
  3 2 [1, 2, 3]
*/
each({one: 1, two: 2, three: 3}, console.log, console)
/*
  1 'one' {one: 1, two: 2, three: 3}
  2 'two' {one: 1, two: 2, three: 3}
  3 'three' {one: 1, two: 2, three: 3}
*/

map(coll, fn[, context])

Returns an array of the return values of applying fn to each element in coll.

If coll is an array, fn will be called with (element, index, coll). If coll is a JavaScript object, then fn will be called with (value, key, coll).

var square = function(n) {return n*n}
map([2, 4], square) // [4, 16]
map({two: 2, four: 4}, square) // [4, 16]

reduce(coll, fn[, memo[, context]])

Returns a single value from a list of values.

If coll is an array, fn will be called with (memo, element, index, coll). If coll is a JavaScript object, then fn will be called with (memo, value, key, coll).

var sum = function(x, y){return x+y}
reduce([1, 2, 3], sum, 0) // 6
reduce({one: 1, two: 2, three: 3}, sum, 0) // 6

partition(coll, fn[, context])

For Array, String and Arguments objects, returns an array containing two arrays. For anything other than Array, String or Arguments objects, returns an array of two objects. The first array or object contains all the values which the fn evalutates as truthy, the second contains the rest.

If coll is an array, fn will be called with (element, index, coll). If coll is a JavaScript object, then fn will be called with (value, key, coll).

var isEven = function(n) {return n % 2 == 0};
partition([1, 2, 3, 4, 5], isEven) // [[2, 4], [1, 3, 5]]

console.log(partition({one: 1, two: 2, three: 3}, isEven))
/*
  [
    {two: 2},
    {one: 1, three: 3}
  ]
*/