mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-20 00:10:26 +00:00
refactor(fromJson/toJson): move the contents of these files into Angular.js
these files are now mostly empty so it doesn't make sense to keep them separated from other helper functions
This commit is contained in:
parent
35125d2513
commit
af0ad6561c
5 changed files with 103 additions and 108 deletions
1
angularFiles.js
vendored
1
angularFiles.js
vendored
|
|
@ -3,7 +3,6 @@ angularFiles = {
|
|||
'src/Angular.js',
|
||||
'src/loader.js',
|
||||
'src/AngularPublic.js',
|
||||
'src/JSON.js',
|
||||
'src/jqLite.js',
|
||||
'src/apis.js',
|
||||
|
||||
|
|
|
|||
|
|
@ -727,6 +727,59 @@ function bind(self, fn) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
function toJsonReplacer(key, value) {
|
||||
var val = value;
|
||||
|
||||
if (/^\$+/.test(key)) {
|
||||
val = undefined;
|
||||
} else if (isWindow(value)) {
|
||||
val = '$WINDOW';
|
||||
} else if (value && document === value) {
|
||||
val = '$DOCUMENT';
|
||||
} else if (isScope(value)) {
|
||||
val = '$SCOPE';
|
||||
}
|
||||
|
||||
return val;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name angular.toJson
|
||||
* @function
|
||||
*
|
||||
* @description
|
||||
* Serializes input into a JSON-formatted string.
|
||||
*
|
||||
* @param {Object|Array|Date|string|number} obj Input to be serialized into JSON.
|
||||
* @param {boolean=} pretty If set to true, the JSON output will contain newlines and whitespace.
|
||||
* @returns {string} Jsonified string representing `obj`.
|
||||
*/
|
||||
function toJson(obj, pretty) {
|
||||
return JSON.stringify(obj, toJsonReplacer, pretty ? ' ' : null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name angular.fromJson
|
||||
* @function
|
||||
*
|
||||
* @description
|
||||
* Deserializes a JSON string.
|
||||
*
|
||||
* @param {string} json JSON string to deserialize.
|
||||
* @returns {Object|Array|Date|string|number} Deserialized thingy.
|
||||
*/
|
||||
function fromJson(json) {
|
||||
return isString(json)
|
||||
? JSON.parse(json)
|
||||
: json;
|
||||
}
|
||||
|
||||
|
||||
function toBoolean(value) {
|
||||
if (value && value.length !== 0) {
|
||||
var v = lowercase("" + value);
|
||||
|
|
|
|||
49
src/JSON.js
49
src/JSON.js
|
|
@ -1,49 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
var jsonReplacer = function(key, value) {
|
||||
var val = value;
|
||||
if (/^\$+/.test(key)) {
|
||||
val = undefined;
|
||||
} else if (isWindow(value)) {
|
||||
val = '$WINDOW';
|
||||
} else if (value && document === value) {
|
||||
val = '$DOCUMENT';
|
||||
} else if (isScope(value)) {
|
||||
val = '$SCOPE';
|
||||
}
|
||||
|
||||
return val;
|
||||
};
|
||||
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name angular.toJson
|
||||
* @function
|
||||
*
|
||||
* @description
|
||||
* Serializes input into a JSON-formatted string.
|
||||
*
|
||||
* @param {Object|Array|Date|string|number} obj Input to be serialized into JSON.
|
||||
* @param {boolean=} pretty If set to true, the JSON output will contain newlines and whitespace.
|
||||
* @returns {string} Jsonified string representing `obj`.
|
||||
*/
|
||||
function toJson(obj, pretty) {
|
||||
return JSON.stringify(obj, jsonReplacer, pretty ? ' ' : null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name angular.fromJson
|
||||
* @function
|
||||
*
|
||||
* @description
|
||||
* Deserializes a JSON string.
|
||||
*
|
||||
* @param {string} json JSON string to deserialize.
|
||||
* @returns {Object|Array|Date|string|number} Deserialized thingy.
|
||||
*/
|
||||
function fromJson(json) {
|
||||
return isString(json)
|
||||
? JSON.parse(json)
|
||||
: json;
|
||||
}
|
||||
|
|
@ -542,4 +542,54 @@ describe('angular', function() {
|
|||
expect(snake_case('alanBobCharles')).toEqual('alan_bob_charles');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('fromJson', function() {
|
||||
|
||||
it('should delegate to JSON.parse', function() {
|
||||
var spy = spyOn(JSON, 'parse').andCallThrough();
|
||||
|
||||
expect(fromJson('{}')).toEqual({});
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('toJson', function() {
|
||||
|
||||
it('should delegate to JSON.stringify', function() {
|
||||
var spy = spyOn(JSON, 'stringify').andCallThrough();
|
||||
|
||||
expect(toJson({})).toEqual('{}');
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
it('should format objects pretty', function() {
|
||||
expect(toJson({a: 1, b: 2}, true)).
|
||||
toBeOneOf('{\n "a": 1,\n "b": 2\n}', '{\n "a":1,\n "b":2\n}');
|
||||
expect(toJson({a: {b: 2}}, true)).
|
||||
toBeOneOf('{\n "a": {\n "b": 2\n }\n}', '{\n "a":{\n "b":2\n }\n}');
|
||||
});
|
||||
|
||||
|
||||
it('should not serialize properties starting with $', function() {
|
||||
expect(toJson({$few: 'v', $$some:'value'}, false)).toEqual('{}');
|
||||
});
|
||||
|
||||
|
||||
it('should not serialize $window object', function() {
|
||||
expect(toJson(window)).toEqual('"$WINDOW"');
|
||||
});
|
||||
|
||||
|
||||
it('should not serialize $document object', function() {
|
||||
expect(toJson(document)).toEqual('"$DOCUMENT"');
|
||||
});
|
||||
|
||||
|
||||
it('should not serialize scope instances', inject(function($rootScope) {
|
||||
expect(toJson({key: $rootScope})).toEqual('{"key":"$SCOPE"}');
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,58 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
describe('json', function() {
|
||||
|
||||
describe('fromJson', function() {
|
||||
|
||||
it('should delegate to JSON.parse', function() {
|
||||
var spy = spyOn(JSON, 'parse').andCallThrough();
|
||||
|
||||
expect(fromJson('{}')).toEqual({});
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('toJson', function() {
|
||||
|
||||
it('should delegate to JSON.stringify', function() {
|
||||
var spy = spyOn(JSON, 'stringify').andCallThrough();
|
||||
|
||||
expect(toJson({})).toEqual('{}');
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
it('should format objects pretty', function() {
|
||||
expect(toJson({a: 1, b: 2}, true)).
|
||||
toBeOneOf('{\n "a": 1,\n "b": 2\n}', '{\n "a":1,\n "b":2\n}');
|
||||
expect(toJson({a: {b: 2}}, true)).
|
||||
toBeOneOf('{\n "a": {\n "b": 2\n }\n}', '{\n "a":{\n "b":2\n }\n}');
|
||||
});
|
||||
|
||||
|
||||
it('should not serialize properties starting with $', function() {
|
||||
expect(toJson({$few: 'v', $$some:'value'}, false)).toEqual('{}');
|
||||
});
|
||||
|
||||
|
||||
it('should not serialize undefined values', function() {
|
||||
expect(angular.toJson({A:undefined})).toEqual('{}');
|
||||
});
|
||||
|
||||
|
||||
it('should not serialize $window object', function() {
|
||||
expect(toJson(window)).toEqual('"$WINDOW"');
|
||||
});
|
||||
|
||||
|
||||
it('should not serialize $document object', function() {
|
||||
expect(toJson(document)).toEqual('"$DOCUMENT"');
|
||||
});
|
||||
|
||||
|
||||
it('should not serialize scope instances', inject(function($rootScope) {
|
||||
expect(toJson({key: $rootScope})).toEqual('{"key":"$SCOPE"}');
|
||||
}));
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue