From ca04938f92e0cb46c4b4a225aaedc922cd241be2 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Wed, 29 Mar 2023 18:48:23 +0300 Subject: [PATCH] 1.0.4 - Added "camelizeKeys" function --- README.md | 17 +++++++++++++---- package-lock.json | 2 +- package.json | 2 +- src/data/camelizer.js | 31 +++++++++++++++++++++++++++++++ src/index.js | 1 + 5 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 src/data/camelizer.js diff --git a/README.md b/README.md index 5b0a5c8..6746d88 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # js-utils -## Install +## Introduction + +js-utils is a collection of utilities that facilitate software development in a javascript environment. + +## Package installation ```bash // with npm @@ -13,8 +17,13 @@ yarn add @flare/js-utils --registry https://lab.code-rove.com/public-node-regist ## Usage ```jsx -import { typeValidator, localStorage } from "@flare/js-utils"; +import { typeValidator, localStorage, camelizeKeys } from "@flare/js-utils"; ``` -**1.0.0** -This version includes data type validators and local storage utils +## Changelog + +1.0.0 - This version includes data type validators and local storage utils +1.0.1 - Set new domain in configs +1.0.2 - Utilities function export fix +1.0.3 - Typescript warnings fix +1.0.4 - Added "camelizeKeys" function diff --git a/package-lock.json b/package-lock.json index c53ef12..9bd31ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@flare/js-utils", - "version": "1.0.3", + "version": "1.0.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index f31d40b..176df23 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@flare/js-utils", - "version": "1.0.3", + "version": "1.0.4", "description": "Javascript utils", "author": { "name": "Tudor Stanciu", diff --git a/src/data/camelizer.js b/src/data/camelizer.js new file mode 100644 index 0000000..5691b1f --- /dev/null +++ b/src/data/camelizer.js @@ -0,0 +1,31 @@ +function camelizeKeys(o) { + var newO, origKey, newKey, value; + if (o instanceof Array) { + return o.map(function (value) { + if (typeof value === "object") { + value = camelizeKeys(value); + } + return value; + }); + } else { + newO = {}; + for (origKey in o) { + if (Object.prototype.hasOwnProperty.call(o, origKey)) { + newKey = ( + origKey.charAt(0).toLowerCase() + origKey.slice(1) || origKey + ).toString(); + value = o[origKey]; + if ( + value instanceof Array || + (value !== null && value.constructor === Object) + ) { + value = camelizeKeys(value); + } + newO[newKey] = value; + } + } + } + return newO; +} + +export { camelizeKeys }; diff --git a/src/index.js b/src/index.js index 25297f6..01ab72c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ import * as typeValidator from "./data/typeValidator"; import * as localStorage from "./storage/localStorage"; +export * from "./data/camelizer"; export { typeValidator, localStorage };