Loading src/connector-client.ts +32 −0 Original line number Diff line number Diff line Loading @@ -155,6 +155,38 @@ export class ConnectorClient extends Client { return thingWithoutReferences as SCAssociatedThingWithoutReferences<THING>; } /** * Recursively deletes all undefined properties from an object instance * * @param obj Object to delete undefined properties from */ static removeUndefinedProperties(obj: object): void { // return atomic data types and arrays (recursion anchor) if (typeof obj !== 'object' || Array.isArray(obj)) { return; } // check each key for (const key in obj) { /* istanbul ignore if */ if (!obj.hasOwnProperty(key)) { continue; } const indexedObj = obj as { [k: string]: unknown; }; if (typeof indexedObj[key] === 'undefined') { // delete undefined keyss delete indexedObj[key]; } else { // check recursive ConnectorClient.removeUndefinedProperties(indexedObj[key] as object); } } return; } /** * Request a bulk transfer to the backend * Loading test/connector-client.spec.ts +21 −0 Original line number Diff line number Diff line Loading @@ -361,6 +361,27 @@ export class ConnectorClientSpec { } } @test async removeUndefinedProperties() { const objectWithUndefinedProperties = {value: 'foo', novalue: undefined, nested: { value: 'foo', novalue: undefined}, }; const objectWithoutUndefinedProperties = {value: 'foo', nested: { value: 'foo'}, }; ConnectorClient.removeUndefinedProperties(objectWithUndefinedProperties); expect(objectWithUndefinedProperties).to.deep.equal(objectWithoutUndefinedProperties, JSON.stringify( [objectWithUndefinedProperties, objectWithoutUndefinedProperties], null, 2, )); } @test async update() { const message: SCMessage = { Loading Loading
src/connector-client.ts +32 −0 Original line number Diff line number Diff line Loading @@ -155,6 +155,38 @@ export class ConnectorClient extends Client { return thingWithoutReferences as SCAssociatedThingWithoutReferences<THING>; } /** * Recursively deletes all undefined properties from an object instance * * @param obj Object to delete undefined properties from */ static removeUndefinedProperties(obj: object): void { // return atomic data types and arrays (recursion anchor) if (typeof obj !== 'object' || Array.isArray(obj)) { return; } // check each key for (const key in obj) { /* istanbul ignore if */ if (!obj.hasOwnProperty(key)) { continue; } const indexedObj = obj as { [k: string]: unknown; }; if (typeof indexedObj[key] === 'undefined') { // delete undefined keyss delete indexedObj[key]; } else { // check recursive ConnectorClient.removeUndefinedProperties(indexedObj[key] as object); } } return; } /** * Request a bulk transfer to the backend * Loading
test/connector-client.spec.ts +21 −0 Original line number Diff line number Diff line Loading @@ -361,6 +361,27 @@ export class ConnectorClientSpec { } } @test async removeUndefinedProperties() { const objectWithUndefinedProperties = {value: 'foo', novalue: undefined, nested: { value: 'foo', novalue: undefined}, }; const objectWithoutUndefinedProperties = {value: 'foo', nested: { value: 'foo'}, }; ConnectorClient.removeUndefinedProperties(objectWithUndefinedProperties); expect(objectWithUndefinedProperties).to.deep.equal(objectWithoutUndefinedProperties, JSON.stringify( [objectWithUndefinedProperties, objectWithoutUndefinedProperties], null, 2, )); } @test async update() { const message: SCMessage = { Loading