UNPKG

2.82 kBJavaScriptView Raw
1/**
2 * Catharsis
3 * A parser for Google Closure Compiler type expressions, powered by PEG.js.
4 *
5 * @author Jeff Williams <jeffrey.l.williams@gmail.com>
6 * @license MIT License <http://opensource.org/licenses/mit-license.php/>
7 */
8
9'use strict';
10
11var parse = require('./lib/parser').parse;
12var stringify = require('./lib/stringify');
13
14var typeExpressionCache = {
15 normal: {},
16 jsdoc: {}
17};
18
19var parsedTypeCache = {
20 normal: {},
21 htmlSafe: {}
22};
23
24function getTypeExpressionCache(options) {
25 if (options.useCache === false) {
26 return null;
27 } else if (options.jsdoc === true) {
28 return typeExpressionCache.jsdoc;
29 } else {
30 return typeExpressionCache.normal;
31 }
32}
33
34function getParsedTypeCache(options) {
35 if (options.useCache === false || options.links !== null || options.links !== undefined) {
36 return null;
37 } else if (options.htmlSafe === true) {
38 return parsedTypeCache.htmlSafe;
39 } else {
40 return parsedTypeCache.normal;
41 }
42}
43
44// can't return the original if any of the following are true:
45// 1. restringification was requested
46// 2. htmlSafe option was requested
47// 3. links option was provided
48// 4. typeExpression property is missing
49function canReturnOriginalExpression(parsedType, options) {
50 return options.restringify !== true && options.htmlSafe !== true &&
51 (options.links === null || options.links === undefined) &&
52 Object.prototype.hasOwnProperty.call(parsedType, 'typeExpression');
53}
54
55function cachedParse(expr, options) {
56 var cache = getTypeExpressionCache(options);
57 var parsedType;
58
59 if (cache && cache[expr]) {
60 return cache[expr];
61 } else {
62 parsedType = parse(expr, options);
63
64 Object.defineProperties(parsedType, {
65 typeExpression: {
66 value: expr
67 },
68 jsdoc: {
69 value: options.jsdoc === true ? true : false
70 }
71 });
72 parsedType = Object.freeze(parsedType);
73
74 if (cache) {
75 cache[expr] = parsedType;
76 }
77
78 return parsedType;
79 }
80}
81
82function cachedStringify(parsedType, options) {
83 var cache = getParsedTypeCache(options);
84 var json;
85
86 if (canReturnOriginalExpression(parsedType, options)) {
87 return parsedType.typeExpression;
88 } else if (cache) {
89 json = JSON.stringify(parsedType);
90 cache[json] = cache[json] || stringify(parsedType, options);
91 return cache[json];
92 } else {
93 return stringify(parsedType, options);
94 }
95}
96
97function Catharsis() {
98 this.Types = require('./lib/types');
99}
100
101Catharsis.prototype.parse = function(typeExpr, options) {
102 options = options || {};
103
104 typeExpr = typeExpr.replace(/[\r\n]/g, '')
105 .replace(/\s+/g, ' ')
106 .trim();
107
108 return cachedParse(typeExpr, options);
109};
110
111Catharsis.prototype.stringify = function(parsedType, options) {
112 options = options || {};
113 var result;
114
115 result = cachedStringify(parsedType, options);
116 if (options.validate) {
117 this.parse(result, options);
118 }
119
120 return result;
121};
122
123module.exports = new Catharsis();