1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- function inArray(arr, key, val) {
- if (!arr || !arr.length || typeof arr != 'object' || !Array.isArray(arr)) {
- return -1
- }
- for (let i = 0; i < arr.length; i++) {
- if (!key) {
- if (arr[i] == val) {
- return i
- }
- } else if (arr[i][key] === val) {
- return i
- }
- }
- return -1;
- }
-
- // ArrayBuffer转16进度字符串示例
- function ab2hex(buffer, split) {
- var hexArr = Array.prototype.map.call(
- new Uint8Array(buffer),
- function (bit) {
- return ('00' + bit.toString(16)).slice(-2)
- }
- )
- return hexArr.join(split);
- }
-
- const {TextDecoder, TextEncoder} = require('./encoding')
-
- function hex2str(arr) {
- let decoder = new TextDecoder('utf8')
- let uint8 = new Uint8Array(arr)
- let res = decoder.decode(uint8)
- return res
- }
-
- function str2hex(str) {
- let encoder = new TextEncoder('utf8')
- return encoder.encode(str)
- }
-
- module.exports = {
- inArray,
- ab2hex,
- hex2str,
- str2hex,
- }
|