Enhance : JavaScript extension

更新时间:
2024-05-14
下载文档

Enhance : JavaScript extension

JSRE does some enhancements to standard JavaScript rules, such as object destructor, object cloning, array extensions, etc.

Number Object

In JavaScript and JSRE, most API time units are milliseconds, so there are time conversions in seconds, minutes, and hours. JSRE provides three methods for rapid conversion of Number objects.

number.second()

Convert seconds to milliseconds.

Example

var ms = (1).second(); // ms: 1000

setTimeout(function() {
  // Delay one second execution
}, (1).second());

number.minute()

Convert minutes to milliseconds.

Example

var ms = (1).minute(); // ms: 60 * 1000

setTimeout(function() {
  // Delay one minute execution
}, (1).minute());

number.hour()

Convert hours to milliseconds.

Example

var ms = (1).hour(); // ms: 60 * 60 * 1000

setTimeout(function() {
  // Delay one hour execution
}, (1).hour());

Byte Length

String length property can get the number of characters in the string, but cannot get string bytes. JSRE provides a byteLength property for the string object, which can directly get the string bytes length.

string.byteLength

Example

var str1 = String.fromCharCode(65);
var str2 = String.fromCharCode(128);

console.log(str1.length); // 1
console.log(str2.length); // 1

console.log(str1.byteLength); // 1
console.log(str2.byteLength); // 2

Object.byteLength(obj)

  • obj {Object} Source object.
  • Returns: {Integer} Number of bytes.

Get number of bytes of memory after the object is converted to a JSON string.

JSON.byteLength(obj)

  • obj {Object} Source object.
  • Returns: {Integer} Number of bytes.

Same as Object.byteLength().

String Class

JSRE adds some methods of the String class:

String.isAscii(str)

  • str {String} String to be judged.
  • Returns: {Boolean} Whether it is an ASCII string.

Determines whether a specified string is an ASCII string.

Example

String.isAscii('abcdefg'); // true
String.isAscii(''); // true

String.isAsciiPrintable(str)

  • str {String} String to be judged.
  • Returns: {Boolean} Whether it is an ASCII string.

Determines whether a specified string is a printable ASCII string.

Example

String.isAsciiPrintable(' '); // false
String.isAsciiPrintable('a'); // true

String.isAlpha(str)

  • str {String} String to be judged.
  • Returns: {Boolean} Whether it is an alphabet string.

Determines whether a specified string is an alphabet string.

Example

String.isAlpha('abc'); // true
String.isAlpha('1a1'); // false

String.isAlphaNumber(str)

  • str {String} String to be judged.
  • Returns: {Boolean} Whether it is an alphabet and number string.

Determines whether a specified string is an alphabet and number string.

Example

String.isAlphaNumber('abc'); // true
String.isAlphaNumber('1a1'); // true

String.isNumber(str)

  • str {String} String to be judged.
  • Returns: {Boolean} Whether it is a numeric string.

Determines whether a specified string is a numeric string.

Example

String.isNumber('123'); // true
String.isNumber('1.23'); // true
String.isNumber('.123'); // false
String.isNumber('0x001'); // true

String.isInteger(str)

  • str {String} String to be judged.
  • Returns: {Boolean} Whether it is a integer string.

Determines whether a specified string is a integer string.

Example

String.isInteger('123'); // true
String.isInteger('1.23'); // false
String.isInteger('0x01'); // true

String Object

The following functions have been added to the String object prototype by JSRE.

string.equals(str)

  • str {String} String to be compaired.
  • Returns: {Boolean} Whether two strings are the same.

Determine whether two strings are the same. EdgerOS 1.5.2 and later versions are supported.

Example

var s = 'aaa';
s.equals('aaa'); // true
s.equals('AAA'); // false

string.equalsIgnoreCase(str)

  • str {String} String to be compaired.
  • Returns: {Boolean} Whether two strings are the same (ignoring case).

Determine whether two strings are the same (ignoring case). EdgerOS 1.5.2 and later versions are supported.

Example

var s = 'aaa';
s.equalsIgnoreCase('aaa'); // true
s.equalsIgnoreCase('AAA'); // true

File Descriptor Array

When using I/O multiplexing such as iosched.select(), you need a file descriptor array operation. JSRE provides two common static methods for Array objects: addFd() and deleteFd() to facilitate the addition and deletion of file descriptor arrays.

Example

var iosched = require('iosched'); // Must import this module.

var fd1 = open(...);
var fd2 = open(...);
var fds = [[], [], []];

Array.addFd(fds[0], fd1);
Array.addFd(fds[0], fd2);

while (true) {
  var res = iosched.select(fds);
  if (res) {
    if (res[0].includes(fd1)) {
      // Read fd1 data.
    }
    if (res[0].includes(fd2)) {
      // Read fd2 data.
    }
  }
}

Array.addFd(array, fd)

  • array {Array} File descriptor array.
  • fd {Integer} File descriptor.

Add a specified file descriptor to the array.

Array.deleteFd(array, fd)

  • array {Array} File descriptor array.
  • fd {Integer} File descriptor.

Remove a file descriptors from a specified array.

Object Clone

JavaScript is an object-oriented language. The assignment of objects is a reference. JSRE provides an easy way to copy objects.

Example

var obj1 = { a: 3, b: 5 };
var obj2 = obj1;
var obj3 = Object.clone(obj1);

obj1.a = 4;
obj1.b = 6;

console.log(obj1);
console.log(obj2);
console.log(obj3);

Object.clone(obj)

  • obj {Object} Source object.
  • Returns: {Object} A cloned object.

Recursively clones all the properties of an obj and references all methods. Return a new object.

Object.decycle

JSRE provides circular reference breaking operation for objects.

Object.decycle(obj)

  • obj {Object} Object.

Example

var obj1 = { a: 1, b: 2 };
var obj2 = { a: 1, b: 2, c: obj1 };
var obj3 = { a: 1, b: 2, c: obj2 };
obj1.c = obj3;

Object.decycle(obj1);
Object.decycle(obj2);
Object.decycle(obj3);

console.log(obj1);
console.log(obj2);
console.log(obj3);

JSON Object

JSRE has added JSON.parseBuffer, which can quickly parse the JSON string in Buffer, which is faster than using JSON.parse directly.

JSON.parseBuffer(buffer)

  • buffer {Buffer} Buffer object containing the JSON string.
  • Returns: {Any} JSON parse result.
文档内容是否对您有所帮助?
有帮助
没帮助