wishthis/node_modules/yamljs/lib/Utils.js

298 lines
8.1 KiB
JavaScript
Raw Normal View History

2022-01-21 08:28:41 +00:00
// Generated by CoffeeScript 1.12.4
var Pattern, Utils,
hasProp = {}.hasOwnProperty;
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';
}
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' || (value instanceof Array && value.length === 0) || this.isEmptyObject(value);
};
Utils.isEmptyObject = function(value) {
var k;
return value instanceof Object && ((function() {
var results;
results = [];
for (k in value) {
if (!hasProp.call(value, k)) continue;
results.push(k);
}
return results;
})()).length === 0;
};
Utils.subStrCount = function(string, subString, start, length) {
var c, i, j, len, ref, sublen;
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 = j = 0, ref = len; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
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, j, len1, name, ref, req, xhr;
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 (j = 0, len1 = ref.length; j < len1; j++) {
name = ref[j];
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(String(data));
}
});
} else {
data = fs.readFileSync(path);
if (data != null) {
return String(data);
}
return null;
}
}
};
return Utils;
})();
module.exports = Utils;