I am currently using umdjs(https://github.com/umdjs/umd) to create a nodejs modules.
I would know if their is a predefined pattern more specificly for umd pollyfills.This is because I am writting a polyfill for javascript using the umd pattern.
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], function () {
return (root = factory());
});
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals
return root.countByRadix = factory();
}
}(this, function () {
var that = this;
// UMD Definition above, do not remove this line
let hasOwnProperty = Object.prototype.hasOwnProperty;
/**
* American Standard for Communication
* and Information Interchange
* @param str
* @returns
*/
function toAscii(str) {
return [...str]
.map(c => c.charCodeAt(0))
}
function toRadixOf(str,base) {
return toAscii(str).map(c => c.toString(base));
}
if (!(hasOwnProperty.call(String.prototype, "countByRadix"))){
Object.defineProperty(String.prototype, "countByRadix", {
value: function (radix) {
return Number(
toRadixOf(this, radix)
.map(r => String(r).length)
.reduce((a, b) => a + b))
}
})
}
return that
}));