Comparing version 3.5.22 to 3.5.23
@@ -5,2 +5,7 @@ # Changelog | ||
## [3.5.23][] - 2022-08-12 | ||
- Add `flatObject(sourceObject: object, fieldNames: Array<string>): object` | ||
- Add `unflatObject(sourceObject: object, fieldNames: Array<string>): object;` | ||
## [3.5.22][] - 2022-07-29 | ||
@@ -11,2 +16,4 @@ | ||
- Add year support to `Every` format | ||
- Add function `flatObject(sourceObject: object, fieldNames: Array<string>): object` | ||
- Add function `isHashObject(o: string | number | boolean | object): boolean` | ||
@@ -159,3 +166,4 @@ ## [3.5.21][] - 2022-06-27 | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v3.5.22....HEAD | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v3.5.23....HEAD | ||
[3.5.23]: https://github.com/metarhia/metautil/compare/v3.5.22...v3.5.23 | ||
[3.5.22]: https://github.com/metarhia/metautil/compare/v3.5.21...v3.5.22 | ||
@@ -162,0 +170,0 @@ [3.5.21]: https://github.com/metarhia/metautil/compare/v3.5.20...v3.5.21 |
@@ -90,2 +90,5 @@ 'use strict'; | ||
const isHashObject = (o) => | ||
typeof o === 'object' && o !== null && !Array.isArray(o); | ||
const toLowerCamel = (s) => s.charAt(0).toLowerCase() + s.slice(1); | ||
@@ -360,2 +363,50 @@ | ||
const flatObject = (sourceObject, fieldNames = []) => { | ||
const target = {}; | ||
for (const [key, value] of Object.entries(sourceObject)) { | ||
if (!isHashObject(value)) { | ||
target[key] = value; | ||
continue; | ||
} | ||
if (fieldNames.length > 0 && !fieldNames.includes(key)) { | ||
target[key] = { ...value }; | ||
continue; | ||
} | ||
for (const [childKey, childValue] of Object.entries(value)) { | ||
const combinedKey = `${key}${toUpperCamel(childKey)}`; | ||
if (sourceObject[combinedKey] !== undefined) { | ||
const error = `Can not combine keys: key "${combinedKey}" already exists`; | ||
throw new Error(error); | ||
} | ||
target[combinedKey] = childValue; | ||
} | ||
} | ||
return target; | ||
}; | ||
const unflatObject = (sourceObject, fieldNames) => { | ||
const result = {}; | ||
for (const [key, value] of Object.entries(sourceObject)) { | ||
const prefix = fieldNames.find((name) => key.startsWith(name)); | ||
if (prefix) { | ||
if (Object.prototype.hasOwnProperty.call(sourceObject, prefix)) { | ||
throw new Error(`Can not combine keys: key "${prefix}" already exists`); | ||
} | ||
const newKey = key.substring(prefix.length).toLowerCase(); | ||
const section = result[prefix]; | ||
if (section) section[newKey] = value; | ||
else result[prefix] = { [newKey]: value }; | ||
continue; | ||
} | ||
result[key] = value; | ||
} | ||
return result; | ||
}; | ||
module.exports = { | ||
@@ -375,2 +426,3 @@ random, | ||
isFirstLetter, | ||
isHashObject, | ||
toLowerCamel, | ||
@@ -397,2 +449,4 @@ toUpperCamel, | ||
jsonParse, | ||
flatObject, | ||
unflatObject, | ||
}; |
@@ -43,2 +43,3 @@ import { EventEmitter } from 'events'; | ||
export function isConstant(s: string): boolean; | ||
export function isHashObject(o: string | number | boolean | object): boolean; | ||
export function nowDate(date?: Date): string; | ||
@@ -122,1 +123,9 @@ export function nowDateTimeUTC(date?: Date, timeSep?: string): string; | ||
export function receiveBody(req: IncomingMessage): Promise<Buffer | null>; | ||
export function flatObject( | ||
sourceObject: object, | ||
fieldNames: Array<string>, | ||
): object; | ||
export function unflatObject( | ||
sourceObject: object, | ||
fieldNames: Array<string>, | ||
): object; |
{ | ||
"name": "metautil", | ||
"version": "3.5.22", | ||
"version": "3.5.23", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -41,4 +41,4 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@types/node": "^18.0.0", | ||
"eslint": "^8.18.0", | ||
"@types/node": "^18.7.2", | ||
"eslint": "^8.21.0", | ||
"eslint-config-metarhia": "^8.1.0", | ||
@@ -45,0 +45,0 @@ "eslint-config-prettier": "^8.5.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36313
820