(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; // truerutil; // false
isArray(arr)
rutil; // true
merge(obj1, obj2)
var obj1 = foo: 'bar' baz: 1234; var obj2 = foo: 'qux'; var obj3 = rutil; obj3 // {foo: "qux", baz: 1234}
serialize(obj)
var obj = uid: 123 t: 'foo' 'bar' o: baz: 'qux' q: 'foo bar'; rutil; // 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; shuffled // ['c', 'e', 'b', 'd', 'f', 'a']
getParams([url])
// current url: http://example.com/?foo=bar&baz=qux rutil; // {foo: "bar", baz: "qux"}
var url = 'http://example.com/?foo=bar&baz=qux'; rutil // {foo: "bar", baz: "qux"}
setQueryStringParam(uri, key, val)
var uri = 'http://example.com?foo=bar'; uri = rutil; uri // http://example.com?foo=qux
generateUUID()
rutil; // 049128ed-b16c-4689-90d2-e910860d2797
generateRandomString([length], [str])
rutil; // Ne46OxeEbWeDdFSDmwbOq4kfGkoKlMSh rutil; // mOPJBXXc9MR7nQf8 rutil; // 388048
random(min, max)
rutil; // 6
hexToRgb(hex)
var hex = '#0077aa'; var rgb = rutil; // {"r":0,"g":119,"b":170} rgbg // 119
getDatesInbetween(dateObj1, dateObj2)
var from = 20131022;var until = 20131125; var dates = rutil; dates;
parseHashtag(str, url)
var string = '#foo #bar'; var linkifiedString = rutil; 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; 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; 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; text // foo
formatPhone(num)
var phone = 1234567890; var formattedPhone = rutil; formattedPhone // (123) 456-7890
isValidEmail(str)
var email = 'foo.bar-5@qux.com'; rutil; // true
isValidZip(num)
var zip = 12345; rutil; // true
isValidName(str)
rutil; // truerutil; // falserutil; // false
isValidUsername(str)
rutil // truerutil // truerutil // false
var zip = '12345-2453'; rutil; // true
isValidMinAge(dateObj, num)
var birthDate = 1998 02 20; rutil; // true
addCommas(num)
var number = 12345678901234; var numberWithCommas = rutil; numberWithCommas // 1,234,567,890.1234
repeat(str, [times])
rutil; // foofoorutil; // foofoofoofoofoo
pad(num)
rutil; // 10rutil; // 9
capitalize(str, [lowercase])
rutil; // Foorutil; // 'FooBarQux'rutil; // 'Foobarqux'
isMobileDevice([device])
rutil; // bool rutil; // bool
options: 'iphone', 'ipad', 'ios', 'ios7', 'android', 'blackberry', 'ie', 'opera', 'webos'
createPixel(url)
var url = 'http://example.com/pixel-tracker?id=1234567890'; rutil; // appends image tag to body
toBool(str)
var string = 'true'; rutil; // true
options: 'true', 'yes', 'on', '1'
sleep(num)
console;rutil;console;
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.