Skip to content

Commit

Permalink
Defensive code against undefined values (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
olamothe committed Feb 7, 2017
1 parent 6ce4462 commit 4b3055d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils/HashUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class HashUtils {
encodedValue = HashUtils.encodeArray(valueToEncode);
} else if (_.isObject(valueToEncode) && Utils.isNonEmptyArray(_.keys(valueToEncode))) {
encodedValue = HashUtils.encodeObject(valueToEncode);
} else {
} else if (!Utils.isNullOrUndefined(valueToEncode)) {
encodedValue = encodeURIComponent(valueToEncode.toString());
}
if (encodedValue != '') {
Expand Down
8 changes: 8 additions & 0 deletions test/utils/HashUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ export function HashUtilsTest() {
expect(encodedValue).toEqual(expectedEncodedValue);
});

it('should not throw when encoding null or undefined values', () => {
let toEncode = null;
let expectedEncodedValue = '';
let encodedValue = HashUtils.encodeValues({ a: toEncode });
expect(() => HashUtils.encodeValues({ a: toEncode })).not.toThrowError();
expect(encodedValue).toEqual(expectedEncodedValue);
});

it('joins values correctly', () => {
let firstValue = 'test';
let secondValue = [1];
Expand Down

0 comments on commit 4b3055d

Please sign in to comment.