README
es-stl
EMCAScript Standard Template Library
Install
npm:
$ npm install --save es-stl
yarn:
$ yarn add es-stl
Usage
Using es5
const binarySearch = require('ts-stl/algm/binarySearch');
binarySearch([1, 3, 5], 3);// => 1
Using es6 or typescript
import binarySearch from 'ts-stl/algm/binarySearch';
binarySearch([1, 3, 5], 3);// => 1
Reference
Modules
- algm/binarySearch ⇒
number
Binary Search
- algm/binarySearchByRange ⇒
number
Binary search by range
- algm/exponentialSearch ⇒
number
Exponential Search
- algm/interpolationSearch ⇒
number
Interpolation Search
- algm/mergeSortArr ⇒
Array.<T>
To combine two sorted arrays into an array sorted by original rules
- algm/mergeSortSingle ⇒
Array.<T>
Inserts an element into an sorted array, keeping the original order
- ds/Dictionary
Dictionary
- ds/Map
Map
- ds/Queue
Queue
- ds/Stack
Stack
- utils/clamp ⇒
number
Clamps
number
within the inclusivelower
andupper
bounds.- utils/Compare
Compare
- utils/ellipsis ⇒
string
String abbreviation with ellipsis
- utils/error
Throw error
- utils/format ⇒
string
String format, just support
%s
, it's simple, but useful.- utils/formats ⇒
string
String format, just support
%s
, it's simple, but useful.- utils/getClass ⇒
string
Determine the internal JavaScript [[Class]] of an object.
- utils/inRange ⇒
boolean
Checks if
num
is betweenstart
andend
, such asstart <= num <= end
- utils/isArrayLike ⇒
boolean
Checks if
value
is array-like. A value is considered array-like if it's not a function and not a window and has avalue.length
that's an integer greater than or equal to0
and less than or equal toNumber.MAX_SAFE_INTEGER
.- utils/isBool ⇒
boolean
Checks if
value
is classified as a boolean primitive or object.- utils/isClass ⇒
boolean
Checks if
value
is classified as aClass
primitive or object.- utils/isFunc ⇒
boolean
Checks if
value
is classified as aFunction
object.- utils/isInt ⇒
boolean
Checks if
value
is an integer.- utils/isLength ⇒
boolean
Checks if
value
is a valid array-like length.Note: This method is loosely based on
ToLength
.- utils/isNum ⇒
boolean
Checks if
value
is classified as aNumber
primitive or object.- utils/isObj ⇒
boolean
Checks if
value
is the language type ofObject
. (e.g. arrays, functions, objects, regexes,new Number(0)
, andnew String('')
)- utils/isObjLike ⇒
boolean
Checks if
value
is object-like. A value is object-like if it's notnull
and has atypeof
result of "object".- utils/isPlainObj ⇒
boolean
Checks if
value
is a plain object, that is, an object created by theObject
constructor or one with a[[Prototype]]
ofnull
.- utils/isStr ⇒
boolean
Checks if
value
is classified as aString
primitive or object.- utils/merge ⇒
T
Merge the contents of a object into the first object.
- utils/mergeWith ⇒
T
Merge the contents of a object into the first object by customizer.
- utils/noop
This method returns
undefined
.- utils/random ⇒
number
Produces a random number between the inclusive
lower
andupper
bounds.- utils/repeat ⇒
string
Repeats the given string
n
times.- utils/toFixed ⇒
number
Returns a number in fixed-point notation.
Functions
- arraySwap(arr, i, j)
Array elements swap.
- giveBackCb(args) ⇒
*
giveBackCb
number
algm/binarySearch ⇒ Binary Search
Param | Type | Default | Description |
---|---|---|---|
arr | Array.<T> |
an array after sort from small to large | |
searchVal | number |
||
[cb] | ISearchCb.<T> |
giveBackCb |
|
[left] | number |
0 |
|
[right] | number |
arr.length - 1 |
number
algm/binarySearchByRange ⇒ Binary search by range
Param | Type | Description |
---|---|---|
arr | Array.<T> |
an object array after sort from small to large |
searchVal | number |
|
cb | ISearchRangeCb.<T> |
Example
const dataList = [{s:0, e: 5}, {s:6, e: 18}, {s:19, e: 30}, ...]
binarySearchByRange(dataList, 10, (range: number[], data: {s: number, e: number}) => {
range[0] = data.s;
range[1] = data.e;
}); => 1
number
algm/exponentialSearch ⇒ Exponential Search
Param | Type | Default | Description |
---|---|---|---|
arr | Array.<T> |
an array after sort from small to large | |
searchVal | number |
||
[cb] | ISearchCb.<T> |
giveBackCb |
number
algm/interpolationSearch ⇒ Interpolation Search
Param | Type | Default | Description |
---|---|---|---|
arr | Array.<T> |
an array after sort from small to large | |
searchVal | number |
||
[cb] | ISearchCb.<T> |
giveBackCb |
Array.<T>
algm/mergeSortArr ⇒ To combine two sorted arrays into an array sorted by original rules
Param | Type | Description |
---|---|---|
arr1 | Array.<T> |
a non-sparse array after sort |
arr2 | Array.<T> |
a non-sparse array after sort |
cb | ISortCb.<T> |
Array.<T>
algm/mergeSortSingle ⇒ Inserts an element into an sorted array, keeping the original order
Param | Type | Description |
---|---|---|
arr | Array.<T> |
a non-sparse array after sort |
el | T |
|
cb | ISortCb.<T> |
ds/Dictionary
Dictionary
- ds/Dictionary
- ~Dictionary
- .fromObject(obj) ⇒
this
- .toObject() ⇒
IKeyValue.<V>
- .isEmpty() ⇒
boolean
- .size() ⇒
number
- .has(key) ⇒
boolean
- .get(key) ⇒
V
- .set(key, value) ⇒
this
- .remove(key) ⇒
boolean
- .keys() ⇒
Array.<string>
- .values([values]) ⇒
Array.<V>
- .every(cb) ⇒
boolean
- .some(cb) ⇒
boolean
- .forEach(cb)
- .map(cb) ⇒
Array.<T>
- .filter(cb) ⇒
Array.<V>
- .find(cb) ⇒
V
- .toString() ⇒
string
- .clear()
- .fromObject(obj) ⇒
- ~Dictionary
ds/Dictionary~Dictionary
Kind: inner class of ds/Dictionary
- ~Dictionary
- .fromObject(obj) ⇒
this
- .toObject() ⇒
IKeyValue.<V>
- .isEmpty() ⇒
boolean
- .size() ⇒
number
- .has(key) ⇒
boolean
- .get(key) ⇒
V
- .set(key, value) ⇒
this
- .remove(key) ⇒
boolean
- .keys() ⇒
Array.<string>
- .values([values]) ⇒
Array.<V>
- .every(cb) ⇒
boolean
- .some(cb) ⇒
boolean
- .forEach(cb)
- .map(cb) ⇒
Array.<T>
- .filter(cb) ⇒
Array.<V>
- .find(cb) ⇒
V
- .toString() ⇒
string
- .clear()
- .fromObject(obj) ⇒
this
dictionary.fromObject(obj) ⇒ Object transform to Dictionary
Kind: instance method of Dictionary
Param | Type |
---|---|
obj | IKeyValue.<V> |
IKeyValue.<V>
dictionary.toObject() ⇒ Dictionary transform to Object
Kind: instance method of Dictionary
boolean
dictionary.isEmpty() ⇒ isEmpty
Kind: instance method of Dictionary
number
dictionary.size() ⇒ size
Kind: instance method of Dictionary
boolean
dictionary.has(key) ⇒ has
Kind: instance method of Dictionary
Param | Type |
---|---|
key | K |
V
dictionary.get(key) ⇒ get
Kind: instance method of Dictionary
Param | Type |
---|---|
key | K |
this
dictionary.set(key, value) ⇒ set
Kind: instance method of Dictionary
Param | Type |
---|---|
key | K |
value | V |
boolean
dictionary.remove(key) ⇒ remove
Kind: instance method of Dictionary
Param | Type |
---|---|
key | K |
Array.<string>
dictionary.keys() ⇒ keys
Kind: instance method of Dictionary
Array.<V>
dictionary.values([values]) ⇒ values
Kind: instance method of Dictionary
Param | Type | Default |
---|---|---|
[values] | Array.<V> |
[] |
boolean
dictionary.every(cb) ⇒ every
Kind: instance method of Dictionary
Returns: boolean
- Returns true if all elements pass the predicate check, else false
Param | Type |
---|---|
cb | IFilterCb.<V, string> |
boolean
dictionary.some(cb) ⇒ some
Kind: instance method of Dictionary
Returns: boolean
- Returns true if any element passes the predicate check, else false
Param | Type |
---|---|
cb | IFilterCb.<V, string> |
dictionary.forEach(cb)
forEach
Kind: instance method of Dictionary
Param | Type | Description |
---|---|---|
cb | IIterateCb.<V, string> |
Iteratee functions may exit iteration early by explicitly returning false |
Array.<T>
dictionary.map(cb) ⇒ map
Kind: instance method of Dictionary
Param | Type |
---|---|
cb | IMapCb.<V, string, T> |
Array.<V>
dictionary.filter(cb) ⇒ filter
Kind: instance method of Dictionary
Param | Type |
---|---|
cb | IFilterCb.<V, string> |
V
dictionary.find(cb) ⇒ find
Kind: instance method of Dictionary
Param | Type |
---|---|
cb | IFilterCb.<V, string> |
string
dictionary.toString() ⇒ toString
Kind: instance method of Dictionary
dictionary.clear()
Clear the dictionary
Kind: instance method of Dictionary
ds/Map
Map
- ds/Map
- ~Map
- .isEmpty() ⇒
boolean
- .size() ⇒
number
- .keys() ⇒
Array.<K>
- .values() ⇒
Array.<V>
- .has(key) ⇒
boolean
- .get(key) ⇒
V
- .set(key, value) ⇒
this
- .indexOf(key) ⇒
number
- .remove(key) ⇒
boolean
- .sortByKey(cb) ⇒
this
- .sortByValue(cb) ⇒
this
- .clear()
- .isEmpty() ⇒
- ~Map
ds/Map~Map
Kind: inner class of ds/Map
- ~Map
- .isEmpty() ⇒
boolean
- .size() ⇒
number
- .keys() ⇒
Array.<K>
- .values() ⇒
Array.<V>
- .has(key) ⇒
boolean
- .get(key) ⇒
V
- .set(key, value) ⇒
this
- .indexOf(key) ⇒
number
- .remove(key) ⇒
boolean
- .sortByKey(cb) ⇒
this
- .sortByValue(cb) ⇒
this
- .clear()
- .isEmpty() ⇒
boolean
map.isEmpty() ⇒ isEmpty
Kind: instance method of Map
number
map.size() ⇒ size
Kind: instance method of Map
Array.<K>
map.keys() ⇒ keys
Kind: instance method of Map
Array.<V>
map.values() ⇒ values
Kind: instance method of Map
boolean
map.has(key) ⇒ has
Kind: instance method of Map
Param | Type |
---|---|
key | K |
V
map.get(key) ⇒ get
Kind: instance method of Map
Param | Type |
---|---|
key | K |
this
map.set(key, value) ⇒ set
Kind: instance method of Map
Param | Type |
---|---|
key | K |
value | V |
number
map.indexOf(key) ⇒ indexOf
Kind: instance method of Map
Param | Type |
---|---|
key | K |
boolean
map.remove(key) ⇒ remove
Kind: instance method of Map
Param | Type |
---|---|
key | K |
this
map.sortByKey(cb) ⇒ sortByKey
Kind: instance method of Map
Param | Type |
---|---|
cb | ISortCb.<K> |
this
map.sortByValue(cb) ⇒ sortByValue
Kind: instance method of Map
Param | Type |
---|---|
cb | ISortCb.<V> |
map.clear()
Clear the map
Kind: instance method of Map
ds/Queue
Queue
- ds/Queue
- ~Queue
- .isEmpty() ⇒
boolean
- .size() ⇒
number
- .enqueue(el) ⇒
this
- .dequeue() ⇒
T
- .front() ⇒
T
- .back() ⇒
T
- .clear()
- .isEmpty() ⇒
- ~Queue
ds/Queue~Queue
Kind: inner class of ds/Queue
- ~Queue
- .isEmpty() ⇒
boolean
- .size() ⇒
number
- .enqueue(el) ⇒
this
- .dequeue() ⇒
T
- .front() ⇒
T
- .back() ⇒
T
- .clear()
- .isEmpty() ⇒
boolean
queue.isEmpty() ⇒ isEmpty
Kind: instance method of Queue
number
queue.size() ⇒ size
Kind: instance method of Queue
this
queue.enqueue(el) ⇒ enqueue
Kind: instance method of Queue
Param | Type |
---|---|
el | T |
T
queue.dequeue() ⇒ dequeue
Kind: instance method of Queue
T
queue.front() ⇒ Return the first element
Kind: instance method of Queue
T
queue.back() ⇒ Return the last element
Kind: instance method of Queue
queue.clear()
Clear the queue
Kind: instance method of Queue
ds/Stack
Stack
ds/Stack~Stack
Kind: inner class of ds/Stack
boolean
stack.isEmpty() ⇒ isEmpty
Kind: instance method of Stack
number
stack.size() ⇒ size
Kind: instance method of Stack
this
stack.push(el) ⇒ push
Kind: instance method of Stack
Param | Type |
---|---|
el | T |
T
stack.pop() ⇒ pop
Kind: instance method of Stack
T
stack.peek() ⇒ Return the top element of the stack
Kind: instance method of Stack
stack.clear()
Clear the stack
Kind: instance method of Stack
number
utils/clamp ⇒ Clamps number
within the inclusive lower
and upper
bounds.
Returns: number
- Returns the clamped number.
Param | Type | Description |
---|---|---|
num | number |
The number to clamp. |
[lower] | number |
The lower bound. |
upper | number |
The upper bound. |
Example
clamp(-10, -5, 5);
// => -5
clamp(10, -5, 5);
// => 5
utils/Compare
Compare
- utils/Compare
- ~Compare
- .start([cb]) ⇒
TCompare
- .then([cb]) ⇒
TCompare
- .reverse() ⇒
TCompare
- .end() ⇒
ISortCb.<any>
- .start([cb]) ⇒
- ~Compare
utils/Compare~Compare
Kind: inner class of utils/Compare
- ~Compare
- .start([cb]) ⇒
TCompare
- .then([cb]) ⇒
TCompare
- .reverse() ⇒
TCompare
- .end() ⇒
ISortCb.<any>
- .start([cb]) ⇒
TCompare
Compare.start([cb]) ⇒ Start compare, The method must be called at first
Kind: static method of Compare
Param | Type | Description |
---|---|---|
[cb] | ICompareCb |
If compare number array, You can not pass this parameter |
TCompare
Compare.then([cb]) ⇒ Then compare
Kind: static method of Compare
Param | Type | Default |
---|---|---|
[cb] | ICompareCb |
giveBackCb |
TCompare
Compare.reverse() ⇒ Reverse
Kind: static method of Compare
ISortCb.<any>
Compare.end() ⇒ End compare, The method must be called at last
Kind: static method of Compare
string
utils/ellipsis ⇒ String abbreviation with ellipsis
Param | Type | Default |
---|---|---|
str | string |
|
num | number |
|
[template] | string |
"'...'" |
Example
ellipsis('0123456789', 4);
// => 0123...
ellipsis('012', 4);
// => 012
utils/error
Throw error
Param | Type |
---|---|
msg | string |
string
utils/format ⇒ String format, just support %s
, it's simple, but useful.
Returns: string
- Returns the formatted string.
Param | Type | Description |
---|---|---|
template | string |
Text with placeholders for data . |
data | * |
Data to interpolate into template . |
Example
formats('The %s is %s.', 'code', 123);
=> The code is 123.
string
utils/formats ⇒ String format, just support %s
, it's simple, but useful.
Returns: string
- Returns the formatted string.
Param | Type | Description |
---|---|---|
template | string |
Text with placeholders for data . |
data | Array.<any> |
Data to interpolate into template . |
Example
formats('I like %s and %s.', ['swimming', 'skiing']);
=> I like swimming and skiing.
string
utils/getClass ⇒ Determine the internal JavaScript [[Class]] of an object.
Returns: string
- Object to get the internal JavaScript [[Class]] of.
Param | Type | Description |
---|---|---|
value | * |
value The value to check. |
Example
class Foo {
private f: number = 1;
}
getClass(new Foo()) === Foo;
// => true
getClass(undefined) === undefined;
// => true
getClass(null) === null;
// => true
getClass(true) === Boolean;
// => true
getClass(new Boolean()) === Boolean;
// => true
getClass(3) === Number;
// => true
getClass("test") === String;
// => true
getClass(new String("test")) === String;
// => true
getClass(function(){}) === Function;
// => true
getClass([]) === Array;
// => true
getClass(new Array()) === Array;
// => true
getClass(new Date()) === Date;
// => true
getClass(new Error()) === Error;
// => true
getClass(Symbol()) === Symbol;
// => true
getClass(Object(Symbol())) === Symbol;
// => true
getClass(/test/) === RegExp;
// => true
getClass(Date) === Function;
// => true
getClass(Date) === Date;
// => false
boolean
utils/inRange ⇒ Checks if num
is between start
and end
, such as start <= num <= end
Returns: boolean
- Returns true
if number
is in the range, else false
.
Param | Type | Description |
---|---|---|
num | number |
The number to check. |
start | number |
The start of the range. |
end | number |
The end of the range. |
Example
inRange(3, 2, 4);
// => true
inRange(4.5, 2, 4.5);
// => true
inRange(-3, -6, -2);
// => true
boolean
utils/isArrayLike ⇒ Checks if value
is array-like. A value is considered array-like if it's
not a function and not a window and has a value.length
that's an integer greater than or
equal to 0
and less than or equal to Number.MAX_SAFE_INTEGER
.
Returns: boolean
- Returns true
if value
is array-like, else false
.
Param | Type | Description |
---|---|---|
value | * |
The value to check. |
Example
isArrayLike([1, 2, 3]);
// => true
isArrayLike(document.body.children);
// => true
isArrayLike('abc');
// => true
isArrayLike(noop);
// => false
isArrayLike(window);
// => false
boolean
utils/isBool ⇒ Checks if value
is classified as a boolean primitive or object.
Returns: boolean
- Returns true
if value
is a boolean, else false
.
Param | Type | Description |
---|---|---|
value | * |
The value to check. |
Example
isBool(false);
// => true
isBool(null);
// => false
boolean
utils/isClass ⇒ Checks if value
is classified as a Class
primitive or object.
Param | Type | Description |
---|---|---|
value | * |
The value to check. |
Class | function |
Which class. |
Example
isClass(Date, Function);
// => true
isClass(Date, Date);
// => false
isClass("test", String);
// => true
isClass(3, Number);
// => true
isClass(true, Boolean);
// => true
boolean
utils/isFunc ⇒ Checks if value
is classified as a Function
object.
Returns: boolean
- Returns true
if value
is a function, else false
.
Param | Type | Description |
---|---|---|
value | * |
The value to check. |
Example
isFunc(noop);
// => true
isFunc(/abc/);
// => false
boolean
utils/isInt ⇒ Checks if value
is an integer.
Returns: boolean
- Returns true
if value
is an integer, else false
.
Param | Type | Description |
---|---|---|
value | * |
The value to check. |
Example
isInt(3);
// => true
isInt(3.3);
// => false
isInt(Number.MAX_VALUE);
// => true
isInt(Number.MIN_VALUE);
// => false
isInt(Infinity);
// => false
isInt('3');
// => true
boolean
utils/isLength ⇒ Checks if value
is a valid array-like length.
Note: This method is loosely based on
ToLength
.
Returns: boolean
- Returns true
if value
is a valid length, else false
.
Param | Type | Description |
---|---|---|
value | * |
The value to check. |
Example
isLength(3);
// => true
isLength(Number.MIN_VALUE);
// => false
isLength(Infinity);
// => false
isLength('3');
// => false
boolean
utils/isNum ⇒ Checks if value
is classified as a Number
primitive or object.
Returns: boolean
- Returns true
if value
is a number, else false
.
Param | Type | Description |
---|---|---|
value | * |
The value to check. |
Example
isNum(3);
// => true
isNum(Number.MIN_VALUE);
// => true
isNum(Infinity);
// => true
isNum('3');
// => false
boolean
utils/isObj ⇒ Checks if value
is the
language type
of Object
. (e.g. arrays, functions, objects, regexes, new Number(0)
, and new String('')
)
Returns: boolean
- Returns true
if value
is an object, else false
.
Param | Type | Description |
---|---|---|
value | * |
The value to check. |
Example
isObj({});
// => true
isObj([1, 2, 3]);
// => true
isObj(noop);
// => true
isObj(null);
// => false
boolean
utils/isObjLike ⇒ Checks if value
is object-like. A value is object-like if it's not null
and has a typeof
result of "object".
Returns: boolean
- Returns true
if value
is object-like, else false
.
Param | Type | Description |
---|---|---|
value | * |
The value to check. |
Example
isObjLike({});
// => true
isObjLike([1, 2, 3]);
// => true
isObjLike(function(){});
// => false
isObjLike(null);
// => false
boolean
utils/isPlainObj ⇒ Checks if value
is a plain object, that is, an object created by the
Object
constructor or one with a [[Prototype]]
of null
.
Returns: boolean
- Returns true
if value
is a plain object, else false
.
Param | Type | Description |
---|---|---|
value | * |
The value to check. |
Example
function Foo() {
this.a = 1;
}
isPlainObj(new Foo);
// => false
isPlainObj([1, 2, 3]);
// => false
isPlainObj({ 'x': 0, 'y': 0 });
// => true
isPlainObj(Object.create(null));
// => true
boolean
utils/isStr ⇒ Checks if value
is classified as a String
primitive or object.
Returns: boolean
- Returns true
if value
is a string, else false
.
Param | Type | Description |
---|---|---|
value | * |
The value to check. |
Example
isStr('abc');
// => true
isStr(1);
// => false
T
utils/merge ⇒ Merge the contents of a object into the first object.
Returns: T
- Returns object
.
Param | Type | Description |
---|---|---|
tar | T |
An object that will receive the new properties. |
src | S |
An object containing additional properties to merge in. |
deep | boolean |
If true, the merge becomes recursive (aka. deep copy). passing false for this argument is not supported, default is true. |
Example
var object = {
'a': [{ 'b': 2 }, { 'd': 4 }]
};
var other = {
'a': [{ 'c': 3 }, { 'e': 5 }]
};
merge(object, other);
// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }
T
utils/mergeWith ⇒ Merge the contents of a object into the first object by customizer.
Returns: T
- Returns object
.
Param | Type | Description |
---|---|---|
tar | T |
An object that will receive the new properties. |
src | S |
An object containing additional properties to merge in. |
cb | IMergeCb |
Customizer the function to customize assigned values, the prop will be skip when return value is undefined, |
deep | boolean |
If true, the merge becomes recursive (aka. deep copy). passing false for this argument is not supported. |
Example
function customizer(srcValue, tarValue) {
if (Array.isArray(tarValue)) {
return tarValue.concat(srcValue);
}
}
var object = { 'a': [1], 'b': [2] };
var other = { 'a': [3], 'b': [4] };
mergeWith(object, other, customizer);
// => { 'a': [1, 3], 'b': [2, 4] }
utils/noop
This method returns undefined
.
Example
btn.onclick = fn || noop;
// => undefined
number
utils/random ⇒ Produces a random number between the inclusive lower
and upper
bounds.
Returns: number
- Returns the random floating-point number.
Param | Type | Description |
---|---|---|
lower | number |
The lower bound. |
upper | number |
The upper bound. |
Example
random(1, 5);
// => a floating-point number between 1 and 5
string
utils/repeat ⇒ Repeats the given string n
times.
Returns: string
- Returns the repeated string.
Param | Type | Description |
---|---|---|
str | string |
The string to repeat. |
n | number |
The number of times to repeat the string. |
Example
repeat('*', 3);
// => '***'
repeat('abc', 2);
// => 'abcabc'
repeat('abc', 0);
// => ''
number
utils/toFixed ⇒ Returns a number in fixed-point notation.
Returns: number
- Returns a number in fixed-point notation.
Param | Type | Default | Description |
---|---|---|---|
num | number |
The number to fixed. | |
[fixed] | number |
0 |
Number of digits after the decimal point. |
Example
toFixed(3.14);
// => 3
toFixed(3.1415926, 2);
// => 3.14
toFixed(3.1415926, 4);
// => 3.1415
arraySwap(arr, i, j)
Array elements swap.
Kind: global function
Param | Type |
---|---|
arr | Array.<T> |
i | number |
j | number |
*
giveBackCb(args) ⇒ giveBackCb
Kind: global function
Param | Type |
---|---|
args | * |