0.5.1 • Published 7 years ago

lang2 v0.5.1

Weekly downloads
6
License
MIT
Repository
github
Last release
7 years ago

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:

ParameterTypeDescription
strStringThe 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:

ParameterTypeDescription
strStringThe 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:

ParameterTypeDescription
arrArrayThe 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:

ParameterTypeDescription
arrArrayThe array that will be used to iterate through.
callbackFunctionThe function to execute for each element. Takes two arguments:* valueNumber index.
thisArgObjectThe 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:

ParameterTypeDescription
subjectStrStringThe string where to search.
searchStrStringThe 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:

ParameterTypeDescription
targetObjectObject to be extended.
sourceObjectThe 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:

ParameterTypeDescription
arrArrayThe array to be filtered.
callbackFunctionThe function to test each element of the array. Invoked with two arguments: - * item - Number index.Return true to keep the element, false otherwise.
thisArgObjectThe 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:

ParameterTypeDescription
objObjectThe 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:

ParameterTypeDescription
strStringThe 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:

ParameterTypeDescription
arrArrayThe 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:

ParameterTypeDescription
arrArrayArray the item to be inserted to.
item*The item to be inserted.
indexNumberThe 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:

ParameterTypeDescription
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:

ParameterTypeDescription
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:

ParameterTypeDescription
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:

ParameterTypeDescription
arrArrayThe array to iterate over.
callbackFunctionThe function that produces an element of the new array. Takes two arguments:* valueNumber index.
thisArgObjectThe 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:

ParameterTypeDescription
strStringThe string to truncate.
limitNumberMax length of the string.
ellipsisStringThe string to indicate string is truncated. By defaults "...". Optional.
toleranceNumberTolerance 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

LibraryOps/sec
Lang21,322,446
Lodash1,667,349

filter

LibraryOps/sec
Lang6,551,629
Lodash4,457,868
Native2,340,407

indexOf

LibraryOps/sec
Lang24,107,064
Lodash20,183,423
Native39,333,583

isArray

LibraryOps/sec
Lang3,576,782
Lodash52,384,477
Native55,281,306

map

LibraryOps/sec
Lang6,807,678
Lodash4,917,279
Native2,818,327

pick

LibraryOps/sec
Lang2,176,441
Lodash328,171

truncate

LibraryOps/sec
Lang21,895,153
Lodash3,937,711

LICENSE

MIT

0.5.1

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.7

8 years ago

0.3.6

8 years ago

0.3.5

8 years ago

0.3.4

8 years ago

0.3.3

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago