Skip to content

Commit 734bbd5

Browse files
Rename to stringifyJSON
1 parent 3dfa753 commit 734bbd5

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
jQuery.stringifyJSON
1+
stringifyJSON
22
====================
33

44
### The jQuery.parseJSON's best friend
55

66
Converts a value to JSON string.
77

88
```js
9-
var json = jQuery.stringifyJSON({name: "Donghwan"});
10-
alert(json === '{"name":"Donghwan"}');
9+
alert(stringifyJSON({name: "Donghwan"}) === '{"name":"Donghwan"}');
1110
```
1211

13-
`jQuery.stringifyJSON` is useful for those who want to use `JSON.stringify` in browsers which don't implement the JSON object and are reluctant to declare global variable `JSON` and add `toJSON` methods to prototypes, such as plugin developers. In cases where the native `JSON.stringify` is avilable, `jQuery.stringifyJSON` uses it by default.
12+
`stringifyJSON` is useful for those who want to use `JSON.stringify` in browsers which don't implement the JSON object and are reluctant to declare global variable `JSON` and add `toJSON` methods to prototypes, such as plugin developers. In cases where the native `JSON.stringify` is avilable, `stringifyJSON` uses it by default.
1413

15-
Because the JSON standard is frozen and this plugin is heavily based on Douglas Crockford's reference implementation, these sources are unlikely to be modified. So, you may take the latest or copy and paste them freely. `jQuery.stringifyJSON` is actually a utility function which has nothing to do with jQuery.
14+
Because the JSON standard is frozen and this plugin is heavily based on Douglas Crockford's [reference implementation](https://github.com/douglascrockford/JSON-js/blob/master/json2.js), these sources are unlikely to be modified. So, you may take the latest or copy and paste it freely.
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/*
2-
* jQuery stringifyJSON
3-
* http://github.com/flowersinthesand/jquery-stringifyJSON
2+
* stringifyJSON
3+
* http://github.com/flowersinthesand/stringifyJSON
44
*
55
* Copyright 2011, Donghwan Kim
66
* Licensed under the Apache License, Version 2.0
77
* http://www.apache.org/licenses/LICENSE-2.0
88
*/
99
// This plugin is heavily based on Douglas Crockford's reference implementation
10-
(function($) {
10+
(function() {
11+
12+
"use strict";
1113

1214
var escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
1315
meta = {
@@ -53,8 +55,10 @@
5355

5456
switch (Object.prototype.toString.call(value)) {
5557
case "[object Date]":
56-
return isFinite(value.valueOf()) ? '"' + value.getUTCFullYear() + "-" + f(value.getUTCMonth() + 1) + "-" + f(value.getUTCDate()) + "T" +
57-
f(value.getUTCHours()) + ":" + f(value.getUTCMinutes()) + ":" + f(value.getUTCSeconds()) + "Z" + '"' : "null";
58+
return isFinite(value.valueOf()) ?
59+
'"' + value.getUTCFullYear() + "-" + f(value.getUTCMonth() + 1) + "-" + f(value.getUTCDate()) +
60+
"T" + f(value.getUTCHours()) + ":" + f(value.getUTCMinutes()) + ":" + f(value.getUTCSeconds()) + "Z" + '"' :
61+
"null";
5862
case "[object Array]":
5963
len = value.length;
6064
partial = [];
@@ -79,12 +83,15 @@
7983
}
8084
}
8185

82-
$.stringifyJSON = function(value) {
86+
function stringifyJSON(value) {
8387
if (window.JSON && window.JSON.stringify) {
8488
return window.JSON.stringify(value);
8589
}
8690

8791
return str("", {"": value});
88-
};
92+
}
93+
94+
// Expose stringifyJSON to the global object
95+
window.stringifyJSON = stringifyJSON;
8996

90-
}(jQuery));
97+
}());

test/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
<html>
33
<head>
44
<meta charset="utf-8">
5-
<title>jQuery stringifyJSON Test Suite</title>
5+
<title>stringifyJSON Test Suite</title>
66
<link rel="stylesheet" media="screen" href="http://code.jquery.com/qunit/qunit-git.css" />
77
<script type="text/javascript" src="http://code.jquery.com/qunit/qunit-git.js"></script>
88
<script type="text/javascript" src="http://code.jquery.com/jquery-git.js"></script>
9-
<script type="text/javascript" src="../jquery.stringifyjson.js"></script>
9+
<script type="text/javascript" src="../stringifyjson.js"></script>
1010
<script type="text/javascript" src="test.js"></script>
1111
</head>
1212
<body>
13-
<h1 id="qunit-header">jQuery stringifyJSON Test Suite</h1>
13+
<h1 id="qunit-header">stringifyJSON Test Suite</h1>
1414
<h2 id="qunit-banner"></h2>
1515
<div id="qunit-testrunner-toolbar"></div>
1616
<h2 id="qunit-userAgent"></h2>

test/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function teardown() {
3333
}
3434
}
3535

36-
module("jQuery.stringifyJSON", {setup: setup, teardown: teardown});
36+
module("stringifyJSON", {setup: setup, teardown: teardown});
3737

3838
$.each({
3939
chars: {
@@ -87,7 +87,7 @@ $.each({
8787
}, function(testName, data) {
8888
test(testName, function() {
8989
$.each(data.value, function(i, val) {
90-
deepEqual($.stringifyJSON(val), data.answer[i]);
90+
deepEqual(stringifyJSON(val), data.answer[i]);
9191
});
9292
});
9393
});

0 commit comments

Comments
 (0)