UNPKG

@techmely/utils

Version:

Collection of helpful JavaScript / TypeScript utils

34 lines (27 loc) 785 B
'use strict'; /*! * @techmely/utils * Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com> * MIT Licensed */ // src/cacheStringFunc.ts function cacheStringFunction(fn) { const cache = /* @__PURE__ */ Object.create(null); return (str) => { const hit = cache[str]; return hit || (cache[str] = fn(str)); }; } // src/camel2Snake.ts var camel2snake = cacheStringFunction((str) => { return str.replace(/[A-Z0-9]/g, (char) => `_${char.toLocaleLowerCase()}`); }); // src/camel2SnakeObject.ts function camel2SnakeObject(obj) { return Object.entries(obj).reduce( // biome-ignore lint/performance/noAccumulatingSpread: Ignore here (acc, cur) => ({ ...acc, [camel2snake(cur[0])]: cur[1] }), {} ); } exports.camel2SnakeObject = camel2SnakeObject;