es-stl

EMCAScript Standard Template Library

Usage no npm install needed!

<script type="module">
  import esStl from 'https://cdn.skypack.dev/es-stl';
</script>

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

lodash

jquery

Modules

algm/binarySearchnumber

Binary Search

algm/binarySearchByRangenumber

Binary search by range

algm/exponentialSearchnumber

Exponential Search

algm/interpolationSearchnumber

Interpolation Search

algm/mergeSortArrArray.<T>

To combine two sorted arrays into an array sorted by original rules

algm/mergeSortSingleArray.<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/clampnumber

Clamps number within the inclusive lower and upper bounds.

utils/Compare

Compare

utils/ellipsisstring

String abbreviation with ellipsis

utils/error

Throw error

utils/formatstring

String format, just support %s, it's simple, but useful.

utils/formatsstring

String format, just support %s, it's simple, but useful.

utils/getClassstring

Determine the internal JavaScript [[Class]] of an object.

utils/inRangeboolean

Checks if num is between start and end, such as start <= num <= end

utils/isArrayLikeboolean

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.

utils/isBoolboolean

Checks if value is classified as a boolean primitive or object.

utils/isClassboolean

Checks if value is classified as a Class primitive or object.

utils/isFuncboolean

Checks if value is classified as a Function object.

utils/isIntboolean

Checks if value is an integer.

utils/isLengthboolean

Checks if value is a valid array-like length.

Note: This method is loosely based on ToLength.

utils/isNumboolean

Checks if value is classified as a Number primitive or object.

utils/isObjboolean

Checks if value is the language type of Object. (e.g. arrays, functions, objects, regexes, new Number(0), and new String(''))

utils/isObjLikeboolean

Checks if value is object-like. A value is object-like if it's not null and has a typeof result of "object".

utils/isPlainObjboolean

Checks if value is a plain object, that is, an object created by the Object constructor or one with a [[Prototype]] of null.

utils/isStrboolean

Checks if value is classified as a String primitive or object.

utils/mergeT

Merge the contents of a object into the first object.

utils/mergeWithT

Merge the contents of a object into the first object by customizer.

utils/noop

This method returns undefined.

utils/randomnumber

Produces a random number between the inclusive lower and upper bounds.

utils/repeatstring

Repeats the given string n times.

utils/toFixednumber

Returns a number in fixed-point notation.

Functions

arraySwap(arr, i, j)

Array elements swap.

giveBackCb(args)*

giveBackCb

algm/binarySearch ⇒ number

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

algm/binarySearchByRange ⇒ number

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

algm/exponentialSearch ⇒ number

Exponential Search

Param Type Default Description
arr Array.<T> an array after sort from small to large
searchVal number
[cb] ISearchCb.<T> giveBackCb

algm/interpolationSearch ⇒ number

Interpolation Search

Param Type Default Description
arr Array.<T> an array after sort from small to large
searchVal number
[cb] ISearchCb.<T> giveBackCb

algm/mergeSortArr ⇒ Array.<T>

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>

algm/mergeSortSingle ⇒ Array.<T>

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

Kind: inner class of ds/Dictionary

dictionary.fromObject(obj) ⇒ this

Object transform to Dictionary

Kind: instance method of Dictionary

Param Type
obj IKeyValue.<V>

dictionary.toObject() ⇒ IKeyValue.<V>

Dictionary transform to Object

Kind: instance method of Dictionary

dictionary.isEmpty() ⇒ boolean

isEmpty

Kind: instance method of Dictionary

dictionary.size() ⇒ number

size

Kind: instance method of Dictionary

dictionary.has(key) ⇒ boolean

has

Kind: instance method of Dictionary

Param Type
key K

dictionary.get(key) ⇒ V

get

Kind: instance method of Dictionary

Param Type
key K

dictionary.set(key, value) ⇒ this

set

Kind: instance method of Dictionary

Param Type
key K
value V

dictionary.remove(key) ⇒ boolean

remove

Kind: instance method of Dictionary

Param Type
key K

dictionary.keys() ⇒ Array.<string>

keys

Kind: instance method of Dictionary

dictionary.values([values]) ⇒ Array.<V>

