1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var parse = require('./lib/parser').parse;
|
12 | var stringify = require('./lib/stringify');
|
13 |
|
14 | var typeExpressionCache = {
|
15 | normal: {},
|
16 | jsdoc: {}
|
17 | };
|
18 |
|
19 | var parsedTypeCache = {
|
20 | normal: {},
|
21 | htmlSafe: {}
|
22 | };
|
23 |
|
24 | function 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 |
|
34 | function 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 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function 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 |
|
55 | function 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 |
|
82 | function 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 |
|
97 | function Catharsis() {
|
98 | this.Types = require('./lib/types');
|
99 | }
|
100 |
|
101 | Catharsis.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 |
|
111 | Catharsis.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 |
|
123 | module.exports = new Catharsis();
|