jQuery like style dom manipulator.
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...
});
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 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'
});
Element class manipulations.
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 |
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 |
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 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
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
Wrapper of $attr, adds data- prefix to keys.
$data('#test', 'attr1', 'eustia');
bind events to certain dom elements.
function clickHandler()
{
// Do something...
}
$event.on('#test', 'click', clickHandler);
$event.off('#test', 'click', clickHandler);
Insert html on different position.
Insert content before elements.
Insert content after elements.
Insert content to the beginning of elements.
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>licia</div>');
// -> <div>licia</div><div id="test"><div class="mark"></div></div>
$insert.after('#test', '<div>licia</div>');
// -> <div id="test"><div class="mark"></div></div><div>licia</div>
$insert.prepend('#test', '<div>licia</div>');
// -> <div id="test"><div>licia</div><div class="mark"></div></div>
$insert.append('#test', '<div>licia</div>');
// -> <div id="test"><div class="mark"></div><div>licia</div></div>
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}
Element property html, text, val getter and setter.
Get the HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element.
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.
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', 'licia');
$property.html('#test'); // -> licia
Remove the set of matched elements from the DOM.
Name | Type | Desc |
---|---|---|
element | string array element | Elements to delete |
$remove('#test');
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 elements.
Name | Type | Desc |
---|---|---|
element | string array element | Elements to show |
$show('#test');
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
Event emitter class which provides observer pattern.
Bind event.
Unbind event.
Bind event that trigger once.
Name | Type | Desc |
---|---|---|
event | string | Event name |
listener | function | Event listener |
Emit event.
Name | Type | Desc |
---|---|---|
event | string | Event name |
...args | * | Arguments passed to listener |
[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({});
Enum type implementation.
Name | Type | Desc |
---|---|---|
arr | array | Array of strings |
Name | Type | Desc |
---|---|---|
obj | object | Pairs of key and value |
var importance = new Enum([
'NONE', 'TRIVIAL', 'REGULAR', 'IMPORTANT', 'CRITICAL'
]);
if (val === importance.CRITICAL)
{
// Do something.
}
LocalStorage wrapper.
Extend from Store.
Name | Type | Desc |
---|---|---|
name | string | LocalStorage item name |
data | object | Default data |
var store = new LocalStore('licia');
store.set('name', 'licia');
Simple logger with level filter.
Name | Type | Desc |
---|---|---|
name | string | Logger name |
[level=DEBUG] | number | Logger level |
Name | Type | Desc |
---|---|---|
level | number string | Logger level |
Get current level.
Logging methods.
TRACE, DEBUG, INFO, WARN, ERROR and SILENT.
var logger = new Logger('licia', Logger.level.ERROR);
logger.trace('test');
// Format output.
logger.formatter = function (type, argList)
{
argList.push(new Date().getTime());
return argList;
};
logger.on('all', function (type, argList)
{
// It's not affected by log level.
});
logger.on('debug', function (argList)
{
// Affected by log level.
});
Safe MutationObserver, does nothing if MutationObserver is not supported.
var observer = new MutationObserver(function (mutations)
{
// Do something.
});
observer.observe(document.htmlElement);
observer.disconnect();
Simple wrapper of querySelectorAll to make dom selection easier.
Name | Type | Desc |
---|---|---|
selector | string | Dom selector string |
Get desdendants of current matched elements.
Name | Type | Desc |
---|---|---|
selector | string | Dom selector string |
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
});
Memory storage.
Extend from Emitter.
Name | Type | Desc |
---|---|---|
data | object | Initial data |
Set value.
Name | Type | Desc |
---|---|---|
key | string | Value key |
val | * | Value to set |
Set values.
Name | Type | Desc |
---|---|---|
vals | object | Key value pairs |
This emit a change event whenever is called.
Get value.
Name | Type | Desc |
---|---|---|
key | string | Value key |
return | * | Value of given key |
Get values.
Name | Type | Desc |
---|---|---|
keys | array | Array of keys |
return | object | Key value pairs |
Remove value.
Name | Type | Desc |
---|---|---|
key | string array | Key to remove |
Clear all data.
Iterate over values.
Name | Type | Desc |
---|---|---|
fn | function | Function invoked per interation |
var store = new Store('test');
store.set('user', {name: 'licia'});
store.get('user').name; // -> 'licia'
store.clear();
store.each(function (val, key)
{
// Do something.
});
store.on('change', function (key, newVal, oldVal)
{
// It triggers whenever set is called.
});
Simple url manipulator.
Name | Type | Desc |
---|---|---|
url=location | string | Url string |
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 |
Remove query value.
Name | Type | Desc |
---|---|---|
name | string array | Query name |
return | Url | this |
[static] Parse url into an object.
Name | Type | Desc |
---|---|---|
url | string | Url string |
return | object | Url object |
[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('https://example.com:8080?eruda=true');
console.log(url.port); // -> '8080'
url.query.foo = 'bar';
url.rmQuery('eruda');
utl.toString(); // -> 'https://example.com:8080/?foo=bar'
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) |
contentType=application/x-www-form-urlencoded | string | Request header Content-Type |
success | function | Success callback |
error | function | Error callback |
complete | function | Callback after request |
timeout | number | Request timeout |
Shortcut for type = GET;
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: 'https://example.com',
data: {test: 'true'},
error: function () {},
success: function (data)
{
// ...
},
dataType: 'json'
});
ajax.get('https://example.com', {}, function (data)
{
// ...
});
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']
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.
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
Cast value into a property path array.
Name | Type | Desc |
---|---|---|
str | * | Value to inspect |
[obj] | object | Object to query |
return | array | Property path array |
castPath('a.b.c'); // -> ['a', 'b', 'c']
castPath(['a']); // -> ['a']
castPath('a[0].b'); // -> ['a', '0', 'b']
castPath('a.b.c', {'a.b.c': true}); // -> ['a.b.c']
Split array into groups the length of given size.
Name | Type | Desc |
---|---|---|
arr | array | Array to process |
size=1 | number | Length of each chunk |
chunk([1, 2, 3, 4], 2); // -> [[1, 2], [3, 4]]
chunk([1, 2, 3, 4], 3); // -> [[1, 2, 3], [4]]
chunk([1, 2, 3, 4]); // -> [[1], [2], [3], [4]]
Clamp number within the inclusive lower and upper bounds.
Name | Type | Desc |
---|---|---|
n | number | Number to clamp |
[lower] | number | Lower bound |
upper | number | Upper bound |
return | number | Clamped number |
clamp(-10, -5, 5); // -> -5
clamp(10, -5, 5); // -> 5
clamp(2, -5, 5); // -> 2
clamp(10, 5); // -> 5
clamp(2, 5); // -> 2
Create a shallow-copied clone of the provided plain object.
Any nested objects or arrays will be copied by reference, not duplicated.
Name | Type | Desc |
---|---|---|
val | * | Value to clone |
return | * | Cloned value |
clone({name: 'eustia'}); // -> {name: 'eustia'}
Recursively clone value.
Name | Type | Desc |
---|---|---|
val | * | Value to clone |
return | * | Deep cloned Value |
var obj = [{a: 1}, {a: 2}];
var obj2 = cloneDeep(obj);
console.log(obj[0] === obj2[1]); // -> false
Concat multiple arrays into a single array.
Name | Type | Desc |
---|---|---|
...arr | array | Arrays to concat |
return | array | Concatenated array |
concat([1, 2], [3], [4, 5]); // -> [1, 2, 3, 4, 5]
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
Simple api for handling browser cookies.
Get cookie value.
Name | Type | Desc |
---|---|---|
key | string | Cookie key |
return | string | Corresponding cookie value |
Set cookie value.
Name | Type | Desc |
---|---|---|
key | string | Cookie key |
val | string | Cookie value |
[options] | object | Cookie options |
return | exports | Module cookie |
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');
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... |
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
Better decodeURIComponent that does not throw if input is invalid.
Name | Type | Desc |
---|---|---|
str | string | String to decode |
return | string | Decoded string |
decodeUriComponent('%%25%'); // -> '%%%'
decodeUriComponent('%E0%A4%A'); // -> '\xE0\xA4%A'
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}
Event delegation.
Add event delegation.
Name | Type | Desc |
---|---|---|
el | element | Parent element |
type | string | Event type |
selector | string | Match selector |
cb | function | Event callback |
Remove event delegation.
var container = document.getElementById('container');
function clickHandler()
{
// Do something...
}
delegate.add(container, 'click', '.children', clickHandler);
delegate.remove(container, 'click', '.children', clickHandler);
Detect browser info using ua.
Name | Type | Desc |
---|---|---|
[ua=navigator.userAgent] | string | Browser userAgent |
return | object | Object containing name and version |
Browsers supported: ie, chrome, edge, firefox, opera, safari, ios(mobile safari), android(android browser)
var browser = detectBrowser();
if (browser.name === 'ie' && browser.version < 9)
{
// Do something about old IE...
}
Detect if mocha is running.
detectMocha(); // -> True if mocha is running.
Detect operating system using ua.
Name | Type | Desc |
---|---|---|
[ua=navigator.userAgent] | string | Browser userAgent |
return | string | Operating system name |
Supported os: windows, os x, linux, ios, android, windows phone
if (detectOs() === 'ios')
{
// Do something about ios...
}
Iterate 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) {});
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
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'
Escape string to be a valid JavaScript string literal between quotes.
https://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
Name | Type | Desc |
---|---|---|
str | string | String to escape |
return | string | Escaped string |
escapeJsStr('\"\n'); // -> '\\"\\\\n'
Escape json string.
Escape special chars to be used as literals in RegExp constructors.
Name | Type | Desc |
---|---|---|
str | string | String to escape |
return | string | Escaped string |
escapeRegExp('[licia]'); // -> '\\[licia\\]'
Eval css.
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}
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}
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'
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]
Shortcut for Object.freeze.
Use Object.defineProperties if Object.freeze is not supported.
Name | Type | Desc |
---|---|---|
obj | object | Object to freeze |
return | object | Object passed in |
var a = {b: 1};
freeze(a);
a.b = 2;
console.log(a); // -> {b: 1}
Add origin to url if needed.
Extract file name from url.
Get object type.
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
Return the first argument given.
Name | Type | Desc |
---|---|---|
val | * | Any value |
return | * | Given value |
identity('a'); // -> 'a'
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
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'
Check if value is classified as an arguments object.
Name | Type | Desc |
---|---|---|
val | * | Value to check |
return | boolean | True if value is an arguments object |
(function () {
isArgs(arguments); // -> true
})();
Check if value is an Array
object.
Name | Type | Desc |
---|---|---|
val | * | Value to check |
return | boolean | True if value is an Array object |
isArr([]); // -> true
isArr({}); // -> false
Check if value is array-like.
Name | Type | Desc |
---|---|---|
val | * | 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
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
Check if running in a browser.
console.log(isBrowser); // -> true if running in a browser
Check if a url is cross origin.
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
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
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
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
See if an element is within eruda.
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
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
Check if running in wechat mini program.
console.log(isMiniProgram); // -> true if running in mini program.
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);
Check if value is an NaN.
Name | Type | Desc |
---|---|---|
val | * | Value to check |
return | boolean | True if value is an NaN |
Undefined is not an NaN, different from global isNaN function.
isNaN(0); // -> false
isNaN(NaN); // -> true
Check if value is a native function.
Name | Type | Desc |
---|---|---|
val | * | Value to check |
return | boolean | True if value is a native function |
isNative(function () {}); // -> false
isNative(Math.min); // -> true
Check if value is null or undefined, the same as value == null.
Name | Type | Desc |
---|---|---|
val | * | Value to check |
return | boolean | True if value is null or undefined |
isNil(null); // -> true
isNil(void 0); // -> true
isNil(undefined); // -> true
isNil(false); // -> false
isNil(0); // -> false
isNil([]); // -> false
Check if value is an Null.
Name | Type | Desc |
---|---|---|
val | * | Value to check |
return | boolean | True if value is an Null |
isNull(null); // -> true
Check if value is classified as a Number primitive or object.
Name | Type | Desc |
---|---|---|
val | * | Value to check |
return | boolean | True if value is correctly classified |
isNum(5); // -> true
isNum(5.1); // -> true
isNum({}); // -> false
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
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
Check if value is a string primitive.
Name | Type | Desc |
---|---|---|
val | * | Value to check |
return | boolean | True if value is a string primitive |
isStr('licia'); // -> true
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
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
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']
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
Inject script tag into page with given src value.
Name | Type | Desc |
---|---|---|
src | string | Script source |
cb | function | Onload callback |
loadJs('main.js', function (isLoaded)
{
// Do something...
});
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'
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_'
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]
Map for objects.
Name | Type | Desc |
---|---|---|
obj | object | Object to iterate over |
iteratee | function | Function invoked per iteration |
[ctx] | * | Function context |
return | object | New mapped object |
mapObj({a: 1, b: 2}, function (val, key) { return val + 1 }); // -> {a: 2, b: 3}
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 }]
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', 'licia');
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);
});
Document meta manipulation, turn name and content into key value pairs.
Get meta content with given name. If name is omitted, all pairs will be return.
Name | Type | Desc |
---|---|---|
[name] | string array | Meta name |
return | string | Meta content |
Set meta content.
Name | Type | Desc |
---|---|---|
name | string | Meta name |
content | string | Meta content |
Name | Type | Desc |
---|---|---|
metas | object | Object of name content pairs |
Remove metas.
Name | Type | Desc |
---|---|---|
name | string array | Meta name |
// <meta name="a" content="1"/> <meta name="b" content="2"/> <meta name="c" content="3"/>
meta(); // -> {a: '1', b: '2', c: '3'}
meta('a'); // -> '1'
meta(['a', 'c']); // -> {a: '1', c: '3'}
meta('d', '4');
meta({
d: '5',
e: '6',
f: '7'
});
meta.remove('d');
meta.remove(['e', 'f']);
Convert time string formats to milliseconds.
Turn time string into milliseconds.
Name | Type | Desc |
---|---|---|
str | string | String format |
return | number | Milliseconds |
Turn milliseconds into time string.
Name | Type | Desc |
---|---|---|
num | number | Milliseconds |
return | string | String format |
ms('1s'); // -> 1000
ms('1m'); // -> 60000
ms('1.5h'); // -> 5400000
ms('1d'); // -> 86400000
ms('1y'); // -> 31557600000
ms('1000'); // -> 1000
ms(1500); // -> '1.5s'
ms(60000); // -> '1m'
Next tick for both node and browser.
Name | Type | Desc |
---|---|---|
cb | function | Function to call |
Use process.nextTick if available.
Otherwise setImmediate or setTimeout is used as fallback.
nextTick(function ()
{
// Do something...
});
A no-operation function.
noop(); // Does nothing
Gets the number of milliseconds that have elapsed since the Unix epoch.
now(); // -> 1468826678701
Alias of Object.prototype.toString.
Name | Type | Desc |
---|---|---|
value | * | Source value |
return | string | String representation of given value |
objToStr(5); // -> '[object Number]'
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
Used for function context binding.
Screen orientation helper.
Bind change event.
Unbind change event.
Get current orientation(landscape or portrait).
orientation.on('change', function (direction)
{
console.log(direction); // -> 'portrait'
});
orientation.get(); // -> 'landscape'
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
High resolution time up to microsecond precision.
var start = perfNow();
// Do something.
console.log(perfNow() - start);
Add vendor prefixes to a CSS attribute.
Name | Type | Desc |
---|---|---|
name | string | Property name |
return | string | Prefixed property name |
Create a dasherize version.
prefix('text-emphasis'); // -> 'WebkitTextEmphasis'
prefix.dash('text-emphasis'); // -> '-webkit-text-emphasis'
prefix('color'); // -> 'color'
Turn string like '0px' to number.
Parse and stringify url query strings.
Parse a query string into an object.
Name | Type | Desc |
---|---|---|
str | string | Query string |
return | object | Query object |
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 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); // -> ''
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]
Loop through all possible path and domain to remove cookie.
Name | Type | Desc |
---|---|---|
key | string | Cookie key |
rmCookie('test');
Root object reference, global
in nodeJs, window
in browser.
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'
Create callback based on input value.
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
Safe localStorage and sessionStorage.
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]
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
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']
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
JSON stringify with support for circular object, function etc.
Undefined is treated as null value.
Name | Type | Desc |
---|---|---|
obj | object | Object to stringify |
spaces | number | Indent spaces |
return | string | Stringified object |
stringify({a: function () {}}); // -> '{"a":"[Function function () {}]"}'
var obj = {a: 1};
obj.b = obj;
stringify(obj); // -> '{"a":1,"b":"[Circular ~]"}'
Strip html tags from a string.
Name | Type | Desc |
---|---|---|
str | string | String to strip |
return | string | Resulted string |
stripHtmlTag('<p>Hello</p>'); // -> 'Hello'
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); // -> []
Convert value to an integer.
Name | Type | Desc |
---|---|---|
val | * | Value to convert |
return | number | Converted integer |
toInt(1.1); // -> 1
toInt(undefined); // -> 0
Convert value to a number.
Name | Type | Desc |
---|---|---|
val | * | Value to process |
return | number | Resulted number |
toNum('5'); // -> 5
Convert function to its source code.
Name | Type | Desc |
---|---|---|
fn | function | Function to convert |
return | string | Source code |
toSrc(Math.min); // -> 'function min() { [native code] }'
toSrc(function () {}) // -> 'function () { }'
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'
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'
Run function in a try catch.
Name | Type | Desc |
---|---|---|
fn | function | Function to try catch |
[cb] | function | Callback |
tryIt(function ()
{
// Do something that might cause an error.
}, function (err, result)
{
if (err) console.log(err);
});
Determine the internal JavaScript [[Class]] of an object.
Name | Type | Desc |
---|---|---|
val | * | Value to get type |
return | string | Type of object, lowercased |
type(5); // -> 'number'
type({}); // -> 'object'
type(function () {}); // -> 'function'
type([]); // -> 'array'
UCS-2 encoding and decoding.
Create a string using an array of code point values.
Name | Type | Desc |
---|---|---|
arr | array | Array of code points |
return | string | Encoded string |
Create an array of code point values using a string.
Name | Type | Desc |
---|---|---|
str | string | Input string |
return | array | Array of code points |
ucs2.encode([0x61, 0x62, 0x63]); // -> 'abc'
ucs2.decode('abc'); // -> [0x61, 0x62, 0x63]
'π'.length; // -> 2
ucs2.decode('π').length; // -> 1
Generate a globally-unique id.
Name | Type | Desc |
---|---|---|
prefix | string | Id prefix |
return | string | Globally-unique id |
uniqId('eusita_'); // -> 'eustia_xxx'
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]
Convert the first character of string to upper case.
Name | Type | Desc |
---|---|---|
str | string | String to convert |
return | string | Converted string |
upperFirst('red'); // -> Red
UTF-8 encoding and decoding.
Turn any UTF-8 decoded string into UTF-8 encoded string.
Name | Type | Desc |
---|---|---|
str | string | String to encode |
return | string | Encoded string |
Name | Type | Desc |
---|---|---|
str | string | String to decode |
[safe=false] | boolean | Suppress error if true |
return | string | Decoded string |
Turn any UTF-8 encoded string into UTF-8 decoded string.
utf8.encode('\uD800\uDC00'); // -> '\xF0\x90\x80\x80'
utf8.decode('\xF0\x90\x80\x80'); // -> '\uD800\uDC00'
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]
Get viewport scale.
viewportScale(); // -> 3
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>'