Skip to content

Commit e7e8b18

Browse files
rhendricblakeembrey
authored andcommitted
Use hasOwnProperty for key check (#17)
1 parent 8f55d17 commit e7e8b18

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

javascript-stringify.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
* @return {boolean}
7979
*/
8080
function isValidVariableName (name) {
81-
return !RESERVED_WORDS[name] && IS_VALID_IDENTIFIER.test(name);
81+
return !RESERVED_WORDS.hasOwnProperty(name) && IS_VALID_IDENTIFIER.test(name);
8282
}
8383

8484
/**

test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ describe('javascript-stringify', function () {
6767
'should stringify omit undefined keys',
6868
test({ a: true, b: undefined }, "{a:true}", null, { skipUndefinedProperties: true })
6969
);
70+
71+
it(
72+
'should quote reserved word keys',
73+
test({ "if": true, "else": false }, "{'if':true,'else':false}")
74+
);
75+
76+
it(
77+
'should not quote Object.prototype keys',
78+
test({ "constructor": 1, "toString": 2 }, "{constructor:1,toString:2}")
79+
);
7080
});
7181

7282
describe('native instances', function () {

0 commit comments

Comments
 (0)