util.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. 'use strict';
  2. module.exports = {
  3. copy: copy,
  4. checkDataType: checkDataType,
  5. checkDataTypes: checkDataTypes,
  6. coerceToTypes: coerceToTypes,
  7. toHash: toHash,
  8. getProperty: getProperty,
  9. escapeQuotes: escapeQuotes,
  10. equal: require('fast-deep-equal'),
  11. ucs2length: require('./ucs2length'),
  12. varOccurences: varOccurences,
  13. varReplace: varReplace,
  14. cleanUpCode: cleanUpCode,
  15. finalCleanUpCode: finalCleanUpCode,
  16. schemaHasRules: schemaHasRules,
  17. schemaHasRulesExcept: schemaHasRulesExcept,
  18. toQuotedString: toQuotedString,
  19. getPathExpr: getPathExpr,
  20. getPath: getPath,
  21. getData: getData,
  22. unescapeFragment: unescapeFragment,
  23. unescapeJsonPointer: unescapeJsonPointer,
  24. escapeFragment: escapeFragment,
  25. escapeJsonPointer: escapeJsonPointer
  26. };
  27. function copy(o, to) {
  28. to = to || {};
  29. for (var key in o) to[key] = o[key];
  30. return to;
  31. }
  32. function checkDataType(dataType, data, negate) {
  33. var EQUAL = negate ? ' !== ' : ' === '
  34. , AND = negate ? ' || ' : ' && '
  35. , OK = negate ? '!' : ''
  36. , NOT = negate ? '' : '!';
  37. switch (dataType) {
  38. case 'null': return data + EQUAL + 'null';
  39. case 'array': return OK + 'Array.isArray(' + data + ')';
  40. case 'object': return '(' + OK + data + AND +
  41. 'typeof ' + data + EQUAL + '"object"' + AND +
  42. NOT + 'Array.isArray(' + data + '))';
  43. case 'integer': return '(typeof ' + data + EQUAL + '"number"' + AND +
  44. NOT + '(' + data + ' % 1)' +
  45. AND + data + EQUAL + data + ')';
  46. default: return 'typeof ' + data + EQUAL + '"' + dataType + '"';
  47. }
  48. }
  49. function checkDataTypes(dataTypes, data) {
  50. switch (dataTypes.length) {
  51. case 1: return checkDataType(dataTypes[0], data, true);
  52. default:
  53. var code = '';
  54. var types = toHash(dataTypes);
  55. if (types.array && types.object) {
  56. code = types.null ? '(': '(!' + data + ' || ';
  57. code += 'typeof ' + data + ' !== "object")';
  58. delete types.null;
  59. delete types.array;
  60. delete types.object;
  61. }
  62. if (types.number) delete types.integer;
  63. for (var t in types)
  64. code += (code ? ' && ' : '' ) + checkDataType(t, data, true);
  65. return code;
  66. }
  67. }
  68. var COERCE_TO_TYPES = toHash([ 'string', 'number', 'integer', 'boolean', 'null' ]);
  69. function coerceToTypes(optionCoerceTypes, dataTypes) {
  70. if (Array.isArray(dataTypes)) {
  71. var types = [];
  72. for (var i=0; i<dataTypes.length; i++) {
  73. var t = dataTypes[i];
  74. if (COERCE_TO_TYPES[t]) types[types.length] = t;
  75. else if (optionCoerceTypes === 'array' && t === 'array') types[types.length] = t;
  76. }
  77. if (types.length) return types;
  78. } else if (COERCE_TO_TYPES[dataTypes]) {
  79. return [dataTypes];
  80. } else if (optionCoerceTypes === 'array' && dataTypes === 'array') {
  81. return ['array'];
  82. }
  83. }
  84. function toHash(arr) {
  85. var hash = {};
  86. for (var i=0; i<arr.length; i++) hash[arr[i]] = true;
  87. return hash;
  88. }
  89. var IDENTIFIER = /^[a-z$_][a-z$_0-9]*$/i;
  90. var SINGLE_QUOTE = /'|\\/g;
  91. function getProperty(key) {
  92. return typeof key == 'number'
  93. ? '[' + key + ']'
  94. : IDENTIFIER.test(key)
  95. ? '.' + key
  96. : "['" + escapeQuotes(key) + "']";
  97. }
  98. function escapeQuotes(str) {
  99. return str.replace(SINGLE_QUOTE, '\\$&')
  100. .replace(/\n/g, '\\n')
  101. .replace(/\r/g, '\\r')
  102. .replace(/\f/g, '\\f')
  103. .replace(/\t/g, '\\t');
  104. }
  105. function varOccurences(str, dataVar) {
  106. dataVar += '[^0-9]';
  107. var matches = str.match(new RegExp(dataVar, 'g'));
  108. return matches ? matches.length : 0;
  109. }
  110. function varReplace(str, dataVar, expr) {
  111. dataVar += '([^0-9])';
  112. expr = expr.replace(/\$/g, '$$$$');
  113. return str.replace(new RegExp(dataVar, 'g'), expr + '$1');
  114. }
  115. var EMPTY_ELSE = /else\s*{\s*}/g
  116. , EMPTY_IF_NO_ELSE = /if\s*\([^)]+\)\s*\{\s*\}(?!\s*else)/g
  117. , EMPTY_IF_WITH_ELSE = /if\s*\(([^)]+)\)\s*\{\s*\}\s*else(?!\s*if)/g;
  118. function cleanUpCode(out) {
  119. return out.replace(EMPTY_ELSE, '')
  120. .replace(EMPTY_IF_NO_ELSE, '')
  121. .replace(EMPTY_IF_WITH_ELSE, 'if (!($1))');
  122. }
  123. var ERRORS_REGEXP = /[^v.]errors/g
  124. , REMOVE_ERRORS = /var errors = 0;|var vErrors = null;|validate.errors = vErrors;/g
  125. , REMOVE_ERRORS_ASYNC = /var errors = 0;|var vErrors = null;/g
  126. , RETURN_VALID = 'return errors === 0;'
  127. , RETURN_TRUE = 'validate.errors = null; return true;'
  128. , RETURN_ASYNC = /if \(errors === 0\) return data;\s*else throw new ValidationError\(vErrors\);/
  129. , RETURN_DATA_ASYNC = 'return data;'
  130. , ROOTDATA_REGEXP = /[^A-Za-z_$]rootData[^A-Za-z0-9_$]/g
  131. , REMOVE_ROOTDATA = /if \(rootData === undefined\) rootData = data;/;
  132. function finalCleanUpCode(out, async) {
  133. var matches = out.match(ERRORS_REGEXP);
  134. if (matches && matches.length == 2) {
  135. out = async
  136. ? out.replace(REMOVE_ERRORS_ASYNC, '')
  137. .replace(RETURN_ASYNC, RETURN_DATA_ASYNC)
  138. : out.replace(REMOVE_ERRORS, '')
  139. .replace(RETURN_VALID, RETURN_TRUE);
  140. }
  141. matches = out.match(ROOTDATA_REGEXP);
  142. if (!matches || matches.length !== 3) return out;
  143. return out.replace(REMOVE_ROOTDATA, '');
  144. }
  145. function schemaHasRules(schema, rules) {
  146. if (typeof schema == 'boolean') return !schema;
  147. for (var key in schema) if (rules[key]) return true;
  148. }
  149. function schemaHasRulesExcept(schema, rules, exceptKeyword) {
  150. if (typeof schema == 'boolean') return !schema && exceptKeyword != 'not';
  151. for (var key in schema) if (key != exceptKeyword && rules[key]) return true;
  152. }
  153. function toQuotedString(str) {
  154. return '\'' + escapeQuotes(str) + '\'';
  155. }
  156. function getPathExpr(currentPath, expr, jsonPointers, isNumber) {
  157. var path = jsonPointers // false by default
  158. ? '\'/\' + ' + expr + (isNumber ? '' : '.replace(/~/g, \'~0\').replace(/\\//g, \'~1\')')
  159. : (isNumber ? '\'[\' + ' + expr + ' + \']\'' : '\'[\\\'\' + ' + expr + ' + \'\\\']\'');
  160. return joinPaths(currentPath, path);
  161. }
  162. function getPath(currentPath, prop, jsonPointers) {
  163. var path = jsonPointers // false by default
  164. ? toQuotedString('/' + escapeJsonPointer(prop))
  165. : toQuotedString(getProperty(prop));
  166. return joinPaths(currentPath, path);
  167. }
  168. var JSON_POINTER = /^\/(?:[^~]|~0|~1)*$/;
  169. var RELATIVE_JSON_POINTER = /^([0-9]+)(#|\/(?:[^~]|~0|~1)*)?$/;
  170. function getData($data, lvl, paths) {
  171. var up, jsonPointer, data, matches;
  172. if ($data === '') return 'rootData';
  173. if ($data[0] == '/') {
  174. if (!JSON_POINTER.test($data)) throw new Error('Invalid JSON-pointer: ' + $data);
  175. jsonPointer = $data;
  176. data = 'rootData';
  177. } else {
  178. matches = $data.match(RELATIVE_JSON_POINTER);
  179. if (!matches) throw new Error('Invalid JSON-pointer: ' + $data);
  180. up = +matches[1];
  181. jsonPointer = matches[2];
  182. if (jsonPointer == '#') {
  183. if (up >= lvl) throw new Error('Cannot access property/index ' + up + ' levels up, current level is ' + lvl);
  184. return paths[lvl - up];
  185. }
  186. if (up > lvl) throw new Error('Cannot access data ' + up + ' levels up, current level is ' + lvl);
  187. data = 'data' + ((lvl - up) || '');
  188. if (!jsonPointer) return data;
  189. }
  190. var expr = data;
  191. var segments = jsonPointer.split('/');
  192. for (var i=0; i<segments.length; i++) {
  193. var segment = segments[i];
  194. if (segment) {
  195. data += getProperty(unescapeJsonPointer(segment));
  196. expr += ' && ' + data;
  197. }
  198. }
  199. return expr;
  200. }
  201. function joinPaths (a, b) {
  202. if (a == '""') return b;
  203. return (a + ' + ' + b).replace(/' \+ '/g, '');
  204. }
  205. function unescapeFragment(str) {
  206. return unescapeJsonPointer(decodeURIComponent(str));
  207. }
  208. function escapeFragment(str) {
  209. return encodeURIComponent(escapeJsonPointer(str));
  210. }
  211. function escapeJsonPointer(str) {
  212. return str.replace(/~/g, '~0').replace(/\//g, '~1');
  213. }
  214. function unescapeJsonPointer(str) {
  215. return str.replace(/~1/g, '/').replace(/~0/g, '~');
  216. }