lang2
v0.5.1
Published
Lang utilities
Downloads
5
Readme
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:
<script src="path/to/lang2.js"></script>
<script>
// window.lang is available here
</script>
As a CommonJS module:
var Lang = require('lang2')
As AMD:
define(['lang2'], function (Lang) {
// Use lang here
});
As a ES2015 module:
import { * as Lang } from 'lang2'
Modules
.camelize()
Converts a string to the camel case format.
Params:
| Parameter | Type | Description |
|:----------|:---------|:----------------------------|
| str | String
| The string to be formatted. |
Returns: String
Example:
import { camelize } from 'lang2'
const a = camelize('foo-bar'); // "fooBar"
const b = camelize('foo_bar'); // "fooBar"
const c = camelize('FooBar'); // "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:
import { capitalize } from 'lang2'
const a = capitalize('foo bar'); // "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:
import { contains } from 'lang2'
const hasBar = contains(['foo', 'bar', 'xyz'], 'bar'); // true
const hasTwo = contains([0, 1, 3], 2); // 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:
import { each } from 'lang2'
each(['foo', 'bar', 'xyz'], (item, idx) => {
console.log(item, idx);
});
// "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:
import { endsWith } from 'lang2'
endsWith('Hello world', 'world'); // true
endsWith('Hello world', 'Hello'); // 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:
import { extend } from 'lang2'
const obj = extend({}, { foo: 'bar' }, { baz: 'xyz' });
// { 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:
import { filter } from 'lang2'
const arr = filter([0, 6, 2, 11, 4], (value, idx) => {
return value > 5;
});
// [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:
import { hasOwnProps } from 'lang2'
hasOwnProps({ foo: 'bar' }); // true
hasOwnProps(); // false
hasOwnProps(Object.create({
foo: 'bar'
})); // 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:
import { htmlToString } from 'lang2'
const str = htmlToString('<html><span>foo</span><div>bar</div></html>'); // "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:
import { indexOf } from 'lang2'
const index22 = indexOf([0, 11, 22, 33], 22); // 2
const indexFoo = indexOf(['foo', 'bar', 'baz', 'foo'], 'foo'); // 0
const indexD = indexOf(['a', 'b', 'c'], 'd'); // -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:
import { isArray } from 'lang2'
isArray([]); // true
isArray(''); // false
isArray(document.querySelectorAll('.foo')); // 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:
import { isFunction } from 'lang2'
isFunction(function() {}); // true
isFunction(new Function()); // true
isFunction(null); // 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:
import { isObject } from 'lang2'
isObject({}); // true
isObject(''); // false
isObject(null); // 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:
import { map } from 'lang2'
const arr = map([0, 1, 2, 3], (value, idx) => {
return value * idx;
});
// [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