react / react-0.13.3 / node_modules / coffee-react / node_modules / coffee-react-transform / lib / helpers.js
80758 views// Generated by CoffeeScript 1.9.11var extend, flatten, last, ref, ref1, repeat, syntaxErrorToString;23exports.starts = function(string, literal, start) {4return literal === string.substr(start, literal.length);5};67exports.ends = function(string, literal, back) {8var len;9len = literal.length;10return literal === string.substr(string.length - len - (back || 0), len);11};1213exports.repeat = repeat = function(str, n) {14var res;15res = '';16while (n > 0) {17if (n & 1) {18res += str;19}20n >>>= 1;21str += str;22}23return res;24};2526exports.compact = function(array) {27var i, item, len1, results;28results = [];29for (i = 0, len1 = array.length; i < len1; i++) {30item = array[i];31if (item) {32results.push(item);33}34}35return results;36};3738exports.count = function(string, substr) {39var num, pos;40num = pos = 0;41if (!substr.length) {42return 1 / 0;43}44while (pos = 1 + string.indexOf(substr, pos)) {45num++;46}47return num;48};4950exports.merge = function(options, overrides) {51return extend(extend({}, options), overrides);52};5354extend = exports.extend = function(object, properties) {55var key, val;56for (key in properties) {57val = properties[key];58object[key] = val;59}60return object;61};6263exports.flatten = flatten = function(array) {64var element, flattened, i, len1;65flattened = [];66for (i = 0, len1 = array.length; i < len1; i++) {67element = array[i];68if (element instanceof Array) {69flattened = flattened.concat(flatten(element));70} else {71flattened.push(element);72}73}74return flattened;75};7677exports.del = function(obj, key) {78var val;79val = obj[key];80delete obj[key];81return val;82};8384exports.last = last = function(array, back) {85return array[array.length - (back || 0) - 1];86};8788exports.some = (ref = Array.prototype.some) != null ? ref : function(fn) {89var e, i, len1;90for (i = 0, len1 = this.length; i < len1; i++) {91e = this[i];92if (fn(e)) {93return true;94}95}96return false;97};9899exports.find = (ref1 = Array.prototype.find) != null ? ref1 : function(fn) {100var e, i, len1;101for (i = 0, len1 = this.length; i < len1; i++) {102e = this[i];103if (fn(e)) {104return e;105}106}107};108109exports.throwSyntaxError = function(message, location) {110var error;111error = new SyntaxError(message);112error.location = location;113error.toString = syntaxErrorToString;114error.stack = error.toString();115throw error;116};117118exports.updateSyntaxError = function(error, code, filename) {119if (error.toString === syntaxErrorToString) {120error.code || (error.code = code);121error.filename || (error.filename = filename);122error.stack = error.toString();123}124return error;125};126127syntaxErrorToString = function() {128var codeLine, colorize, colorsEnabled, end, filename, first_column, first_line, last_column, last_line, marker, ref2, ref3, start;129if (!(this.code && this.location)) {130return Error.prototype.toString.call(this);131}132ref2 = this.location, first_line = ref2.first_line, first_column = ref2.first_column, last_line = ref2.last_line, last_column = ref2.last_column;133if (last_line == null) {134last_line = first_line;135}136if (last_column == null) {137last_column = first_column;138}139filename = this.filename || '[stdin]';140codeLine = this.code.split('\n')[first_line];141start = first_column;142end = first_line === last_line ? last_column + 1 : codeLine.length;143marker = repeat(' ', start) + repeat('^', end - start);144if (typeof process !== "undefined" && process !== null) {145colorsEnabled = process.stdout.isTTY && !process.env.NODE_DISABLE_COLORS;146}147if ((ref3 = this.colorful) != null ? ref3 : colorsEnabled) {148colorize = function(str) {149return "\x1B[1;31m" + str + "\x1B[0m";150};151codeLine = codeLine.slice(0, start) + colorize(codeLine.slice(start, end)) + codeLine.slice(end);152marker = colorize(marker);153}154return filename + ":" + (first_line + 1) + ":" + (first_column + 1) + ": error: " + this.message + "\n" + codeLine + "\n" + marker;155};156157exports.nameWhitespaceCharacter = function(string) {158switch (string) {159case ' ':160return 'space';161case '\n':162return 'newline';163case '\r':164return 'carriage return';165case '\t':166return 'tab';167default:168return string;169}170};171172exports.invertLiterate = function(code) {173var line, lines, maybe_code;174maybe_code = true;175lines = (function() {176var i, len1, ref2, results;177ref2 = code.split('\n');178results = [];179for (i = 0, len1 = ref2.length; i < len1; i++) {180line = ref2[i];181if (maybe_code && /^([ ]{4}|[ ]{0,3}\t)/.test(line)) {182results.push(line);183} else if (maybe_code = /^\s*$/.test(line)) {184results.push(line);185} else {186results.push('# ' + line);187}188}189return results;190})();191return lines.join('\n');192};193194195