-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(util): Discontinue using Blob for JSON
Blobs obscure the JSON in Chrome dev tools, etc., and so we will stop using them for JSON. Closes #90
- Loading branch information
Showing
4 changed files
with
59 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
/** | ||
* Create a Blob containing JSON-serialized data. | ||
* Useful for easily creating JSON fetch request bodies. | ||
* Serialize an object to JSON. Useful for easily creating JSON fetch request bodies. | ||
* | ||
* @param body The object to be serialized to JSON. | ||
* @param replacer The JSON.stringify replacer used when serializing. | ||
* @returns A Blob containing the JSON serialized body. | ||
* @returns A JSON string. | ||
*/ | ||
export function json(body: any, replacer?: any): Blob { | ||
return new Blob([JSON.stringify((body !== undefined ? body : {}), replacer)], { type: 'application/json' }); | ||
export function json(body: any, replacer?: any): string { | ||
return JSON.stringify((body !== undefined ? body : {}), replacer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,11 @@ | ||
import 'aurelia-polyfills'; | ||
import {json} from '../src/util'; | ||
|
||
describe('util', () => { | ||
describe('json', () => { | ||
it('creates JSON blobs', (done) => { | ||
it('returns valid JSON', () => { | ||
let data = { test: 'data' }; | ||
let blob = json(data); | ||
|
||
expect(blob.type).toEqual('application/json'); | ||
|
||
let reader = new FileReader(); | ||
reader.addEventListener('loadend', () => { | ||
expect(JSON.parse(reader.result)).toEqual(data); | ||
done(); | ||
}); | ||
|
||
reader.readAsText(blob); | ||
let result = JSON.parse(json(data)); | ||
expect(data).toEqual(result); | ||
}); | ||
}); | ||
}); |