Refactored toJsonArray(), added isBoolean() function

This commit is contained in:
Vojta Jina 2010-11-05 23:14:44 +00:00 committed by Igor Minar
parent b7027b9d87
commit 5be325a0c1
2 changed files with 11 additions and 11 deletions

View file

@ -312,6 +312,7 @@ function isString(value){ return typeof value == $string;}
function isNumber(value){ return typeof value == $number;} function isNumber(value){ return typeof value == $number;}
function isArray(value) { return value instanceof Array; } function isArray(value) { return value instanceof Array; }
function isFunction(value){ return typeof value == $function;} function isFunction(value){ return typeof value == $function;}
function isBoolean(value) { return typeof value == $boolean;}
function isTextNode(node) { return nodeName(node) == '#text'; } function isTextNode(node) { return nodeName(node) == '#text'; }
function trim(value) { return isString(value) ? value.replace(/^\s*/, '').replace(/\s*$/, '') : value; } function trim(value) { return isString(value) ? value.replace(/^\s*/, '').replace(/\s*$/, '') : value; }
function isElement(node) { function isElement(node) {

View file

@ -1,6 +1,6 @@
var array = [].constructor; var array = [].constructor;
function toJson(obj, pretty){ function toJson(obj, pretty) {
var buf = []; var buf = [];
toJsonArray(buf, obj, pretty ? "\n " : _null, []); toJsonArray(buf, obj, pretty ? "\n " : _null, []);
return buf.join(''); return buf.join('');
@ -35,37 +35,36 @@ function toJsonArray(buf, obj, pretty, stack) {
} }
if (includes(stack, obj)) { if (includes(stack, obj)) {
buf.push("RECURSION"); buf.push('RECURSION');
return; return;
} }
stack.push(obj); stack.push(obj);
} }
var type = typeof obj;
if (obj === _null) { if (obj === _null) {
buf.push($null); buf.push($null);
} else if (obj instanceof RegExp) { } else if (obj instanceof RegExp) {
buf.push(angular['String']['quoteUnicode'](obj.toString())); buf.push(angular['String']['quoteUnicode'](obj.toString()));
} else if (type === $function) { } else if (isFunction(obj)) {
return; return;
} else if (type === $boolean) { } else if (isBoolean(obj)) {
buf.push('' + obj); buf.push('' + obj);
} else if (type === $number) { } else if (isNumber(obj)) {
if (isNaN(obj)) { if (isNaN(obj)) {
buf.push($null); buf.push($null);
} else { } else {
buf.push('' + obj); buf.push('' + obj);
} }
} else if (type === $string) { } else if (isString(obj)) {
return buf.push(angular['String']['quoteUnicode'](obj)); return buf.push(angular['String']['quoteUnicode'](obj));
} else if (type === $object) { } else if (isObject(obj)) {
if (obj instanceof Array) { if (isArray(obj)) {
buf.push("["); buf.push("[");
var len = obj.length; var len = obj.length;
var sep = false; var sep = false;
for(var i=0; i<len; i++) { for(var i=0; i<len; i++) {
var item = obj[i]; var item = obj[i];
if (sep) buf.push(","); if (sep) buf.push(",");
if (!(item instanceof RegExp) && (typeof item == $function || typeof item == $undefined)) { if (!(item instanceof RegExp) && (isFunction(item) || isUndefined(item))) {
buf.push($null); buf.push($null);
} else { } else {
toJsonArray(buf, item, pretty, stack); toJsonArray(buf, item, pretty, stack);
@ -104,7 +103,7 @@ function toJsonArray(buf, obj, pretty, stack) {
buf.push("}"); buf.push("}");
} }
} }
if (typeof obj == $object) { if (isObject(obj)) {
stack.pop(); stack.pop();
} }
} }