Revision | 663 (tree) |
---|---|
Zeit | 2017-07-15 14:36:54 |
Autor | y-moriguchi |
Example: polyfill of JSON object
@@ -2,9 +2,12 @@ | ||
2 | 2 | function ev(prog) { |
3 | 3 | return JSONx.parse(prog); |
4 | 4 | } |
5 | - function es(str) { | |
5 | + function es0(str) { | |
6 | 6 | return JSONx.stringify(str); |
7 | 7 | } |
8 | + function es(str, rep, spc) { | |
9 | + expect(JSONx.stringify(str, rep, spc)).toBe(JSON.stringify(str, rep, spc)); | |
10 | + } | |
8 | 11 | function isArray(item) { |
9 | 12 | return Object.prototype.toString.call(item) === '[object Array]'; |
10 | 13 | } |
@@ -104,42 +107,100 @@ | ||
104 | 107 | |
105 | 108 | describe("testing stringifier", function () { |
106 | 109 | it("stringify true/false/null", function () { |
107 | - expect(es(true)).toBe('true'); | |
108 | - expect(es(false)).toBe('false'); | |
109 | - expect(es(null)).toBe('null'); | |
110 | + es(true); | |
111 | + es(false); | |
112 | + es(null); | |
110 | 113 | }); |
111 | 114 | it("stringify numbers", function () { |
112 | - expect(es(0)).toBe('0'); | |
113 | - expect(es(765)).toBe('765'); | |
114 | - expect(es(7.65)).toBe('7.65'); | |
115 | - expect(es(0.765)).toBe('0.765'); | |
116 | - expect(es(-765)).toBe('-765'); | |
115 | + es(0); | |
116 | + es(765); | |
117 | + es(7.65); | |
118 | + es(0.765); | |
119 | + es(-765); | |
117 | 120 | }); |
118 | 121 | it("stringify strings", function() { |
119 | - expect(es('a')).toBe('"a"'); | |
120 | - expect(es('ひでぶ!')).toBe('"ひでぶ!"'); | |
121 | - expect(es('\"\\\b\f\n\r\t')).toBe('"\\"\\\\\\b\\f\\n\\r\\t"'); | |
122 | - expect(es('')).toBe('""'); | |
123 | - expect(es('/*aaaaa*/')).toBe('"/*aaaaa*/"'); | |
124 | - expect(es('//aaaa')).toBe('"//aaaa"'); | |
122 | + es('a'); | |
123 | + es('ひでぶ!'); | |
124 | + es('\"\\\b\f\n\r\t'); | |
125 | + es(''); | |
126 | + es('/*aaaaa*/'); | |
127 | + es('//aaaa'); | |
125 | 128 | }); |
126 | 129 | it("stringify objects", function () { |
127 | - expect(es({})).toBe('{}'); | |
128 | - expect(es({prop1:1})).toBe('{"prop1":1}'); | |
129 | - expect(es({prop1:[1,2,'3']})).toBe('{"prop1":[1,2,"3"]}'); | |
130 | - expect(es({prop1:1, prop2:undefined, prop3:Object})).toBe('{"prop1":1}'); | |
131 | - expect(ew(es({prop1:1,prop2:2}), {prop1:1,prop2:2})).toBeTruthy(); | |
130 | + es({}); | |
131 | + es({prop1:1}); | |
132 | + es({prop1:[1,2,'3']}); | |
133 | + es({prop1:1, prop2:undefined, prop3:Object}); | |
134 | + expect(ew(es0({prop1:1,prop2:2}), {prop1:1,prop2:2})).toBeTruthy(); | |
132 | 135 | }); |
133 | 136 | it("stringify arrays", function () { |
134 | - expect(es([])).toBe('[]'); | |
135 | - expect(es([1,false,'false'])).toBe('[1,false,"false"]'); | |
136 | - expect(es([1,{prop1:1}])).toBe('[1,{"prop1":1}]'); | |
137 | - expect(es([1,undefined,Object])).toBe('[1,null,null]'); | |
137 | + es([]); | |
138 | + es([1,false,'false']); | |
139 | + es([1,{prop1:1}]); | |
140 | + es([1,undefined,Object]); | |
138 | 141 | }); |
139 | 142 | it("others", function () { |
140 | - expect(es(undefined)).toBe(undefined); | |
141 | - expect(es(Object)).toBe(undefined); | |
142 | - expect(es(function() {})).toBe(undefined); | |
143 | + expect(es0(undefined)).toBe(undefined); | |
144 | + expect(es0(Object)).toBe(undefined); | |
145 | + expect(es0(function() {})).toBe(undefined); | |
143 | 146 | }); |
147 | + it("stringify objects with replacer", function () { | |
148 | + function rep1(key, value) { | |
149 | + return typeof value === 'object' || value % 3 === 0 ? value : undefined; | |
150 | + } | |
151 | + function rep2(key, value) { | |
152 | + return typeof value === 'object' || key === 'prop1' ? value : undefined; | |
153 | + } | |
154 | + es({}, rep1); | |
155 | + es({prop1:1,prop2:2,prop3:3,prop4:4}, rep1); | |
156 | + es({prop1:1,prop2:2,prop3:3,prop4:4}, rep2); | |
157 | + es({prop1:1,prop2:2,prop3:{prop1:1,prop2:2,prop3:3,prop4:4},prop4:4}, rep1); | |
158 | + es({prop1:{prop1:1,prop2:2,prop3:3,prop4:4},prop2:2,prop3:3,prop4:4}, rep2); | |
159 | + es({prop1:1,prop2:2,prop3:3,prop4:{prop1:1,prop2:2,prop3:3,prop4:4}}, rep1); | |
160 | + es({prop1:1,prop2:2,prop3:3,prop4:{prop1:1,prop2:2,prop3:3,prop4:4}}, rep2); | |
161 | + es({prop1:{prop1:1,prop2:2,prop3:3,prop4:4},prop2:2,prop3:3,prop4:4}, ['prop1']); | |
162 | + }); | |
163 | + it("stringify arrays with replacer", function () { | |
164 | + function rep1(key, value) { | |
165 | + return Array.isArray(value) || value % 3 === 0 ? value : undefined; | |
166 | + } | |
167 | + function rep2(key, value) { | |
168 | + return Array.isArray(value) || key === '0' ? value : undefined; | |
169 | + } | |
170 | + es([], rep1); | |
171 | + es([1,2,3,4], rep1); | |
172 | + es([1,2,3,4], rep2); | |
173 | + es([1,2,[1,2,3,4],4], rep1); | |
174 | + es([[1,2,3,4],2,3,4], rep2); | |
175 | + es([1,2,3,[1,2,3,4]], rep1); | |
176 | + es([1,2,3,[1,2,3,4]], rep2); | |
177 | + es([1,2,3,[1,2,3,4]], ['0','2']); | |
178 | + }); | |
179 | + it("stringify objects with spaces", function () { | |
180 | + es({}, null, 1); | |
181 | + es({prop1:1}, null, 1); | |
182 | + es({prop1:[1,2,'3']}, null, 1); | |
183 | + es([1,2,{prop1:[1,2,'3']}], null, 1); | |
184 | + es({}, null, 4); | |
185 | + es({prop1:1}, null, 4); | |
186 | + es({prop1:[1,2,'3']}, null, 4); | |
187 | + es([1,2,{prop1:[1,2,'3']}], null, 4); | |
188 | + es({}, null, 10); | |
189 | + es({prop1:1}, null, 10); | |
190 | + es({prop1:[1,2,'3']}, null, 10); | |
191 | + es([1,2,{prop1:[1,2,'3']}], null, 10); | |
192 | + es({}, null, 11); | |
193 | + es({prop1:1}, null, 11); | |
194 | + es({prop1:[1,2,'3']}, null, 11); | |
195 | + es([1,2,{prop1:[1,2,'3']}], null, 11); | |
196 | + es({}, null, 0); | |
197 | + es({prop1:1}, null, 0); | |
198 | + es({prop1:[1,2,'3']}, null, 0); | |
199 | + es([1,2,{prop1:[1,2,'3']}], null, 0); | |
200 | + es({}, null, '\t'); | |
201 | + es({prop1:1}, null, '\t'); | |
202 | + es({prop1:[1,2,'3']}, null, '\t'); | |
203 | + es([1,2,{prop1:[1,2,'3']}], null, '\t'); | |
204 | + }); | |
144 | 205 | }); |
145 | 206 | }); |