Skip to content

Commit af0a96f

Browse files
authored
Fix deletePropertyFromObject bug; If null was inside an array would cause the code to break. (#23)
* Fix deletePropertyFromObject bug; If null was inside an array would cause the code to break. * update packages
1 parent 101a065 commit af0a96f

File tree

5 files changed

+2667
-3044
lines changed

5 files changed

+2667
-3044
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ export default class CarModel extends BaseModel {
320320

321321
## Release History
322322

323+
- 2020-07-29 v1.9.1 Fix deletePropertyFromObject bug; If null was inside an array would cause the code to break.
324+
323325
- 2019-06-23 v1.9.0 IConvertOption now happens before data is assigned to properties. Now if you use ConversionTypeEnum.JSON and have a default model it will create that new model instead of having a plain object.
324326

325327
- 2019-01-28 v1.8.2 ConversionTypeEnum.String now coverts objects with JSON.stringify.

__tests__/Util.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,18 @@ describe('Util', () => {
2424
});
2525

2626
test('Util.deletePropertyFromObject()', () => {
27-
const obj: any = {name: 'Robert', gender: 'male', phone: '555-555-5555'};
28-
const expected: any = {name: 'Robert'};
27+
const obj: any = {
28+
name: 'Robert',
29+
gender: 'male',
30+
phone: '555-555-5555',
31+
nested: {name: 'Robert', gender: 'male', phone: '555-555-5555'},
32+
arrayWithNullAlso: [{name: 'Robert', gender: 'male', phone: '555-555-5555'}, null, 0, 'one', true],
33+
};
34+
const expected: any = {
35+
name: 'Robert',
36+
nested: {name: 'Robert'},
37+
arrayWithNullAlso: [{name: 'Robert'}, null, 0, 'one', true],
38+
};
2939
const actual: any = Util.deletePropertyFromObject(obj, ['phone', 'gender']);
3040

3141
expect(expected).toEqual(actual);

package.json

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"prewatch": "npm run clean && npm run build:types",
2626
"watch": "rollup -cw",
2727
"---------- BUILD -------------------------------------------": "",
28-
"prebuild": "npm run type-check && npm run clean && npm run build:types",
28+
"prebuild": "npm run type-check && npm run test && npm run clean && npm run build:types",
2929
"build": "rollup -c",
3030
"---------- TESTING -----------------------------------------": "",
3131
"test": "npm run lint && npm run unit",
@@ -47,29 +47,29 @@
4747
"dependencies": {},
4848
"license": "MIT",
4949
"devDependencies": {
50-
"@babel/core": "7.4.5",
51-
"@babel/plugin-proposal-class-properties": "7.4.4",
52-
"@babel/plugin-proposal-object-rest-spread": "7.4.4",
53-
"@babel/plugin-transform-runtime": "7.4.4",
54-
"@babel/preset-env": "7.4.5",
55-
"@babel/preset-typescript": "7.3.3",
56-
"@babel/runtime": "7.4.5",
57-
"@types/jest": "24.0.15",
58-
"husky": "2.4.1",
59-
"jest": "24.8.0",
60-
"prettier": "1.18.2",
61-
"pretty-quick": "1.11.1",
62-
"rimraf": "2.6.3",
50+
"@babel/core": "7.10.5",
51+
"@babel/plugin-proposal-class-properties": "7.10.4",
52+
"@babel/plugin-proposal-object-rest-spread": "7.10.4",
53+
"@babel/plugin-transform-runtime": "7.10.5",
54+
"@babel/preset-env": "7.10.4",
55+
"@babel/preset-typescript": "7.10.4",
56+
"@babel/runtime": "7.10.5",
57+
"@types/jest": "26.0.7",
58+
"husky": "4.2.5",
59+
"jest": "26.1.0",
60+
"prettier": "2.0.5",
61+
"pretty-quick": "2.0.1",
62+
"rimraf": "3.0.2",
6363
"rollup": "1.16.1",
64-
"rollup-plugin-babel": "4.3.2",
65-
"rollup-plugin-commonjs": "10.0.0",
66-
"rollup-plugin-node-resolve": "5.0.3",
67-
"rollup-plugin-uglify": "6.0.2",
68-
"ts-jest": "24.0.2",
69-
"tslib": "1.10.0",
70-
"tslint": "5.18.0",
64+
"rollup-plugin-babel": "4.4.0",
65+
"rollup-plugin-commonjs": "10.1.0",
66+
"rollup-plugin-node-resolve": "5.2.0",
67+
"rollup-plugin-uglify": "6.0.4",
68+
"ts-jest": "26.1.4",
69+
"tslib": "2.0.0",
70+
"tslint": "6.1.2",
7171
"tslint-config-prettier": "1.18.0",
72-
"typescript": "3.5.2"
72+
"typescript": "3.9.7"
7373
},
7474
"author": {
7575
"name": "Robert S. (codeBelt)",

src/Util.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ export class Util {
6161
* // { name: 'Robert' }
6262
*/
6363
public static deletePropertyFromObject(object: any, value: string | string[]): any {
64+
if (!Util.isObject(object)) {
65+
return object;
66+
}
67+
6468
// If properties is not an array then make it an array object.
6569
const propertyNameList: any = value instanceof Array ? value : [value];
6670

@@ -71,7 +75,7 @@ export class Util {
7175
delete object[key];
7276
} else if (propertyData instanceof Array) {
7377
propertyData.forEach((item: any) => Util.deletePropertyFromObject(item, propertyNameList));
74-
} else if (propertyData instanceof Object) {
78+
} else if (Util.isObject(propertyData)) {
7579
Util.deletePropertyFromObject(propertyData, propertyNameList);
7680
}
7781
});

0 commit comments

Comments
 (0)