welsonjs/lib/std.js

196 lines
6.0 KiB
JavaScript
Raw Normal View History

2020-06-28 14:22:57 +00:00
//////////////////////////////////////////////////////////////////////////////////
//
// std.js
//
// Common routines. Defines LIB object which contains the API, as well as
// a global DBG function.
//
// References
// * https://github.com/redskyit/wsh-appjs
// * https://github.com/JSman-/JS-Framework
//
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// Global APIs
/////////////////////////////////////////////////////////////////////////////////
2020-06-28 16:37:15 +00:00
if (!('GetResource' in Function.prototype)) {
Function.prototype.GetResource = function(ResourceName) {
2020-07-03 09:15:23 +00:00
if (!this.Resources) {
var UnNamedResourceIndex = 0,
_this = this;
2020-06-28 16:37:15 +00:00
this.Resources = {};
2020-07-03 09:15:23 +00:00
function f(match, resType, Content) {
_this.Resources[(resType == "[[") ? UnNamedResourceIndex++ : resType.slice(1, -1)] = Content;
2020-06-28 16:37:15 +00:00
}
this.toString().replace(/\/\*(\[(?:[^\[]+)?\[)((?:[\r\n]|.)*?)\]\]\*\//gi, f);
2020-06-28 14:22:57 +00:00
}
2020-07-03 09:15:23 +00:00
2020-06-28 16:37:15 +00:00
return this.Resources[ResourceName];
2020-06-28 14:22:57 +00:00
}
}
2020-06-28 16:37:15 +00:00
global.GetResource = function(ResourceName) {
2020-06-28 14:22:57 +00:00
return arguments.callee.caller.GetResource(ResourceName);
}
global.sleep = function(ms, callback) {
2020-07-03 09:15:23 +00:00
if (typeof(WScript) !== "undefined") {
2020-06-28 16:37:15 +00:00
WScript.Sleep(ms);
2020-07-03 09:15:23 +00:00
if (typeof(callback) === "function") {
2020-06-28 16:37:15 +00:00
callback();
}
} else {
2020-07-03 09:15:23 +00:00
if (typeof(callback) === "function") {
2020-06-28 16:37:15 +00:00
setTimeout(callback, ms);
}
2020-06-28 14:22:57 +00:00
}
}
global.exit = function() {
2020-07-03 09:15:23 +00:00
if (typeof(WScript) !== "undefined") {
2020-06-28 16:37:15 +00:00
WScript.Quit();
2020-07-03 09:15:23 +00:00
} else if (typeof(window) !== "undefined") {
2020-06-28 16:37:15 +00:00
window.close();
}
2020-06-28 14:22:57 +00:00
}
if (!('toArray' in Enumerator.prototype)) {
Enumerator.prototype.toArray = function() {
var Result = [];
2020-07-03 09:15:23 +00:00
for (; !this.atEnd(); this.moveNext())
Result.push(this.item())
2020-06-28 14:22:57 +00:00
return Result;
}
}
if (!('forEach' in Enumerator.prototype)) {
2020-07-03 09:15:23 +00:00
Enumerator.prototype.forEach = function(action, that /*opt*/ ) {
2020-06-28 14:22:57 +00:00
this.toArray().forEach(action, that);
}
}
// Add ECMA262-5 method binding if not supported natively
if (!('bind' in Function.prototype)) {
Function.prototype.bind = function(owner) {
2020-07-03 09:15:23 +00:00
var that = this;
if (arguments.length <= 1) {
2020-06-28 14:22:57 +00:00
return function() {
return that.apply(owner, arguments);
};
} else {
2020-07-03 09:15:23 +00:00
var args = Array.prototype.slice.call(arguments, 1);
2020-06-28 14:22:57 +00:00
return function() {
2020-07-03 09:15:23 +00:00
return that.apply(owner, arguments.length === 0 ? args : args.concat(Array.prototype.slice.call(arguments)));
2020-06-28 14:22:57 +00:00
};
}
};
}
// Add ECMA262-5 string trim if not supported natively
//
if (!('trim' in String.prototype)) {
String.prototype.trim = function() {
return this.replace(/^\s+/, '').replace(/\s+$/, '');
};
}
// Add ECMA262-5 Array methods if not supported natively
//
if (!('indexOf' in Array.prototype)) {
2020-07-03 09:15:23 +00:00
Array.prototype.indexOf = function(find, i /*opt*/ ) {
if (i === undefined) i = 0;
if (i < 0) i += this.length;
if (i < 0) i = 0;
for (var n = this.length; i < n; i++)
if (this[i] === find) return i;
//if (i in this && this[i]===find) return i;
2020-06-28 14:22:57 +00:00
return -1;
};
}
if (!('lastIndexOf' in Array.prototype)) {
2020-07-03 09:15:23 +00:00
Array.prototype.lastIndexOf = function(find, i /*opt*/ ) {
if (i === undefined) i = this.length - 1;
if (i < 0) i += this.length;
if (i > this.length - 1) i = this.length - 1;
for (i++; i-- > 0;) /* i++ because from-argument is sadly inclusive */
// if (i in this && this[i]===find)
if (this[i] === find)
return i;
2020-06-28 14:22:57 +00:00
return -1;
};
}
if (!('forEach' in Array.prototype)) {
2020-07-03 09:15:23 +00:00
Array.prototype.forEach = function(action, that /*opt*/ ) {
for (var i = 0, n = this.length; i < n; i++)
2020-06-28 14:22:57 +00:00
//if (i in this)
2020-07-03 09:15:23 +00:00
action.call(that, this[i], i, this);
2020-06-28 14:22:57 +00:00
};
}
if (!('map' in Array.prototype)) {
2020-07-03 09:15:23 +00:00
Array.prototype.map = function(mapper, that /*opt*/ ) {
var other = new Array(this.length);
for (var i = 0, n = this.length; i < n; i++)
2020-06-28 14:22:57 +00:00
//if (i in this)
2020-07-03 09:15:23 +00:00
other[i] = mapper.call(that, this[i], i, this);
2020-06-28 14:22:57 +00:00
return other;
};
}
if (!('filter' in Array.prototype)) {
2020-07-03 09:15:23 +00:00
Array.prototype.filter = function(filter, that /*opt*/ ) {
var other = [],
v;
for (var i = 0, n = this.length; i < n; i++)
2020-06-28 14:22:57 +00:00
//if (i in this && filter.call(that, v= this[i], i, this))
2020-07-03 09:15:23 +00:00
if (filter.call(that, v = this[i], i, this))
2020-06-28 14:22:57 +00:00
other.push(v);
return other;
};
}
if (!('every' in Array.prototype)) {
2020-07-03 09:15:23 +00:00
Array.prototype.every = function(tester, that /*opt*/ ) {
for (var i = 0, n = this.length; i < n; i++)
2020-06-28 14:22:57 +00:00
//if (i in this && !tester.call(that, this[i], i, this))
2020-07-03 09:15:23 +00:00
if (!tester.call(that, this[i], i, this))
2020-06-28 14:22:57 +00:00
return false;
return true;
};
}
if (!('some' in Array.prototype)) {
2020-07-03 09:15:23 +00:00
Array.prototype.some = function(tester, that /*opt*/ ) {
for (var i = 0, n = this.length; i < n; i++)
2020-06-28 14:22:57 +00:00
//if (i in this && tester.call(that, this[i], i, this))
2020-07-03 09:15:23 +00:00
if (tester.call(that, this[i], i, this))
2020-06-28 14:22:57 +00:00
return true;
return false;
};
}
/////////////////////////////////////////////////////////////////////////////////
// Private APIs / Utility functions
/////////////////////////////////////////////////////////////////////////////////
2020-07-03 09:15:23 +00:00
var scope = {
VERSIONINFO: "Standard Lib (std.js) version 0.2",
global: global,
require: global.require
};
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
// Emulate Server.CreateObject
/////////////////////////////////////////////////////////////////////////////////
2020-07-03 09:15:23 +00:00
scope.CreateObject = function(n) {
return new ActiveXObject(n);
};
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
2020-07-03 09:15:23 +00:00
return scope;