# lang2

> Lang utilities

Latest version **0.5.1** (published 2017-01-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install lang2
pnpm add lang2
yarn add lang2
bun add lang2
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.1 |
| Published | 2017-01-22 |
| First published | 2016-03-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Dmitry Shimkin |
| Maintainers | dmitryshimkin |

## Links

- npm: https://www.npmjs.com/package/lang2
- Repository: https://github.com/dmitryshimkin/lang
- Homepage: https://github.com/dmitryshimkin/lang#readme
- Issues: https://github.com/dmitryshimkin/lang/issues
- npm.io page: https://npm.io/package/lang2

## Recent versions

- 0.5.1 (latest) — 2017-01-22
- 0.5.0 — 2016-11-23
- 0.4.0 — 2016-11-22
- 0.3.7 — 2016-04-19
- 0.3.6 — 2016-04-17
- 0.3.5 — 2016-04-11
- 0.3.4 — 2016-04-11
- 0.3.3 — 2016-04-07
- 0.3.1 — 2016-04-07
- 0.3.0 — 2016-04-07
- 0.2.1 — 2016-04-07
- 0.2.0 — 2016-03-21

## README

# Lang

Javascript utilities library.

## Features

- All browsers support
- Unmatched performance
- Only 1.14 KB (compressed and gzipped)

## Table of content

- [Install](#install)
- [Usage](#usage)
- [API](#api)
  - [.camelize()](#camelize)
  - [.capitalize()](#capitalize)
  - [.contains()](#contains)
  - [.each()](#each)
  - [.endsWith()](#endsWith)
  - [.extend()](#extend)
  - [.htmlToString()](#htmlToString)
  - [.indexOf()](#indexOf)
  - [.insert()](#insert)
  - [.isArray()](#isarray)
  - [.isFunction()](#isfunction)
  - [.isObject()](#isobject)
  - [.map()](#map)
  - [.truncate()](#truncate)
- [Performance](#performance)
- [License](#license)


<a name="install"></a>
## Install

```
npm install lang2
```

<a name="usage"></a>
## Usage

In a browser:

```html
<script src="path/to/lang2.js"></script>
<script>
  // window.lang is available here
</script>
```

As a CommonJS module:

```javascript
var Lang = require('lang2')
```

As AMD:

```javascript
define(['lang2'], function (Lang) {
  // Use lang here
});
```

As a ES2015 module:

```javascript
import { * as Lang } from 'lang2'
```


<a name="api"></a>
### Modules


<a name="camelize"></a>
#### .camelize()

Converts a string to the camel case format.

Params:

| Parameter | Type     | Description                 |
|:----------|:---------|:----------------------------|
| str       | `String` | The string to be formatted. |

Returns: `String`

Example:

```javascript
import { camelize } from 'lang2'

const a = camelize('foo-bar'); // "fooBar"
const b = camelize('foo_bar'); // "fooBar"
const c = camelize('FooBar');  // "fooBar"
```


<a name="capitalize"></a>
#### .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:

```javascript
import { capitalize } from 'lang2'

const a = capitalize('foo bar'); // "Foo bar"
```


<a name="contains"></a>
#### .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:

```javascript
import { contains } from 'lang2'

const hasBar = contains(['foo', 'bar', 'xyz'], 'bar'); // true
const hasTwo = contains([0, 1, 3], 2); // false
```


<a name="each"></a>
#### .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:<br> – `*` value<br> – `Number` index. |
| thisArg   | `Object`    | The value to use as this when executing callback. Optional. |

Returns: `Undefined`.

Example:

```javascript
import { each } from 'lang2'

each(['foo', 'bar', 'xyz'], (item, idx) => {
  console.log(item, idx);
});

// "foo", 0
// "bar", 1
// "xyz", 2
```


<a name="endsWith"></a>
#### .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:

```javascript
import { endsWith } from 'lang2'

endsWith('Hello world', 'world'); // true
endsWith('Hello world', 'Hello'); // false
```


<a name="extend"></a>
#### .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:

```javascript
import { extend } from 'lang2'

const obj = extend({}, { foo: 'bar' }, { baz: 'xyz' });
// { foo: "bar", baz: "xyz" }
```


<a name="filter"></a>
#### .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: <br> - `*` item<br> - `Number` index.<br>Return `true` to keep the element, `false` otherwise. |
| thisArg   | `Object`   | The value to use as this when executing callback. Optional. |

Returns: `Undefined`

Example:

```javascript
import { filter } from 'lang2'

const arr = filter([0, 6, 2, 11, 4], (value, idx) => {
  return value > 5;
});

// [0, 2, 4]
```


<a name="hasOwnProps"></a>
#### .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:

```javascript
import { hasOwnProps } from 'lang2'

hasOwnProps({ foo: 'bar' }); // true
hasOwnProps(); // false
hasOwnProps(Object.create({
  foo: 'bar'
})); // false
```


<a name="htmlToString"></a>
#### .htmlToString()

Returns the text content of given HTML string.

Params:

| Parameter | Type      | Description                           |
|:----------|:----------|:--------------------------------------|
| str       | `String`  | The string which might contain HTML.  |

Returns: `String`

Example:

```javascript
import { htmlToString } from 'lang2'

const str = htmlToString('<html><span>foo</span><div>bar</div></html>'); // "foobar"
```


<a name="indexOf"></a>
#### .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:

```javascript
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
```


<a name="insert"></a>
#### .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:

```javascript
```


<a name="isArray"></a>
#### .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:

```javascript
import { isArray } from 'lang2'

isArray([]); // true
isArray(''); // false
isArray(document.querySelectorAll('.foo')); // false
```


<a name="isFunction"></a>
#### .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:

```javascript
import { isFunction } from 'lang2'

isFunction(function() {}); // true
isFunction(new Function()); // true
isFunction(null); // false
```


<a name="isObject"></a>
#### .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:

```javascript
import { isObject } from 'lang2'

isObject({}); // true
isObject(''); // false
isObject(null); // false
```


<a name="map"></a>
#### .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:<br> – `*` value<br> – `Number` index. |
| thisArg   | `Object`   | The value to use as this when executing callback. Optional. |

Returns: `Undefined`

Example:

```javascript
import { map } from 'lang2'

const arr = map([0, 1, 2, 3], (value, idx) => {
  return value * idx;
});

// [0, 1, 4, 9]
```


<a name="truncate"></a>
#### .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`.


<a name="performance"></a>
## Performance

The code of the benchmark suites can be found in [benchmark/suite](./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

---
_Source: https://npm.io/package/lang2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
