I have a file whose sole purpose is to provide an enormous collection of key/value pairs in an object. It looks something like this:
var myobj = {
some_key: 'some value',
another_key: 'another value',
// thousands of other key/value pairs...
some_key: 'accidental duplicate key with different value'
};
Now when I reference that file and reference the some_key I get the accidental duplicate value because JavaScript will store the last declared key.
I want to write a unit test that checks this object for accidental duplicate keys. Problem is that JavaScript has already stripped out the duplicates. How would I acplish this checking through unit tests?
(One answer would be to manually parse the file using string parsing to find the duplicates. This is brittle and I'd want to stay away from this.)
I have a file whose sole purpose is to provide an enormous collection of key/value pairs in an object. It looks something like this:
var myobj = {
some_key: 'some value',
another_key: 'another value',
// thousands of other key/value pairs...
some_key: 'accidental duplicate key with different value'
};
Now when I reference that file and reference the some_key I get the accidental duplicate value because JavaScript will store the last declared key.
I want to write a unit test that checks this object for accidental duplicate keys. Problem is that JavaScript has already stripped out the duplicates. How would I acplish this checking through unit tests?
(One answer would be to manually parse the file using string parsing to find the duplicates. This is brittle and I'd want to stay away from this.)
Share Improve this question asked Jun 30, 2014 at 15:37 GuyGuy 67.4k101 gold badges265 silver badges331 bronze badges 6- 1 And changing the source of the file is impossible? – Henrik Andersson Commented Jun 30, 2014 at 15:39
- 2 "use strict" - will throw an error if you have duplicate keys (I think, haven't tested) – soktinpk Commented Jun 30, 2014 at 15:40
- 2 The problem is with who's creating that file, you should fix that rather than patch it later – Ruan Mendes Commented Jun 30, 2014 at 15:40
- @soktinpk: Yeah, it does! :-) – gen_Eric Commented Jun 30, 2014 at 15:41
-
@DhavalMarthak I don't think that's what the OP is asking about, the OP's structure is something like
{a: 1, a: 2}
– Ruan Mendes Commented Jun 30, 2014 at 15:43
1 Answer
Reset to default 8Add "use strict";
to the top of your file. Example usage:
(function() {
"use strict";
// Throws a syntax error
var a = {
b: 5,
b: 6
};
})();
Edit: This answer is no longer strictly up to date due to ES6 puted property values. If you want, you can write your object as a JSON object (if that's possible) and put it through http://www.jslint./, which will check for duplicate keys. See as well: "use strict"; now allows duplicated properties?