Global

Members

(constant) ArrayUtils

Provides helper functions for JavaScript native arrays.

Author:
  • Haixing Hu

Methods

addClassToHmlElement(el, cls)

Adds CSS classes to the specified HTML element.

Author:
  • Haixing Hu
Parameters:
Name Type Description
el HTMLElement

The specified HTML element object.

cls String

The name of the CSS class to be added; it can be multiple class names, separated by spaces.

addSearchParams(params, url) → {String}

Add a parameter to the query string of the current address. Note that the added parameter will be URL encoded.

Because the addresses of Vue.js are all in hash form for routing, the basic window.location.search cannot be used to obtain the ocrrect query string. For example: http://dev.example.com/test/#/finish?params=xxx.

Author:
  • Haixing Hu
Parameters:
Name Type Description
params Object

Each attribute name and attribute value of this argument will be added to the query string of the current address as a key-value pair.

url String | URL

An optional argument, indicating the URL address to be parsed, as a String or URL object. If this argument is missing, window.location is used.

Returns:
Type:
String

A new link address with the specified parameters added and properly regularized.

checkArgumentType(name, value, type, nullable)

Checks if a value is of a given type.

Parameters:
Name Type Default Description
name string

The name of the parameter to check.

value any

The value of the parameter to check.

type any

The constructor of the specified type. If the value is a primitive type value, use the constructor of the corresponding wrapping object. If this argument is an array, the value must be one of the specified types in the array.

nullable boolean false

If true, the parameter value is allowed to be null or undefined; otherwise, it is not allowed.

Throws:

TypeError Throws an exception if the value of the parameter to check is not of the specified type.

convert(value, replacer, path, pathMap)

Recursively convert objects that may have circular references.

This function replaces the circularly referenced sub-object in the object with the form: {'$ref': PATH}, where PATH is the path of the first occurrence of the sub-object in the object structure tree. For example,

const a = [];
a.push(a);
console.dir(decycle(a));   // output `[{ $ref: '$' }]`
Author:
  • Haixing Hu
Parameters:
Name Type Description
value any

The value or object to be converted.

replacer function

Optional replacement function. If it is not undefined, the function will be applied to each value to be converted and its return value will be converted.

path String

The current value or path of the object in its root object tree.

pathMap WeakMap

Used to store a mapping of objects to their paths.

createDate(year, month, day, hour, minute, second, millisecond) → {Date}

Creates a JavaScript built-in Date object.

This function is similar to the Date constructor, but it has the following differences:

  • The year argument of this function must be specified in the full 4-digit form. For example, 1995. While the year argument of the Date constructor must be specified in 2-digit form, which has the y2k problem.
  • The month argument of this function is an integer between 1 and 12. While the month argument of the Date constructor is an integer between 0 and 11.
Author:
  • Haixing Hu
See:
Parameters:
Name Type Default Description
year number

The year of the date. The year must be specified in the full 4-digit form. For example, 1995.

month number 1

The month of the date. The month is an integer between 1 and 12. The default value of this argument is 1.

day number 1

The day of the month. The day is an integer between 1 and 31. The default value of this argument is 1.

hour number 0

The hour of the date. The hour is an integer between 0 and 23. The default value of this argument is 0.

minute number 0

The minute of the date. The minute is an integer between 0 and 59. The default value of this argument is 0.

second number 0

The second of the date. The second is an integer between 0 and 59. The default value of this argument is 0.

millisecond number 0

The millisecond of the date. The millisecond is an integer between 0 and 999. The default value of this argument is 0.

Throws:
  • if ary argument is not an integer number.

    Type
    TypeError
  • if any argument is out of range.

    Type
    RangeError
Returns:
Type:
Date

a JavaScript built-in Date object, whose value is specified by the arguments.

decycle(value, replacer)

Converts an object that may have circular references to sub-objects into a simple object without circular references.

This function replaces the circularly referenced sub-object in the object with the form: {'$ref': PATH}, where PATH is the path of the first occurrence of the sub-object in the object structure tree. For example,

const a = [];
a.push(a);
console.dir(decycle(a));   // output `[{ $ref: '$' }]`
Parameters:
Name Type Description
value any

The value or object to be converted.

replacer function

Optionally, this function is used to replace certain types of objects.

