Home

Awesome

\  / _. |  _  ._ _|_ o ._   _
 \/ (_| | (/_ | | |_ | | | (/_

Build Status JavaScript's Sister, and protector — inspired by Underscore; Valentine provides you with type checking, functional iterators, and common utility helpers such as waterfalls, queues, and parallels; all utilizing native JavaScript methods for optimal speed.

Deprecation notice

As of version 2.0.0 — Valentine no longer supports <= IE8 and <= Safari 4. It's been real, but time to move on. To access this level of support, use the 1.8 tag.

Browser usage:

<script src="valentine.js"></script>
<script>
  v.forEach(['a', 'b', 'c'], function (letter) {

  })
</script>

Node users

npm install valentine
var v = require('valentine')

// showcase object style
v(['a', 'b', 'c']).map(function (letter) {
  return letter.toUpperCase()
}).join(' '); // => 'A B C'

API

<h3>iterators</h3>

*callback is defined as:

// when array
function callback(item, index, array) {

}
// when object
function callback(key, value, object) {

}

**calback is defined as:

function callback(memo, item, index, array) {

}

utility

// use memo to cache expensive methods
var getAllTheDom = v.memo(function () {
  return v(document.getElementsByTagName('*')).toArray()
})
getAllTheDom().each(modifier)

parallel api

v.parallel(
  function (fn) {
    getTimeline(function (e, timeline) {
      fn(e, timeline)
    })
  }
, function (fn) {
    getUser(function (e, user) {
      fn(e, user)
    })
  }
, function (e, timeline, user) {
    if (e) return console.log(e)
    ok(timeline == 'one', 'first result is "one"')
    ok(user == 'two', 'second result is "two"')
  }
)

waterfall api

v.waterfall(
  function (callback) {
    callback(null, 'one', 'two')
  }
, function (a, b, callback) {
    console.log(a == 'one')
    console.log(b == 'two')
    callback(null, 'three')
  }
, function (c, callback) {
    console.log(c == 'three')
    callback(null, 'final result')
  }
, function (err, result) {
    console.log(!!err == false)
    console.log(result == 'final result')
  }
)

series api

v.series([
  function (callback) {
    setTimeout(callback, 2000)
  }
, function (callback) {
    process.nextTick(callback)
  }
, function (callback) {
    callback(null)
  }]
, function (err) {
    console.log('done')
  }
)

Queue api

var it = v.queue(
  function () {
    console.log('one')
    it.next()
  }
, function () {
    console.log('two')
    it.next()
  }
, function () {
    console.log('three')
  }
)
it.next()

throttle, debounce, throttleDebounce

window.onscroll = v.throttle(50, function (e) {
  // expensive scroll function
})

window.mousemove = v.debounce(500, function (e) {
  // user has paused momentarily
})

textarea.onkeypress = v.throttleDebounce(20000, 1000, function () {
  // autosave(this.value)
  // called after 1s if not called again within 1s
  // but guaranteed to be called within 20s
})

type checking

Each method returns a boolean

Object Style

v(['a', 'b', 'c']).map(function (letter) {
  return letter.toUpperCase()
}); // => ['A', 'B', 'C']

Chains

v(['a', 'b', [['c']], 0, false,,,null,['a', 'b', 'c']])
  .chain().flatten().compact().uniq().map(function (letter) {
    return letter.toUpperCase()
  }).value(); // => ['A', 'B', 'C']

Ender Support

ender add valentine

// available as a top level method on `$`
$.v(['a', ['virus'], 'b', 'c']).reject(function (el, i) {
  return $.is.arr(el[i])
})

// top level methods in bridge
$.each
  map
  merge
  extend
  toArray
  keys
  values
  trim
  bind
  curry
  parallel
  waterfall
  inArray
  queue

Or just require the valentine module

!function (v) {
  v(['a', ['virus'], 'b', 'c']).reject(function (el, i) {
    return v.is.arr(el[i])
  })
}(require('valentine'))

Developers

Care to contribute? Make your edits to src/valentine.js and get your environment up and running

npm install
make
make test
open tests/index.html

Happy iterating!