mysterium-vpn-js
Version:
Javascript SDK for Mysterium node
64 lines (63 loc) • 2.26 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.camelcaseKeys = void 0;
/**
* Copyright (c) 2021 BlockDev AG
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const map_obj_1 = __importDefault(require("map-obj"));
const camelcase_1 = __importDefault(require("camelcase"));
const quick_lru_1 = __importDefault(require("@alloc/quick-lru"));
const cache = new quick_lru_1.default({ maxSize: 100000 });
// Reproduces behavior from `map-obj`
const isObject = (value) => typeof value === 'object' &&
value !== null &&
!(value instanceof RegExp) &&
!(value instanceof Error) &&
!(value instanceof Date);
const camelCaseConvert = (input) => {
if (!isObject(input)) {
return input;
}
const options = {
deep: true,
pascalCase: false,
locale: 'en-US',
};
const makeMapper = (parentPath) => (key, value) => {
if (options.deep && isObject(value)) {
const path = parentPath === undefined ? key : `${parentPath}.${key}`;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
value = (0, map_obj_1.default)(value, makeMapper(path));
}
const cacheKey = options.pascalCase ? `${key}_` : key;
if (cache.has(cacheKey)) {
key = cache.get(cacheKey);
}
else {
const returnValue = (0, camelcase_1.default)(key, options);
if (key.length < 100) {
// Prevent abuse
cache.set(cacheKey, returnValue);
}
key = returnValue;
}
return [key, value];
};
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return (0, map_obj_1.default)(input, makeMapper(undefined));
};
const camelcaseKeys = (input) => {
if (Array.isArray(input)) {
return Object.keys(input).map((key) => camelCaseConvert(input[key]));
}
return camelCaseConvert(input);
};
exports.camelcaseKeys = camelcaseKeys;