diff --git a/bin/build.js b/bin/build.js index 2a7a1e9..896cffb 100644 --- a/bin/build.js +++ b/bin/build.js @@ -11,14 +11,14 @@ pkg = require('../package.json'), J = require('../jsus.js').JSUS, version = pkg.version; - +// console.log(J); function buildIt(options) { var out = options.output || "jsus"; if (path.extname(out) === '.js') { - out = path.basename(out, '.js'); + out = path.basename(out, '.js'); } console.log('Building JSUS v.' + version + ' with:'); @@ -42,8 +42,8 @@ function buildIt(options) { console.log(' - JSUS core'); if (options.all) { - files = files.concat(J.obj2Array(jsus_libs)); - console.log(' - JSUS lib: all available libs included'); + files = files.concat(J.obj2Array(jsus_libs)); + console.log(' - JSUS lib: all available libs included'); } else { var selected = options.lib; diff --git a/build/jsus.js b/build/jsus.js index 82ad1a9..023dd90 100644 --- a/build/jsus.js +++ b/build/jsus.js @@ -9,7 +9,9 @@ * --- */ (function(exports) { - + + var files, inits; + var JSUS = exports.JSUS = {}; // ## JSUS._classes @@ -143,17 +145,36 @@ // ## Node.JS includes if (JSUS.isNodeJS()) { - require('./lib/compatibility'); - require('./lib/obj'); - require('./lib/array'); - require('./lib/time'); - require('./lib/eval'); - require('./lib/dom'); - require('./lib/random'); - require('./lib/parse'); - require('./lib/queue'); - require('./lib/fs'); + inits = []; + files = [ + 'compatibility', + 'obj', + 'array', + 'time', + 'eval', + 'dom', + 'random', + 'parse', + 'queue', + 'fs' + ]; + // files = [ 'array' ] + + // Load all files. + files.forEach(function(file) { + const arr = require('./lib/' + file); + // destructuring does not work with Uglify JS [ FN, init ] + JSUS.extend(arr[0]); + inits.push(arr[1]); + }); + + // After all is loaded, JSUS is complete, initialize the libraries + // with cross-references (this approach avoids circular references). + inits.forEach(function(init) { + init(JSUS); + }); } + else { // Exports J in the browser. exports.J = exports.JSUS; @@ -166,15 +187,17 @@ /** * # ARRAY - * Copyright(c) 2017 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Collection of static functions to manipulate arrays */ -(function(JSUS) { +(function() { "use strict"; + var _J, init; + function ARRAY() {} /** @@ -263,7 +286,7 @@ if (end === Infinity) return false; // TODO: increment zero might be fine if start=end. Check. if (increment === 0) return false; - if (!JSUS.inArray(typeof increment, ['undefined', 'number'])) { + if (!ARRAY.inArray(typeof increment, ['undefined', 'number'])) { return false; } if (start === end) { @@ -342,12 +365,12 @@ func = arguments[1]; if (!ARRAY.isArray(array)) { - JSUS.log('ARRAY.map: first parameter must be array. Found: ' + + console.log('ARRAY.map: first parameter must be array. Found: ' + array); return; } if ('function' !== typeof func) { - JSUS.log('ARRAY.map: second parameter must be function. Found: ' + + console.log('ARRAY.map: second parameter must be function. Found: ' + func); return; } @@ -395,7 +418,7 @@ if ('undefined' === typeof needle || !haystack) return false; if ('object' === typeof needle) { - func = JSUS.equals; + func = _J.equals; } else { func = function(a, b) { @@ -430,7 +453,7 @@ ARRAY.inArray = function(needle, haystack) { var func, i, len; if (!haystack) return false; - func = JSUS.equals; + func = _J.equals; len = haystack.length; for (i = 0; i < len; i++) { if (func.call(this, needle, haystack[i])) { @@ -559,9 +582,9 @@ for (i=0; i < N; i++) { do { - idx = JSUS.randomInt(start,limit); + idx = _J.randomInt(start,limit); } - while (JSUS.inArray(idx, extracted)); + while (ARRAY.inArray(idx, extracted)); extracted.push(idx); if (idx == 1) { @@ -723,7 +746,7 @@ if (!ARRAY.isArray(array)) array = [ array ]; if (!times) return array.slice(0); if (times < 1) { - JSUS.log('times must be greater or equal 1', 'ERR'); + console.log('times must be greater or equal 1', 'ERR'); return; } i = 1; @@ -770,7 +793,7 @@ if (!times) return array.slice(0); if ('number' === typeof times) { if (times < 1) { - JSUS.log('times must be greater or equal 1', 'ERR'); + console.log('times must be greater or equal 1', 'ERR'); return; } times = ARRAY.rep([times], array.length); @@ -801,7 +824,7 @@ */ ARRAY.arrayIntersect = function(a1, a2) { return a1.filter( function(i) { - return JSUS.inArray(i, a2); + return ARRAY.inArray(i, a2); }); }; @@ -819,7 +842,7 @@ */ ARRAY.arrayDiff = function(a1, a2) { return a1.filter( function(i) { - return !(JSUS.inArray(i, a2)); + return !(ARRAY.inArray(i, a2)); }); }; @@ -921,23 +944,33 @@ return t; }; - JSUS.extend(ARRAY); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(ARRAY); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ ARRAY, init ]; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); /** * # COMPATIBILITY * - * Copyright(c) 2015 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Tests browsers ECMAScript 5 compatibility * * For more information see http://kangax.github.com/es5-compat-table/ */ -(function(JSUS) { +(function() { "use strict"; + var _J, init; + function COMPATIBILITY() {} /** @@ -985,23 +1018,32 @@ }; - JSUS.extend(COMPATIBILITY); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(COMPATIBILITY); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ COMPATIBILITY, init ]; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); /** * # DOM - * Copyright(c) 2019 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Helper library to perform generic operation with DOM elements. */ -(function(JSUS) { +(function() { "use strict"; - + + var _J, init; var onFocusChange, changeTitle; - + function DOM() {} // ## GET/ADD @@ -1165,7 +1207,7 @@ DOM.write = function(root, text) { var content; if ('undefined' === typeof text || text === null) text = ""; - if (JSUS.isNode(text) || JSUS.isElement(text)) content = text; + if (DOM.isNode(text) || DOM.isElement(text)) content = text; else content = document.createTextNode(text); root.appendChild(content); return content; @@ -1180,7 +1222,7 @@ */ DOM.write2 = function(root, text) { if ('undefined' === typeof text) text = ""; - if (JSUS.isNode(text) || JSUS.isElement(text)) root.appendChild(text); + if (DOM.isNode(text) || DOM.isElement(text)) root.appendChild(text); else root.innerHTML += text; }; @@ -1289,7 +1331,7 @@ idx_finish = string.indexOf(key, idx_replace); if (idx_finish === -1) { - JSUS.log('Error. Could not find closing key: ' + key); + console.log('Error. Could not find closing key: ' + key); continue; } @@ -1307,20 +1349,20 @@ break; default: - JSUS.log('Identifier not in [!,@,%]: ' + key[0]); + console.log('Identifier not in [!,@,%]: ' + key[0]); } } } // No span to create, return what we have. - if (!JSUS.size(spans)) { + if (!_J.size(spans)) { return root.appendChild(document.createTextNode(string)); } // Re-assamble the string. - idxs = JSUS.keys(spans).sort(function(a, b){ return a - b; }); + idxs = _J.keys(spans).sort(function(a, b){ return a - b; }); idx_finish = 0; for (i = 0; i < idxs.length; i++) { @@ -1427,12 +1469,12 @@ DOM.shuffleElements = function(parent, order, cb) { var i, len, numOrder, idOrder, children, child; var id; - if (!JSUS.isNode(parent)) { + if (!DOM.isNode(parent)) { throw new TypeError('DOM.shuffleElements: parent must be a node. ' + 'Found: ' + parent); } if (!parent.children || !parent.children.length) { - JSUS.log('DOM.shuffleElements: parent has no children.', 'ERR'); + console.log('DOM.shuffleElements: parent has no children.', 'ERR'); return false; } if (order) { @@ -1440,7 +1482,7 @@ cb = order; } else { - if (!JSUS.isArray(order)) { + if (!_J.isArray(order)) { throw new TypeError('DOM.shuffleElements: order must be ' + 'array. Found: ' + order); } @@ -1472,7 +1514,7 @@ len = children.length; idOrder = new Array(len); if (cb) numOrder = new Array(len); - if (!order) order = JSUS.sample(0, (len-1)); + if (!order) order = _J.sample(0, (len-1)); for (i = 0 ; i < len; i++) { id = children[order[i]].id; if ('string' !== typeof id || id === "") { @@ -1582,7 +1624,7 @@ throw new Error('DOM.addCSS: root is undefined, and could not ' + 'detect a valid root for css: ' + cssPath); } - attributes = JSUS.mixin({ + attributes = _J.mixin({ rel : 'stylesheet', type: 'text/css', href: cssPath @@ -1615,7 +1657,7 @@ throw new Error('DOM.addCSS: root is undefined, and could not ' + 'detect a valid root for css: ' + jsPath); } - attributes = JSUS.mixin({ + attributes = _J.mixin({ charset : 'utf-8', type: 'text/javascript', src: jsPath @@ -1776,7 +1818,7 @@ } } else { - prefix = JSUS.randomString(8, 'a'); + prefix = _J.randomString(8, 'a'); } id = prefix + '_'; @@ -1790,7 +1832,7 @@ found = true; counter = -1; while (found) { - id = prefix + '_' + JSUS.randomInt(1000); + id = prefix + '_' + _J.randomInt(1000); found = scanDocuments(windows, id); if (++counter > limit) { throw new Error('DOM.generateUniqueId: could not ' + @@ -2128,7 +2170,7 @@ disable = 'undefined' === typeof disable ? true : disable; if (disable && !isDisabled) { if (!history.pushState || !history.go) { - JSUS.log('DOM.disableBackButton: method not ' + + console.log('DOM.disableBackButton: method not ' + 'supported by browser.'); return null; } @@ -2242,7 +2284,7 @@ // Option repeatFor. if ('undefined' !== typeof options.repeatFor) { - nRepeats = JSUS.isInt(options.repeatFor, 0); + nRepeats = _J.isInt(options.repeatFor, 0); if (false === nRepeats) { throw new TypeError(where + 'options.repeatFor must be ' + 'a positive integer. Found: ' + @@ -2252,7 +2294,7 @@ // Option stopOnFocus. if (options.stopOnFocus) { - JSUS.onFocusIn(function() { + DOM.onFocusIn(function() { clearBlinkInterval(); onFocusChange(null, null); }); @@ -2275,8 +2317,8 @@ // Option startOnBlur. if (options.startOnBlur) { options.startOnBlur = null; - JSUS.onFocusOut(function() { - JSUS.blinkTitle(titles, options); + DOM.onFocusOut(function() { + DOM.blinkTitle(titles, options); }); return null; } @@ -2285,7 +2327,7 @@ if ('string' === typeof titles) { titles = [titles, '!!!']; } - else if (!JSUS.isArray(titles)) { + else if (!_J.isArray(titles)) { throw new TypeError(where + 'titles must be string, ' + 'array of strings or undefined. Found: ' + titles); @@ -2451,7 +2493,7 @@ if (!document) { return function() { - JSUS.log('onFocusChange: no document detected.'); + console.log('onFocusChange: no document detected.'); return; }; } @@ -2531,9 +2573,16 @@ } }; - JSUS.extend(DOM); - -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(DOM); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ DOM, init ]; + } +})(); /** * # DOM @@ -2564,7 +2613,7 @@ * Only the methods which do not follow the above-mentioned syntax * will receive further explanation. */ -(function(JSUS) { +(function() { "use strict"; @@ -4105,21 +4154,30 @@ } }; - JSUS.extend(DOM); -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); + if ('undefined' !== typeof JSUS) { + JSUS.extend(DOM); + } + // Node.JS ESM or CJS + else { + module.exports = DOM; + } + +})(); /** * # EVAL - * Copyright(c) 2015 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Evaluation of strings as JavaScript commands */ -(function(JSUS) { +(function() { "use strict"; + var _J, init; + function EVAL() {} /** @@ -4161,13 +4219,22 @@ return func.call(context, str); }; - JSUS.extend(EVAL); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(EVAL); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ EVAL, init ]; + } + -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); /** * # FS - * Copyright(c) 2018 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Collection of static functions related to file system operations. @@ -4178,19 +4245,15 @@ * @see https://github.com/jprichardson/node-fs-extra * @see https://github.com/substack/node-resolve */ -(function(JSUS) { +(function() { "use strict"; - if (!JSUS.isNodeJS()){ - JSUS.log('Cannot load JSUS.FS outside of Node.JS.'); - return false; - } - - var resolve = require('resolve'), - path = require('path'), - fs = require('fs'); + const resolve = require('resolve'); + const path = require('path'); + const fs = require('fs'); + var _J, init; function FS() {} @@ -4300,7 +4363,7 @@ FS.cleanDir = function(dir, ext, cb) { var filterFunc; if (!dir) { - JSUS.log('You must specify a directory to clean.'); + console.log('You must specify a directory to clean.'); return false; } if (ext) { @@ -4319,11 +4382,11 @@ fs.readdir(dir, function(err, files) { var asq, mycb; if (err) { - JSUS.log(err); + console.log(err); return; } // Create async queue if a callback was specified. - if (cb) asq = JSUS.getQueue(); + if (cb) asq = _J.getQueue(); // Create a nested callback for the async queue, if necessary. files.filter(filterFunc).forEach(function(file) { @@ -4331,7 +4394,7 @@ asq.add(file); mycb = asq.getRemoveCb(file); } - JSUS.deleteIfExists(dir + file, mycb); + FS.deleteIfExists(dir + file, mycb); }); if (cb) { @@ -4368,11 +4431,11 @@ FS.copyFromDir = function(dirIn, dirOut, ext, cb) { var i, dir, dirs, stats; if (!dirIn) { - JSUS.log('You must specify a source directory.'); + console.log('You must specify a source directory.'); return false; } if (!dirOut) { - JSUS.log('You must specify a destination directory.'); + console.log('You must specify a destination directory.'); return false; } @@ -4396,11 +4459,11 @@ fs.readdir(dirIn, function(err, files) { var asq, i, mycb; if (err) { - JSUS.log(err); + console.log(err); throw new Error(); } // Create async queue if a callback was specified. - if (cb) asq = JSUS.getQueue(); + if (cb) asq = _J.getQueue(); for (i in files) { if (ext && path.extname(files[i]) !== ext) { continue; @@ -4446,28 +4509,25 @@ return fdr.pipe(fdw); }; - JSUS.extend(FS); + init = function(J) { _J = J; }; + module.exports = [ FS, init ]; -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); /** * # OBJ - * Copyright(c) 2019 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Collection of static functions to manipulate JavaScript objects */ -(function(JSUS) { +(function() { "use strict"; - function OBJ() {} - - var compatibility = null; + var _J, init, compatibility; - if ('undefined' !== typeof JSUS.compatibility) { - compatibility = JSUS.compatibility(); - } + function OBJ() {} /** * ## OBJ.createObj @@ -4920,7 +4980,7 @@ res.push(tmp); } // If array, expand it. - else if (JSUS.isArray(tmp) && tmp.length) { + else if (_J.isArray(tmp) && tmp.length) { if (tmp.length < 4) { res.push(tmp[0]); if (tmp.length > 1) { @@ -5087,12 +5147,12 @@ if (obj && 'object' === typeof obj) { clone = Object.prototype.toString.call(obj) === '[object Array]' ? - [] : JSUS.createObj(obj.constructor.prototype); + [] : OBJ.createObj(obj.constructor.prototype); for (i in obj) { if (obj.hasOwnProperty(i)) { if (obj[i] && 'object' === typeof obj[i]) { - clone[i] = JSUS.classClone(obj[i], depth - 1); + clone[i] = OBJ.classClone(obj[i], depth - 1); } else { clone[i] = obj[i]; @@ -5102,7 +5162,7 @@ return clone; } else { - return JSUS.clone(obj); + return OBJ.clone(obj); } }; @@ -5415,7 +5475,7 @@ OBJ.setNestedValue = function(str, value, obj) { var keys, k; if (!str) { - JSUS.log('Cannot set value of undefined property', 'ERR'); + console.log('Cannot set value of undefined property', 'ERR'); return false; } obj = ('object' === typeof obj) ? obj : {}; @@ -5579,18 +5639,18 @@ makeClone = function(value, out, keys) { var i, len, tmp, copy; - copy = JSUS.clone(model); + copy = OBJ.clone(model); switch(keys.length) { case 0: - copy[_key] = JSUS.clone(value); + copy[_key] = OBJ.clone(value); break; case 1: - copy[_key][keys[0]] = JSUS.clone(value); + copy[_key][keys[0]] = OBJ.clone(value); break; case 2: copy[_key][keys[0]] = {}; - copy[_key][keys[0]][keys[1]] = JSUS.clone(value); + copy[_key][keys[0]][keys[1]] = OBJ.clone(value); break; default: i = -1, len = keys.length-1; @@ -5599,7 +5659,7 @@ tmp[keys[i]] = {}; tmp = tmp[keys[i]]; } - tmp[keys[keys.length-1]] = JSUS.clone(value); + tmp[keys[keys.length-1]] = OBJ.clone(value); } out.push(copy); return; @@ -5614,7 +5674,7 @@ } else { - curPosAsKey = posAsKeys || !JSUS.isArray(value); + curPosAsKey = posAsKeys || !_J.isArray(value); for (i in value) { if (value.hasOwnProperty(i)) { @@ -5649,7 +5709,7 @@ throw new TypeError('JSUS.split: l must a non-negative ' + 'number or undefined. Found: ' + l); } - model = JSUS.clone(o); + model = OBJ.clone(o); if ('object' !== typeof o[key]) return [model]; // Init. out = []; @@ -5716,7 +5776,7 @@ OBJ.uniqueKey = function(obj, prefixName, stop) { var name, duplicateCounter; if (!obj) { - JSUS.log('Cannot find unique name in undefined object', 'ERR'); + console.log('Cannot find unique name in undefined object', 'ERR'); return; } duplicateCounter = 1; @@ -5925,21 +5985,34 @@ return res; }; - JSUS.extend(OBJ); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(OBJ); + } + // Node.JS ESM or CJS + else { + init = function(J) { + _J = J; + compatibility = _J.compatibility(); + }; + module.exports = [ OBJ, init ]; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); /** * # PARSE - * Copyright(c) 2017 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Collection of static functions related to parsing strings */ -(function(JSUS) { +(function() { "use strict"; + var _J, init; + function PARSE() {} /** @@ -6045,7 +6118,7 @@ pattern = '['; - JSUS.each(separators, function(s) { + separators.forEach(function(s) { if (s === ' ') s = '\\s'; pattern += s; @@ -6179,7 +6252,7 @@ return value; } else if (value.substring(0, len_func) === PARSE.marker_func) { - return JSUS.eval(value.substring(len_prefix)); + return _J.eval(value.substring(len_prefix)); } else if (value.substring(0, len_null) === PARSE.marker_null) { return null; @@ -6401,7 +6474,7 @@ if ('undefined' === typeof available) { available = expr; } - else if (JSUS.isArray(available)) { + else if (_J.isArray(available)) { if (available.length === 0) return solution; begin = Math.min.apply(null, available); end = Math.max.apply(null, available); @@ -6550,11 +6623,11 @@ throw new Error('PARSE.range: invalid dot found: ' + expr); } - if (JSUS.isArray(available)) { + if (_J.isArray(available)) { i = -1, len = available.length; for ( ; ++i < len ; ) { x = parseInt(available[i], 10); - if (JSUS.eval(expr.replace(/x/g, x))) { + if (_J.eval(expr.replace(/x/g, x))) { solution.push(x); } } @@ -6562,7 +6635,7 @@ else { while (!available.isFinished()) { x = parseInt(available.next(), 10); - if (JSUS.eval(expr.replace(/x/g, x))) { + if (_J.eval(expr.replace(/x/g, x))) { solution.push(x); } } @@ -6640,21 +6713,31 @@ }; } - JSUS.extend(PARSE); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(PARSE); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ PARSE, init ]; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); /** * # QUEUE - * Copyright(c) 2015 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Handles a simple queue of operations */ -(function(JSUS) { +(function() { "use strict"; + var _J, init; + var QUEUE = {}; QUEUE.getQueue = function() { @@ -6689,7 +6772,7 @@ * @return {boolean} TRUE, if no operation is in progress */ Queue.prototype.isReady = function() { - return JSUS.isEmpty(this.inProgress); + return _J.isEmpty(this.inProgress); }; /** @@ -6709,7 +6792,7 @@ throw new TypeError('Queue.onReady: cb must be function. Found: ' + cb); } - if (JSUS.isEmpty(this.inProgress)) cb(); + if (_J.isEmpty(this.inProgress)) cb(); else this.queue.push(cb); }; @@ -6726,7 +6809,7 @@ if (key && 'string' !== typeof key) { throw new Error('Queue.add: key must be string.'); } - key = JSUS.uniqueKey(this.inProgress, key); + key = _J.uniqueKey(this.inProgress, key); if ('string' !== typeof key) { throw new Error('Queue.add: an error occurred ' + 'generating unique key.'); @@ -6747,7 +6830,7 @@ throw new Error('Queue.remove: key must be string.'); } delete this.inProgress[key]; - if (JSUS.isEmpty(this.inProgress)) { + if (_J.isEmpty(this.inProgress)) { this.executeAndClear(); } }; @@ -6787,21 +6870,31 @@ } }; - JSUS.extend(QUEUE); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(QUEUE); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ QUEUE, init ]; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); /** * # RANDOM - * Copyright(c) 2017 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Generates pseudo-random numbers */ -(function(JSUS) { +(function() { "use strict"; + var _J, init; + function RANDOM() {} /** @@ -6915,9 +7008,9 @@ */ RANDOM.sample = function(a, b) { var out; - out = JSUS.seq(a,b); + out = _J.seq(a,b); if (!out) return false; - return JSUS.shuffle(out); + return _J.shuffle(out); }; /** @@ -7212,7 +7305,7 @@ if (nSpaces > -1) { nSpaces = chars.charAt(nSpaces + 1); // nSpaces is integer > 0 or 1. - nSpaces = JSUS.isInt(nSpaces, 0) || 1; + nSpaces = _J.isInt(nSpaces, 0) || 1; if (nSpaces === 1) mask += ' '; else if (nSpaces === 2) mask += ' '; else if (nSpaces === 3) mask += ' '; @@ -7250,22 +7343,32 @@ RANDOM.randomString(RANDOM.randomInt(2,3)); }; - JSUS.extend(RANDOM); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(RANDOM); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ RANDOM, init ]; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); /** * # TIME - * Copyright(c) 2021 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Collection of static functions related to the generation, * manipulation, and formatting of time strings in JavaScript */ -(function (JSUS) { +(function() { "use strict"; + var _J, init; + function TIME() {} function pad(number) { @@ -7388,6 +7491,14 @@ TIME.now = 'function' === typeof Date.now ? Date.now : function() { return new Date().getTime(); } - JSUS.extend(TIME); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(TIME); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ TIME, init ]; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); diff --git a/build/jsus.min.js b/build/jsus.min.js index 60151e8..b083021 100644 --- a/build/jsus.min.js +++ b/build/jsus.min.js @@ -8,4 +8,4 @@ * See README.md for extra help. * --- */ -(function(e){var t=e.JSUS={};t._classes={},"undefined"==typeof console&&(console={}),"undefined"==typeof console.log&&(console.log=function(){}),t.log=function(e){console.log(e)},t.extend=function(e,n){var r,i;if("object"!=typeof e&&"function"!=typeof e)return n;"undefined"==typeof n&&(n=n||this,"function"==typeof e?(r=e.toString(),r=r.substr("function ".length),r=r.substr(0,r.indexOf("("))):r=e.constructor||e.__proto__.constructor,r&&(this._classes[r]=e));for(i in e)e.hasOwnProperty(i)&&(typeof n[i]!="object"?n[i]=e[i]:t.extend(e[i],n[i]));return e.prototype&&t.extend(e.prototype,n.prototype||n),n},t.require=function(e,n){var r;n="undefined"==typeof n?!0:n;if(n&&"undefined"==typeof t.clone)return t.log("JSUS.require: JSUS.clone not found, but clone requested. Cannot continue."),!1;if("undefined"==typeof e)r=t._classes;else{r=t._classes[e];if("undefined"==typeof r)return t.log("JSUS.require: could not find component "+e),!1}return n?t.clone(r):r},t.isNodeJS=function(){return"undefined"!=typeof module&&"undefined"!=typeof module.exports&&"function"==typeof require},t.isNodeJS()?(require("./lib/compatibility"),require("./lib/obj"),require("./lib/array"),require("./lib/time"),require("./lib/eval"),require("./lib/dom"),require("./lib/random"),require("./lib/parse"),require("./lib/queue"),require("./lib/fs")):e.J=e.JSUS})("undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports:window),function(e){"use strict";function t(){}Array.prototype.filter||(Array.prototype.filter=function(e){if(this===void 0||this===null)throw new TypeError;var t=new Object(this),n=t.length>>>0;if(typeof e!="function")throw new TypeError;var r=[],i=arguments[1];for(var s=0;s=n)o.push(i(s)),s-=r;return o},t.each=function(e,t,n){var r,i;if("object"!=typeof e)throw new TypeError("ARRAY.each: array must be object. Found: "+e);if("function"!=typeof t)throw new TypeError("ARRAY.each: cb must be function. Found: "+t);n=n||this,i=e.length;for(r=0;r=t&&(s.push(a),f=0,a=[]),a.push(n[u]),n.splice(u,1),r=n.length,f++;return a.length>0&&s.push(a),s},t._latinSquare=function(t,n,r){r="undefined"==typeof r?!0:r;if(t===n&&!r)return!1;var i=[],s=[];for(var o=0;oe&&(n=e),t._latinSquare(e,n,!0))},t.latinSquareNoSelf=function(e,n){return n||(n=e-1),!e||e<0||n<0?!1:(n>e&&(n=e-1),t._latinSquare(e,n,!1))},t.generateCombinations=function n(e,t){var r,i,s,o,u;s=[];for(r=0;r0;s--)r=Math.floor(Math.random()*(s+1)),i=t[r],t[r]=t[s],t[s]=i;return t},t.getNRandom=function(e,n){return t.shuffle(e).slice(0,n)},t.distinct=function(e){var n=[];return e?(t.each(e,function(e){t.inArray(e,n)||n.push(e)}),n):n},t.transpose=function(e){if(!e)return;var n,r,i,s,o=[];n=e.length||0,r=t.isArray(e[0])?e[0].length:0;if(n===0||r===0)return o;for(i=0;it)throw new Error("DOM.generateUniqueId: could not find unique id within "+t+" trials.")}return s}}(),r.removeClass=function(e,t,n){var i;if(!n&&!r.isElement(e))throw new TypeError("DOM.removeClass: elem must be HTMLElement. Found: "+e);if(t){if("string"!=typeof t||t.trim()==="")throw new TypeError("DOM.removeClass: className must be HTMLElement. Found: "+t);i=new RegExp("(?:^|\\s)"+t+"(?!\\S)"),e.className=e.className.replace(i,"")}return e},r.addClass=function(e,t,n){if(!n&&!r.isElement(e))throw new TypeError("DOM.addClass: elem must be HTMLElement. Found: "+e);if(t){t instanceof Array&&(t=t.join(" "));if("string"!=typeof t||t.trim()==="")throw new TypeError("DOM.addClass: className must be HTMLElement. Found: "+t);e.className?e.className+=" "+t:e.className=t}return e},r.getElementsByClassName=function(e,t,n){var r,i,s,o,u,a;r=[],s=n||"*";if(e.evaluate){o="//"+s+'[contains(concat(" ", normalize-space(@class), " "), "'+t+' ")]',o=e.evaluate(o,e,null,0,null);while(i=o.iterateNext())r.push(i)}else{a=new RegExp("(^| )"+t+"( |$)"),o=e.getElementsByTagName(s);for(u=0;u1){var i=new Object(arguments[1]);for(var s in i)t.call(i,s)&&(r[s]=i[s])}return r}}():Object.create}(),t.equals=function(e,n){var r,i,s,o;r=typeof e,i=typeof n;if(r!==i)return!1;if("undefined"===r||"undefined"===i)return e===n;if(e===null||n===null)return e===n;if("number"===r&&isNaN(e)&&"number"===i&&isNaN(n))return isNaN(e)&&isNaN(n);s={number:"",string:"","boolean":""};if(r in s)return e===n;if("function"===r)return e.toString()===n.toString();for(o in e)if(e.hasOwnProperty(o)){if("undefined"==typeof n[o]&&"undefined"!=typeof e[o])return!1;if(!n[o]&&e[o])return!1;if("function"==typeof e[o]){if(e[o].toString()!==n[o].toString())return!1}else if(!t.equals(e[o],n[o]))return!1}for(o in n)if(n.hasOwnProperty(o)){if("undefined"==typeof e[o]&&"undefined"!=typeof n[o])return!1;if(!e[o]&&n[o])return!1}return!0},t.isEmpty=function(e){var t;if(!e)return!0;if("string"==typeof e)return e.trim()==="";if("number"==typeof e)return!1;if("function"==typeof e)return!1;for(t in e)if(e.hasOwnProperty(t))return!1;return!0},t.size=t.getListSize=function(e){var t,n;if(!e)return 0;if("number"==typeof e)return 0;if("string"==typeof e)return 0;t=0;for(n in e)e.hasOwnProperty(n)&&t++;return t},t._obj2Array=function(e,n,r,i){var s,o;if("object"!=typeof e)return[e];if(r){i="undefined"!=typeof i?i:1;if(i>r)return[e];i+=1}s=[];for(o in e)e.hasOwnProperty(o)&&(n&&s.push(o),"object"==typeof e[o]?s=s.concat(t._obj2Array(e[o],n,r,i)):s.push(e[o]));return s},t.obj2Array=function(e,n){return t._obj2Array(e,!1,n)},t.obj2KeyedArray=t.obj2KeyArray=function(e,n){return t._obj2Array(e,!0,n)},t.obj2QueryString=function(e){var t,n;if("object"!=typeof e)throw new TypeError("JSUS.objectToQueryString: obj must be object.");t=[];for(n in e)e.hasOwnProperty(n)&&t.push(encodeURIComponent(n)+"="+encodeURIComponent(e[n]));return"?"+t.join("&")},t.keys=function(){function t(e,r,i,s,o,u,a,f,l,c,h,p,d){var v,m,g,y;m=i===r;for(v in e)if(e.hasOwnProperty(v)){g="object"==typeof e[v];if(u||a&&(m||!g)||f&&m)o?(y=s+v,p[y]||(d?n(y,c,d):c.push(y))):p[v]||(h?h[v]||(d?n(v,c,d):c.push(v),h[v]=!0):d?n(v,c,d):c.push(v));g&&i1&&(n.push(i[1]),i.length>2&&n.push(i[2]))):function(){var e=-1,t=i.length;for(;++e=i)t(r,s,a);else{l=o||!e.isArray(r);for(f in r)r.hasOwnProperty(f)&&("object"==typeof r[f]&&i&&u+1<=i?n(r[f],s,u+1,l?a.concat(f):a):t(r[f],s,l?a.concat(f):a))}},function(t,u,a,f){var l;if("object"!=typeof t)throw new TypeError("JSUS.split: o must be object. Found: "+t);if("string"!=typeof u||u.trim()==="")throw new TypeError("JSUS.split: key must a non-empty string. Found: "+u);if(a&&("number"!=typeof a||a<0))throw new TypeError("JSUS.split: l must a non-negative number or undefined. Found: "+a);return r=e.clone(t),"object"!=typeof t[u]?[r]:(l=[],s=u,r[u]={},i="undefined"==typeof a?1:a,o=f,n(t[u],l,0,[]),s=undefined,r=undefined,i=undefined,o=undefined,l)}}(),t.melt=function(e,t){var n={},r=t.length;for(var i=0;ir)return}return i},t.randomKey=function(e){var t;if("object"!=typeof e)throw new TypeError("OBJ.randomKey: obj must be object. Found: "+e);return t=Object.keys(e),t[t.length*Math.random()<<0]},t.augment=function(e,n,r){var i,s;r=r||t.keys(e);for(i=0;in:e>=n)?!1:e)},t.isEmail=function(e){var t;return"string"!=typeof e?!1:e.trim().length<5?!1:(t=e.indexOf("@"),t===-1||t===0||t===e.length-1?!1:(t=e.lastIndexOf("."),t===-1||t===e.length-1||t>t+1?!1:!0))},t.range=function(r,i){var s,o,u,a,f,l,c,h,p,d,v;a=[];if("undefined"==typeof r)return a;if("number"==typeof r)r=""+r;else if("string"!=typeof r)throw new TypeError("PARSE.range: expr must be string, number, undefined. Found: "+r);if("undefined"==typeof i)i=r;else if(e.isArray(i)){if(i.length===0)return a;f=Math.min.apply(null,i),l=Math.max.apply(null,i)}else if("object"==typeof i){if("function"!=typeof i.next)throw new TypeError("PARSE.range: available.next must be function. Found: "+i.next);if("function"!=typeof i.isFinished)throw new TypeError("PARSE.range: available.isFinished must be function. Found: "+i.isFinished);if("number"!=typeof i.begin)throw new TypeError("PARSE.range: available.begin must be number. Found: "+i.begin);if("number"!=typeof i.end)throw new TypeError("PARSE.range: available.end must be number. Found: "+i.end);f=i.begin,l=i.end}else{if("string"!=typeof i)throw new TypeError("PARSE.range: available must be string, array, object or undefined. Found: "+i);i=n(i),h=i.match(/([-+]?\d+)/g);if(h===null)throw new Error("PARSE.range: no numbers in available: "+i);c=Math.min.apply(null,h),i=t.range(i,{begin:c,end:Math.max.apply(null,h),value:c,next:function(){return this.value++},isFinished:function(){return this.value>this.end}}),f=Math.min.apply(null,i),l=Math.max.apply(null,i)}r=r.replace(/end/g,parseInt(l,10)),r=r.replace(/begin/g,parseInt(f,10)),r=n(r),r=r.replace(/([-+]?\d+\.\d+)/g,function(e,t){return parseInt(t,10)}),p=/[^ \*\d<>=!\|&\.\[\],\(\)\-\+%]/g;if(r.match(p))throw new Error("PARSE.range: invalid characters found: "+r);r=r.replace(/([^& ]) *& *([^& ])/g,"$1&&$2"),r=r.replace(/([^| ]) *\| *([^| ])/g,"$1||$2"),r=r.replace(/([-+]?\d+)/g,"(x==$1)"),r=r.replace(/% *\(x==([-+]?\d+)\)/,"!(x%$1)"),r=r.replace(/!\(x%([-+]?\d+)\) *={1,} *\(x==([-+]?\d+)\)/g,"(x%$1==$2)"),r=r.replace(/([<>]=?) *\(x==([-+]?\d+)\)/g,"(x$1$2)"),r=r.replace(/\(x==([-+]?\d+)\)\.{2,}\(x==(\+?\d+)\)\.{2,}\(x==([-+]?\d+)\)/g,"(x>=$1&&x<=$3&&!((x- $1)%$2))"),r=r.replace(/\(x==([-+]?\d+)\)\.{2,}\(x==(-\d+)\)\.{2,}\(x==([-+]?\d+)\)/g,"(x<=$1&&x>=$3&&!((x- $1)%$2))"),r=r.replace(/\(x==([-+]?\d+)\)\.{2,}\(x==([-+]?\d+)\)/g,"(x>=$1&&x<=$2)"),r=r.replace(/([(\[]) *\(x==([-+]?\d+)\) *, *\(x==([-+]?\d+)\) *([\])])/g,function(e,t,n,r,i){return"(x>"+(t=="("?"":"=")+n+"&&x<"+(i==")"?"":"=")+r+")"}),r=r.replace("*",1),r=r.replace(/\s/g,""),r=r.replace(/\)[,] *(!*)\(/g,")||$1("),p=/[^ \d<>=!\|&,\(\)\-\+%x\.]/g,d=/[^ &!|\(] *\(/g,v=/\.[^\d]|[^\d]\./;if(r.match(p))throw new Error("PARSE.range: invalid characters found: "+r);if(r.match(d))throw new Error("PARSE.range: invalid character before opending bracket found: "+r);if(r.match(v))throw new Error("PARSE.range: invalid dot found: "+r);if(e.isArray(i)){s=-1,o=i.length;for(;++s1?n[1].trim():""},e.extend(t)}("undefined"!=typeof JSUS?JSUS:module.parent.exports.JSUS),function(e){"use strict";function n(){this.queue=[],this.inProgress={}}var t={};t.getQueue=function(){return new n},n.prototype.isReady=function(){return e.isEmpty(this.inProgress)},n.prototype.onReady=function(t){if("function"!=typeof t)throw new TypeError("Queue.onReady: cb must be function. Found: "+t);e.isEmpty(this.inProgress)?t():this.queue.push(t)},n.prototype.add=function(t){if(t&&"string"!=typeof t)throw new Error("Queue.add: key must be string.");t=e.uniqueKey(this.inProgress,t);if("string"!=typeof t)throw new Error("Queue.add: an error occurred generating unique key.");return this.inProgress[t]=t,t},n.prototype.remove=function(t){if("string"!=typeof t)throw new Error("Queue.remove: key must be string.");delete this.inProgress[t],e.isEmpty(this.inProgress)&&this.executeAndClear()},n.prototype.getRemoveCb=function(e){var t;if("string"!=typeof e)throw new Error("Queue.getRemoveCb: key must be string.");return t=this,function(){t.remove(e)}},n.prototype.executeAndClear=function(){var e,t;e=-1,t=this.queue.length;for(;++e=1?s(o,u):(r=Math.sqrt(-2*Math.log(p)/p),a=c*r,n=h*r,i=!0,u*a+o))}}()},t.nextNormal=t.getNormalGenerator(),t.nextLogNormal=function(e,n){if("number"!=typeof e)throw new TypeError("nextLogNormal: mu must be number.");if("number"!=typeof n)throw new TypeError("nextLogNormal: sigma must be number.");return Math.exp(t.nextNormal(e,n))},t.nextExponential=function(e){if("number"!=typeof e)throw new TypeError("nextExponential: lambda must be number.");if(e<=0)throw new TypeError("nextExponential: lambda must be greater than 0.");return-Math.log(1-Math.random())/e},t.nextBinomial=function(e,t){var n,r;if("number"!=typeof e)throw new TypeError("nextBinomial: p must be number.");if("number"!=typeof t)throw new TypeError("nextBinomial: trials must be number.");if(e<0||e>1)throw new TypeError("nextBinomial: p must between 0 and 1.");if(t<1)throw new TypeError("nextBinomial: trials must be greater than 0.");n=0,r=0;while(n 0 or undefined. Found: "+t);if("undefined"!=typeof n){if("string"!=typeof n||n.trim()==="")throw new Error("randomString: chars must a non-empty string or undefined. Found: "+n)}else if(r)throw new Error("randomString: useChars is TRUE, but chars is undefined.");t=t||6,n=n||"a",i="";if(!r){n.indexOf("a")>-1&&(i+="abcdefghijklmnopqrstuvwxyz"),n.indexOf("A")>-1&&(i+="ABCDEFGHIJKLMNOPQRSTUVWXYZ"),n.indexOf("1")>-1&&(i+="0123456789"),n.indexOf("!")>-1&&(i+="!~`@#$%^&*()_+-={}[]:\";'<>?,./|\\"),u=n.indexOf("_");if(u>-1){u=n.charAt(u+1),u=e.isInt(u,0)||1;if(u===1)i+=" ";else if(u===2)i+=" ";else if(u===3)i+=" ";else{o=-1;for(;++o>>0;if(typeof e!="function")throw new TypeError;var r=[],i=arguments[1];for(var s=0;s=t)o.push(i(s)),s-=r;return o},n.each=function(e,t,n){var r,i;if("object"!=typeof e)throw new TypeError("ARRAY.each: array must be object. Found: "+e);if("function"!=typeof t)throw new TypeError("ARRAY.each: cb must be function. Found: "+t);n=n||this,i=e.length;for(r=0;r=t&&(s.push(a),f=0,a=[]),a.push(n[u]),n.splice(u,1),r=n.length,f++;return a.length>0&&s.push(a),s},n._latinSquare=function(t,r,i){i="undefined"==typeof i?!0:i;if(t===r&&!i)return!1;var s=[],o=[];for(var u=0;ue&&(t=e),n._latinSquare(e,t,!0))},n.latinSquareNoSelf=function(e,t){return t||(t=e-1),!e||e<0||t<0?!1:(t>e&&(t=e-1),n._latinSquare(e,t,!1))},n.generateCombinations=function r(e,t){var n,i,s,o,u;s=[];for(n=0;n0;s--)r=Math.floor(Math.random()*(s+1)),i=t[r],t[r]=t[s],t[s]=i;return t},n.getNRandom=function(e,t){return n.shuffle(e).slice(0,t)},n.distinct=function(e){var t=[];return e?(n.each(e,function(e){n.inArray(e,t)||t.push(e)}),t):t},n.transpose=function(e){if(!e)return;var t,r,i,s,o=[];t=e.length||0,r=n.isArray(e[0])?e[0].length:0;if(t===0||r===0)return o;for(i=0;it)throw new Error("DOM.generateUniqueId: could not find unique id within "+t+" trials.")}return s}}(),i.removeClass=function(e,t,n){var r;if(!n&&!i.isElement(e))throw new TypeError("DOM.removeClass: elem must be HTMLElement. Found: "+e);if(t){if("string"!=typeof t||t.trim()==="")throw new TypeError("DOM.removeClass: className must be HTMLElement. Found: "+t);r=new RegExp("(?:^|\\s)"+t+"(?!\\S)"),e.className=e.className.replace(r,"")}return e},i.addClass=function(e,t,n){if(!n&&!i.isElement(e))throw new TypeError("DOM.addClass: elem must be HTMLElement. Found: "+e);if(t){t instanceof Array&&(t=t.join(" "));if("string"!=typeof t||t.trim()==="")throw new TypeError("DOM.addClass: className must be HTMLElement. Found: "+t);e.className?e.className+=" "+t:e.className=t}return e},i.getElementsByClassName=function(e,t,n){var r,i,s,o,u,a;r=[],s=n||"*";if(e.evaluate){o="//"+s+'[contains(concat(" ", normalize-space(@class), " "), "'+t+' ")]',o=e.evaluate(o,e,null,0,null);while(i=o.iterateNext())r.push(i)}else{a=new RegExp("(^| )"+t+"( |$)"),o=e.getElementsByTagName(s);for(u=0;u1){var i=new Object(arguments[1]);for(var s in i)t.call(i,s)&&(r[s]=i[s])}return r}}():Object.create}(),r.equals=function(e,t){var n,i,s,o;n=typeof e,i=typeof t;if(n!==i)return!1;if("undefined"===n||"undefined"===i)return e===t;if(e===null||t===null)return e===t;if("number"===n&&isNaN(e)&&"number"===i&&isNaN(t))return isNaN(e)&&isNaN(t);s={number:"",string:"","boolean":""};if(n in s)return e===t;if("function"===n)return e.toString()===t.toString();for(o in e)if(e.hasOwnProperty(o)){if("undefined"==typeof t[o]&&"undefined"!=typeof e[o])return!1;if(!t[o]&&e[o])return!1;if("function"==typeof e[o]){if(e[o].toString()!==t[o].toString())return!1}else if(!r.equals(e[o],t[o]))return!1}for(o in t)if(t.hasOwnProperty(o)){if("undefined"==typeof e[o]&&"undefined"!=typeof t[o])return!1;if(!e[o]&&t[o])return!1}return!0},r.isEmpty=function(e){var t;if(!e)return!0;if("string"==typeof e)return e.trim()==="";if("number"==typeof e)return!1;if("function"==typeof e)return!1;for(t in e)if(e.hasOwnProperty(t))return!1;return!0},r.size=r.getListSize=function(e){var t,n;if(!e)return 0;if("number"==typeof e)return 0;if("string"==typeof e)return 0;t=0;for(n in e)e.hasOwnProperty(n)&&t++;return t},r._obj2Array=function(e,t,n,i){var s,o;if("object"!=typeof e)return[e];if(n){i="undefined"!=typeof i?i:1;if(i>n)return[e];i+=1}s=[];for(o in e)e.hasOwnProperty(o)&&(t&&s.push(o),"object"==typeof e[o]?s=s.concat(r._obj2Array(e[o],t,n,i)):s.push(e[o]));return s},r.obj2Array=function(e,t){return r._obj2Array(e,!1,t)},r.obj2KeyedArray=r.obj2KeyArray=function(e,t){return r._obj2Array(e,!0,t)},r.obj2QueryString=function(e){var t,n;if("object"!=typeof e)throw new TypeError("JSUS.objectToQueryString: obj must be object.");t=[];for(n in e)e.hasOwnProperty(n)&&t.push(encodeURIComponent(n)+"="+encodeURIComponent(e[n]));return"?"+t.join("&")},r.keys=function(){function t(e,r,i,s,o,u,a,f,l,c,h,p,d){var v,m,g,y;m=i===r;for(v in e)if(e.hasOwnProperty(v)){g="object"==typeof e[v];if(u||a&&(m||!g)||f&&m)o?(y=s+v,p[y]||(d?n(y,c,d):c.push(y))):p[v]||(h?h[v]||(d?n(v,c,d):c.push(v),h[v]=!0):d?n(v,c,d):c.push(v));g&&i1&&(n.push(i[1]),i.length>2&&n.push(i[2]))):function(){var e=-1,t=i.length;for(;++e=s)t(r,i,a);else{l=u||!e.isArray(r);for(f in r)r.hasOwnProperty(f)&&("object"==typeof r[f]&&s&&o+1<=s?n(r[f],i,o+1,l?a.concat(f):a):t(r[f],i,l?a.concat(f):a))}},function(e,t,a,f){var l;if("object"!=typeof e)throw new TypeError("JSUS.split: o must be object. Found: "+e);if("string"!=typeof t||t.trim()==="")throw new TypeError("JSUS.split: key must a non-empty string. Found: "+t);if(a&&("number"!=typeof a||a<0))throw new TypeError("JSUS.split: l must a non-negative number or undefined. Found: "+a);return i=r.clone(e),"object"!=typeof e[t]?[i]:(l=[],o=t,i[t]={},s="undefined"==typeof a?1:a,u=f,n(e[t],l,0,[]),o=undefined,i=undefined,s=undefined,u=undefined,l)}}(),r.melt=function(e,t){var n={},r=t.length;for(var i=0;in)return}return r},r.randomKey=function(e){var t;if("object"!=typeof e)throw new TypeError("OBJ.randomKey: obj must be object. Found: "+e);return t=Object.keys(e),t[t.length*Math.random()<<0]},r.augment=function(e,t,n){var i,s;n=n||r.keys(e);for(i=0;in:e>=n)?!1:e)},n.isEmail=function(e){var t;return"string"!=typeof e?!1:e.trim().length<5?!1:(t=e.indexOf("@"),t===-1||t===0||t===e.length-1?!1:(t=e.lastIndexOf("."),t===-1||t===e.length-1||t>t+1?!1:!0))},n.range=function(t,i){var s,o,u,a,f,l,c,h,p,d,v;a=[];if("undefined"==typeof t)return a;if("number"==typeof t)t=""+t;else if("string"!=typeof t)throw new TypeError("PARSE.range: expr must be string, number, undefined. Found: "+t);if("undefined"==typeof i)i=t;else if(e.isArray(i)){if(i.length===0)return a;f=Math.min.apply(null,i),l=Math.max.apply(null,i)}else if("object"==typeof i){if("function"!=typeof i.next)throw new TypeError("PARSE.range: available.next must be function. Found: "+i.next);if("function"!=typeof i.isFinished)throw new TypeError("PARSE.range: available.isFinished must be function. Found: "+i.isFinished);if("number"!=typeof i.begin)throw new TypeError("PARSE.range: available.begin must be number. Found: "+i.begin);if("number"!=typeof i.end)throw new TypeError("PARSE.range: available.end must be number. Found: "+i.end);f=i.begin,l=i.end}else{if("string"!=typeof i)throw new TypeError("PARSE.range: available must be string, array, object or undefined. Found: "+i);i=r(i),h=i.match(/([-+]?\d+)/g);if(h===null)throw new Error("PARSE.range: no numbers in available: "+i);c=Math.min.apply(null,h),i=n.range(i,{begin:c,end:Math.max.apply(null,h),value:c,next:function(){return this.value++},isFinished:function(){return this.value>this.end}}),f=Math.min.apply(null,i),l=Math.max.apply(null,i)}t=t.replace(/end/g,parseInt(l,10)),t=t.replace(/begin/g,parseInt(f,10)),t=r(t),t=t.replace(/([-+]?\d+\.\d+)/g,function(e,t){return parseInt(t,10)}),p=/[^ \*\d<>=!\|&\.\[\],\(\)\-\+%]/g;if(t.match(p))throw new Error("PARSE.range: invalid characters found: "+t);t=t.replace(/([^& ]) *& *([^& ])/g,"$1&&$2"),t=t.replace(/([^| ]) *\| *([^| ])/g,"$1||$2"),t=t.replace(/([-+]?\d+)/g,"(x==$1)"),t=t.replace(/% *\(x==([-+]?\d+)\)/,"!(x%$1)"),t=t.replace(/!\(x%([-+]?\d+)\) *={1,} *\(x==([-+]?\d+)\)/g,"(x%$1==$2)"),t=t.replace(/([<>]=?) *\(x==([-+]?\d+)\)/g,"(x$1$2)"),t=t.replace(/\(x==([-+]?\d+)\)\.{2,}\(x==(\+?\d+)\)\.{2,}\(x==([-+]?\d+)\)/g,"(x>=$1&&x<=$3&&!((x- $1)%$2))"),t=t.replace(/\(x==([-+]?\d+)\)\.{2,}\(x==(-\d+)\)\.{2,}\(x==([-+]?\d+)\)/g,"(x<=$1&&x>=$3&&!((x- $1)%$2))"),t=t.replace(/\(x==([-+]?\d+)\)\.{2,}\(x==([-+]?\d+)\)/g,"(x>=$1&&x<=$2)"),t=t.replace(/([(\[]) *\(x==([-+]?\d+)\) *, *\(x==([-+]?\d+)\) *([\])])/g,function(e,t,n,r,i){return"(x>"+(t=="("?"":"=")+n+"&&x<"+(i==")"?"":"=")+r+")"}),t=t.replace("*",1),t=t.replace(/\s/g,""),t=t.replace(/\)[,] *(!*)\(/g,")||$1("),p=/[^ \d<>=!\|&,\(\)\-\+%x\.]/g,d=/[^ &!|\(] *\(/g,v=/\.[^\d]|[^\d]\./;if(t.match(p))throw new Error("PARSE.range: invalid characters found: "+t);if(t.match(d))throw new Error("PARSE.range: invalid character before opending bracket found: "+t);if(t.match(v))throw new Error("PARSE.range: invalid dot found: "+t);if(e.isArray(i)){s=-1,o=i.length;for(;++s1?n[1].trim():""},"undefined"!=typeof JSUS?(e=JSUS,JSUS.extend(n)):(t=function(t){e=t},module.exports=[n,t])}(),function(){"use strict";function r(){this.queue=[],this.inProgress={}}var e,t,n={};n.getQueue=function(){return new r},r.prototype.isReady=function(){return e.isEmpty(this.inProgress)},r.prototype.onReady=function(t){if("function"!=typeof t)throw new TypeError("Queue.onReady: cb must be function. Found: "+t);e.isEmpty(this.inProgress)?t():this.queue.push(t)},r.prototype.add=function(t){if(t&&"string"!=typeof t)throw new Error("Queue.add: key must be string.");t=e.uniqueKey(this.inProgress,t);if("string"!=typeof t)throw new Error("Queue.add: an error occurred generating unique key.");return this.inProgress[t]=t,t},r.prototype.remove=function(t){if("string"!=typeof t)throw new Error("Queue.remove: key must be string.");delete this.inProgress[t],e.isEmpty(this.inProgress)&&this.executeAndClear()},r.prototype.getRemoveCb=function(e){var t;if("string"!=typeof e)throw new Error("Queue.getRemoveCb: key must be string.");return t=this,function(){t.remove(e)}},r.prototype.executeAndClear=function(){var e,t;e=-1,t=this.queue.length;for(;++e=1?s(o,u):(r=Math.sqrt(-2*Math.log(p)/p),a=c*r,n=h*r,i=!0,u*a+o))}}()},n.nextNormal=n.getNormalGenerator(),n.nextLogNormal=function(e,t){if("number"!=typeof e)throw new TypeError("nextLogNormal: mu must be number.");if("number"!=typeof t)throw new TypeError("nextLogNormal: sigma must be number.");return Math.exp(n.nextNormal(e,t))},n.nextExponential=function(e){if("number"!=typeof e)throw new TypeError("nextExponential: lambda must be number.");if(e<=0)throw new TypeError("nextExponential: lambda must be greater than 0.");return-Math.log(1-Math.random())/e},n.nextBinomial=function(e,t){var n,r;if("number"!=typeof e)throw new TypeError("nextBinomial: p must be number.");if("number"!=typeof t)throw new TypeError("nextBinomial: trials must be number.");if(e<0||e>1)throw new TypeError("nextBinomial: p must between 0 and 1.");if(t<1)throw new TypeError("nextBinomial: trials must be greater than 0.");n=0,r=0;while(n 0 or undefined. Found: "+t);if("undefined"!=typeof n){if("string"!=typeof n||n.trim()==="")throw new Error("randomString: chars must a non-empty string or undefined. Found: "+n)}else if(r)throw new Error("randomString: useChars is TRUE, but chars is undefined.");t=t||6,n=n||"a",i="";if(!r){n.indexOf("a")>-1&&(i+="abcdefghijklmnopqrstuvwxyz"),n.indexOf("A")>-1&&(i+="ABCDEFGHIJKLMNOPQRSTUVWXYZ"),n.indexOf("1")>-1&&(i+="0123456789"),n.indexOf("!")>-1&&(i+="!~`@#$%^&*()_+-={}[]:\";'<>?,./|\\"),u=n.indexOf("_");if(u>-1){u=n.charAt(u+1),u=e.isInt(u,0)||1;if(u===1)i+=" ";else if(u===2)i+=" ";else if(u===3)i+=" ";else{o=-1;for(;++o + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Collection of static functions to manipulate arrays */ -(function(JSUS) { +(function() { "use strict"; + var _J, init; + function ARRAY() {} /** @@ -97,7 +99,7 @@ if (end === Infinity) return false; // TODO: increment zero might be fine if start=end. Check. if (increment === 0) return false; - if (!JSUS.inArray(typeof increment, ['undefined', 'number'])) { + if (!ARRAY.inArray(typeof increment, ['undefined', 'number'])) { return false; } if (start === end) { @@ -176,12 +178,12 @@ func = arguments[1]; if (!ARRAY.isArray(array)) { - JSUS.log('ARRAY.map: first parameter must be array. Found: ' + + console.log('ARRAY.map: first parameter must be array. Found: ' + array); return; } if ('function' !== typeof func) { - JSUS.log('ARRAY.map: second parameter must be function. Found: ' + + console.log('ARRAY.map: second parameter must be function. Found: ' + func); return; } @@ -229,7 +231,7 @@ if ('undefined' === typeof needle || !haystack) return false; if ('object' === typeof needle) { - func = JSUS.equals; + func = _J.equals; } else { func = function(a, b) { @@ -264,7 +266,7 @@ ARRAY.inArray = function(needle, haystack) { var func, i, len; if (!haystack) return false; - func = JSUS.equals; + func = _J.equals; len = haystack.length; for (i = 0; i < len; i++) { if (func.call(this, needle, haystack[i])) { @@ -393,9 +395,9 @@ for (i=0; i < N; i++) { do { - idx = JSUS.randomInt(start,limit); + idx = _J.randomInt(start,limit); } - while (JSUS.inArray(idx, extracted)); + while (ARRAY.inArray(idx, extracted)); extracted.push(idx); if (idx == 1) { @@ -557,7 +559,7 @@ if (!ARRAY.isArray(array)) array = [ array ]; if (!times) return array.slice(0); if (times < 1) { - JSUS.log('times must be greater or equal 1', 'ERR'); + console.log('times must be greater or equal 1', 'ERR'); return; } i = 1; @@ -604,7 +606,7 @@ if (!times) return array.slice(0); if ('number' === typeof times) { if (times < 1) { - JSUS.log('times must be greater or equal 1', 'ERR'); + console.log('times must be greater or equal 1', 'ERR'); return; } times = ARRAY.rep([times], array.length); @@ -635,7 +637,7 @@ */ ARRAY.arrayIntersect = function(a1, a2) { return a1.filter( function(i) { - return JSUS.inArray(i, a2); + return ARRAY.inArray(i, a2); }); }; @@ -653,7 +655,7 @@ */ ARRAY.arrayDiff = function(a1, a2) { return a1.filter( function(i) { - return !(JSUS.inArray(i, a2)); + return !(ARRAY.inArray(i, a2)); }); }; @@ -755,6 +757,14 @@ return t; }; - JSUS.extend(ARRAY); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(ARRAY); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ ARRAY, init ]; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); diff --git a/lib/compatibility.js b/lib/compatibility.js index 20b6c49..851d8d6 100644 --- a/lib/compatibility.js +++ b/lib/compatibility.js @@ -1,16 +1,18 @@ /** * # COMPATIBILITY * - * Copyright(c) 2015 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Tests browsers ECMAScript 5 compatibility * * For more information see http://kangax.github.com/es5-compat-table/ */ -(function(JSUS) { +(function() { "use strict"; + var _J, init; + function COMPATIBILITY() {} /** @@ -58,6 +60,14 @@ }; - JSUS.extend(COMPATIBILITY); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(COMPATIBILITY); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ COMPATIBILITY, init ]; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); diff --git a/lib/dom.js b/lib/dom.js index f7ac3a1..319f769 100644 --- a/lib/dom.js +++ b/lib/dom.js @@ -1,16 +1,17 @@ /** * # DOM - * Copyright(c) 2019 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Helper library to perform generic operation with DOM elements. */ -(function(JSUS) { +(function() { "use strict"; - + + var _J, init; var onFocusChange, changeTitle; - + function DOM() {} // ## GET/ADD @@ -174,7 +175,7 @@ DOM.write = function(root, text) { var content; if ('undefined' === typeof text || text === null) text = ""; - if (JSUS.isNode(text) || JSUS.isElement(text)) content = text; + if (DOM.isNode(text) || DOM.isElement(text)) content = text; else content = document.createTextNode(text); root.appendChild(content); return content; @@ -189,7 +190,7 @@ */ DOM.write2 = function(root, text) { if ('undefined' === typeof text) text = ""; - if (JSUS.isNode(text) || JSUS.isElement(text)) root.appendChild(text); + if (DOM.isNode(text) || DOM.isElement(text)) root.appendChild(text); else root.innerHTML += text; }; @@ -298,7 +299,7 @@ idx_finish = string.indexOf(key, idx_replace); if (idx_finish === -1) { - JSUS.log('Error. Could not find closing key: ' + key); + console.log('Error. Could not find closing key: ' + key); continue; } @@ -316,20 +317,20 @@ break; default: - JSUS.log('Identifier not in [!,@,%]: ' + key[0]); + console.log('Identifier not in [!,@,%]: ' + key[0]); } } } // No span to create, return what we have. - if (!JSUS.size(spans)) { + if (!_J.size(spans)) { return root.appendChild(document.createTextNode(string)); } // Re-assamble the string. - idxs = JSUS.keys(spans).sort(function(a, b){ return a - b; }); + idxs = _J.keys(spans).sort(function(a, b){ return a - b; }); idx_finish = 0; for (i = 0; i < idxs.length; i++) { @@ -436,12 +437,12 @@ DOM.shuffleElements = function(parent, order, cb) { var i, len, numOrder, idOrder, children, child; var id; - if (!JSUS.isNode(parent)) { + if (!DOM.isNode(parent)) { throw new TypeError('DOM.shuffleElements: parent must be a node. ' + 'Found: ' + parent); } if (!parent.children || !parent.children.length) { - JSUS.log('DOM.shuffleElements: parent has no children.', 'ERR'); + console.log('DOM.shuffleElements: parent has no children.', 'ERR'); return false; } if (order) { @@ -449,7 +450,7 @@ cb = order; } else { - if (!JSUS.isArray(order)) { + if (!_J.isArray(order)) { throw new TypeError('DOM.shuffleElements: order must be ' + 'array. Found: ' + order); } @@ -481,7 +482,7 @@ len = children.length; idOrder = new Array(len); if (cb) numOrder = new Array(len); - if (!order) order = JSUS.sample(0, (len-1)); + if (!order) order = _J.sample(0, (len-1)); for (i = 0 ; i < len; i++) { id = children[order[i]].id; if ('string' !== typeof id || id === "") { @@ -591,7 +592,7 @@ throw new Error('DOM.addCSS: root is undefined, and could not ' + 'detect a valid root for css: ' + cssPath); } - attributes = JSUS.mixin({ + attributes = _J.mixin({ rel : 'stylesheet', type: 'text/css', href: cssPath @@ -624,7 +625,7 @@ throw new Error('DOM.addCSS: root is undefined, and could not ' + 'detect a valid root for css: ' + jsPath); } - attributes = JSUS.mixin({ + attributes = _J.mixin({ charset : 'utf-8', type: 'text/javascript', src: jsPath @@ -785,7 +786,7 @@ } } else { - prefix = JSUS.randomString(8, 'a'); + prefix = _J.randomString(8, 'a'); } id = prefix + '_'; @@ -799,7 +800,7 @@ found = true; counter = -1; while (found) { - id = prefix + '_' + JSUS.randomInt(1000); + id = prefix + '_' + _J.randomInt(1000); found = scanDocuments(windows, id); if (++counter > limit) { throw new Error('DOM.generateUniqueId: could not ' + @@ -1137,7 +1138,7 @@ disable = 'undefined' === typeof disable ? true : disable; if (disable && !isDisabled) { if (!history.pushState || !history.go) { - JSUS.log('DOM.disableBackButton: method not ' + + console.log('DOM.disableBackButton: method not ' + 'supported by browser.'); return null; } @@ -1251,7 +1252,7 @@ // Option repeatFor. if ('undefined' !== typeof options.repeatFor) { - nRepeats = JSUS.isInt(options.repeatFor, 0); + nRepeats = _J.isInt(options.repeatFor, 0); if (false === nRepeats) { throw new TypeError(where + 'options.repeatFor must be ' + 'a positive integer. Found: ' + @@ -1261,7 +1262,7 @@ // Option stopOnFocus. if (options.stopOnFocus) { - JSUS.onFocusIn(function() { + DOM.onFocusIn(function() { clearBlinkInterval(); onFocusChange(null, null); }); @@ -1284,8 +1285,8 @@ // Option startOnBlur. if (options.startOnBlur) { options.startOnBlur = null; - JSUS.onFocusOut(function() { - JSUS.blinkTitle(titles, options); + DOM.onFocusOut(function() { + DOM.blinkTitle(titles, options); }); return null; } @@ -1294,7 +1295,7 @@ if ('string' === typeof titles) { titles = [titles, '!!!']; } - else if (!JSUS.isArray(titles)) { + else if (!_J.isArray(titles)) { throw new TypeError(where + 'titles must be string, ' + 'array of strings or undefined. Found: ' + titles); @@ -1460,7 +1461,7 @@ if (!document) { return function() { - JSUS.log('onFocusChange: no document detected.'); + console.log('onFocusChange: no document detected.'); return; }; } @@ -1540,6 +1541,13 @@ } }; - JSUS.extend(DOM); - -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(DOM); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ DOM, init ]; + } +})(); diff --git a/lib/dom_legacy.js b/lib/dom_legacy.js index f0631af..76f9a39 100644 --- a/lib/dom_legacy.js +++ b/lib/dom_legacy.js @@ -27,7 +27,7 @@ * Only the methods which do not follow the above-mentioned syntax * will receive further explanation. */ -(function(JSUS) { +(function() { "use strict"; @@ -1568,6 +1568,13 @@ } }; - JSUS.extend(DOM); -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); + if ('undefined' !== typeof JSUS) { + JSUS.extend(DOM); + } + // Node.JS ESM or CJS + else { + module.exports = DOM; + } + +})(); diff --git a/lib/eval.js b/lib/eval.js index e35499d..c0123df 100644 --- a/lib/eval.js +++ b/lib/eval.js @@ -1,14 +1,16 @@ /** * # EVAL - * Copyright(c) 2015 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Evaluation of strings as JavaScript commands */ -(function(JSUS) { +(function() { "use strict"; + var _J, init; + function EVAL() {} /** @@ -50,6 +52,15 @@ return func.call(context, str); }; - JSUS.extend(EVAL); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(EVAL); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ EVAL, init ]; + } + -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); diff --git a/lib/fs.js b/lib/fs.js index 1883596..decb17f 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1,6 +1,6 @@ /** * # FS - * Copyright(c) 2018 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Collection of static functions related to file system operations. @@ -11,19 +11,15 @@ * @see https://github.com/jprichardson/node-fs-extra * @see https://github.com/substack/node-resolve */ -(function(JSUS) { +(function() { "use strict"; - if (!JSUS.isNodeJS()){ - JSUS.log('Cannot load JSUS.FS outside of Node.JS.'); - return false; - } - - var resolve = require('resolve'), - path = require('path'), - fs = require('fs'); + const resolve = require('resolve'); + const path = require('path'); + const fs = require('fs'); + var _J, init; function FS() {} @@ -133,7 +129,7 @@ FS.cleanDir = function(dir, ext, cb) { var filterFunc; if (!dir) { - JSUS.log('You must specify a directory to clean.'); + console.log('You must specify a directory to clean.'); return false; } if (ext) { @@ -152,11 +148,11 @@ fs.readdir(dir, function(err, files) { var asq, mycb; if (err) { - JSUS.log(err); + console.log(err); return; } // Create async queue if a callback was specified. - if (cb) asq = JSUS.getQueue(); + if (cb) asq = _J.getQueue(); // Create a nested callback for the async queue, if necessary. files.filter(filterFunc).forEach(function(file) { @@ -164,7 +160,7 @@ asq.add(file); mycb = asq.getRemoveCb(file); } - JSUS.deleteIfExists(dir + file, mycb); + FS.deleteIfExists(dir + file, mycb); }); if (cb) { @@ -201,11 +197,11 @@ FS.copyFromDir = function(dirIn, dirOut, ext, cb) { var i, dir, dirs, stats; if (!dirIn) { - JSUS.log('You must specify a source directory.'); + console.log('You must specify a source directory.'); return false; } if (!dirOut) { - JSUS.log('You must specify a destination directory.'); + console.log('You must specify a destination directory.'); return false; } @@ -229,11 +225,11 @@ fs.readdir(dirIn, function(err, files) { var asq, i, mycb; if (err) { - JSUS.log(err); + console.log(err); throw new Error(); } // Create async queue if a callback was specified. - if (cb) asq = JSUS.getQueue(); + if (cb) asq = _J.getQueue(); for (i in files) { if (ext && path.extname(files[i]) !== ext) { continue; @@ -279,6 +275,7 @@ return fdr.pipe(fdw); }; - JSUS.extend(FS); + init = function(J) { _J = J; }; + module.exports = [ FS, init ]; -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); diff --git a/lib/obj.js b/lib/obj.js index c2e474b..352f1f8 100644 --- a/lib/obj.js +++ b/lib/obj.js @@ -1,21 +1,17 @@ /** * # OBJ - * Copyright(c) 2019 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Collection of static functions to manipulate JavaScript objects */ -(function(JSUS) { +(function() { "use strict"; - function OBJ() {} - - var compatibility = null; + var _J, init, compatibility; - if ('undefined' !== typeof JSUS.compatibility) { - compatibility = JSUS.compatibility(); - } + function OBJ() {} /** * ## OBJ.createObj @@ -468,7 +464,7 @@ res.push(tmp); } // If array, expand it. - else if (JSUS.isArray(tmp) && tmp.length) { + else if (_J.isArray(tmp) && tmp.length) { if (tmp.length < 4) { res.push(tmp[0]); if (tmp.length > 1) { @@ -635,12 +631,12 @@ if (obj && 'object' === typeof obj) { clone = Object.prototype.toString.call(obj) === '[object Array]' ? - [] : JSUS.createObj(obj.constructor.prototype); + [] : OBJ.createObj(obj.constructor.prototype); for (i in obj) { if (obj.hasOwnProperty(i)) { if (obj[i] && 'object' === typeof obj[i]) { - clone[i] = JSUS.classClone(obj[i], depth - 1); + clone[i] = OBJ.classClone(obj[i], depth - 1); } else { clone[i] = obj[i]; @@ -650,7 +646,7 @@ return clone; } else { - return JSUS.clone(obj); + return OBJ.clone(obj); } }; @@ -963,7 +959,7 @@ OBJ.setNestedValue = function(str, value, obj) { var keys, k; if (!str) { - JSUS.log('Cannot set value of undefined property', 'ERR'); + console.log('Cannot set value of undefined property', 'ERR'); return false; } obj = ('object' === typeof obj) ? obj : {}; @@ -1127,18 +1123,18 @@ makeClone = function(value, out, keys) { var i, len, tmp, copy; - copy = JSUS.clone(model); + copy = OBJ.clone(model); switch(keys.length) { case 0: - copy[_key] = JSUS.clone(value); + copy[_key] = OBJ.clone(value); break; case 1: - copy[_key][keys[0]] = JSUS.clone(value); + copy[_key][keys[0]] = OBJ.clone(value); break; case 2: copy[_key][keys[0]] = {}; - copy[_key][keys[0]][keys[1]] = JSUS.clone(value); + copy[_key][keys[0]][keys[1]] = OBJ.clone(value); break; default: i = -1, len = keys.length-1; @@ -1147,7 +1143,7 @@ tmp[keys[i]] = {}; tmp = tmp[keys[i]]; } - tmp[keys[keys.length-1]] = JSUS.clone(value); + tmp[keys[keys.length-1]] = OBJ.clone(value); } out.push(copy); return; @@ -1162,7 +1158,7 @@ } else { - curPosAsKey = posAsKeys || !JSUS.isArray(value); + curPosAsKey = posAsKeys || !_J.isArray(value); for (i in value) { if (value.hasOwnProperty(i)) { @@ -1197,7 +1193,7 @@ throw new TypeError('JSUS.split: l must a non-negative ' + 'number or undefined. Found: ' + l); } - model = JSUS.clone(o); + model = OBJ.clone(o); if ('object' !== typeof o[key]) return [model]; // Init. out = []; @@ -1264,7 +1260,7 @@ OBJ.uniqueKey = function(obj, prefixName, stop) { var name, duplicateCounter; if (!obj) { - JSUS.log('Cannot find unique name in undefined object', 'ERR'); + console.log('Cannot find unique name in undefined object', 'ERR'); return; } duplicateCounter = 1; @@ -1473,6 +1469,17 @@ return res; }; - JSUS.extend(OBJ); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(OBJ); + } + // Node.JS ESM or CJS + else { + init = function(J) { + _J = J; + compatibility = _J.compatibility(); + }; + module.exports = [ OBJ, init ]; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); diff --git a/lib/parse.js b/lib/parse.js index 7ef3fad..5a38ded 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1,14 +1,16 @@ /** * # PARSE - * Copyright(c) 2017 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Collection of static functions related to parsing strings */ -(function(JSUS) { +(function() { "use strict"; + var _J, init; + function PARSE() {} /** @@ -114,7 +116,7 @@ pattern = '['; - JSUS.each(separators, function(s) { + separators.forEach(function(s) { if (s === ' ') s = '\\s'; pattern += s; @@ -248,7 +250,7 @@ return value; } else if (value.substring(0, len_func) === PARSE.marker_func) { - return JSUS.eval(value.substring(len_prefix)); + return _J.eval(value.substring(len_prefix)); } else if (value.substring(0, len_null) === PARSE.marker_null) { return null; @@ -470,7 +472,7 @@ if ('undefined' === typeof available) { available = expr; } - else if (JSUS.isArray(available)) { + else if (_J.isArray(available)) { if (available.length === 0) return solution; begin = Math.min.apply(null, available); end = Math.max.apply(null, available); @@ -619,11 +621,11 @@ throw new Error('PARSE.range: invalid dot found: ' + expr); } - if (JSUS.isArray(available)) { + if (_J.isArray(available)) { i = -1, len = available.length; for ( ; ++i < len ; ) { x = parseInt(available[i], 10); - if (JSUS.eval(expr.replace(/x/g, x))) { + if (_J.eval(expr.replace(/x/g, x))) { solution.push(x); } } @@ -631,7 +633,7 @@ else { while (!available.isFinished()) { x = parseInt(available.next(), 10); - if (JSUS.eval(expr.replace(/x/g, x))) { + if (_J.eval(expr.replace(/x/g, x))) { solution.push(x); } } @@ -709,6 +711,14 @@ }; } - JSUS.extend(PARSE); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(PARSE); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ PARSE, init ]; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); diff --git a/lib/queue.js b/lib/queue.js index c41b44d..1eea708 100644 --- a/lib/queue.js +++ b/lib/queue.js @@ -1,14 +1,16 @@ /** * # QUEUE - * Copyright(c) 2015 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Handles a simple queue of operations */ -(function(JSUS) { +(function() { "use strict"; + var _J, init; + var QUEUE = {}; QUEUE.getQueue = function() { @@ -43,7 +45,7 @@ * @return {boolean} TRUE, if no operation is in progress */ Queue.prototype.isReady = function() { - return JSUS.isEmpty(this.inProgress); + return _J.isEmpty(this.inProgress); }; /** @@ -63,7 +65,7 @@ throw new TypeError('Queue.onReady: cb must be function. Found: ' + cb); } - if (JSUS.isEmpty(this.inProgress)) cb(); + if (_J.isEmpty(this.inProgress)) cb(); else this.queue.push(cb); }; @@ -80,7 +82,7 @@ if (key && 'string' !== typeof key) { throw new Error('Queue.add: key must be string.'); } - key = JSUS.uniqueKey(this.inProgress, key); + key = _J.uniqueKey(this.inProgress, key); if ('string' !== typeof key) { throw new Error('Queue.add: an error occurred ' + 'generating unique key.'); @@ -101,7 +103,7 @@ throw new Error('Queue.remove: key must be string.'); } delete this.inProgress[key]; - if (JSUS.isEmpty(this.inProgress)) { + if (_J.isEmpty(this.inProgress)) { this.executeAndClear(); } }; @@ -141,6 +143,14 @@ } }; - JSUS.extend(QUEUE); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(QUEUE); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ QUEUE, init ]; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); diff --git a/lib/random.js b/lib/random.js index cb3ed5c..861ffd2 100644 --- a/lib/random.js +++ b/lib/random.js @@ -1,14 +1,16 @@ /** * # RANDOM - * Copyright(c) 2017 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Generates pseudo-random numbers */ -(function(JSUS) { +(function() { "use strict"; + var _J, init; + function RANDOM() {} /** @@ -122,9 +124,9 @@ */ RANDOM.sample = function(a, b) { var out; - out = JSUS.seq(a,b); + out = _J.seq(a,b); if (!out) return false; - return JSUS.shuffle(out); + return _J.shuffle(out); }; /** @@ -419,7 +421,7 @@ if (nSpaces > -1) { nSpaces = chars.charAt(nSpaces + 1); // nSpaces is integer > 0 or 1. - nSpaces = JSUS.isInt(nSpaces, 0) || 1; + nSpaces = _J.isInt(nSpaces, 0) || 1; if (nSpaces === 1) mask += ' '; else if (nSpaces === 2) mask += ' '; else if (nSpaces === 3) mask += ' '; @@ -457,6 +459,14 @@ RANDOM.randomString(RANDOM.randomInt(2,3)); }; - JSUS.extend(RANDOM); - -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(RANDOM); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ RANDOM, init ]; + } + +})(); diff --git a/lib/time.js b/lib/time.js index d1dc4f9..b92e890 100644 --- a/lib/time.js +++ b/lib/time.js @@ -1,15 +1,17 @@ /** * # TIME - * Copyright(c) 2021 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Collection of static functions related to the generation, * manipulation, and formatting of time strings in JavaScript */ -(function (JSUS) { +(function() { "use strict"; + var _J, init; + function TIME() {} function pad(number) { @@ -132,6 +134,14 @@ TIME.now = 'function' === typeof Date.now ? Date.now : function() { return new Date().getTime(); } - JSUS.extend(TIME); + if ('undefined' !== typeof JSUS) { + _J = JSUS; + JSUS.extend(TIME); + } + // Node.JS ESM or CJS + else { + init = function(J) { _J = J; }; + module.exports = [ TIME, init ]; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})();