43 KiB
Eruda Util Documentation
$
jQuery like style dom manipulator.
Available methods
offset, hide, show, first, last, get, eq, on, off, html, text, val, css, attr, data, rmAttr, remove, addClass, rmClass, toggleClass, hasClass, append, prepend, before, after
var $btn = $('#btn');
$btn.html('eustia');
$btn.addClass('btn');
$btn.show();
$btn.on('click', function ()
{
// Do something...
});
$attr
Element attribute manipulation.
Get the value of an attribute for the first element in the set of matched elements.
| Name | Type | Desc |
|---|---|---|
| element | string array element | Elements to manipulate |
| name | string | Attribute name |
| return | string | Attribute value of first element |
Set one or more attributes for the set of matched elements.
| Name | Type | Desc |
|---|---|---|
| element | string array element | Elements to manipulate |
| name | string | Attribute name |
| value | string | Attribute value |
| Name | Type | Desc |
|---|---|---|
| element | string array element | Elements to manipulate |
| attributes | object | Object of attribute-value pairs to set |
remove
Remove an attribute from each element in the set of matched elements.
| Name | Type | Desc |
|---|---|---|
| element | string array element | Elements to manipulate |
| name | string | Attribute name |
$attr('#test', 'attr1', 'test');
$attr('#test', 'attr1'); // -> test
$attr.remove('#test', 'attr1');
$attr('#test', {
'attr1': 'test',
'attr2': 'test'
});
$class
Element class manipulations.
add
Add the specified class(es) to each element in the set of matched elements.
| Name | Type | Desc |
|---|---|---|
| element | string array element | Elements to manipulate |
| names | string array | Classes to add |
has
Determine whether any of the matched elements are assigned the given class.
| Name | Type | Desc |
|---|---|---|
| element | string array element | Elements to manipulate |
| name | string | Class name |
| return | boolean | True if elements has given class name |
toggle
Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the state argument.
| Name | Type | Desc |
|---|---|---|
| element | string array element | Elements to manipulate |
| name | string | Class name to toggle |
remove
Remove a single class, multiple classes, or all classes from each element in the set of matched elements.
| Name | Type | Desc |
|---|---|---|
| element | string array element | Elements to manipulate |
| names | string | Class names to remove |
$class.add('#test', 'class1');
$class.add('#test', ['class1', 'class2']);
$class.has('#test', 'class1'); // -> true
$class.remove('#test', 'class1');
$class.has('#test', 'class1'); // -> false
$class.toggle('#test', 'class1');
$class.has('#test', 'class1'); // -> true
$css
Element css manipulation.
Get the computed style properties for the first element in the set of matched elements.
| Name | Type | Desc |
|---|---|---|
| element | string array element | Elements to manipulate |
| name | string | Property name |
| return | string | Css value of first element |
Set one or more CSS properties for the set of matched elements.
| Name | Type | Desc |
|---|---|---|
| element | string array element | Elements to manipulate |
| name | string | Property name |
| value | string | Css value |
| Name | Type | Desc |
|---|---|---|
| element | string array element | Elements to manipulate |
| properties | object | Object of css-value pairs to set |
$css('#test', {
'color': '#fff',
'background': 'black'
});
$css('#test', 'display', 'block');
$css('#test', 'color'); // -> #fff
$data
Wrapper of $attr, adds data- prefix to keys.
$data('#test', 'attr1', 'eustia');
$event
bind events to certain dom elements.
function clickHandler()
{
// Do something...
}
$event.on('#test', 'click', clickHandler);
$event.off('#test', 'click', clickHandler);
$insert
Insert html on different position.
before
Insert content before elements.
after
Insert content after elements.
prepend
Insert content to the beginning of elements.
append
Insert content to the end of elements.
| Name | Type | Desc |
|---|---|---|
| element | string array element | Elements to manipulate |
| content | string | Html strings |
// <div id="test"><div class="mark"></div></div>
$insert.before('#test', '<div>eris</div>');
// -> <div>eris</div><div id="test"><div class="mark"></div></div>
$insert.after('#test', '<div>eris</div>');
// -> <div id="test"><div class="mark"></div></div><div>eris</div>
$insert.prepend('#test', '<div>eris</div>');
// -> <div id="test"><div>eris</div><div class="mark"></div></div>
$insert.append('#test', '<div>eris</div>');
// -> <div id="test"><div class="mark"></div><div>eris</div></div>
$offset
Get the position of the element in document.
| Name | Type | Desc |
|---|---|---|
| element | string array element | Elements to get offset |
$offset('#test'); // -> {left: 0, top: 0, width: 0, height: 0}
$property
Element property html, text, val getter and setter.
html
Get the HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element.
text
Get the combined text contents of each element in the set of matched elements, including their descendants, or set the text contents of the matched elements.
val
Get the current value of the first element in the set of matched elements or set the value of every matched element.
$property.html('#test', 'eris');
$property.html('#test'); // -> eris
$remove
Remove the set of matched elements from the DOM.
| Name | Type | Desc |
|---|---|---|
| element | string array element | Elements to delete |
$remove('#test');
$safeEls
Convert value into an array, if it's a string, do querySelector.
| Name | Type | Desc |
|---|---|---|
| value | element array string | Value to convert |
| return | array | Array of elements |
$safeEls('.test'); // -> Array of elements with test class
$show
Show elements.
| Name | Type | Desc |
|---|---|---|
| element | string array element | Elements to show |
$show('#test');
Class
Create JavaScript class.
| Name | Type | Desc |
|---|---|---|
| methods | object | Public methods |
| [statics] | object | Static methods |
| return | function | Function used to create instances |
var People = Class({
initialize: function People(name, age)
{
this.name = name;
this.age = age;
},
introduce: function ()
{
return 'I am ' + this.name + ', ' + this.age + ' years old.';
}
});
var Student = People.extend({
initialize: function Student(name, age, school)
{
this.callSuper(People, 'initialize', arguments);
this.school = school;
},
introduce: function ()
{
return this.callSuper(People, 'introduce') + '\n I study at ' + this.school + '.';
}
}, {
is: function (obj)
{
return obj instanceof Student;
}
});
var a = new Student('allen', 17, 'Hogwarts');
a.introduce(); // -> 'I am allen, 17 years old. \n I study at Hogwarts.'
Student.is(a); // -> true
Emitter
Event emitter class which provides observer pattern.
on
Bind event.
off
Unbind event.
once
Bind event that trigger once.
| Name | Type | Desc |
|---|---|---|
| event | string | Event name |
| listener | function | Event listener |
emit
Emit event.
| Name | Type | Desc |
|---|---|---|
| event | string | Event name |
| ...args | * | Arguments passed to listener |
mixin
[static] Mixin object class methods.
| Name | Type | Desc |
|---|---|---|
| obj | object | Object to mixin |
var event = new Emitter();
event.on('test', function () { console.log('test') });
event.emit('test'); // Logs out 'test'.
Emitter.mixin({});
Select
Simple wrapper of querySelectorAll to make dom selection easier.
constructor
| Name | Type | Desc |
|---|---|---|
| selector | string | Dom selector string |
find
Get desdendants of current matched elements.
| Name | Type | Desc |
|---|---|---|
| selector | string | Dom selector string |
each
Iterate over matched elements.
| Name | Type | Desc |
|---|---|---|
| fn | function | Function to execute for each element |
var $test = new Select('#test');
$test.find('.test').each(function (idx, element)
{
// Manipulate dom nodes
});
Url
Simple url manipulator.
constructor
| Name | Type | Desc |
|---|---|---|
| [url=window.location] | string | Url string |
setQuery
Set query value.
| Name | Type | Desc |
|---|---|---|
| name | string | Query name |
| value | string | Query value |
| return | Url | this |
| Name | Type | Desc |
|---|---|---|
| names | object | query object |
| return | Url | this |
rmQuery
Remove query value.
| Name | Type | Desc |
|---|---|---|
| name | string array | Query name |
| return | Url | this |
parse
[static] Parse url into an object.
| Name | Type | Desc |
|---|---|---|
| url | string | Url string |
| return | object | Url object |
stringify
[static] Stringify url object into a string.
| Name | Type | Desc |
|---|---|---|
| url | object | Url object |
| return | string | Url string |
An url object contains the following properties:
| Name | Desc |
|---|---|
| protocol | The protocol scheme of the URL (e.g. http:) |
| slashes | A boolean which indicates whether the protocol is followed by two forward slashes (//) |
| auth | Authentication information portion (e.g. username:password) |
| hostname | Host name without port number |
| port | Optional port number |
| pathname | URL path |
| query | Parsed object containing query string |
| hash | The "fragment" portion of the URL including the pound-sign (#) |
var url = new Url('http://example.com:8080?eruda=true');
console.log(url.port); // -> '8080'
url.query.foo = 'bar';
url.rmQuery('eruda');
utl.toString(); // -> 'http://example.com:8080/?foo=bar'
ajax
Perform an asynchronous HTTP request.
| Name | Type | Desc |
|---|---|---|
| options | object | Ajax options |
Available options:
| Name | Type | Desc |
|---|---|---|
| url | string | Request url |
| data | string object | Request data |
| dataType=json | string | Response type(json, xml) |
| success | function | Success callback |
| error | function | Error callback |
| complete | function | Callback after request |
| timeout | number | Request timeout |
get
Shortcut for type = GET;
post
Shortcut for type = POST;
| Name | Type | Desc |
|---|---|---|
| url | string | Request url |
| [data] | string object | Request data |
| success | function | Success callback |
| dataType | function | Response type |
ajax({
url: 'http://example.com',
data: {test: 'true'},
error: function () {},
success: function (data)
{
// ...
},
dataType: 'json'
});
ajax.get('http://example.com', {}, function (data)
{
// ...
});
allKeys
Retrieve all the names of object's own and inherited properties.
| Name | Type | Desc |
|---|---|---|
| obj | object | Object to query |
| return | array | Array of all property names |
Members of Object's prototype won't be retrieved.
var obj = Object.create({zero: 0});
obj.one = 1;
allKeys(obj) // -> ['zero', 'one']
before
Create a function that invokes less than n times.
| Name | Type | Desc |
|---|---|---|
| n | number | Number of calls at which fn is no longer invoked |
| fn | function | Function to restrict |
| return | function | New restricted function |
Subsequent calls to the created function return the result of the last fn invocation.
$(element).on('click', before(5, function() {}));
// -> allow function to be call 4 times at last.
camelCase
Convert string to "camelCase".
| Name | Type | Desc |
|---|---|---|
| str | string | String to convert |
| return | string | Camel cased string |
camelCase('foo-bar'); // -> fooBar
camelCase('foo bar'); // -> fooBar
camelCase('foo_bar'); // -> fooBar
camelCase('foo.bar'); // -> fooBar
contain
Check if the value is present in the list.
| Name | Type | Desc |
|---|---|---|
| array | array object | Target list |
| value | * | Value to check |
| return | boolean | True if value is present in the list |
contain([1, 2, 3], 1); // -> true
contain({a: 1, b: 2}, 1); // -> true
cookie
Simple api for handling browser cookies.
get
Get cookie value.
| Name | Type | Desc |
|---|---|---|
| key | string | Cookie key |
| return | string | Corresponding cookie value |
set
Set cookie value.
| Name | Type | Desc |
|---|---|---|
| key | string | Cookie key |
| val | string | Cookie value |
| [options] | object | Cookie options |
| return | exports | Module cookie |
remove
Remove cookie value.
| Name | Type | Desc |
|---|---|---|
| key | string | Cookie key |
| [options] | object | Cookie options |
| return | exports | Module cookie |
cookie.set('a', '1', {path: '/'});
cookie.get('a'); // -> '1'
cookie.remove('a');
createAssigner
Used to create extend, extendOwn and defaults.
| Name | Type | Desc |
|---|---|---|
| keysFn | function | Function to get object keys |
| defaults | boolean | No override when set to true |
| return | function | Result function, extend... |
dateFormat
Simple but extremely useful date format function.
| Name | Type | Desc |
|---|---|---|
| [date=new Date] | Date | Date object to format |
| mask | string | Format mask |
| [utc=false] | boolean | UTC or not |
| [gmt=false] | boolean | GMT or not |
| Mask | Description |
|---|---|
| d | Day of the month as digits; no leading zero for single-digit days |
| dd | Day of the month as digits; leading zero for single-digit days |
| ddd | Day of the week as a three-letter abbreviation |
| dddd | Day of the week as its full name |
| m | Month as digits; no leading zero for single-digit months |
| mm | Month as digits; leading zero for single-digit months |
| mmm | Month as a three-letter abbreviation |
| mmmm | Month as its full name |
| yy | Year as last two digits; leading zero for years less than 10 |
| yyyy | Year represented by four digits |
| h | Hours; no leading zero for single-digit hours (12-hour clock) |
| hh | Hours; leading zero for single-digit hours (12-hour clock) |
| H | Hours; no leading zero for single-digit hours (24-hour clock) |
| HH | Hours; leading zero for single-digit hours (24-hour clock) |
| M | Minutes; no leading zero for single-digit minutes |
| MM | Minutes; leading zero for single-digit minutes |
| s | Seconds; no leading zero for single-digit seconds |
| ss | Seconds; leading zero for single-digit seconds |
| l L | Milliseconds. l gives 3 digits. L gives 2 digits |
| t | Lowercase, single-character time marker string: a or p |
| tt | Lowercase, two-character time marker string: am or pm |
| T | Uppercase, single-character time marker string: A or P |
| TT | Uppercase, two-character time marker string: AM or PM |
| Z | US timezone abbreviation, e.g. EST or MDT |
| o | GMT/UTC timezone offset, e.g. -0500 or +0230 |
| S | The date's ordinal suffix (st, nd, rd, or th) |
| UTC: | Must be the first four characters of the mask |
dateFormat('isoDate'); // -> 2016-11-19
dateFormat('yyyy-mm-dd HH:MM:ss'); // -> 2016-11-19 19:00:04
dateFormat(new Date(), 'yyyy-mm-dd'); // -> 2016-11-19
defaults
Fill in undefined properties in object with the first value present in the following list of defaults objects.
| Name | Type | Desc |
|---|---|---|
| obj | object | Destination object |
| *src | object | Sources objects |
| return | object | Destination object |
defaults({name: 'RedHood'}, {name: 'Unknown', age: 24}); // -> {name: 'RedHood', age: 24}
delegate
Event delegation.
add
Add event delegation.
| Name | Type | Desc |
|---|---|---|
| el | element | Parent element |
| type | string | Event type |
| selector | string | Match selector |
| cb | function | Event callback |
remove
Remove event delegation.
var container = document.getElementById('container');
function clickHandler()
{
// Do something...
}
delegate.add(container, 'click', '.children', clickHandler);
delegate.remove(container, 'click', '.children', clickHandler);
each
Iterates over elements of collection and invokes iteratee for each element.
| Name | Type | Desc |
|---|---|---|
| obj | object array | Collection to iterate over |
| iteratee | function | Function invoked per iteration |
| [ctx] | * | Function context |
each({'a': 1, 'b': 2}, function (val, key) {});
endWith
Check if string ends with the given target string.
| Name | Type | Desc |
|---|---|---|
| str | string | The string to search |
| suffix | string | String suffix |
| return | boolean | True if string ends with target |
endWith('ab', 'b'); // -> true
escape
Escapes a string for insertion into HTML, replacing &, <, >, ", `, and ' characters.
| Name | Type | Desc |
|---|---|---|
| str | string | String to escape |
| return | string | Escaped string |
escape('You & Me'); -> // -> 'You & Me'
escapeJsonStr
No documentation.
escapeRegExp
Escape special chars to be used as literals in RegExp constructors.
| Name | Type | Desc |
|---|---|---|
| str | string | String to escape |
| return | string | Escaped string |
escapeRegExp('[eris]'); // -> '\\[eris\\]'
evalCss
No documentation.
extend
Copy all of the properties in the source objects over to the destination object.
| Name | Type | Desc |
|---|---|---|
| obj | object | Destination object |
| ...src | object | Sources objects |
| return | object | Destination object |
extend({name: 'RedHood'}, {age: 24}); // -> {name: 'RedHood', age: 24}
extendOwn
Like extend, but only copies own properties over to the destination object.
| Name | Type | Desc |
|---|---|---|
| obj | object | Destination object |
| *src | object | Sources objects |
| return | object | Destination object |
extendOwn({name: 'RedHood'}, {age: 24}); // -> {name: 'RedHood', age: 24}
fileSize
Turn bytes into human readable file size.
| Name | Type | Desc |
|---|---|---|
| bytes | number | File bytes |
| return | string | Readable file size |
fileSize(5); // -> '5'
fileSize(1500); // -> '1.46K'
fileSize(1500000); // -> '1.43M'
fileSize(1500000000); // -> '1.4G'
fileSize(1500000000000); // -> '1.36T'
filter
Iterates over elements of collection, returning an array of all the values that pass a truth test.
| Name | Type | Desc |
|---|---|---|
| obj | array | Collection to iterate over |
| predicate | function | Function invoked per iteration |
| [ctx] | * | Predicate context |
| return | array | Array of all values that pass predicate |
filter([1, 2, 3, 4, 5], function (val)
{
return val % 2 === 0;
}); // -> [2, 4]
getFileName
dependencies last trim
getObjType
dependencies upperFirst
has
Checks if key is a direct property.
| Name | Type | Desc |
|---|---|---|
| obj | object | Object to query |
| key | string | Path to check |
| return | boolean | True if key is a direct property |
has({one: 1}, 'one'); // -> true
identity
Return the first argument given.
| Name | Type | Desc |
|---|---|---|
| val | * | Any value |
| return | * | Given value |
identity('a'); // -> 'a'
idxOf
Get the index at which the first occurrence of value.
| Name | Type | Desc |
|---|---|---|
| arr | array | Array to search |
| val | * | Value to search for |
| [fromIdx=0] | number | Index to search from |
idxOf([1, 2, 1, 2], 2, 2); // -> 3
inherits
Inherit the prototype methods from one constructor into another.
| Name | Type | Desc |
|---|---|---|
| Class | function | Child Class |
| SuperClass | function | Super Class |
function People(name)
{
this._name = name;
}
People.prototype = {
getName: function ()
{
return this._name;
}
};
function Student(name)
{
this._name = name;
}
inherits(Student, People);
var s = new Student('RedHood');
s.getName(); // -> 'RedHood'
isArgs
Check if value is classified as an arguments object.
| Name | Type | Desc |
|---|---|---|
| value | * | Value to check |
| return | boolean | True if value is an arguments object |
(function () {
isArgs(arguments); // -> true
})();
isArr
Check if value is an Array object.
| Name | Type | Desc |
|---|---|---|
| val | * | The value to check |
| return | boolean | True if value is an Array object |
isArr([]); // -> true
isArr({}); // -> false
isArrLike
Check if value is array-like.
| Name | Type | Desc |
|---|---|---|
| value | * | Value to check |
| return | boolean | True if value is array like |
Function returns false.
isArrLike('test'); // -> true
isArrLike(document.body.children); // -> true;
isArrLike([1, 2, 3]); // -> true
isBool
Check if value is a boolean primitive.
| Name | Type | Desc |
|---|---|---|
| val | * | Value to check |
| return | boolean | True if value is a boolean |
isBool(true); // -> true
isBool(false); // -> true
isBool(1); // -> false
isBrowser
Check if running in a browser.
console.log(isBrowser); // -> true if running in a browser
isCrossOrig
dependencies startWith
isDate
Check if value is classified as a Date object.
| Name | Type | Desc |
|---|---|---|
| val | * | value to check |
| return | boolean | True if value is a Date object |
isDate(new Date()); // -> true
isEl
Check if value is a DOM element.
| Name | Type | Desc |
|---|---|---|
| val | * | Value to check |
| return | boolean | True if value is a DOM element |
isEl(document.body); // -> true
isEmpty
Check if value is an empty object or array.
| Name | Type | Desc |
|---|---|---|
| val | * | Value to check |
| return | boolean | True if value is empty |
isEmpty([]); // -> true
isEmpty({}); // -> true
isEmpty(''); // -> true
isErr
Check if value is an error.
| Name | Type | Desc |
|---|---|---|
| val | * | Value to check |
| return | boolean | True if value is an error |
isErr(new Error()); // -> true
isErudaEl
No documentation.
isFn
Check if value is a function.
| Name | Type | Desc |
|---|---|---|
| val | * | Value to check |
| return | boolean | True if value is a function |
Generator function is also classified as true.
isFn(function() {}); // -> true
isFn(function*() {}); // -> true
isMatch
Check if keys and values in src are contained in obj.
| Name | Type | Desc |
|---|---|---|
| obj | object | Object to inspect |
| src | object | Object of property values to match |
| return | boolean | True if object is match |
isMatch({a: 1, b: 2}, {a: 1}); // -> true
isMobile
Check whether client is using a mobile browser using ua.
| Name | Type | Desc |
|---|---|---|
| [ua=navigator.userAgent] | string | User agent |
| return | boolean | True if ua belongs to mobile browsers |
isMobile(navigator.userAgent);
isNull
Check if value is an Null.
| Name | Type | Desc |
|---|---|---|
| value | * | Value to check |
| return | boolean | True if value is an Null |
isNull(null); // -> true
isNum
Checks if value is classified as a Number primitive or object.
| Name | Type | Desc |
|---|---|---|
| value | * | Value to check |
| return | boolean | True if value is correctly classified |
isObj
Check if value is the language type of Object.
| Name | Type | Desc |
|---|---|---|
| val | * | Value to check |
| return | boolean | True if value is an object |
isObj({}); // -> true
isObj([]); // -> true
isRegExp
Check if value is a regular expression.
| Name | Type | Desc |
|---|---|---|
| val | * | Value to check |
| return | boolean | True if value is a regular expression |
isRegExp(/a/); // -> true
isStr
Check if value is a string primitive.
| Name | Type | Desc |
|---|---|---|
| val | * | Value to check |
| return | boolean | True if value is a string primitive |
isStr('eris'); // -> true
isUndef
Check if value is undefined.
| Name | Type | Desc |
|---|---|---|
| val | * | Value to check |
| return | boolean | True if value is undefined |
isUndef(void 0); // -> true
isUndef(null); // -> false
kebabCase
Convert string to "kebabCase".
| Name | Type | Desc |
|---|---|---|
| str | string | String to convert |
| return | string | Kebab cased string |
kebabCase('fooBar'); // -> foo-bar
kebabCase('foo bar'); // -> foo-bar
kebabCase('foo_bar'); // -> foo-bar
kebabCase('foo.bar'); // -> foo-bar
keys
Create an array of the own enumerable property names of object.
| Name | Type | Desc |
|---|---|---|
| obj | object | Object to query |
| return | array | Array of property names |
keys({a: 1}); // -> ['a']
last
Get the last element of array.
| Name | Type | Desc |
|---|---|---|
| arr | array | The array to query |
| return | * | The last element of array |
last([1, 2]); // -> 2
loadJs
Inject script tag into page with given src value.
| Name | Type | Desc |
|---|---|---|
| src | string | Script source |
| cb | function | Onload callback |
loadJs('main.js', function ()
{
// Do something...
});
lpad
Pad string on the left side if it's shorter than length.
| Name | Type | Desc |
|---|---|---|
| str | string | String to pad |
| len | number | Padding length |
| [chars] | string | String used as padding |
| return | string | Resulted string |
lpad('a', 5); // -> ' a'
lpad('a', 5, '-'); // -> '----a'
lpad('abc', 3, '-'); // -> 'abc'
lpad('abc', 5, 'ab'); // -> 'ababc'
ltrim
Remove chars or white-spaces from beginning of string.
| Name | Type | Desc |
|---|---|---|
| str | string | String to trim |
| chars | string array | Characters to trim |
| return | string | Trimmed string |
ltrim(' abc '); // -> 'abc '
ltrim('_abc_', '_'); // -> 'abc_'
ltrim('_abc_', ['a', '_']); // -> 'bc_'
map
Create an array of values by running each element in collection through iteratee.
| Name | Type | Desc |
|---|---|---|
| obj | array object | Collection to iterate over |
| iteratee | function | Function invoked per iteration |
| [ctx] | * | Function context |
| return | array | New mapped array |
map([4, 8], function (n) { return n * n; }); // -> [16, 64]
matcher
Return a predicate function that checks if attrs are contained in an object.
| Name | Type | Desc |
|---|---|---|
| attrs | object | Object of property values to match |
| return | function | New predicate function |
var objects = [
{a: 1, b: 2, c: 3 },
{a: 4, b: 5, c: 6 }
];
filter(objects, matcher({a: 4, c: 6 })); // -> [{a: 4, b: 5, c: 6 }]
memStorage
Memory-backed implementation of the Web Storage API.
A replacement for environments where localStorage or sessionStorage is not available.
var localStorage = window.localStorage || memStorage;
localStorage.setItem('test', 'eris');
memoize
Memoize a given function by caching the computed result.
| Name | Type | Desc |
|---|---|---|
| fn | function | Function to have its output memoized |
| [hashFn] | function | Function to create cache key |
| return | function | New memoized function |
var fibonacci = memoize(function(n)
{
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
});
noop
A no-operation function.
noop(); // Does nothing
now
Gets the number of milliseconds that have elapsed since the Unix epoch.
now(); // -> 1468826678701
objToStr
Alias of Object.prototype.toString.
| Name | Type | Desc |
|---|---|---|
| value | * | Source value |
| return | string | String representation of given value |
objToStr(5); // -> '[object Number]'
once
Create a function that invokes once.
| Name | Type | Desc |
|---|---|---|
| fn | function | Function to restrict |
| return | function | New restricted function |
function init() {};
var initOnce = once(init);
initOnce();
initOnce(); // -> init is invoked once
optimizeCb
Used for function context binding.
orientation
dependencies Emitter
partial
Partially apply a function by filling in given arguments.
| Name | Type | Desc |
|---|---|---|
| fn | function | Function to partially apply arguments to |
| ...partials | * | Arguments to be partially applied |
| return | function | New partially applied function |
var sub5 = partial(function (a, b) { return b - a }, 5);
sub(20); // -> 15
pxToNum
dependencies toNum
query
Parse and stringify url query strings.
parse
Parse a query string into an object.
| Name | Type | Desc |
|---|---|---|
| str | string | Query string |
| return | object | Query object |
stringify
Stringify an object into a query string.
| Name | Type | Desc |
|---|---|---|
| obj | object | Query object |
| return | string | Query string |
query.parse('foo=bar&eruda=true'); // -> {foo: 'bar', eruda: 'true'}
query.stringify({foo: 'bar', eruda: 'true'}); // -> 'foo=bar&eruda=true'
query.parse('name=eruda&name=eustia'); // -> {name: ['eruda', 'eustia']}
repeat
Repeat string n-times.
| Name | Type | Desc |
|---|---|---|
| str | string | String to repeat |
| n | number | Repeat times |
| return | string | Repeated string |
repeat('a', 3); // -> 'aaa'
repeat('ab', 2); // -> 'abab'
repeat('*', 0); // -> ''
restArgs
This accumulates the arguments passed into an array, after a given index.
| Name | Type | Desc |
|---|---|---|
| function | function | Function that needs rest parameters |
| startIndex | number | The start index to accumulates |
| return | function | Generated function with rest parameters |
var paramArr = _.restArgs(function (rest) { return rest });
paramArr(1, 2, 3, 4); // -> [1, 2, 3, 4]
rtrim
Remove chars or white-spaces from end of string.
| Name | Type | Desc |
|---|---|---|
| str | string | String to trim |
| chars | string array | Characters to trim |
| return | string | Trimmed string |
rtrim(' abc '); // -> ' abc'
rtrim('_abc_', '_'); // -> '_abc'
rtrim('_abc_', ['c', '_']); // -> '_ab'
safeCb
Create callback based on input value.
safeGet
Get object property, don't throw undefined error.
| Name | Type | Desc |
|---|---|---|
| obj | object | Object to query |
| path | array string | Path of property to get |
| return | * | Target value or undefined |
var obj = {a: {aa: {aaa: 1}}};
safeGet(obj, 'a.aa.aaa'); // -> 1
safeGet(obj, ['a', 'aa']); // -> {aaa: 1}
safeGet(obj, 'a.b'); // -> undefined
safeStorage
dependencies isUndef memStorage
slice
Create slice of source array or array-like object.
| Name | Type | Desc |
|---|---|---|
| array | array | Array to slice |
| [start=0] | number | Start position |
| [end=array.length] | number | End position, not included |
slice([1, 2, 3, 4], 1, 2); // -> [2]
some
Check if predicate return truthy for any element.
| Name | Type | Desc |
|---|---|---|
| obj | array object | Collection to iterate over |
| predicate | function | Function to invoked per iteration |
| ctx | * | Predicate context |
| return | boolean | True if any element passes the predicate check |
some([2, 5], function (val)
{
return val % 2 === 0;
}); // -> true
splitCase
Split different string case to an array.
| Name | Type | Desc |
|---|---|---|
| str | string | String to split |
| return | array | Result array |
splitCase('foo-bar'); // -> ['foo', 'bar']
splitCase('foo bar'); // -> ['foo', 'bar']
splitCase('foo_bar'); // -> ['foo', 'bar']
splitCase('foo.bar'); // -> ['foo', 'bar']
splitCase('fooBar'); // -> ['foo', 'bar']
splitCase('foo-Bar'); // -> ['foo', 'bar']
startWith
Check if string starts with the given target string.
| Name | Type | Desc |
|---|---|---|
| str | string | String to search |
| prefix | string | String prefix |
| return | boolean | True if string starts with prefix |
startWith('ab', 'a'); // -> true
stripHtmlTag
Strip html tags from a string.
| Name | Type | Desc |
|---|---|---|
| str | string | String to strip |
| return | string | Resulted string |
stripHtmlTag('<p>Hello</p>'); // -> 'Hello'
toArr
Convert value to an array.
| Name | Type | Desc |
|---|---|---|
| val | * | Value to convert |
| return | array | Converted array |
toArr({a: 1, b: 2}); // -> [{a: 1, b: 2}]
toArr('abc'); // -> ['abc']
toArr(1); // -> [1]
toArr(null); // -> []
toInt
Convert value to an integer.
| Name | Type | Desc |
|---|---|---|
| val | * | Value to convert |
| return | number | Converted integer |
toInt(1.1); // -> 1
toInt(undefined); // -> 0
toNum
Convert value to a number.
| Name | Type | Desc |
|---|---|---|
| val | * | Value to process |
| return | number | Resulted number |
toNum('5'); // -> 5
toStr
Convert value to a string.
| Name | Type | Desc |
|---|---|---|
| val | * | Value to convert |
| return | string | Resulted string |
toStr(null); // -> ''
toStr(1); // -> '1'
toStr(false); // -> 'false'
toStr([1, 2, 3]); // -> '1,2,3'
trim
Remove chars or white-spaces from beginning end of string.
| Name | Type | Desc |
|---|---|---|
| str | string | String to trim |
| chars | string array | Characters to trim |
| return | string | Trimmed string |
trim(' abc '); // -> 'abc'
trim('_abc_', '_'); // -> 'abc'
trim('_abc_', ['a', 'c', '_']); // -> 'b'
uniqId
Generate a globally-unique id.
| Name | Type | Desc |
|---|---|---|
| prefix | string | Id prefix |
| return | string | Globally-unique id |
uniqId('eusita_'); // -> 'eustia_xxx'
unique
Create duplicate-free version of an array.
| Name | Type | Desc |
|---|---|---|
| arr | array | Array to inspect |
| [compare] | function | Function for comparing values |
| return | array | New duplicate free array |
unique([1, 2, 3, 1]); // -> [1, 2, 3]
upperFirst
Convert the first character of string to upper case.
| Name | Type | Desc |
|---|---|---|
| str | string | String to convert |
| return | string | Converted string |
upperFirst('red'); // -> Red
values
Create an array of the own enumerable property values of object.
| Name | Type | Desc |
|---|---|---|
| obj | object | Object to query |
| return | array | Array of property values |
values({one: 1, two: 2}); // -> [1, 2]
wrap
Wrap the function inside a wrapper function, passing it as the first argument.
| Name | Type | Desc |
|---|---|---|
| fn | * | Function to wrap |
| wrapper | function | Wrapper function |
| return | function | New function |
var p = wrap(escape, function(fn, text)
{
return '<p>' + fn(text) + '</p>';
});
p('You & Me'); // -> '<p>You & Me</p>'