catharsis
Advanced tools
Comparing version 0.4.2 to 0.4.3
@@ -14,13 +14,42 @@ /** | ||
var typeExpressionCache = {}; | ||
var lenientTypeExpressionCache = {}; | ||
var typeExpressionCache = { | ||
normal: {}, | ||
lenient: {} | ||
}; | ||
var parsedTypeCache = {}; | ||
var lenientParsedTypeCache = {}; | ||
var parsedTypeCache = { | ||
normal: {}, | ||
htmlSafe: {} | ||
}; | ||
function getTypeExpressionCache(options) { | ||
if (options.useCache === false) { | ||
return null; | ||
} else if (options.lenient === true) { | ||
return typeExpressionCache.lenient; | ||
} else { | ||
return typeExpressionCache.normal; | ||
} | ||
} | ||
function getParsedTypeCache(options) { | ||
if (options.useCache === false) { | ||
return null; | ||
} else if (options.htmlSafe === true) { | ||
return parsedTypeCache.htmlSafe; | ||
} else { | ||
return parsedTypeCache.normal; | ||
} | ||
} | ||
function canReturnOriginalExpression(parsedType, options) { | ||
return options.restringify !== true && options.htmlSafe !== true && | ||
Object.prototype.hasOwnProperty.call(parsedType, 'typeExpression'); | ||
} | ||
function cachedParse(expr, options) { | ||
var cache = options.lenient ? lenientTypeExpressionCache : typeExpressionCache; | ||
var cache = getTypeExpressionCache(options); | ||
var parsedType; | ||
if (options.useCache !== false && cache[expr]) { | ||
if (cache && cache[expr]) { | ||
return cache[expr]; | ||
@@ -40,3 +69,3 @@ } else { | ||
if (options.useCache !== false) { | ||
if (cache) { | ||
cache[expr] = parsedType; | ||
@@ -50,10 +79,8 @@ } | ||
function cachedStringify(parsedType, options) { | ||
var cache = options.lenient ? lenientParsedTypeCache : parsedTypeCache; | ||
var cache = getParsedTypeCache(options); | ||
var json; | ||
if (options.useCache !== false && Object.prototype.hasOwnProperty.call(parsedType, | ||
'typeExpression')) { | ||
// return the original type expression | ||
if (canReturnOriginalExpression(parsedType, options)) { | ||
return parsedType.typeExpression; | ||
} else if (options.useCache !== false) { | ||
} else if (cache) { | ||
json = JSON.stringify(parsedType); | ||
@@ -60,0 +87,0 @@ cache[json] = cache[json] || stringify(parsedType, options); |
{ | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"name": "catharsis", | ||
@@ -4,0 +4,0 @@ "description": "A JavaScript parser for Google Closure Compiler type expressions.", |
@@ -48,3 +48,3 @@ # Catharsis # | ||
console.log(catharsis.stringify(parsedJsdocType, // (number|string) | ||
{useCache: false})); | ||
{restringify: true})); | ||
@@ -101,5 +101,7 @@ | ||
are not escaped. | ||
+ `options.useCache`: Specifies whether to use the cache of stringified parse results. If the | ||
cache is enabled, and the parsed type expression includes a `typeExpression` property, the | ||
`typeExpression` property will be returned as-is. Defaults to `true`. | ||
+ `options.restringify`: Forces Catharsis to restringify the parsed type. If this option is not | ||
specified, and the parsed type object includes a `typeExpression` property, Catharsis will | ||
return the `typeExpression` property without modification. Defaults to `false`. | ||
+ `options.useCache`: Specifies whether to use the cache of stringified parse results. Defaults | ||
to `true`. | ||
+ `options.validate`: Specifies whether to validate the stringified parse results by attempting | ||
@@ -137,2 +139,7 @@ to parse them as a type expression. Defaults to `false`. | ||
+ 0.4.3 (March 2013): | ||
+ The `stringify()` method no longer caches HTML-safe type expressions as if they were normal | ||
type expressions. | ||
+ The `stringify()` method's options parameter may now include an `options.restringify` | ||
property, and the behavior of the `options.useCache` property has changed. | ||
+ 0.4.2 (March 2013): | ||
@@ -139,0 +146,0 @@ + When lenient parsing is enabled, name expressions can now contain the characters `:` and `/`. |
@@ -68,4 +68,3 @@ /*global describe: true, it: true, xit: true */ | ||
it('should use the regular cache when lenient mode is disabled', function() { | ||
// parse twice to make sure we're getting a cached version. | ||
// there must be a less lame way to do this... | ||
// parse twice to make sure we're getting a cached version | ||
var bar = catharsis.parse('bar'); | ||
@@ -116,3 +115,3 @@ bar = catharsis.parse('bar'); | ||
it('should return the typeExpression property as-is if the cache is enabled', function() { | ||
it('should return the typeExpression property as-is by default', function() { | ||
var quxString = catharsis.stringify({ | ||
@@ -127,3 +126,4 @@ type: Types.NameExpression, | ||
it('should not return the typeExpression property if the cache is disabled', function() { | ||
it('should not return the typeExpression property if restringification is requested', | ||
function() { | ||
var quuxString = catharsis.stringify({ | ||
@@ -135,3 +135,3 @@ type: Types.NameExpression, | ||
{ | ||
useCache: false | ||
restringify: true | ||
}); | ||
@@ -141,5 +141,5 @@ | ||
}); | ||
it('should pass the specified options to the stringifier', function() { | ||
var string = catharsis.stringify({ | ||
it('should not return the typeExpression property if htmlSafe is enabled', function() { | ||
var typeAppString = catharsis.stringify({ | ||
type: Types.TypeApplication, | ||
@@ -153,5 +153,6 @@ expression: { | ||
type: Types.NameExpression, | ||
name: 'string' | ||
name: 'boolean' | ||
} | ||
] | ||
], | ||
typeExpression: 'Array.<boolean>' | ||
}, | ||
@@ -162,5 +163,33 @@ { | ||
typeAppString.should.equal('Array.<boolean>'); | ||
}); | ||
// used for multiple tests | ||
var typeApp = { | ||
type: Types.TypeApplication, | ||
expression: { | ||
type: Types.NameExpression, | ||
name: 'Array' | ||
}, | ||
applications: [ | ||
{ | ||
type: Types.NameExpression, | ||
name: 'string' | ||
} | ||
] | ||
}; | ||
it('should pass the specified options to the stringifier', function() { | ||
var string = catharsis.stringify(typeApp, {htmlSafe: true}); | ||
string.should.equal('Array.<string>'); | ||
}); | ||
it('should not cache an HTML-safe expression, then return it when the htmlSafe option ' + | ||
'is disabled', function() { | ||
var string = catharsis.stringify(typeApp, {}); | ||
string.should.equal('Array.<string>'); | ||
}); | ||
}); | ||
}); |
337249
7107
195