Skip to content

Instantly share code, notes, and snippets.

@katio
Last active November 4, 2021 15:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save katio/08bf3f5e058b950cd957 to your computer and use it in GitHub Desktop.
Save katio/08bf3f5e058b950cd957 to your computer and use it in GitHub Desktop.
Collection of JavaScript polyfills of MDN (Mozilla Developer Network) https://developer.mozilla.org. Polyfill definition in MDN (https://developer.mozilla.org/en-US/docs/Web/Guide/Terminology) "A polyfill is a piece of code that implements a common API that isn't natively supported by a browser". This file can be used in XPages SSJS (Server Side…
"use strict";
/*Start of developer.mozilla.org Polyfill*/
/*WARNING FOR SERVER SIDE JAVASCRIPT IN XPAGES: http://xomino.com/2014/03/02/prototypal-inheritance-of-ssjs-across-the-whole-server-in-xpages/ http://stackoverflow.com/questions/26695434/how-to-clean-ssjs-in-domino-server-after-someone-used-javascript-*/
/*
* Polyfill:
Array.isArray
Array.prototype.indexOf
Array.prototype.lastIndexOf
Array.prototype.forEach
Array.prototype.some
Array.prototype.every
Array.prototype.map
Array.prototype.reduce
Array.prototype.reduceRight
Array.prototype.filter
Function.prototype.bind
String.prototype.trim
Object.keys (This was replaced for: https://coderwall.com/p/q8qacw)
*/
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
/*
if (!Object.keys) {
Object.keys = (function () {
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function (obj) {
if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
throw new TypeError('Object.keys called on non-object');
}
var result = [], prop, i;
for (prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}
if (hasDontEnumBug) {
for (i = 0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
}
}
return result;
};
}());
}
*/
/*https://coderwall.com/p/q8qacw
*
* Also, in addition to make sure you're not capturing secret properties, you can use this:
Object.keys = function (obj, k) {
k=[];
for (k[k.length] in obj) {
!obj.hasOwnProperty(k[k.length - 1]) && k.splice(k.length - 1, 1);
}
return k;
}
Second version:
*/
Object.keys = function (obj, k) {
k=[];
for (o in obj) {
obj.hasOwnProperty(obj[o]) && (k[k.length]=o);
}
return k;
}
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
}
/*if (!Object.keys) {
Object.keys = function (obj, k) {
k=[];
for (k[k.length] in obj);
return k;
}
}*/
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement, fromIndex) {
if ( this === undefined || this === null ) {
throw new TypeError( '"this" is null or not defined' );
}
var length = this.length >>> 0; // Hack to convert object.length to a UInt32
fromIndex = +fromIndex || 0;
if (Math.abs(fromIndex) === Infinity) {
fromIndex = 0;
}
if (fromIndex < 0) {
fromIndex += length;
if (fromIndex < 0) {
fromIndex = 0;
}
}
for (;fromIndex < length; fromIndex++) {
if (this[fromIndex] === searchElement) {
return fromIndex;
}
}
return -1;
};
}
if (!Array.prototype.lastIndexOf) {
Array.prototype.lastIndexOf = function(searchElement /*, fromIndex*/) {
'use strict';
if (this === void 0 || this === null) {
throw new TypeError();
}
var n, k,
t = Object(this),
len = t.length >>> 0;
if (len === 0) {
return -1;
}
n = len - 1;
if (arguments.length > 1) {
n = Number(arguments[1]);
if (n != n) {
n = 0;
}
else if (n != 0 && n != (1 / 0) && n != -(1 / 0)) {
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
}
for (k = n >= 0
? Math.min(n, len - 1)
: len - Math.abs(n); k >= 0; k--) {
if (k in t && t[k] === searchElement) {
return k;
}
}
return -1;
};
}
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
if ( 'function' !== typeof Array.prototype.reduceRight ) {
Array.prototype.reduceRight = function( callback /*, initialValue*/ ) {
'use strict';
if ( null === this || 'undefined' === typeof this ) {
throw new TypeError(
'Array.prototype.reduce called on null or undefined' );
}
if ( 'function' !== typeof callback ) {
throw new TypeError( callback + ' is not a function' );
}
var t = Object( this ), len = t.length >>> 0, k = len - 1, value;
if ( arguments.length >= 2 ) {
value = arguments[1];
} else {
while ( k >= 0 && ! k in t ) k--;
if ( k < 0 )
throw new TypeError('Reduce of empty array with no initial value');
value = t[ k-- ];
}
for ( ; k >= 0 ; k-- ) {
if ( k in t ) {
value = callback( value, t[k], k, t );
}
}
return value;
};
}
if (!Array.prototype.forEach)
{
Array.prototype.forEach = function(fun /*, thisArg */)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++)
{
if (i in t)
fun.call(thisArg, t[i], i, t);
}
};
}
if(!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
if (!Array.prototype.some)
{
Array.prototype.some = function(fun /*, thisArg */)
{
'use strict';
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== 'function')
throw new TypeError();
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++)
{
if (i in t && fun.call(thisArg, t[i], i, t))
return true;
}
return false;
};
}
if (!Array.prototype.every)
{
Array.prototype.every = function(fun /*, thisArg */)
{
'use strict';
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== 'function')
throw new TypeError();
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++)
{
if (i in t && !fun.call(thisArg, t[i], i, t))
return false;
}
return true;
};
}
if ( 'function' !== typeof Array.prototype.reduce ) {
Array.prototype.reduce = function( callback /*, initialValue*/ ) {
'use strict';
if ( null === this || 'undefined' === typeof this ) {
throw new TypeError(
'Array.prototype.reduce called on null or undefined' );
}
if ( 'function' !== typeof callback ) {
throw new TypeError( callback + ' is not a function' );
}
var t = Object( this ), len = t.length >>> 0, k = 0, value;
if ( arguments.length >= 2 ) {
value = arguments[1];
} else {
while ( k < len && ! k in t ) k++;
if ( k >= len )
throw new TypeError('Reduce of empty array with no initial value');
value = t[ k++ ];
}
for ( ; k < len ; k++ ) {
if ( k in t ) {
value = callback( value, t[k], k, t );
}
}
return value;
};
}
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisArg */)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = [];
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++)
{
if (i in t)
{
var val = t[i];
// NOTE: Technically this should Object.defineProperty at
// the next index, as push can be affected by
// properties on Object.prototype and Array.prototype.
// But that method's new, and collisions should be
// rare, so use the more-compatible alternative.
if (fun.call(thisArg, val, i, t))
res.push(val);
}
}
return res;
};
}
if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisArg */)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = new Array(len);
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++)
{
// NOTE: Absolute correctness would demand Object.defineProperty
// be used. But this method is fairly new, and failure is
// possible only if Object.prototype or Array.prototype
// has a property |i| (very unlikely), so use a less-correct
// but more portable alternative.
if (i in t)
res[i] = fun.call(thisArg, t[i], i, t);
}
return res;
};
}
/*End of developer.mozilla.org Polyfill*/
/*Start of $ Helper for SSJS (Server Side Javascript in XPages)*
/*Remove the following code if you are using this library client side and are using another library that uses $ as jQuery*/
var $ = function() {
}
$.each = function(obj, callback){ /*Método estático*/
if ('undefined' === typeof obj){
return false;
}
if (null === obj){/*this may be redundant*/
return false;
}
for (var x in obj) {
if (obj.hasOwnProperty(x)) {
callback.call(obj, x, obj[x]);
}
}
}
$.keys = function(obj){
var arrKeys = []
$.each(obj, function(key, value){
arrKeys.push(key)
})
return arrKeys
}
/*Because Array.prototype.splice is broken in 8.5.3 SSJS*/
/*Written by TOMMY VALAND*/
/*http://dontpanic82.blogspot.se/2010/10/code-snippet-arraysplice-according-to.html*/
function $splice( array, startIndex, numItems ){
try {
var endIndex = startIndex + numItems;
var itemsBeforeSplice = [], splicedItems = [], itemsAfterSplice = [];
for( var i = 0; i < array.length; i++ ){
if( i < startIndex ){ itemsBeforeSplice.push( array[i] ); }
if( i >= startIndex && i < endIndex ){ splicedItems.push( array[i] ); }
if( i >= endIndex ){ itemsAfterSplice.push( array[i] ); }
}
// Insert all arguments/parameters after numItems
for( i = 3; i < arguments.length; i ++ ){
itemsBeforeSplice.push( arguments[ ''+i ] );
}
// Combine before/after arrays
var remainingItems = itemsBeforeSplice.concat( itemsAfterSplice );
// Rewrite array. Arrays can't be overwritten directly in SSJS
for( i = 0, len=Math.max( array.length, remainingItems.length ); i < len; i++ ){
if( remainingItems.length > i ){
array[i] = remainingItems[i];
} else {
array.pop();
}
}
return splicedItems;
//} catch(e){ /*Debug.logException( e );*/ }
} catch(e){ println("Error en polyfill > $.splice: " + e.message) }
}
/*End of $ Helper for SSJS*/
@AutomateEarth
Copy link

Do you have the polyfill for .apply?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment