warped
functions for pattern manipulation in javascript
var vv = warped = stringify = JSONstringify log = consolelog slice = warpedslice repeat = warpedrepeat deepMap = warpeddeepMap randInt = warpedrandInt slice -1 deepMap { return v % 2 ? : v * } stringify log //[[6,[[3,3],[20],[24,[7,7],56]]]]
The use case driving warped's development is manipulating array-based patterns that are to be turned into music or visualised, so any functions it exposes are intended to be used for this purpose. If you are looking for an all-purpose utility library, libraries like lodash would make more sense. At the moment, warped is pretty bare and still needs some work before it'll be useful enough.
install
node:
$ npm install warped
var warped = require('warped')
browser:
$ bower install warped
api
map(arr, fn[, arg2[, ...]])
Maps the values in array arr
through a function fn
. If extra arguments are given, they are used as extra arguments to each call to fn
when mapping the values. This does, however, mean that the index value of each item isn't available as it is with the native .map
// => [4, 5] { return a + b}
deepMap(arr, fn[, arg2[, ...]])
Walks a nested array data structure arr
, mapping each non-array value through a function fn
. If extra arguments are given, they are used as extra arguments to each call to fn
when mapping the values.
// => [2, [3, [4, 5]]]
rmAt(arr, i)
Returns a shallow copy of array arr
with the value at index i
removed.
// => [1, 2, 4]
If i
is negative, it is resolved relative to the array's length. For example, if -1
is given for an array with length 3
, i
is resolved to index 2
.
rmAt(arr, start, end)
Returns a shallow copy of array arr
with the values between indexes start
and end
removed.
// => [1, 2, 4]
Similar to rmAt(arr, i)
, start
and end
can be negative values.
repeat(v, n)
Returns an array containing value v
repeated n
times.
// => [23, 23, 23]
run(v[, arg1[, arg2[, ...]]])
If v
is a function, it is called with the given arguments and the this
value run
is called with. If v
is not a function, it is simply returned.
// => [3, 23, 3]
exists(v)
Returns false
if v
is undefined
or null
, and true
if it is not.
// => false // => false // => true
identity(v)
Simply returns v
.
// => 23
random
Identical to Math.random
.
// => 0.029231984401121736
randInt(n)
Returns a random integer between 0
and n
.
// => 8
randInt(lo, hi)
Returns a random integer between lo
and hi
.
// => 13
randIdx(arr)
Returns a random index value in array arr
.
// => 2
randVal(arr)
Returns a random value in array arr
.
// => 5
wrappers
warped exposes wrappers around some of the native Array
functions to allow arrays to be given as the first argument instead of as the this
value. For example, Array.prototype.slice()
is wrapped to work like this:
// => [2, 3]
List of the wrapped methods:
Array.prototype.slice()
Array.prototype.concat()
Array.prototype.reverse()