grpc-web-from-object
Advanced tools
Comparing version 2.1.2 to 2.2.0
# Changelog | ||
## [2.2.0](https://github.com/infodusha/grpc-web-from-object/compare/v2.1.2...v2.2.0) (2023-03-04) | ||
### Features | ||
* refactor ([63189b6](https://github.com/infodusha/grpc-web-from-object/commit/63189b6de05f7392ca982df75927102dbef6ccf8)) | ||
* refactor checks ([5d9a20d](https://github.com/infodusha/grpc-web-from-object/commit/5d9a20d95fd7698884c90e7106b458eda45796c4)) | ||
## [2.1.2](https://github.com/infodusha/grpc-web-from-object/compare/v2.1.1...v2.1.2) (2023-03-03) | ||
@@ -4,0 +12,0 @@ |
103
lib/index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createFromObject = void 0; | ||
const SETTER_PREFIX = 'set'; | ||
const CLEAR_PREFIX = 'clear'; | ||
function createFromObject(MessageType, factories) { | ||
@@ -9,22 +11,6 @@ const allFactories = factories ?? {}; | ||
validateMissingProps(instance, data); | ||
for (const [key, value] of Object.entries(data)) { | ||
if (value === null || typeof value !== 'object') { | ||
setValue(instance, key, value); | ||
continue; | ||
} | ||
if (key in allFactories) { | ||
const factoryKey = key; | ||
if (Array.isArray(value)) { | ||
const childrenInstance = value.map((item) => allFactories[factoryKey](item)); | ||
setValue(instance, key, childrenInstance); | ||
} | ||
else { | ||
const childInstance = allFactories[factoryKey](value); | ||
setValue(instance, key, childInstance); | ||
} | ||
} | ||
else { | ||
validateMissingFactory(instance, key, value); | ||
setValue(instance, key, value); | ||
} | ||
for (const [prop, value] of Object.entries(filterExtraProps(instance, data))) { | ||
const result = getResult(allFactories, prop, value); | ||
const setter = getMethod(prop); | ||
callMethod(instance, setter, result); | ||
} | ||
@@ -35,20 +21,40 @@ return instance; | ||
exports.createFromObject = createFromObject; | ||
function setValue(instance, key, value) { | ||
const setter = getSetter(key); | ||
if (setter in instance) { | ||
if (value === null) { | ||
throw new Error(`Null value for key '${key}'`); | ||
function getResult(factories, prop, value) { | ||
if (Array.isArray(value)) { | ||
if (value.length === 0 || !isArrayOfObjects(value, prop)) { | ||
return value; | ||
} | ||
instance[setter](value); | ||
validateMissingFactory(factories, prop); | ||
return value.map((child) => callMethod(factories, prop, child)); | ||
} | ||
if (isObject(value, prop)) { | ||
validateMissingFactory(factories, prop); | ||
return callMethod(factories, prop, value); | ||
} | ||
return value; | ||
} | ||
function getSetter(key) { | ||
return `set${key[0].toUpperCase()}${key.slice(1)}`; | ||
function callMethod(obj, key, value) { | ||
return obj[key](value); | ||
} | ||
function getProp(key, prefix = SETTER_PREFIX) { | ||
const prop = key.slice(prefix.length); | ||
return prop.slice(0, 1).toLowerCase() + prop.slice(1); | ||
} | ||
function getMethod(prop, prefix = SETTER_PREFIX) { | ||
return `${prefix}${prop[0].toUpperCase()}${prop.slice(1)}`; | ||
} | ||
function getInstanceProps(instance) { | ||
return Object.keys(Object.getPrototypeOf(instance)) | ||
.filter((key) => key.startsWith(SETTER_PREFIX)) | ||
.map(key => getProp(key)); | ||
} | ||
function isOptional(instance, prop) { | ||
const clearMethod = getMethod(prop, CLEAR_PREFIX); | ||
return clearMethod in instance; | ||
} | ||
function validateMissingProps(instance, data) { | ||
const dataSetters = Object.keys(data).map((key) => getSetter(key)); | ||
const instanceSetters = Object.keys(Object.getPrototypeOf(instance)).filter((key) => key.startsWith('set')); | ||
for (const key of instanceSetters) { | ||
if (!dataSetters.includes(key) && instance['clear' + key.slice(3)] === undefined) { | ||
const prop = key.slice(3, 4).toLowerCase() + key.slice(4); | ||
const instanceProps = getInstanceProps(instance); | ||
const dataProps = Object.keys(data); | ||
for (const prop of instanceProps) { | ||
if (!dataProps.includes(prop) && !isOptional(instance, prop)) { | ||
throw new Error(`Missing property '${prop}'`); | ||
@@ -58,16 +64,25 @@ } | ||
} | ||
function validateMissingFactory(instance, key, value) { | ||
const setter = getSetter(key); | ||
if (!(setter in instance)) { | ||
return; | ||
function filterExtraProps(instance, data) { | ||
const instanceProps = getInstanceProps(instance); | ||
return Object.fromEntries(Object.entries(data).filter(([key]) => instanceProps.includes(key))); | ||
} | ||
function validateMissingFactory(factories, prop) { | ||
if (!(prop in factories)) { | ||
throw new Error(`Missing factory for '${prop}'`); | ||
} | ||
if (Array.isArray(value)) { | ||
for (const v of value) { | ||
validateMissingFactory(instance, key, v); | ||
} | ||
return; | ||
} | ||
function isObject(value, prop) { | ||
if (value === null) { | ||
throw new Error(`Null value for key '${prop}'`); | ||
} | ||
if (typeof value === 'object') { | ||
throw new Error(`Missing factory for '${key}'`); | ||
return typeof value === 'object'; | ||
} | ||
function isArrayOfObjects(arr, prop) { | ||
if (arr.every((item) => isObject(item, prop))) { | ||
return true; | ||
} | ||
if (arr.every((item) => !isObject(item, prop))) { | ||
return false; | ||
} | ||
throw new Error(`Mixed array for '${prop}'`); | ||
} |
{ | ||
"name": "grpc-web-from-object", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"description": "fromObject method for grpc-web", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
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
21591
104