summaryrefslogtreecommitdiff
path: root/lib/Utils.js
diff options
context:
space:
mode:
authorJeremy Faivre <jeremy.faivre@gmail.com>2014-07-22 10:19:12 +0200
committerJeremy Faivre <jeremy.faivre@gmail.com>2014-07-22 10:21:17 +0200
commitc6cbcf0326723c5f0013490d1fea489694790775 (patch)
treefa735c3ee627b64bd546165aed093d1f6cfce055 /lib/Utils.js
parentc5a6a8601d31508bb3e5880b310b2918834e89c9 (diff)
downloadyamljs-c6cbcf0326723c5f0013490d1fea489694790775.tar.gz
Improve performances (more than 2 times faster), rewrite in coffee-script, fix various bugs.
This is the first pass of rewrite of the YAML library (in progress). The focus was on improving code readability, make it well-documented and fix most of the current issues. It didn't focus on the performances yet, even though they are better than the previous version. The next big step (not included in this first pass in progress) will focus on removing most RegExps because they are time consuming and they make the code working on a "line by line" basis. The code will move to a "character by character" parsing, allowing us to deeply improve performances, and add new features from the 1.2 Yaml spec.
Diffstat (limited to 'lib/Utils.js')
-rw-r--r--lib/Utils.js284
1 files changed, 284 insertions, 0 deletions
diff --git a/lib/Utils.js b/lib/Utils.js
new file mode 100644
index 0000000..8d81f06
--- /dev/null
+++ b/lib/Utils.js
@@ -0,0 +1,284 @@
+// Generated by CoffeeScript 1.7.1
+var Pattern, Utils;
+
+Pattern = require('./Pattern');
+
+Utils = (function() {
+ function Utils() {}
+
+ Utils.REGEX_LEFT_TRIM_BY_CHAR = {};
+
+ Utils.REGEX_RIGHT_TRIM_BY_CHAR = {};
+
+ Utils.REGEX_SPACES = /\s+/g;
+
+ Utils.REGEX_DIGITS = /^\d+$/;
+
+ Utils.REGEX_OCTAL = /[^0-7]/gi;
+
+ Utils.REGEX_HEXADECIMAL = /[^a-f0-9]/gi;
+
+ Utils.PATTERN_DATE = new Pattern('^' + '(?<year>[0-9][0-9][0-9][0-9])' + '-(?<month>[0-9][0-9]?)' + '-(?<day>[0-9][0-9]?)' + '(?:(?:[Tt]|[ \t]+)' + '(?<hour>[0-9][0-9]?)' + ':(?<minute>[0-9][0-9])' + ':(?<second>[0-9][0-9])' + '(?:\.(?<fraction>[0-9]*))?' + '(?:[ \t]*(?<tz>Z|(?<tz_sign>[-+])(?<tz_hour>[0-9][0-9]?)' + '(?::(?<tz_minute>[0-9][0-9]))?))?)?' + '$', 'i');
+
+ Utils.LOCAL_TIMEZONE_OFFSET = new Date().getTimezoneOffset() * 60 * 1000;
+
+ Utils.trim = function(str, char) {
+ var regexLeft, regexRight;
+ if (char == null) {
+ char = '\\s';
+ }
+ return str.trim();
+ regexLeft = this.REGEX_LEFT_TRIM_BY_CHAR[char];
+ if (regexLeft == null) {
+ this.REGEX_LEFT_TRIM_BY_CHAR[char] = regexLeft = new RegExp('^' + char + '' + char + '*');
+ }
+ regexLeft.lastIndex = 0;
+ regexRight = this.REGEX_RIGHT_TRIM_BY_CHAR[char];
+ if (regexRight == null) {
+ this.REGEX_RIGHT_TRIM_BY_CHAR[char] = regexRight = new RegExp(char + '' + char + '*$');
+ }
+ regexRight.lastIndex = 0;
+ return str.replace(regexLeft, '').replace(regexRight, '');
+ };
+
+ Utils.ltrim = function(str, char) {
+ var regexLeft;
+ if (char == null) {
+ char = '\\s';
+ }
+ regexLeft = this.REGEX_LEFT_TRIM_BY_CHAR[char];
+ if (regexLeft == null) {
+ this.REGEX_LEFT_TRIM_BY_CHAR[char] = regexLeft = new RegExp('^' + char + '' + char + '*');
+ }
+ regexLeft.lastIndex = 0;
+ return str.replace(regexLeft, '');
+ };
+
+ Utils.rtrim = function(str, char) {
+ var regexRight;
+ if (char == null) {
+ char = '\\s';
+ }
+ regexRight = this.REGEX_RIGHT_TRIM_BY_CHAR[char];
+ if (regexRight == null) {
+ this.REGEX_RIGHT_TRIM_BY_CHAR[char] = regexRight = new RegExp(char + '' + char + '*$');
+ }
+ regexRight.lastIndex = 0;
+ return str.replace(regexRight, '');
+ };
+
+ Utils.isEmpty = function(value) {
+ return !value || value === '' || value === '0';
+ };
+
+ Utils.subStrCount = function(string, subString, start, length) {
+ var c, i, len, sublen, _i;
+ c = 0;
+ string = '' + string;
+ subString = '' + subString;
+ if (start != null) {
+ string = string.slice(start);
+ }
+ if (length != null) {
+ string = string.slice(0, length);
+ }
+ len = string.length;
+ sublen = subString.length;
+ for (i = _i = 0; 0 <= len ? _i < len : _i > len; i = 0 <= len ? ++_i : --_i) {
+ if (subString === string.slice(i, sublen)) {
+ c++;
+ i += sublen - 1;
+ }
+ }
+ return c;
+ };
+
+ Utils.isDigits = function(input) {
+ this.REGEX_DIGITS.lastIndex = 0;
+ return this.REGEX_DIGITS.test(input);
+ };
+
+ Utils.octDec = function(input) {
+ this.REGEX_OCTAL.lastIndex = 0;
+ return parseInt((input + '').replace(this.REGEX_OCTAL, ''), 8);
+ };
+
+ Utils.hexDec = function(input) {
+ this.REGEX_HEXADECIMAL.lastIndex = 0;
+ input = this.trim(input);
+ if ((input + '').slice(0, 2) === '0x') {
+ input = (input + '').slice(2);
+ }
+ return parseInt((input + '').replace(this.REGEX_HEXADECIMAL, ''), 16);
+ };
+
+ Utils.utf8chr = function(c) {
+ var ch;
+ ch = String.fromCharCode;
+ if (0x80 > (c %= 0x200000)) {
+ return ch(c);
+ }
+ if (0x800 > c) {
+ return ch(0xC0 | c >> 6) + ch(0x80 | c & 0x3F);
+ }
+ if (0x10000 > c) {
+ return ch(0xE0 | c >> 12) + ch(0x80 | c >> 6 & 0x3F) + ch(0x80 | c & 0x3F);
+ }
+ return ch(0xF0 | c >> 18) + ch(0x80 | c >> 12 & 0x3F) + ch(0x80 | c >> 6 & 0x3F) + ch(0x80 | c & 0x3F);
+ };
+
+ Utils.parseBoolean = function(input, strict) {
+ var lowerInput;
+ if (strict == null) {
+ strict = true;
+ }
+ if (typeof input === 'string') {
+ lowerInput = input.toLowerCase();
+ if (!strict) {
+ if (lowerInput === 'no') {
+ return false;
+ }
+ }
+ if (lowerInput === '0') {
+ return false;
+ }
+ if (lowerInput === 'false') {
+ return false;
+ }
+ if (lowerInput === '') {
+ return false;
+ }
+ return true;
+ }
+ return !!input;
+ };
+
+ Utils.isNumeric = function(input) {
+ this.REGEX_SPACES.lastIndex = 0;
+ return typeof input === 'number' || typeof input === 'string' && !isNaN(input) && input.replace(this.REGEX_SPACES, '') !== '';
+ };
+
+ Utils.stringToDate = function(str) {
+ var date, day, fraction, hour, info, minute, month, second, tz_hour, tz_minute, tz_offset, year;
+ if (!(str != null ? str.length : void 0)) {
+ return null;
+ }
+ info = this.PATTERN_DATE.exec(str);
+ if (!info) {
+ return null;
+ }
+ year = parseInt(info.year, 10);
+ month = parseInt(info.month, 10) - 1;
+ day = parseInt(info.day, 10);
+ if (info.hour == null) {
+ date = new Date(Date.UTC(year, month, day));
+ return date;
+ }
+ hour = parseInt(info.hour, 10);
+ minute = parseInt(info.minute, 10);
+ second = parseInt(info.second, 10);
+ if (info.fraction != null) {
+ fraction = info.fraction.slice(0, 3);
+ while (fraction.length < 3) {
+ fraction += '0';
+ }
+ fraction = parseInt(fraction, 10);
+ } else {
+ fraction = 0;
+ }
+ if (info.tz != null) {
+ tz_hour = parseInt(info.tz_hour, 10);
+ if (info.tz_minute != null) {
+ tz_minute = parseInt(info.tz_minute, 10);
+ } else {
+ tz_minute = 0;
+ }
+ tz_offset = (tz_hour * 60 + tz_minute) * 60000;
+ if ('-' === info.tz_sign) {
+ tz_offset *= -1;
+ }
+ }
+ date = new Date(Date.UTC(year, month, day, hour, minute, second, fraction));
+ if (tz_offset) {
+ date.setTime(date.getTime() + tz_offset);
+ }
+ return date;
+ };
+
+ Utils.strRepeat = function(str, number) {
+ var i, res;
+ res = '';
+ i = 0;
+ while (i < number) {
+ res += str;
+ i++;
+ }
+ return res;
+ };
+
+ Utils.getStringFromFile = function(path, callback) {
+ var data, fs, name, req, xhr, _i, _len, _ref;
+ if (callback == null) {
+ callback = null;
+ }
+ xhr = null;
+ if (typeof window !== "undefined" && window !== null) {
+ if (window.XMLHttpRequest) {
+ xhr = new XMLHttpRequest();
+ } else if (window.ActiveXObject) {
+ _ref = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ name = _ref[_i];
+ try {
+ xhr = new ActiveXObject(name);
+ } catch (_error) {}
+ }
+ }
+ }
+ if (xhr != null) {
+ if (callback != null) {
+ xhr.onreadystatechange = function() {
+ if (xhr.readyState === 4) {
+ if (xhr.status === 200 || xhr.status === 0) {
+ return callback(xhr.responseText);
+ } else {
+ return callback(null);
+ }
+ }
+ };
+ xhr.open('GET', path, true);
+ return xhr.send(null);
+ } else {
+ xhr.open('GET', path, false);
+ xhr.send(null);
+ if (xhr.status === 200 || xhr.status === 0) {
+ return xhr.responseText;
+ }
+ return null;
+ }
+ } else {
+ req = require;
+ fs = req('fs');
+ if (callback != null) {
+ return fs.readFile(path, function(err, data) {
+ if (err) {
+ return callback(null);
+ } else {
+ return callback(data);
+ }
+ });
+ } else {
+ data = fs.readFileSync(path);
+ if (data != null) {
+ return '' + data;
+ }
+ return null;
+ }
+ }
+ };
+
+ return Utils;
+
+})();
+
+module.exports = Utils;