Lang
Javascript utilities library.
Features
- All browsers support
- Unmatched performance
- Only 1.14 KB (compressed and gzipped)
Table of content
Install
npm install lang2
Usage
In a browser:
As a CommonJS module:
var Lang =
As AMD:
;
As a ES2015 module:
Modules
.camelize()
Converts a string to the camel case format.
Params:
Parameter | Type | Description |
---|---|---|
str | String |
The string to be formatted. |
Returns: String
Example:
const a = ; // "fooBar"const b = ; // "fooBar"const c = ; // "fooBar"
.capitalize()
Converts the first char of a string to the upper case.
Params:
Parameter | Type | Description |
---|---|---|
str | String |
The string to be capitalized. |
Returns: String
Example:
const a = ; // "Foo bar"
.contains()
Returns true
if a given array contains a given item.
Otherwise returns false
.
Params:
Parameter | Type | Description |
---|---|---|
arr | Array |
The array where to search. |
item | * |
The item to search for. |
Returns: Boolean
Example:
const hasBar = ; // trueconst hasTwo = ; // false
.each()
Executes a given function per each element of a given array.
Params:
Parameter | Type | Description |
---|---|---|
arr | Array |
The array that will be used to iterate through. |
callback | Function |
The function to execute for each element. Takes two arguments: – * value– Number index. |
thisArg | Object |
The value to use as this when executing callback. Optional. |
Returns: Undefined
.
Example:
; // "foo", 0// "bar", 1// "xyz", 2
.endsWith()
Determines whether a string ends with the characters of another string, returning true or false as appropriate.
Params:
Parameter | Type | Description |
---|---|---|
subjectStr | String |
The string where to search. |
searchStr | String |
The substring to be searched for at the end of the subject string. |
Returns: Boolean
Example:
; // true; // false
.extend()
Extends a given object with the contents of one or more objects.
Params:
Parameter | Type | Description |
---|---|---|
target | Object |
Object to be extended. |
source | Object |
The object containing additional properties to extend with. |
Returns: Object
Example:
const obj = ;// { foo: "bar", baz: "xyz" }
.filter()
Creates a new array with all elements that pass the test implemented by a given function.
Params:
Parameter | Type | Description |
---|---|---|
arr | Array |
The array to be filtered. |
callback | Function |
The function to test each element of the array. Invoked with two arguments: - * item- Number index.Return true to keep the element, false otherwise. |
thisArg | Object |
The value to use as this when executing callback. Optional. |
Returns: Undefined
Example:
const arr = ; // [0, 2, 4]
.hasOwnProps()
Checks whether a given object has any own properties. Returns true if it has at least one own property, false otherwise.
Params:
Parameter | Type | Description |
---|---|---|
obj | Object |
The object to check. |
Returns: Boolean
Example:
; // true; // false; // false
.htmlToString()
Returns the text content of given HTML string.
Params:
Parameter | Type | Description |
---|---|---|
str | String |
The string which might contain HTML. |
Returns: String
Example:
const str = ; // "foobar"
.indexOf()
Returns the first index at which a given element can be found in a given array, or -1 if it is not present.
Params:
Parameter | Type | Description |
---|---|---|
arr | Array |
The array where to search. |
item | * |
The item to locate in the array. |
Returns: Number
Example:
const index22 = ; // 2const indexFoo = ; // 0const indexD = ; // -1
.insert()
Inserts a given item into a given array.
Params:
Parameter | Type | Description |
---|---|---|
arr | Array |
Array the item to be inserted to. |
item | * |
The item to be inserted. |
index | Number |
The needed index of the item. |
Returns: Undefined
Example:
.isArray()
Checks whether a given argument array or not.
Returns true
, if the argument is an array, false
otherwise.
Params:
Parameter | Type | Description |
---|---|---|
arg | * |
The argument to check. |
Returns: Boolean
Example:
; // true; // false; // false
.isFunction()
Checks whether a given argument a function or not.
Returns true
, if the argument is a function, false
otherwise.
Params:
Parameter | Type | Description |
---|---|---|
arg | * |
The argument to check. |
Returns: Boolean
Example:
; // true; // true; // false
.isObject()
Checks whether a given argument an object or not.
Returns true
, if the argument is an object, false
otherwise.
Params:
Parameter | Type | Description |
---|---|---|
arg | * |
The argument to check. |
Returns: Boolean
Example:
; // true; // false; // false
.map()
Creates a new array with the results of calling a given function on every element in a given array.
Params:
Parameter | Type | Description |
---|---|---|
arr | Array |
The array to iterate over. |
callback | Function |
The function that produces an element of the new array. Takes two arguments: – * value– Number index. |
thisArg | Object |
The value to use as this when executing callback. Optional. |
Returns: Undefined
Example:
const arr = ; // [0, 1, 4, 9]
.truncate()
Truncates string with ellipsis.
Params:
Parameter | Type | Description |
---|---|---|
str | String |
The string to truncate. |
limit | Number |
Max length of the string. |
ellipsis | String |
The string to indicate string is truncated. By defaults "..." . Optional. |
tolerance | Number |
Tolerance for max length. |
Returns: String
.
Performance
The code of the benchmark suites can be found in benchmark/suite directory.
Start benchmark:
npm run bench
On Macbook Pro 2015 with NodeJS v4.4.7
I got the following result:
contains
Library | Ops/sec |
---|---|
Lang | 21,322,446 |
Lodash | 1,667,349 |
filter
Library | Ops/sec |
---|---|
Lang | 6,551,629 |
Lodash | 4,457,868 |
Native | 2,340,407 |
indexOf
Library | Ops/sec |
---|---|
Lang | 24,107,064 |
Lodash | 20,183,423 |
Native | 39,333,583 |
isArray
Library | Ops/sec |
---|---|
Lang | 3,576,782 |
Lodash | 52,384,477 |
Native | 55,281,306 |
map
Library | Ops/sec |
---|---|
Lang | 6,807,678 |
Lodash | 4,917,279 |
Native | 2,818,327 |
pick
Library | Ops/sec |
---|---|
Lang | 2,176,441 |
Lodash | 328,171 |
truncate
Library | Ops/sec |
---|---|
Lang | 21,895,153 |
Lodash | 3,937,711 |
LICENSE
MIT