PageRenderTime 23ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/testacular/node_modules/lodash/build/pre-compile.js

https://bitbucket.org/websurgeon/cordova-test-on-device
JavaScript | 398 lines | 357 code | 17 blank | 24 comment | 3 complexity | 32fc325cce29834acb08cdc3dca28aa5 MD5 | raw file
  1. #!/usr/bin/env node
  2. ;(function() {
  3. 'use strict';
  4. /** The Node filesystem module */
  5. var fs = require('fs');
  6. /** Used to minify variables embedded in compiled strings */
  7. var compiledVars = [
  8. 'argsIndex',
  9. 'argsLength',
  10. 'callback',
  11. 'collection',
  12. 'createCallback',
  13. 'ctor',
  14. 'guard',
  15. 'hasOwnProperty',
  16. 'index',
  17. 'isArguments',
  18. 'isString',
  19. 'iteratee',
  20. 'length',
  21. 'nativeKeys',
  22. 'object',
  23. 'objectTypes',
  24. 'ownIndex',
  25. 'ownProps',
  26. 'propertyIsEnumerable',
  27. 'result',
  28. 'skipProto',
  29. 'source',
  30. 'thisArg'
  31. ];
  32. /** Used to minify `compileIterator` option properties */
  33. var iteratorOptions = [
  34. 'args',
  35. 'arrayLoop',
  36. 'bottom',
  37. 'firstArg',
  38. 'hasDontEnumBug',
  39. 'isKeysFast',
  40. 'objectLoop',
  41. 'nonEnumArgs',
  42. 'noCharByIndex',
  43. 'shadowed',
  44. 'top',
  45. 'useHas',
  46. 'useStrict'
  47. ];
  48. /** Used to minify variables and string values to a single character */
  49. var minNames = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
  50. minNames.push.apply(minNames, minNames.map(function(value) {
  51. return value + value;
  52. }));
  53. /** Used to protect the specified properties from getting minified */
  54. var propWhitelist = [
  55. '_',
  56. '__wrapped__',
  57. 'after',
  58. 'all',
  59. 'amd',
  60. 'any',
  61. 'assign',
  62. 'attachEvent',
  63. 'bind',
  64. 'bindAll',
  65. 'bindKey',
  66. 'clone',
  67. 'cloneDeep',
  68. 'collect',
  69. 'compact',
  70. 'compose',
  71. 'contains',
  72. 'countBy',
  73. 'criteria',
  74. 'debounce',
  75. 'defaults',
  76. 'defer',
  77. 'delay',
  78. 'detect',
  79. 'difference',
  80. 'drop',
  81. 'each',
  82. 'environment',
  83. 'escape',
  84. 'evaluate',
  85. 'every',
  86. 'exports',
  87. 'extend',
  88. 'filter',
  89. 'find',
  90. 'first',
  91. 'flatten',
  92. 'foldl',
  93. 'foldr',
  94. 'forEach',
  95. 'forIn',
  96. 'forOwn',
  97. 'functions',
  98. 'global',
  99. 'groupBy',
  100. 'has',
  101. 'head',
  102. 'identity',
  103. 'include',
  104. 'index',
  105. 'indexOf',
  106. 'initial',
  107. 'inject',
  108. 'interpolate',
  109. 'intersection',
  110. 'invert',
  111. 'invoke',
  112. 'isArguments',
  113. 'isArray',
  114. 'isBoolean',
  115. 'isDate',
  116. 'isElement',
  117. 'isEmpty',
  118. 'isEqual',
  119. 'isEqual',
  120. 'isFinite',
  121. 'isFinite',
  122. 'isFunction',
  123. 'isNaN',
  124. 'isNull',
  125. 'isNumber',
  126. 'isObject',
  127. 'isPlainObject',
  128. 'isRegExp',
  129. 'isString',
  130. 'isUndefined',
  131. 'keys',
  132. 'last',
  133. 'lastIndexOf',
  134. 'map',
  135. 'max',
  136. 'memoize',
  137. 'merge',
  138. 'methods',
  139. 'min',
  140. 'mixin',
  141. 'noConflict',
  142. 'object',
  143. 'omit',
  144. 'once',
  145. 'opera',
  146. 'pairs',
  147. 'partial',
  148. 'pick',
  149. 'pluck',
  150. 'random',
  151. 'range',
  152. 'reduce',
  153. 'reduceRight',
  154. 'reject',
  155. 'rest',
  156. 'result',
  157. 'select',
  158. 'shuffle',
  159. 'size',
  160. 'some',
  161. 'sortBy',
  162. 'sortedIndex',
  163. 'source',
  164. 'tail',
  165. 'take',
  166. 'tap',
  167. 'template',
  168. 'templateSettings',
  169. 'throttle',
  170. 'times',
  171. 'toArray',
  172. 'unescape',
  173. 'union',
  174. 'uniq',
  175. 'unique',
  176. 'uniqueId',
  177. 'value',
  178. 'values',
  179. 'variable',
  180. 'VERSION',
  181. 'where',
  182. 'without',
  183. 'wrap',
  184. 'zip',
  185. // properties used by the `backbone` and `underscore` builds
  186. '__chain__',
  187. 'chain',
  188. // properties used by underscore.js
  189. '_chain',
  190. '_wrapped'
  191. ];
  192. /*--------------------------------------------------------------------------*/
  193. /**
  194. * Pre-process a given Lo-Dash `source`, preparing it for minification.
  195. *
  196. * @param {String} [source=''] The source to process.
  197. * @param {Object} [options={}] The options object.
  198. * @returns {String} Returns the processed source.
  199. */
  200. function preprocess(source, options) {
  201. source || (source = '');
  202. options || (options = {});
  203. // remove unrecognized JSDoc tags so the Closure Compiler won't complain
  204. source = source.replace(/@(?:alias|category)\b.*/g, '');
  205. if (options.isTemplate) {
  206. return source;
  207. }
  208. // remove copyright/license header to add later in post-compile.js
  209. source = source.replace(/\/\*![\s\S]+?\*\//, '');
  210. // add brackets to whitelisted properties so the Closure Compiler won't mung them
  211. // http://code.google.com/closure/compiler/docs/api-tutorial3.html#export
  212. source = source.replace(RegExp('\\.(' + propWhitelist.join('|') + ')\\b', 'g'), function(match, prop) {
  213. return "['" + prop.replace(/['\n\r\t]/g, '\\$&') + "']";
  214. });
  215. // remove brackets from `_.escape()` in `_.template`
  216. source = source.replace(/__e *= *_\['escape']/g, '__e=_.escape');
  217. // remove brackets from `_.escape()` in underscore.js `_.template`
  218. source = source.replace(/_\['escape'\]\(__t'/g, '_.escape(__t');
  219. // remove brackets from `collection.indexOf` in `_.contains`
  220. source = source.replace("collection['indexOf'](target)", 'collection.indexOf(target)');
  221. // remove brackets from `result[length].value` in `_.sortBy`
  222. source = source.replace("result[length]['value']", 'result[length].value');
  223. // remove whitespace from string literals
  224. source = source.replace(/^([ "'\w]+:)? *"(?:(?=(\\?))\2.)*?"|'(?:(?=(\\?))\3.)*?'/gm, function(string, captured) {
  225. // remove object literal property name
  226. if (/:$/.test(captured)) {
  227. string = string.slice(captured.length);
  228. }
  229. // avoids removing the '\n' of the `stringEscapes` object
  230. string = string.replace(/\[object |delete |else |function | in |return\s+[\w"']|throw |typeof |use strict|var |@ |(["'])\\n\1|\\\\n|\\n|\s+/g, function(match) {
  231. return match == false || match == '\\n' ? '' : match;
  232. });
  233. // prepend object literal property name
  234. return (captured || '') + string;
  235. });
  236. // add newline to `+"__p+='"` in underscore.js `_.template`
  237. source = source.replace(/\+"__p\+='"/g, '+"\\n__p+=\'"');
  238. // add newline to `body + '}'` in `createFunction`
  239. source = source.replace(/body *\+ *'}'/, 'body+"\\n}"');
  240. // remove whitespace from `_.template` related regexes
  241. source = source.replace(/(?:reEmptyString\w+|reInsertVariable) *=.+/g, function(match) {
  242. return match.replace(/ |\\n/g, '');
  243. });
  244. // remove newline from double-quoted strings in `_.template`
  245. source = source
  246. .replace('"__p += \'"', '"__p+=\'"')
  247. .replace('"\';\n"', '"\';"')
  248. // remove `useSourceURL` variable
  249. source = source.replace(/(?:\n +\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)?\n *try *\{(?:\s*\/\/.*)*\n *var useSourceURL[\s\S]+?catch[^}]+}\n/, '');
  250. // remove debug sourceURL use in `_.template`
  251. source = source.replace(/(?:\s*\/\/.*\n)* *var sourceURL[^;]+;|\+ *sourceURL/g, '');
  252. // minify internal properties used by 'compareAscending' and `_.sortBy`
  253. (function() {
  254. var properties = ['criteria', 'index', 'value'],
  255. snippets = source.match(/( +)function (?:compareAscending|sortBy)\b[\s\S]+?\n\1}/g);
  256. if (!snippets) {
  257. return;
  258. }
  259. snippets.forEach(function(snippet) {
  260. var modified = snippet;
  261. // minify properties
  262. properties.forEach(function(property, index) {
  263. var reBracketProp = RegExp("\\['(" + property + ")'\\]", 'g'),
  264. reDotProp = RegExp('\\.' + property + '\\b', 'g'),
  265. rePropColon = RegExp("([^?\\s])\\s*([\"'])?\\b" + property + "\\2 *:", 'g');
  266. modified = modified
  267. .replace(reBracketProp, "['" + minNames[index] + "']")
  268. .replace(reDotProp, "['" + minNames[index] + "']")
  269. .replace(rePropColon, "$1'" + minNames[index] + "':");
  270. });
  271. // replace with modified snippet
  272. source = source.replace(snippet, modified);
  273. });
  274. }());
  275. // minify all compilable snippets
  276. var snippets = source.match(
  277. RegExp([
  278. // match the `iteratorTemplate`
  279. '( +)var iteratorTemplate\\b[\\s\\S]+?\\n\\1}',
  280. // match methods created by `createIterator` calls
  281. 'createIterator\\((?:{|[a-zA-Z]+)[\\s\\S]+?\\);\\n',
  282. // match variables storing `createIterator` options
  283. '( +)var [a-zA-Z]+IteratorOptions\\b[\\s\\S]+?\\n\\2}',
  284. // match the the `createIterator` function
  285. '( +)function createIterator\\b[\\s\\S]+?\\n\\3}'
  286. ].join('|'), 'g')
  287. );
  288. // exit early if no compilable snippets
  289. if (!snippets) {
  290. return source;
  291. }
  292. snippets.forEach(function(snippet, index) {
  293. var isCreateIterator = /function createIterator\b/.test(snippet),
  294. isIteratorTemplate = /var iteratorTemplate\b/.test(snippet),
  295. modified = snippet;
  296. // add brackets to iterator option properties so the Closure Compiler won't mung them
  297. modified = modified.replace(RegExp('\\.(' + iteratorOptions.join('|') + ')\\b', 'g'), function(match, prop) {
  298. return "['" + prop.replace(/['\n\r\t]/g, '\\$&') + "']";
  299. });
  300. if (isCreateIterator) {
  301. // replace with modified snippet early and clip snippet to the `factory`
  302. // call so other arguments aren't minified
  303. source = source.replace(snippet, modified);
  304. snippet = modified = modified.replace(/factory\([\s\S]+$/, '');
  305. }
  306. // minify snippet variables / arguments
  307. compiledVars.forEach(function(variable, index) {
  308. // ensure properties in compiled strings aren't minified
  309. modified = modified.replace(RegExp('([^.]\\b)' + variable + '\\b(?!\' *[\\]:])', 'g'), '$1' + minNames[index]);
  310. // correct `typeof` values
  311. if (/^(?:boolean|function|object|number|string|undefined)$/.test(variable)) {
  312. modified = modified.replace(RegExp("(typeof [^']+')" + minNames[index] + "'", 'g'), '$1' + variable + "'");
  313. }
  314. });
  315. // minify `createIterator` option property names
  316. iteratorOptions.forEach(function(property, index) {
  317. if (isIteratorTemplate) {
  318. // minify property names as interpolated template variables
  319. modified = modified.replace(RegExp('\\b' + property + '\\b', 'g'), minNames[index]);
  320. }
  321. else {
  322. // minify property name strings
  323. modified = modified.replace(RegExp("'" + property + "'", 'g'), "'" + minNames[index] + "'");
  324. // minify property names in accessors
  325. if (isCreateIterator) {
  326. modified = modified.replace(RegExp('\\.' + property + '\\b' , 'g'), '.' + minNames[index]);
  327. }
  328. }
  329. });
  330. // replace with modified snippet
  331. source = source.replace(snippet, modified);
  332. });
  333. return source;
  334. }
  335. /*--------------------------------------------------------------------------*/
  336. // expose `preprocess`
  337. if (module != require.main) {
  338. module.exports = preprocess;
  339. }
  340. else {
  341. // read the Lo-Dash source file from the first argument if the script
  342. // was invoked directly (e.g. `node pre-compile.js source.js`) and write to
  343. // the same file
  344. (function() {
  345. var options = process.argv;
  346. if (options.length < 3) {
  347. return;
  348. }
  349. var filePath = options[options.length - 1],
  350. isTemplate = options.indexOf('-t') > -1 || options.indexOf('--template') > -1,
  351. source = fs.readFileSync(filePath, 'utf8');
  352. fs.writeFileSync(filePath, preprocess(source, {
  353. 'isTemplate': isTemplate
  354. }), 'utf8');
  355. }());
  356. }
  357. }());