更新时间:2024-04-10 GMT+08:00
分享

使用JS类库

内置类库列表

断言
  • chai (v4.2.0):用于断言BDD/TDD断言。

加密解密库

  • jsrsasign(10.3.0):用于RSA加密/解密。
Encode、Decode 库
  • crypto-js(v3.1.9-1):编码/解码库,基本包含常用的编码、解码方式,如Base64、MD5、SHA、HMAC、AES等等。

    require方法只允许使用整个模块,不能单独使用类库里的某个子模块,具体看本文档末尾说明。

  • atob(v2.1.2):用于Base64解码。
  • btoa(v1.2.1):用于Base64编码。
  • tv4(v1.3.0):用于校验JSONSchema。
  • xml2js(v0.4.19):用于XML转JSON。
JSONSchema校验库
  • ajv(v6.6.2):校验JSONSchema。
实用工具
  • postman-collection( v3.4.0):Postman Collection库。
  • cheerio(v0.22.0):jQuery的子集。
  • lodash (v4.17.11):JS实用工具库。
  • uuid :生成UUID。
  • moment(v2.22.2):日期处理库 (不含 locales)。
  • mockjs:模拟数据生成,拦截Ajax请求。
  • csv-parse/lib/sync( v1.2.4):处理CSV格式数据。
  • iconv-lite:用于字符编码转换,支持数十种字符编码格式的转换。
内置NodeJS模块
  • path:处理文件路径。
  • assert:提供一组断言测试。
  • buffer:处理二进制数据。
  • util:实用函数的集合。
  • url:解析URL字符串。
  • querystring:处理URL,查询字符串。
  • punycode字符编码方案
  • string-decoder:将Buffer对象解码为字符串。
  • stream:处理流数据。
  • events:处理事件。
  • timers:在给定的毫秒数后调用函数。
通过require方法可以直接使用CodeArts API内置的JS类库。
var cryptoJs = require("crypto-js");
console.log(cryptoJs.SHA256("Message"));

使用方式

/**
 * 示例一:该示例引入加密算法模块 crypto-js,并使用其中 AES 对 message 进行加解密 
 * @param message 需要加密的文本
 * @param key 秘钥
 */
function cryptoJSTest(message, key) {
    const CryptoJS = require('crypto-js');
    // 加密
    const ciphertext = CryptoJS.AES.encrypt(message, key).toString();
    // 解密
    const bytes  = CryptoJS.AES.decrypt(ciphertext, key);
    const originalText = bytes.toString(CryptoJS.enc.Utf8);
    console.log(originalText);
}
cryptoJSTest('test message', 'secret key');

/**
 * 示例二:该示例引入 uuid 模块生成一段 uuid,并引入 btoa 模块进行 Base64 编码
 */
function uuidBtoaTest() {
    const uuid = require('uuid');
    const btoa = require('btoa');
    const id = uuid.v4();
    const base64EncodedId = btoa(id);
    console.log(id);
    console.log(base64EncodedId);
}
uuidBtoaTest();

/**
 * 示例三:该示例引入 lodash 模块,并测试了其中的 uniq 对数组去重
 */
function lodashUniqTest() {
    const lodash = require('lodash');
    const arr =[1, 2, 1, 5, 1, 9]
    const uniqArr = lodash.uniq(arr);
    console.log(uniqArr.toString())
}
lodashUniqTest();
分享:

    相关文档

    相关产品