Nurturing Pluto's Martians

    rutil

    0.0.10 • Public • Published

    (Deprecated)

    These methods will be moved to individual modules that stand on their own.

    rutil

    A whole bunch of utilities.

    Install

    Available via Bower

    bower install rutil

    Available via npm

    npm install rutil

    Utilities

    isObject(obj])

    rutil.isObject({}); // true
    rutil.isObject([]); // false

    isArray(arr)

    rutil.isArray([]); // true

    merge(obj1, obj2)

    var obj1 = {
        foo: 'bar',
        baz: 1234
    };
     
    var obj2 = {
        foo: 'qux'
    };
     
    var obj3 = rutil.merge(obj1, obj2);
     
    obj3 // {foo: "qux", baz: 1234}

    serialize(obj)

    var obj = {
        uid: 123,
        t: [
            'foo',
            'bar'
        ],
        o: {baz: 'qux'},
        q: 'foo bar'
    };
     
    rutil.serialize(obj); // uid=123&t=foo&t=bar&o%5Bbaz%5D=qux&q=foo%20bar

    shuffle(arr)

    var arr = ['a', 'b', 'c', 'd', 'e', 'f'];
     
    var shuffled = rutil.shuffle(arr);
     
    shuffled // ['c', 'e', 'b', 'd', 'f', 'a']

    getParams([url])

    // current url: http://example.com/?foo=bar&baz=qux
     
    rutil.getParams(); // {foo: "bar", baz: "qux"}
    var url = 'http://example.com/?foo=bar&baz=qux';
     
    rutil.getParms(url) // {foo: "bar", baz: "qux"}

    setQueryStringParam(uri, key, val)

    var uri = 'http://example.com?foo=bar';
     
    uri = rutil.setQueryStringParam(uri, 'foo', 'qux');
     
    uri // http://example.com?foo=qux

    generateUUID()

    rutil.generateUUID(); // 049128ed-b16c-4689-90d2-e910860d2797

    generateRandomString([length], [str])

    rutil.generateRandomString(); // Ne46OxeEbWeDdFSDmwbOq4kfGkoKlMSh
     
    rutil.generateRandomString(16); // mOPJBXXc9MR7nQf8
     
    rutil.generateRandomString(6, '0123456789'); // 388048

    random(min, max)

    rutil.random(0,9); // 6

    hexToRgb(hex)

    var hex = '#0077aa';
     
    var rgb = rutil.hexToRgb(hex); // {"r":0,"g":119,"b":170}
     
    rgb.g // 119

    getDatesInbetween(dateObj1, dateObj2)

    var from = new Date(2013,10,22);
    var until = new Date(2013,11,25);
     
    var dates = rutil.getDatesInbetween(from, until);
     
    dates.forEach(function(date) {
      date // obj: Fri Nov 22 2013 00:00:00 GMT-0800 (PST)
    });

    parseHashtag(str, url)

    var string = '#foo #bar';
     
    var linkifiedString = rutil.parseHashtag(string, 'http://twitter.com/search?q={{tag}}');
     
    linkifiedString // <a href="http://twitter.com/search?q=%23foo">#foo</a> <a href="http://twitter.com/search?q=%23bar">#bar</a>

    parseUsername(str, url)

    var string = '@foo @bar';
     
    var linkifiedString = rutil.parseUsername(string, 'http://twitter.com/{{username}}');
     
    linkifiedString // <a href="http://twitter.com/foo">@foo</a> <a href="http://twitter.com/bar">@bar</a>

    parseUrl(str)

    var string = 'http://example.com/ http://github.com/';
     
    var linkifiedString = rutil.parseUrl(string);
     
    linkifiedString // <a href="http://example.com/">http://example.com/</a> <a href="http://github.com/">http://github.com/</a>

    stripTags(str)

    var htmlString = '<p><strong>foo</strong></p>';
     
    var text = rutil.stripTags(htmlString);
     
    text // foo

    formatPhone(num)

    var phone = 1234567890;
     
    var formattedPhone = rutil.formatPhone(phone);
     
    formattedPhone // (123) 456-7890

    isValidEmail(str)

    var email = 'foo.bar-5@qux.com';
     
    rutil.isValidEmail(email); // true

    isValidZip(num)

    var zip = 12345;
     
    rutil.isValidZip(zip); // true

    isValidName(str)

    rutil.isValidName('Foo'); // true
    rutil.isValidName('Foo*'); // false
    rutil.isValidName('Foo1'); // false

    isValidUsername(str)

    rutil.isValidUsername('foo') // true
    rutil.isValidUsername('foo_bar1') // true
    rutil.isValidUsername('foo-bar') // false
    var zip = '12345-2453';
     
    rutil.isValidZip(zip); // true

    isValidMinAge(dateObj, num)

    var birthDate = new Date(1998, 02, 20);
     
    rutil.isValidMinAge(birthDate, 18); // true

    addCommas(num)

    var number = 1234567890.1234;
     
    var numberWithCommas = rutil.addCommas(number);
     
    numberWithCommas // 1,234,567,890.1234

    repeat(str, [times])

    rutil.repeat('foo'); // foofoo
    rutil.repeat('foo', 5); // foofoofoofoofoo

    pad(num)

    rutil.pad(10); // 10
    rutil.pad(9); // 9

    capitalize(str, [lowercase])

    rutil.capitalize('foo'); // Foo
    rutil.capitalize('fooBarQux'); // 'FooBarQux'
    rutil.capitalize('fooBarQux', true); // 'Foobarqux'

    isMobileDevice([device])

    rutil.isMobileDevice(); // bool
     
    rutil.isMobileDevice('ios'); // bool
    options: 'iphone', 'ipad', 'ios', 'ios7', 'android', 'blackberry', 'ie', 'opera', 'webos'
    

    createPixel(url)

    var url = 'http://example.com/pixel-tracker?id=1234567890';
     
    rutil.createPixel(url); // appends image tag to body

    toBool(str)

    var string = 'true';
     
    rutil.toBool(string); // true
    options: 'true', 'yes', 'on', '1'
    

    sleep(num)

    console.log('start sleep');
    rutil.sleep(50000);
    console.log('This will show after 5 seconds');

    More functions

    There's a ton more functions that I have not added to this readme yet. You can look at them in the source file or spec test file.

    Not documented yet, but can use:

    isTruthy
    isFalsy
    isFunction
    functor
    preCondition
    addEvent
    prop
    textNode
    wrap
    append
    hasClass
    addClass
    removeclass
    flatten
    sum
    int
    string
    compactobject
    isEmpty
    forOwn
    isExisty
    isString
    isNumber
    isBoolean
    noop
    setAttributes
    isValidCoordinate
    nth
    isIndexed
    appendStylesheet
    isDateInRange
    htmlContents
    elementById
    remove
    createElement
    anchor
    image
    appendScript
    pred
    comparator
    doWhen
    result
    executeIfHasField
    average
    reduce
    map
    filter
    size
    some
    every
    identity
    complement
    toArray
    first
    rest
    last
    seperatewords
    titleCase
    cat
    construct
    mapcat
    butLast
    

    Extend _

    To extend the _ when using libraries such as underscore or lodash, pass in rutil._() to the underscore mixin function. Rutil functions will not override underscore functions if they already exist, unless you pass rutil._(true)

    _.mixin(rutil._()); // extend underscore
    
    _.isValidEmail('foo@bar.com'); // use rutil function
    

    Note that you can also use the _u global object instead of rutil.

    Test

    Using Jasmine for testing

    grunt test
    

    Build

    grunt build
    

    License

    Released under the MIT License.

    Keywords

    none

    Install

    npm i rutil

    DownloadsWeekly Downloads

    5

    Version

    0.0.10

    License

    MIT

    Last publish

    Collaborators

    • miguelmota