UNPKG

@maherunlocker/custom-react-table

Version:
84 lines 2.98 kB
"use strict"; // copied then trimmed from https://raw.githubusercontent.com/auth0/auth0.js/master/src/helper/object.js Object.defineProperty(exports, "__esModule", { value: true }); exports.camelToWords = exports.toCamelCase = exports.toSnakeCase = void 0; /* eslint-disable no-param-reassign */ /* eslint-disable no-restricted-syntax */ /* eslint-disable guard-for-in */ function camelToSnake(str) { let newKey = ''; let index = 0; let code; let wasPrevNumber = true; let wasPrevUppercase = true; while (index < str.length) { code = str.charCodeAt(index); if ((!wasPrevUppercase && code >= 65 && code <= 90) || (!wasPrevNumber && code >= 48 && code <= 57)) { newKey += '_'; newKey += str[index].toLowerCase(); } else { newKey += str[index].toLowerCase(); } wasPrevNumber = code >= 48 && code <= 57; wasPrevUppercase = code >= 65 && code <= 90; index++; } return newKey; } function snakeToCamel(str) { const parts = str.split('_'); return parts.reduce((p, c) => p + c.charAt(0).toUpperCase() + c.slice(1), parts.shift()); } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types function toSnakeCase(object, exceptions = []) { if (typeof object !== 'object' || object === null) { return object; } return Object.keys(object).reduce((p, key) => { const newKey = exceptions.indexOf(key) === -1 ? camelToSnake(key) : key; p[newKey] = toSnakeCase(object[key]); return p; }, {}); } exports.toSnakeCase = toSnakeCase; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types function toCamelCase(object, exceptions = []) { if (typeof object !== 'object' || object === null) { return object; } return Object.keys(object).reduce((p, key) => { const newKey = exceptions.indexOf(key) === -1 ? snakeToCamel(key) : key; p[newKey] = toCamelCase(object[key]); return p; }, {}); } exports.toCamelCase = toCamelCase; function camelToWords(str) { let newKey = ''; let index = 0; let code; let wasPrevNumber = true; let wasPrevUppercase = true; while (index < str.length) { code = str.charCodeAt(index); if (index === 0) { newKey += str[index].toUpperCase(); } else if ((!wasPrevUppercase && code >= 65 && code <= 90) || (!wasPrevNumber && code >= 48 && code <= 57)) { newKey += ' '; newKey += str[index].toUpperCase(); } else { newKey += str[index].toLowerCase(); } wasPrevNumber = code >= 48 && code <= 57; wasPrevUppercase = code >= 65 && code <= 90; index++; } return newKey; } exports.camelToWords = camelToWords; //# sourceMappingURL=object.js.map