Returns:

The converted simple object no longer contains cyclically referenced sub-objects.

dequote(str) → {string}

Dequotes the given string.

If the given string is quoted by single quotes (') or double quotes ("), then this function will remove the quotes and return the inner string.

Note that if the given string does not start and end with the same quote, this function does nothing and returns the original string.

Parameters:
Name Type Description
str string

The string to be dequoted.

Returns:
Type:
string

The dequoted string.

emptyFieldsToNull(obj)

Creates a new object based on an existing object, but sets all its empty properties values to null.

Deprecated:
  • Yes
Author:
  • Haixing Hu
Parameters:
Name Type Description
obj any

The object or value to be converted.

Returns:
  • If obj is undefined, returns undefined;
    • If obj is null, returns null;
    • If obj is a string, null is returned for an empty string, and obj is returned for other strings;
    • If obj is a boolean, number, bigint symbol or function value, returns obj;
    • If obj is an Array, Set, or Map, a shallow copy of obj is returned. Otherwise, return a copy of obj, but modify all its attribute values that are empty strings to null.

emptyToNull(value)

Map an empty value to null.

If the value is a string, or an array, or a typed array, then it is empty if its length is zero. If the value is a map, or a set, then it is empty if its size is zero. Otherwise, if the value has the numeric length or size property, then it is empty if its length or size property is zero. If the value has the isEmpty() method, then it is empty if the isEmpty() method returns true. Otherwise, the value is not empty.

Author:
  • Haixing Hu
See:
Parameters:
Name Type Description
value any

The value to be mapped.

Returns:

If the value is undefined or null, or an "empty" value, returns null; otherwise, returns the value itself.

extractOssUrlInfo(url)

从阿里云OSS服务器的URL中提取其图片信息。

注意,从阿里云OSS服务器获取的图片URL可能会加上旋转参数,例如 https://a.aliyuncs.com/yangzi/dev/ac622799-c62f-4e64-a76a-0ec685dee893.png?x-oss-process=image/rotate,210 此函数将会将图片的原始URL和旋转参数分隔开。

目前此函数仅支持旋转预处理参数。

Parameters:
Name Type Description
url String

一个阿里云OSS服务器中存储的图片的URL,可能带有预处理参数。

Returns:

返回一个对象,属性url表示该附件原始图片的URL(注意不加OSS处理参数), 属性degree表示该附件图片相对原始图片所旋转的角度。

fixScroll()

修复在iOS下的页面切换滚动定位问题。

Author:
  • 胡海星

floatEqual(x, y, epsilon) → {Boolean}

测试两个浮点数是否相等。

Author:
  • 胡海星
Parameters:
Name Type Description
x Number

待测试的第一个浮点数。

y Number

待测试的第二个浮点数。

epsilon Number

用于比较两个浮点数之差绝对值的epsilon,默认值为1e-8。

Returns:
Type:
Boolean

两个浮点数是否相等。

format(str, args) → {String}

格式化字符串,字符串模板中可以用形如'{0}', '{1}', ... 的占位符。

Author:
  • 胡海星
Parameters:
Name Type Description
str String

待格式化的字符串模板。

args Array

用于格式化的参数数组。

Returns:
Type:
String

格式化结果字符串。

formatDigits(value, digits) → {string}

Format a digits string.

Author:
  • Haixing Hu
Parameters:
Name Type Description
value number

The value to be formatted, which must be a positive or negative integer or zero.

digits number

The digits of the formatted string, which must be an non-negative integer.

Returns:
Type:
string

The formatted string. If the value is undefined or null, return an empty string. If the digits is undefined or null, return the string representation of the value directly. Otherwise, return the string representation of the value with leading zeros to make the length of the string equals to the specified digits.

formatLocalDate(date) → {string}

Format the date part of a built-in Date object as a string in the local time zone.

Author:
  • Haixing Hu
Parameters:
Name Type Description
date Date

a built-in Date object.

Returns:
Type:
string

the date part of the specified date, represented in a string in the local time zone. The format of the returned string is YYYY-MM-DD.

formatLocalDatetime(date) → {string}

Format the datetime part of a built-in Date object as a string in the local time zone.

Author:
  • Haixing Hu
Parameters:
Name Type Description
date Date

a built-in Date object.

Returns:
Type:
string

the datetime part of the specified date, represented in a string in the local time zone. The format of the returned string is YYYY-MM-DD HH:mm:ss.

formatLocalTime(date) → {string}

Gets the time part of a built-in Date object as a string in the local time zone.

Author:
  • Haixing Hu
Parameters:
Name Type Description
date Date

a built-in Date object.

Returns:
Type:
string

the time part of the specified date, represented in a string in the local time zone. The format of the returned string is HH:mm:ss.

getAncestorClasses(Class) → {array}

Get all classes in the ancestor tree of the specified class.

Author:
  • Haixing Hu
Parameters:
Name Type Description
Class function

the constructor of the specified class.

Returns:
Type:
array

The array of the constructors of all classes in ancestor tree of the specified class, sorted from the current class to the ancestor class. The specified class is the first element of the returned array. If the parameter Class is not a function, an empty array is returned.

getDeclaringClass(Class, property) → {function}

Gets the declaring class of the specified property from the ancestor tree of the specified class.

All classes in the ancestor tree of the specified class must have a default constructor.

If the specified property is declared in multiple places in the ancestor tree of the specified class, this function returns the earliest declaring class.

Deprecated:
  • This function **CANNOT** correctly handle the class fields, and we haven't found a correct way to implement it. Do **NOT** use it.
Parameters:
Name Type Description
Class function

The constructor of the specified class.

property string

The name of the specified property.

Returns:
Type:
function

The earliest declaring class of the specified property in the ancestor tree of the specified class; or null if there is no such declaring class.

getHash(url) → {String}

获取当前地址的hash.

因为Vue.js的地址都是hash形式做路由,所以可能存在形如下面的hash和search组合: http://dev.example.com/test/#/finish?params=xxx

Author:
  • 胡海星
Parameters:
Name Type Description
url String | URL

字符串或URL对象,可选参数。表示待解析的URL地址。若无此参数则使用 window.location.href。

Returns:
Type:
String

URL地址中的hash,不包含#,也不解析其中的参数,也不对URL编码的参数进行解 码。若不存在hash(即不存在#),则返回null. 注意返回的hash可能为空字符串。

getParsedSearch(url) → {Object}

获取当前地址的query string,并将其解析为对象返回。

因为Vue.js的地址都是hash形式做路由,因此基本的 window.location.search 不能获得 query string。 例如 http://dev.example.com/test/#/finish?params=xxx

Author:
  • 胡海星
Parameters:
Name Type Description
url String | URL

可选,表示待解析的URL地址。若无此参数则使用 window.location.href。

Returns:
Type:
Object

URL地址中的query string所解析的对象。若不存在query string参数,则返回null。

getProperty(obj, path, defaultValue) → {any}

Gets the value at the specified path of an object.

Author:
  • Haixing Hu
Parameters:
Name Type Description
obj object

The object to query.

path string

The path of the property to get. It should be a string of the property names separated by dots. For example, if the object is {a: {b: {c: 1}}}, then the path 'a.b.c' will return 1. It also supports array index. For example, if the object is {a: {b: [1, 2, 3]}}, then the path 'a.b[1]' will return 2. If the object is {a: {b: [{c: 1}, {c: 2}]}}, then the path 'a.b[1].c' will return 2. If the path is empty, then the object itself is returned.

defaultValue any

The default value. If it is not specified, then undefined is used as the default value.

Returns:
Type:
any

The value at the specified path of the object, or the default value if the path does not exist in the object.

getSearch(url) → {String}

Get the query string of the specified URL.

Because the addresses of Vue.js are all in hash form for routing, the basic window.location.search cannot be used to obtain the correct query string. For example: http://dev.example.com/test/#/finish?params=xxx

Author:
  • Haixing Hu
Parameters:
Name Type Description
url String | URL

Optional, indicating the URL address to be parsed. If this argument is not provided, the window.location.href will be used.

Returns:
Type:
String

The query string in the URL address, which does not contain ?, nor parsed, and the URL-encoded parameters are not decoded. If the query string parameter does not exist, null is returned.

getSearchParam(name, url) → {String}

获取当前地址的query string

因为Vue.js的地址都是hash形式做路由,因此基本的 window.location.search 不能获得 query string。 例如 http://dev.example.com/test/#/finish?params=xxx

Author:
  • 胡海星
Parameters:
Name Type Description
name String

参数名称

url String | URL

可选,表示待解析的URL地址。若无此参数则使用 window.location.href。

Returns:
Type:
String

URL地址中的query string所包含的指定名称的参数值。若不存在则返回null.

hasProperty(obj, path) → {boolean}

Tests whether an object has a property at the specified path.

Author:
  • Haixing Hu
Parameters:
Name Type Description
obj object

The object to query.

path string

The path of the property to get. It should be a string of the property names separated by dots. For example, if the object is {a: {b: {c: 1}}}, then the path 'a.b.c' has the property value of 1. It also supports array index. For example, if the object is {a: {b: [1, 2, 3]}}, then the path 'a.b[1]' has the property value of 2. If the object is {a: {b: [{c: 1}, {c: 2}]}}, then the path 'a.b[1].c' has the property value of 2. If the path is empty, then the object has the property value of itself.

Returns:
Type:
boolean

true if the object has a property at the specified path; false otherwise.

isAndroid() → {boolean}

Checks if the current browser is Android browser.

Author:
  • Haixing Hu
Returns:
Type:
boolean

true if the current browser is Android browser; otherwise, false.

isChrome() → {boolean}

Checks if the current browser is Chrome.

Author:
  • Haixing Hu
Returns:
Type:
boolean

true if the current browser is Chrome; otherwise, false.

isEdge() → {boolean}

Checks if the current browser is Edge.

Author:
  • Haixing Hu
Returns:
Type:
boolean

true if the current browser is Edge; otherwise, false.

isEmpty(value) → {boolean}

Tests whether the specified value is empty.

If the value is a string, or an array, or a typed array, then it is empty if its length is zero. If the value is a map, or a set, then it is empty if its size is zero. Otherwise, if the value has the numeric length or size property, then it is empty if its length or size property is zero. If the value has the isEmpty() method, then it is empty if the isEmpty() method returns true. Otherwise, the value is not empty.

Parameters:
Name Type Description
value any

the specified value to be tested.

Returns:
Type:
boolean

true if the specified value is empty; false otherwise.

isFirefox() → {boolean}

Checks if the current browser is Firefox.

Author:
  • Haixing Hu
Returns:
Type:
boolean

true if the current browser is Firefox; otherwise, false.

isHtmlElement(obj)

判定指定的变量是否是一个HTML的元素节点。

Author:
  • 胡海星
Parameters:
Name Type Description
obj any

待判定的变量。

Returns:

若该变量是一个HTML的元素节点,则返回{@code true};否则返回{@code false}。

isHtmlElementHasClass(el, cls)

判定指定的HTML元素是否具有指定的CSS类。

Author:
  • 胡海星
Parameters:
Name Type Description
el HTMLElement

指定的HTML元素对象。

cls String

指定的CSS类名。

Returns:

如果指定的HTML元素具有指定的CSS类,返回{@code true};否则返回{@code false}。

isIE() → {boolean}

Checks if the current browser is IE.

Author:
  • Haixing Hu
Returns:
Type:
boolean

true if the current browser is IE; otherwise, false.

isIos() → {Boolean}

判断当前是否处于iOS操作系统中。

Author:
  • 胡海星
Returns:
Type:
Boolean

若处于iOS操作系统中,返回true;否则返回false。

isMyNanjingApp() → {Boolean}

判断当前是否处于“我的南京”App中。

Author:
  • 胡海星
Returns:
Type:
Boolean

若处于“我的南京”App中,返回true;否则返回false。

isOpera() → {boolean}

Checks if the current browser is Opera.

Author:
  • Haixing Hu
Returns:
Type:
boolean

true if the current browser is Opera; otherwise, false.

isSafari() → {boolean}

Checks if the current browser is Safari.

Author:
  • Haixing Hu
Returns:
Type:
boolean

true if the current browser is Safari; otherwise, false.

isString(obj)

判定指定的变量是否是一个字符串。

Author:
  • 胡海星
Parameters:
Name Type Description
obj any

待判定的变量。

Returns:

若该变量是JavaScript的{@code String},则返回{@code true};否则返回{@code false}。

isUndefinedOrNull(value) → {boolean}

Tests whether a value is undefined or null.

NOTE: this function is sometimes useful. Although JavaScript treats undefined and null as falsy values in boolean expressions, sometimes we need to distinguish them from other falsy values such as 0, false, NaN, '', etc.

Author:
  • Haixing Hu
See:
Parameters:
Name Type Description
value any

The value to be determined.

Returns:
Type:
boolean

If the value to be determined is undefined or null, returns true; otherwise, return false.

isUndefinedOrNullOrEmptyArray(value) → {boolean}

Tests whether a value is undefined, or null, or an empty array.

NOTE: this function is sometimes useful. Although JavaScript treats undefined, and null as falsy values in boolean expressions, sometimes we need to distinguish them from other falsy values such as 0, false, NaN, etc.

Author:
  • Haixing Hu
See:
Parameters:
Name Type Description
value any

The value to be determined.

Returns:
Type:
boolean

If the value to be determined is undefined, or null, or an empty array, returns true; otherwise, return false.

isUndefinedOrNullOrEmptyString(value) → {boolean}

Tests whether a value is undefined, or null, or an empty string.

NOTE: this function is sometimes useful. Although JavaScript treats undefined, null, and empty strings as falsy values in boolean expressions, sometimes we need to distinguish them from other falsy values such as 0, false, NaN, etc.

Author:
  • Haixing Hu
See:
Parameters:
Name Type Description
value any

The value to be determined.

Returns:
Type:
boolean

If the value to be determined is undefined, or null, or an empty string, returns true; otherwise, return false.

isWechat() → {Boolean}

判断当前是否处于“微信”App中。

Author:
  • 胡海星
Returns:
Type:
Boolean

若处于“微信”App中,返回true;否则返回false。

jsonStringify(value, beautify) → {String}

Serialize a value to a JSON string.

This function functions similarly to JSON.stringify(), but supports some built-in objects above ES6 and can format the printed JSON serialization result string according to the arguments.

Author:
  • Haixing Hu
Parameters:
Name Type Default Description
value any

The value.

beautify boolean false

Optional, indicating whether to beautify the output JSON string. The default value is false.

Returns:
Type:
String

The JSON string of the serialization of the specified value, which will be formatted if the beautify argument is true.

loadScript(src, attrs, parentNode) → {Promise}

Dynamically loads a JavaScript script.

Author:
  • Haixing Hu
Parameters:
Name Type Description
src String

The URL of the script to be loaded.

attrs Object

Optional, attributes that need to be added to the newly created script tag.

parentNode Node

Optional, the parent node of the new script tag.

Returns:
Type:
Promise

A Promise object that executes asynchronously, whose resolving value is the newly created script tag.

move(amount)

Because it's so fucking difficult to detect the scrolling element, just move them all

Parameters:
Name Type Description
amount number

normalizeUrl(url) → {String}

重构指定的URL为标准形式。

因为Vue.js的地址都是hash形式做路由,因此基本的 window.location.search 不能获得 query string。 例如 http://dev.example.com/test/#/finish?params=xxx

Author:
  • 胡海星
Parameters:
Name Type Description
url String | URL

字符串或URL对象,可选参数。表示待解析的URL地址。若无此参数则使用 window.location.href。

Returns:
Type:
String

加上了参数的新的链接地址。

rafThrottle(fn) → {function}

Throttle a high-frequency function to be called at most once in a animation frame.

How it works:

  1. The function uses a locked flag to prevent multiple calls within the same animation frame.
  2. When the returned function is invoked:
    • If locked is true, the function immediately returns, preventing fn from being called.
    • If locked is false, the locked flag is set to true, and fn is scheduled to run on the next animation frame using requestAnimationFrame.
  3. After fn executes, the locked flag is reset to false, allowing the next call to proceed.

Use cases:

  • Useful for reducing the frequency of function executions in high-frequency events like scroll, resize, mousemove, etc., to improve performance.
  • Ensures smooth, browser-friendly execution synchronized with the screen's refresh rate.
Author:
  • Haixing Hu
Parameters:
Name Type Description
fn function

The function to throttle. This function will only be executed once per animation frame (usually 16.7ms for 60fps).

Returns:
Type:
function

A throttled version of the input function fn.

redirect(url, timeout) → {Promise}

Jumps to the specified URL.

Author:
  • Haixing Hu
Parameters:
Name Type Description
url String

The URL to be redirected to.

timeout Number

Optional parameter, indicating jump delay, is milliseconds. If not specified, the default delay in the configuration DEFAULT_TIMEOUT (300ms) is used.

Returns:
Type:
Promise

A Promise object, whose resolving value is the URL to be redirected to.

removeClassFromHmlElement(el, cls)

从指定的HTML元素的类列表中删除指定的CSS类。

Author:
  • 胡海星
Parameters:
Name Type Description
el HTMLElement

指定的HTML元素对象。

cls String

待删除的CSS类名;可以是多个类名,用空格隔开。

removeEmptyFields(obj) → {any}

Create a new object based on an object, but remove all its attribute values that are empty strings or null.

Deprecated:
  • Yes
Author:
  • Haixing Hu
Parameters:
Name Type Description
obj any

The object or value to be converted.

Returns:
Type:
any
  • If obj is undefined, returns undefined;
    • If obj is null, returns undefined;
    • If obj is a string, returns undefined if it is an empty string, and returns obj for other strings;
    • If obj is a number, boolean, bigint, symbol or function, returns obj;
    • If obj is a Array, Set, WeakSet, Map, or WeakMap, a shallow copy of obj is returned;
    • Otherwise, returns a copy of obj, but change all its attribute values that are empty strings to undefined.

removeSearchParam(name, url) → {String}

在当前地址的query string中删除一个参数

因为Vue.js的地址都是hash形式做路由,因此基本的 window.location.search 不能获得 query string。 例如 http://dev.example.com/test/#/finish?params=xxx

Author:
  • 胡海星
Parameters:
Name Type Description
name String

待删除的参数名称

url String | URL

字符串或URL对象,可选参数。表示待解析的URL地址。若无此参数则使用 window.location.href。

Returns:
Type:
String

删除了指定参数的新的链接地址,并且被正确正则化。

restoreVueManaged(obj) → {Object|Array}

将一个被Vue托管的对象或数组还原为原生的对象或数组。

在定义Vue.js的组件时,通过data()函数返回的绑定数据对象会被Vue.js框架托管; 具体而言,为了实现数据绑定,Vue.js框架会把被托管对象的所有属性修改为getter/setter 函数,并且在getter/setter函数实现时检测数据变化。对于被托管数组,Vue.js会 把数组的length属性丢弃,直接修改数组的0,1,...这些属性值。这种不规范地修 改对象和数组会给很多框架带来麻烦。

为了方便处理这类被托管对象和数组,此函数会递归地将其还原为原生的对象和数组, 然后再传递给相关框架做进一步处理。

注意:此函数假设输入的被托管对象属性树中没有循环引用。

Author:
  • 胡海星
Parameters:
Name Type Description
obj Object

被Vue托管的对象或数组,其属性树中不能有循环引用。

Returns:
Type:
Object | Array

原生的对象或数组,其属性值或内部元素也会被递归还原。

round(number, digits) → {Number}

将一个浮点数四舍五入到小数点后面指定的位数。

Author:
  • 胡海星
Parameters:
Name Type Default Description
number Number

待处理的浮点数。

digits Number 0

小数点后数字的个数;介于0到20(包括)之间,实现环境可能支持更大范围。如果 忽略该参数,则默认为0。

Returns:
Type:
Number

将输入参数四舍五入到小数点后指定位数的结果。如果number是NaN,则返回NaN; 如果number是+/-Infinity,则返回number。

scrollTo(to, duration, callback)

Parameters:
Name Type Description
to number
duration number
callback function

setProperty(obj, path, value) → {boolean}

Sets the value at the specified path of an object.

Author:
  • Haixing Hu
Parameters:
Name Type Description
obj object

The object to query.

path string

The path of the property to get. It should be a string of the property names separated by dots. For example, if the object is {a: {b: {c: 1}}}, then set the property 'a.b.c' to 2 will change the object to {a: {b: {c: 2}}}. It also supports array index. For example, if the object is {a: {b: [1, 2, 3]}}, then set the property 'a.b[1]' to 4 will change the object to {a: {b: [1, 4, 3]}} If the object is {a: {b: [{c: 1}, {c: 2}]}}, then the set the property 'a.b[1].c' to 3 will change the object to {a: {b: [{c: 1}, {c: 3}]}}. If the path is empty, then the function does nothing and returns false.

value

The value to be set, which may be null. If it is undefined, the function will do nothing and return false.

Returns:
Type:
boolean

true if the value is set successfully, false otherwise.

(async) sleep(milliseconds) → {Promise}

异步sleep函数。用下面语句调用可让当前异步线程暂停指定的时间:

await sleep(milliseconds)

注意上述调用只能用于 async 函数。

Parameters:
Name Type Description
milliseconds Number

带休眠的时间,单位为毫秒。

Returns:
Type:
Promise

一个Promise对象,用于异步等待。

splitDigits(value, digitCount) → {Array}

把一个非负整数按10进制拆分为数字数组。

Author:
  • 胡海星
Parameters:
Name Type Default Description
value Number

待转换的数字,必须是一个非负整数。

digitCount Number 0

返回的数组的最少位数,如果为0则不作限制。

Throws:

Error 如果输入不是一个合法的非负整数,则抛出异常。

Returns:
Type:
Array

该非负整数按10进制拆分为的数字数组,其长度如果不足digitCount,前面将补0。

stringToFloat(value) → {Number}

把一个字符串解析为浮点数。

Author:
  • 胡海星
Parameters:
Name Type Description
value String | Number

待转换的字符串,也可以本身就是一个数字。

Returns:
Type:
Number

如果输入参数本身就是数字,则直接返回该数字;如果输入参数是字符串,则返回 从该字符串解析出的浮点数;如果输入参数是其他类型,则返回NaN。

stringToMoney(value, digits) → {Number}

把一个字符串解析为表示货币金额的浮点数。

注意货币金额将被四舍五入并只保留指定的小数位(默认为2位小数)。

Author:
  • 胡海星
Parameters:
Name Type Default Description
value String | Number

待转换的字符串,也可以本身就是一个数字。

digits Number 2

四舍五入后小数点后保留的数字位数,必须介于[0, 20];默认值为2,既保留两位小数。

Returns:
Type:
Number

输入值所表示的货币金额。

toString(value, beautify) → {string}

Convert a value or object to a string representation.

For string, number, and Boolean types, the string form of the value is directly given; for other objects, its JSON encoding is given.

Author:
  • Haixing Hu
Parameters:
Name Type Default Description
value any

The value or object to be converted.

beautify boolean false

Optional, indicating whether to beautify the output JSON string. The default value is false.

Returns:
Type:
string

The string representation of this value or object.

trimString(str) → {String}

删除指定的字符串前后空格。

Author:
  • 胡海星
Parameters:
Name Type Description
str String

待转换的字符串,可以是undefined或null,或非字符串对象。

Returns:
Type:
String

对于undefined或null,或非字符串对象,返回一个空字符串;对于其他字 符串,将其头尾空格去除后返回。

trimUppercaseString(str) → {String}

将指定的字符串转化为标准形式。

Author:
  • 胡海星
Parameters:
Name Type Description
str String

待转换的字符串,可以是undefined或null,或非字符串对象。

Returns:
Type:
String

对于undefined或null,或非字符串对象,返回一个空字符串;对于其他字 符串,将其头尾空格去除后,转换为大写返回。

uppercaseString(str) → {String}

将指定的字符串转化为大写。

Author:
  • 胡海星
Parameters:
Name Type Description
str String

待转换的字符串,可以是undefined或null,或非字符串对象。

Returns:
Type:
String

对于undefined或null,或非字符串对象,返回一个空字符串;对 于其他字符串,将其转换为大写返回。

uriDecode(str) → {String}

对URL编码字符串进行解码。

Author:
  • 胡海星
Parameters:
Name Type Description
str String

待解码的URL编码字符串。

Returns:
Type:
String

解码结果。

uriEncode(str) → {String}

对字符串进行URL编码,遵循 RFC 3986 的严格标准。

Author:
  • 胡海星
See:
Parameters:
Name Type Description
str String

待编码的字符串。

Returns:
Type:
String

该字符串的URL编码结果,