Skip to content

Commit

Permalink
Merge pull request #4 from swissmanu/develop
Browse files Browse the repository at this point in the history
fix(map): Allow null as value for Map entry
  • Loading branch information
swissmanu authored Apr 18, 2017
2 parents 436053c + 357edda commit 761433a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/parsers/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ export const fromMap = <T>(map: ParserInput, key: string, valueParser: ParseFn<T
throw new ParserError('Map', map);
}

const value = map[key];

if (isNil(value)) {
if (!(key in map)) {
throw new ParserError(`Map containing key ${key}`, JSON.stringify(map));
}

return valueParser(value);
return valueParser(map[key]);
};
7 changes: 6 additions & 1 deletion test/parsers/map.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from 'chai';
import {ParserError} from '../../src/parsers';
import {ParserError, ParseFn} from '../../src/parsers';
import {fromMap} from '../../src/parsers/map';
import {aBoolean} from '../../src/parsers/boolean';

Expand Down Expand Up @@ -32,5 +32,10 @@ describe('Parser', () => {
it('should throw a ParserError when given a string', () => {
expect(() => fromMap('test', 'a', aBoolean)).to.throw(ParserError);
});

it('should not throw ParserError when given key exists and has value "null"', () => {
const aNull: ParseFn<null> = (): null => null;
expect(() => fromMap({ a: null }, 'a', aNull)).to.not.throw();
})
});
});

0 comments on commit 761433a

Please sign in to comment.