2019-05-14 18:48:48 -04:00
|
|
|
"use strict";
|
2019-08-25 02:39:20 -04:00
|
|
|
import { arrayify } from "@ethersproject/bytes";
|
|
|
|
import { toUtf8Bytes, UnicodeNormalizationForm } from '@ethersproject/strings';
|
|
|
|
export function looseArrayify(hexString) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (hexString) === 'string' && hexString.substring(0, 2) !== '0x') {
|
|
|
|
hexString = '0x' + hexString;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
return arrayify(hexString);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function zpad(value, length) {
|
2019-05-14 18:48:48 -04:00
|
|
|
value = String(value);
|
|
|
|
while (value.length < length) {
|
|
|
|
value = '0' + value;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function getPassword(password) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (password) === 'string') {
|
2019-08-25 02:39:20 -04:00
|
|
|
return toUtf8Bytes(password, UnicodeNormalizationForm.NFKC);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
return arrayify(password);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function searchPath(object, path) {
|
|
|
|
let currentChild = object;
|
2019-11-20 18:57:38 +09:00
|
|
|
const comps = path.toLowerCase().split('/');
|
2019-08-25 02:39:20 -04:00
|
|
|
for (let i = 0; i < comps.length; i++) {
|
2019-05-14 18:48:48 -04:00
|
|
|
// Search for a child object with a case-insensitive matching key
|
2019-08-25 02:39:20 -04:00
|
|
|
let matchingChild = null;
|
2019-11-20 18:57:38 +09:00
|
|
|
for (const key in currentChild) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (key.toLowerCase() === comps[i]) {
|
|
|
|
matchingChild = currentChild[key];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Didn't find one. :'(
|
|
|
|
if (matchingChild === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Now check this child...
|
|
|
|
currentChild = matchingChild;
|
|
|
|
}
|
|
|
|
return currentChild;
|
|
|
|
}
|