Skip to content

Commit

Permalink
add tests and make sure exposed JSON doesnt override the real window.…
Browse files Browse the repository at this point in the history
…JSON object
  • Loading branch information
yields committed Apr 23, 2014
1 parent c116e50 commit 4fb8832
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ components:
clean:
rm -fr build components template.js

.PHONY: clean
test:
@component test phantom

.PHONY: clean test
10 changes: 5 additions & 5 deletions component.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"version": "0.0.1",
"keywords": ["json", "fallback"],
"dependencies": {},
"development": {},
"scripts": [
"index.js"
]
}
"scripts": ["index.js"],
"development": {
"component/assert": "*"
}
}
8 changes: 2 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,11 @@
// Create a JSON object only if one does not already exist. We create the
// methods in a closure to avoid creating global variables.

if (typeof JSON !== 'object') {
JSON = {};
}

(function () {
'use strict';

var JSON = module.exports = {};

function f(n) {
// Format integers to have at least two digits.
return n < 10 ? '0' + n : n;
Expand Down Expand Up @@ -487,5 +485,3 @@ if (typeof JSON !== 'object') {
};
}
}());

module.exports = JSON;
17 changes: 17 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

describe('JSON', function(){
var json = require('json-fallback');
var assert = require('assert');

it('should not leak to the global scope', function(){
assert(json != window.JSON);
})

it('should parse json', function(){
assert(0 == json.parse('[0]')[0]);
})

it('should stringify json', function(){
assert('[0]' == json.stringify([0]));
})
})

0 comments on commit 4fb8832

Please sign in to comment.