| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- function zero2(word) {
- if (word.length === 1) {
- return '0' + word;
- }
- else {
- return word;
- }
- }
- export function toArray(msg, enc) {
- if (Array.isArray(msg)) {
- return msg.slice();
- }
- if (!msg) {
- return [];
- }
- const res: number[] = [];
- if (typeof msg !== 'string') {
- for (let i = 0; i < msg.length; i++) {
- res[i] = msg[i] | 0;
- }
- return res;
- }
- if (enc === 'hex') {
- msg = msg.replace(/[^a-z0-9]+/ig, '');
- if (msg.length % 2 !== 0) {
- msg = '0' + msg;
- }
- for (let i = 0; i < msg.length; i += 2) {
- res.push(parseInt(msg[i] + msg[i + 1], 16));
- }
- } else {
- for (let i = 0; i < msg.length; i++) {
- const c = msg.charCodeAt(i);
- const hi = c >> 8;
- const lo = c & 0xff;
- if (hi) {
- res.push(hi, lo);
- }
- else {
- res.push(lo);
- }
- }
- }
- return res;
- }
-
- export function encodeHex(msg: Buffer | Uint8Array) {
- let res = '';
- for (let i = 0; i < msg.length; i++) {
- res += zero2(msg[i].toString(16));
- }
- return res;
- }
- export function uuid(): string {
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (char) => {
- // tslint:disable-next-line: no-bitwise
- const random = Math.random() * 16 | 0;
- const value = char === "x" ? random : (random % 4 + 8);
- return value.toString(16);
- });
- }
- export function isObject(item) {
- return (item && typeof item === 'object' && !Array.isArray(item));
- }
- export function mergeDeep(target: object, source: object) {
- const output = Object.assign({}, target);
- if (isObject(target) && isObject(source)) {
- Object.keys(source).forEach(key => {
- if (isObject(source[key])) {
- if (!(key in target)) {
- Object.assign(output, { [key]: source[key] });
- }
- else {
- output[key] = mergeDeep(target[key], source[key]);
- }
- } else {
- Object.assign(output, { [key]: source[key] });
- }
- });
- }
- return output;
- }
|