New Project, Monday

    This package has been deprecated

    Author message:

    For now, this project is deprecated. I no longer use or work on it, and doubt others are using it. If by any chance you were using this, and would like to see more of it in some form, let me know: justnvdm@gmail.com

    warped

    0.1.0 • Public • Published

    warped

    Build Status

    functions for pattern manipulation in javascript

    var vv = require('drainpipe'),
        warped = require('warped'),
        stringify = JSON.stringify,
        log = console.log,
        slice = warped.slice,
        repeat = warped.repeat,
        deepMap = warped.deepMap,
        randInt = warped.randInt
     
     
    vv([1, [2, [3, [4], [6, 7, 8]]]])
      (slice, -1)
      (deepMap, function(v) {
        return v % 2
          ? repeat(v, randInt(2, 3))
          : v * randInt(1, 10)
      })
      (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
    
    <script src="/bower_components/warped/warped.js"></script>

    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

    map([1, 2], add, 3)
    // => [4, 5]
     
    function add(a, b) {
      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.

    deepMap([1, [2, [3, 4]]], function(v) {
      return v + 1
    })
     
    // => [2, [3, [4, 5]]]

    rmAt(arr, i)

    Returns a shallow copy of array arr with the value at index i removed.

    rmAt([1, 2, 3, 4], 2)
    // => [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.

    rmAt([1, 2, 3, 4, 5, 6], 2, 4)
    // => [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.

    repeat(23, 3)
    // => [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.

    map([identity, 23, identity], run, 3)
    // => [3, 23, 3]

    exists(v)

    Returns false if v is undefined or null, and true if it is not.

    exists(null)  // => false
    exists(void 0)  // => false
    exists(23)  // => true

    identity(v)

    Simply returns v.

    identity(23)
    // => 23

    random

    Identical to Math.random.

    random()
    // => 0.029231984401121736

    randInt(n)

    Returns a random integer between 0 and n.

    randInt(10)
    // => 8

    randInt(lo, hi)

    Returns a random integer between lo and hi.

    randInt(10, 20)
    // => 13

    randIdx(arr)

    Returns a random index value in array arr.

    randIdx([4, 5, 6])
    // => 2

    randVal(arr)

    Returns a random value in array arr.

    randVal([4, 5, 6])
    // => 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:

    slice([1, 2, 3], 0, -1)
    // => [2, 3]

    List of the wrapped methods:

    Array.prototype.slice() Array.prototype.concat() Array.prototype.reverse()

    Install

    npm i warped

    DownloadsWeekly Downloads

    1

    Version

    0.1.0

    License

    MIT

    Last publish

    Collaborators

    • justinvdm