values

Kind: instance method of Dictionary

Param Type Default
[values] Array.<V> []

dictionary.every(cb) ⇒ boolean

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>

dictionary.some(cb) ⇒ boolean

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

dictionary.map(cb) ⇒ Array.<T>

map

Kind: instance method of Dictionary

Param Type
cb IMapCb.<V, string, T>

dictionary.filter(cb) ⇒ Array.<V>

filter

Kind: instance method of Dictionary

Param Type
cb IFilterCb.<V, string>

dictionary.find(cb) ⇒ V

find

Kind: instance method of Dictionary

Param Type
cb IFilterCb.<V, string>

dictionary.toString() ⇒ string

toString

Kind: instance method of Dictionary

dictionary.clear()

Clear the dictionary

Kind: instance method of Dictionary

ds/Map

Map

ds/Map~Map

Kind: inner class of ds/Map

map.isEmpty() ⇒ boolean

isEmpty

Kind: instance method of Map

map.size() ⇒ number

size

Kind: instance method of Map

map.keys() ⇒ Array.<K>

keys

Kind: instance method of Map

map.values() ⇒ Array.<V>

values

Kind: instance method of Map

map.has(key) ⇒ boolean

has

Kind: instance method of Map

Param Type
key K

map.get(key) ⇒ V

get

Kind: instance method of Map

Param Type
key K

map.set(key, value) ⇒ this

set

Kind: instance method of Map

Param Type
key K
value V

map.indexOf(key) ⇒ number

indexOf

Kind: instance method of Map

Param Type
key K

map.remove(key) ⇒ boolean

remove

Kind: instance method of Map

Param Type
key K

map.sortByKey(cb) ⇒ this

sortByKey

Kind: instance method of Map

Param Type
cb ISortCb.<K>

map.sortByValue(cb) ⇒ this

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

Kind: inner class of ds/Queue

queue.isEmpty() ⇒ boolean

isEmpty

Kind: instance method of Queue

queue.size() ⇒ number

size

Kind: instance method of Queue

queue.enqueue(el) ⇒ this

enqueue

Kind: instance method of Queue

Param Type
el T

queue.dequeue() ⇒ T

dequeue

Kind: instance method of Queue

queue.front() ⇒ T

Return the first element

Kind: instance method of Queue

queue.back() ⇒ T

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

stack.isEmpty() ⇒ boolean

isEmpty

Kind: instance method of Stack

stack.size() ⇒ number

size

Kind: instance method of Stack

stack.push(el) ⇒ this

push

Kind: instance method of Stack

Param Type
el T

stack.pop() ⇒ T

pop

Kind: instance method of Stack

stack.peek() ⇒ T

Return the top element of the stack

Kind: instance method of Stack

stack.clear()

Clear the stack

Kind: instance method of Stack

utils/clamp ⇒ number

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

Kind: inner class of utils/Compare

Compare.start([cb]) ⇒ TCompare

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

Compare.then([cb]) ⇒ TCompare

Then compare

Kind: static method of Compare

Param Type Default
[cb] ICompareCb giveBackCb

Compare.reverse() ⇒ TCompare

Reverse

Kind: static method of Compare

Compare.end() ⇒ ISortCb.<any>

End compare, The method must be called at last

Kind: static method of Compare

utils/ellipsis ⇒ string

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

utils/format ⇒ string

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.

utils/formats ⇒ string

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.

utils/getClass ⇒ string

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

utils/inRange ⇒ boolean

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

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

utils/isBool ⇒ boolean

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

utils/isClass ⇒ boolean

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

utils/isFunc ⇒ boolean

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

utils/isInt ⇒ boolean

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

utils/isLength ⇒ boolean

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

utils/isNum ⇒ boolean

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

utils/isObj ⇒ boolean

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

utils/isObjLike ⇒ boolean

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

utils/isPlainObj ⇒ boolean

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

utils/isStr ⇒ boolean

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

utils/merge ⇒ T

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 }] }

utils/mergeWith ⇒ T

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

utils/random ⇒ number

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

utils/repeat ⇒ string

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);
// => ''

utils/toFixed ⇒ number

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 *