Methods
(static) average(nums)
Average all numbers.
Name | Type | Description |
---|---|---|
nums |
number[] |
(static) between(value, min, max) → {number}
calculate between min, max
Name | Type | Description |
---|---|---|
value |
number | |
min |
number | |
max |
number |
Returns:
- Type
- number
(static) calculateBoundSize(size, minSize, maxSize, keepRatioopt) → {number[]}
calculate bound size
Name | Type | Description |
---|---|---|
size |
number[] | |
minSize |
number[] | |
maxSize |
number[] | |
keepRatio ? |
number | boolean |
Returns:
- Type
- number[]
(static) camelize(text) → {String}
transform strings to camel-case
Name | Type | Description |
---|---|---|
text |
String | string |
Example
import {camelize} from "@daybrush/utils";
console.log(camelize("transform-origin")); // transformOrigin
console.log(camelize("abcd_efg")); // abcdEfg
console.log(camelize("abcd efg")); // abcdEfg
Returns:
camel-case string
- Type
- String
(static) convertUnitSize(pos, size)
convert unit size to px size
Name | Type | Description |
---|---|---|
pos |
string | |
size |
number | IObject<((pos: number) => number) | number> |
(static) counter(num) → {number[]}
Name | Type | Description |
---|---|---|
num |
number |
Returns:
- Type
- number[]
(static) decamelize(text, separatoropt) → {string}
transform a camelized string into a lowercased string.
Name | Type | Default | Description |
---|---|---|---|
text |
string | a camel-cased string | |
separator ? |
string |
"-"
|
a separator |
Example
import {decamelize} from "@daybrush/utils";
console.log(decamelize("transformOrigin")); // transform-origin
console.log(decamelize("abcdEfg", "_")); // abcd_efg
Returns:
a lowercased string
- Type
- string
(static) deepFlat<T extends any[]>(arr) → {Array<FlattedElement<T[0]>>}
Name | Type | Description |
---|---|---|
arr |
T |
Returns:
- Type
- Array<FlattedElement<T[0]>>
(static) dot(a1, a2, b1, b2) → {dot}
Returns the inner product of two numbers(`a1`, `a2`) by two criteria(`b1`, `b2`).
Name | Type | Description |
---|---|---|
a1 |
number | The first number |
a2 |
number | The second number |
b1 |
number | The first number to base on the inner product |
b2 |
number | The second number to base on the inner product |
Returns:
- Returns the inner product
import from "@daybrush/utils";
console.log(dot(0, 15, 2, 3)); // 6
console.log(dot(5, 15, 2, 3)); // 9
console.log(dot(5, 15, 1, 1)); // 10
- Type
- dot
(static) flat<Type>(arr) → {Type[]}
Name | Type | Description |
---|---|---|
arr |
Type[][] |
Returns:
- Type
- Type[]
(static) getCenterPoint(points) → {number[]}
Get the average point of all points.
Name | Type | Description |
---|---|---|
points |
number[][] |
Returns:
- Type
- number[]
(static) getDist(a, bopt)
Get the distance between two points.
Name | Type | Description |
---|---|---|
a |
number[] | |
b ? |
number[] |
(static) getEntries(obj) → {[string, any][]}
Name | Type | Description |
---|---|---|
obj |
IObject<any> |
Returns:
- Type
- [string, any][]
(static) getKeys(obj) → {string[]}
Name | Type | Description |
---|---|---|
obj |
IObject<any> |
Returns:
- Type
- string[]
(static) getRad(pos1, pos2) → {number}
Get the angle of two points. (0 <= rad < 359)
Name | Type | Description |
---|---|---|
pos1 |
number[] | |
pos2 |
number[] |
Returns:
- Type
- number
(static) getShapeDirection(points) → {1 | -1}
Gets the direction of the shape.
Name | Type | Description |
---|---|---|
points |
number[][] |
Returns:
- Type
- 1 | -1
(static) getValues(obj) → {any[]}
Name | Type | Description |
---|---|---|
obj |
IObject<any> |
Returns:
- Type
- any[]
(static) isArray(value) → {value is any[]}
Check the type that the value is isArray.
Name | Type | Description |
---|---|---|
value |
string | Value to check the type |
Example
import {isArray} from "@daybrush/utils";
console.log(isArray([])); // true
console.log(isArray({})); // false
console.log(isArray(undefined)); // false
console.log(isArray(null)); // false
Returns:
true if the type is correct, false otherwise
- Type
- value is any[]
(static) isFunction(value) → {value is (...args: any[]) => any}
Check the type that the value is function.
Name | Type | Description |
---|---|---|
value |
string | Value to check the type |
Example
import {isFunction} from "@daybrush/utils";
console.log(isFunction(function a() {})); // true
console.log(isFunction(() => {})); // true
console.log(isFunction("1234")); // false
console.log(isFunction(1)); // false
console.log(isFunction(null)); // false
Returns:
true if the type is correct, false otherwise
- Type
- value is (...args: any[]) => any
(static) isObject(value) → {value is IObject<any>}
Check the type that the value is object.
Name | Type | Description |
---|---|---|
value |
string | Value to check the type |
Example
import {isObject} from "@daybrush/utils";
console.log(isObject({})); // true
console.log(isObject(undefined)); // false
console.log(isObject("")); // false
console.log(isObject(null)); // false
Returns:
true if the type is correct, false otherwise
- Type
- value is IObject<any>
(static) isString(value) → {value is string}
Check the type that the value is string.
Name | Type | Description |
---|---|---|
value |
string | Value to check the type |
Example
import {isString} from "@daybrush/utils";
console.log(isString("1234")); // true
console.log(isString(undefined)); // false
console.log(isString(1)); // false
console.log(isString(null)); // false
Returns:
true if the type is correct, false otherwise
- Type
- value is string
(static) isUndefined(value) → {boolean}
Check the type that the value is undefined.
Name | Type | Description |
---|---|---|
value |
string | Value to check the type |
Example
import {isUndefined} from "@daybrush/utils";
console.log(isUndefined(undefined)); // true
console.log(isUndefined("")); // false
console.log(isUndefined(1)); // false
console.log(isUndefined(null)); // false
Returns:
true if the type is correct, false otherwise
- Type
- boolean
(static) pushSet<T>(elements, element)
Name | Type | Description |
---|---|---|
elements |
T[] | |
element |
T |
(static) replaceOnce(text, fromText, toText) → {string}
Name | Type | Description |
---|---|---|
text |
string | |
fromText |
RegExp | string | |
toText |
string | ((...args: any[]) => string) |
Returns:
- Type
- string
(static) sortOrders(keys, ordersopt)
Name | Type | Default | Description |
---|---|---|---|
keys |
Array<string | number> | ||
orders ? |
Array<string | number> |
[]
|
(static) splitBracket(text) → {object}
divide text by bracket "(", ")".
Name | Type | Description |
---|---|---|
text |
string | text to divide |
Example
import {splitBracket} from "@daybrush/utils";
console.log(splitBracket("a(1, 2)"));
// {prefix: "a", value: "1, 2", suffix: ""}
console.log(splitBracket("a(1, 2)b"));
// {prefix: "a", value: "1, 2", suffix: "b"}
Returns:
divided texts
- Type
- object
(static) splitComma(text) → {Array}
divide text by comma.
Name | Type | Description |
---|---|---|
text |
string | text to divide |
Example
import {splitComma} from "@daybrush/utils";
console.log(splitComma("a,b,c,d,e,f,g"));
// ["a", "b", "c", "d", "e", "f", "g"]
console.log(splitComma("'a,b',c,'d,e',f,g"));
// ["'a,b'", "c", "'d,e'", "f", "g"]
Returns:
divided texts
- Type
- Array
(static) splitSpace(text) → {Array}
divide text by space.
Name | Type | Description |
---|---|---|
text |
string | text to divide |
Example
import {spliceSpace} from "@daybrush/utils";
console.log(splitSpace("a b c d e f g"));
// ["a", "b", "c", "d", "e", "f", "g"]
console.log(splitSpace("'a,b' c 'd,e' f g"));
// ["'a,b'", "c", "'d,e'", "f", "g"]
Returns:
divided texts
- Type
- Array
(static) splitUnit(text) → {{prefix: string, unit: string, value: number}}
divide text by number and unit.
Name | Type | Description |
---|---|---|
text |
string | text to divide |
Example
import {splitUnit} from "@daybrush/utils";
console.log(splitUnit("10px"));
// {prefix: "", value: 10, unit: "px"}
console.log(splitUnit("-10px"));
// {prefix: "", value: -10, unit: "px"}
console.log(splitUnit("a10%"));
// {prefix: "a", value: 10, unit: "%"}
Returns:
divided texts
- Type
- {prefix: string, unit: string, value: number}
(static) sum(nums) → {number}
Add all the numbers.
Name | Type | Description |
---|---|---|
nums |
number[] |
Returns:
- Type
- number
(static) throttle(num, unitopt)
throttle number depending on the unit.
Name | Type | Description |
---|---|---|
num |
number | |
unit ? |
number |
(static) throttleArray(nums, unitopt)
throttle number array depending on the unit.
Name | Type | Description |
---|---|---|
nums |
number[] | |
unit ? |
number |
(static) toArray<T>(value) → {T[]}
transforms something in an array into an array.
Name | Type | Description |
---|---|---|
value |
IArrayFormat<T> | Array form |
Example
import {toArray} from "@daybrush/utils";
const arr1 = toArray(document.querySelectorAll(".a")); // Element[]
const arr2 = toArray(document.querySelectorAll<HTMLElement>(".a")); // HTMLElement[]
Returns:
an array
- Type
- T[]