From f152b05a5d673434982eb75a17986ff13e6a28a9 Mon Sep 17 00:00:00 2001 From: Stefano Balietti Date: Wed, 5 Mar 2025 17:32:35 +0100 Subject: [PATCH 1/4] works with ESM --- jsus.js | 33 +++++++++++++------- lib/array.js | 45 +++++++++++++++++++--------- lib/compatibility.js | 12 ++++++-- lib/dom.js | 71 +++++++++++++++++++++++++++++--------------- lib/dom_legacy.js | 13 ++++++-- lib/eval.js | 12 ++++++-- lib/fs.js | 34 ++++++++++----------- lib/obj.js | 51 ++++++++++++++++++++----------- lib/parse.js | 34 +++++++++++++++------ lib/queue.js | 31 ++++++++++++++----- lib/random.js | 30 +++++++++++++++---- lib/time.js | 12 ++++++-- 12 files changed, 260 insertions(+), 118 deletions(-) diff --git a/jsus.js b/jsus.js index e18be01..353b93f 100644 --- a/jsus.js +++ b/jsus.js @@ -9,6 +9,8 @@ * --- */ (function(exports) { + + var tmpClass; var JSUS = exports.JSUS = {}; @@ -143,17 +145,28 @@ // ## 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'); + tmpClass = require('./lib/compatibility'); + JSUS.extend(tmpClass); + tmpClass = require('./lib/obj'); + JSUS.extend(tmpClass); + tmpClass = require('./lib/array'); + JSUS.extend(tmpClass); + tmpClass = require('./lib/time'); + JSUS.extend(tmpClass); + tmpClass = require('./lib/eval'); + JSUS.extend(tmpClass); + tmpClass = require('./lib/dom'); + JSUS.extend(tmpClass); + tmpClass = require('./lib/random'); + JSUS.extend(tmpClass); + tmpClass = require('./lib/parse'); + JSUS.extend(tmpClass); + tmpClass = require('./lib/queue'); + JSUS.extend(tmpClass); + tmpClass = require('./lib/fs'); + JSUS.extend(tmpClass); } + else { // Exports J in the browser. exports.J = exports.JSUS; diff --git a/lib/array.js b/lib/array.js index 7096af4..031cb56 100644 --- a/lib/array.js +++ b/lib/array.js @@ -5,10 +5,20 @@ * * Collection of static functions to manipulate arrays */ -(function(JSUS) { +(function() { "use strict"; + var randomInt, equals; + if ('undefined' !== typeof JSUS) { + randomInt = JSUS.randomInt; + equals = JSUS.equals; + } + else { + randomInt = require('./random').randomInt; + equals = require('./obj').equals; + } + function ARRAY() {} /** @@ -97,7 +107,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 +186,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 +239,7 @@ if ('undefined' === typeof needle || !haystack) return false; if ('object' === typeof needle) { - func = JSUS.equals; + func = equals; } else { func = function(a, b) { @@ -264,7 +274,7 @@ ARRAY.inArray = function(needle, haystack) { var func, i, len; if (!haystack) return false; - func = JSUS.equals; + func = equals; len = haystack.length; for (i = 0; i < len; i++) { if (func.call(this, needle, haystack[i])) { @@ -393,9 +403,9 @@ for (i=0; i < N; i++) { do { - idx = JSUS.randomInt(start,limit); + idx = randomInt(start,limit); } - while (JSUS.inArray(idx, extracted)); + while (ARRAY.inArray(idx, extracted)); extracted.push(idx); if (idx == 1) { @@ -557,7 +567,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 +614,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 +645,7 @@ */ ARRAY.arrayIntersect = function(a1, a2) { return a1.filter( function(i) { - return JSUS.inArray(i, a2); + return ARRAY.inArray(i, a2); }); }; @@ -653,7 +663,7 @@ */ ARRAY.arrayDiff = function(a1, a2) { return a1.filter( function(i) { - return !(JSUS.inArray(i, a2)); + return !(ARRAY.inArray(i, a2)); }); }; @@ -755,6 +765,13 @@ return t; }; - JSUS.extend(ARRAY); -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); + if ('undefined' !== typeof JSUS) { + JSUS.extend(ARRAY); + } + // Node.JS ESM or CJS + else { + module.exports = ARRAY; + } + +})(); diff --git a/lib/compatibility.js b/lib/compatibility.js index 20b6c49..69b625d 100644 --- a/lib/compatibility.js +++ b/lib/compatibility.js @@ -8,7 +8,7 @@ * * For more information see http://kangax.github.com/es5-compat-table/ */ -(function(JSUS) { +(function() { "use strict"; function COMPATIBILITY() {} @@ -58,6 +58,12 @@ }; - JSUS.extend(COMPATIBILITY); + if ('undefined' !== typeof JSUS) { + JSUS.extend(COMPATIBILITY); + } + // Node.JS ESM or CJS + else { + module.exports = COMPATIBILITY; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); diff --git a/lib/dom.js b/lib/dom.js index f7ac3a1..ef0fd3d 100644 --- a/lib/dom.js +++ b/lib/dom.js @@ -5,10 +5,27 @@ * * Helper library to perform generic operation with DOM elements. */ -(function(JSUS) { +(function() { "use strict"; + var isArray, randomInt, randomString, keys, size, sample, isInt, mixin; + if ('undefined' !== typeof JSUS) { + isArray = JSUS.isArray; + randomInt = JSUS.randomInt; + randomString = JSUS.randomString; + keys = JSUS.keys; + size = JSUS.size; + sample = JSUS.sample; + isInt = JSUS.isInt; + } + else { + isInt = require('./parse').isInt; + isArray = require('./array').isArray; + ({ keys, mixin, size } = require('./obj')); + ({ sample, randomString, randomInt } = require('./random')); + } + var onFocusChange, changeTitle; function DOM() {} @@ -174,7 +191,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 +206,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 +315,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 +333,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 (!size(spans)) { return root.appendChild(document.createTextNode(string)); } // Re-assamble the string. - idxs = JSUS.keys(spans).sort(function(a, b){ return a - b; }); + idxs = keys(spans).sort(function(a, b){ return a - b; }); idx_finish = 0; for (i = 0; i < idxs.length; i++) { @@ -436,12 +453,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 +466,7 @@ cb = order; } else { - if (!JSUS.isArray(order)) { + if (!isArray(order)) { throw new TypeError('DOM.shuffleElements: order must be ' + 'array. Found: ' + order); } @@ -481,7 +498,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 = sample(0, (len-1)); for (i = 0 ; i < len; i++) { id = children[order[i]].id; if ('string' !== typeof id || id === "") { @@ -591,7 +608,7 @@ throw new Error('DOM.addCSS: root is undefined, and could not ' + 'detect a valid root for css: ' + cssPath); } - attributes = JSUS.mixin({ + attributes = mixin({ rel : 'stylesheet', type: 'text/css', href: cssPath @@ -624,7 +641,7 @@ throw new Error('DOM.addCSS: root is undefined, and could not ' + 'detect a valid root for css: ' + jsPath); } - attributes = JSUS.mixin({ + attributes = mixin({ charset : 'utf-8', type: 'text/javascript', src: jsPath @@ -785,7 +802,7 @@ } } else { - prefix = JSUS.randomString(8, 'a'); + prefix = randomString(8, 'a'); } id = prefix + '_'; @@ -799,7 +816,7 @@ found = true; counter = -1; while (found) { - id = prefix + '_' + JSUS.randomInt(1000); + id = prefix + '_' + randomInt(1000); found = scanDocuments(windows, id); if (++counter > limit) { throw new Error('DOM.generateUniqueId: could not ' + @@ -1137,7 +1154,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 +1268,7 @@ // Option repeatFor. if ('undefined' !== typeof options.repeatFor) { - nRepeats = JSUS.isInt(options.repeatFor, 0); + nRepeats = isInt(options.repeatFor, 0); if (false === nRepeats) { throw new TypeError(where + 'options.repeatFor must be ' + 'a positive integer. Found: ' + @@ -1261,7 +1278,7 @@ // Option stopOnFocus. if (options.stopOnFocus) { - JSUS.onFocusIn(function() { + DOM.onFocusIn(function() { clearBlinkInterval(); onFocusChange(null, null); }); @@ -1284,8 +1301,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 +1311,7 @@ if ('string' === typeof titles) { titles = [titles, '!!!']; } - else if (!JSUS.isArray(titles)) { + else if (!isArray(titles)) { throw new TypeError(where + 'titles must be string, ' + 'array of strings or undefined. Found: ' + titles); @@ -1460,7 +1477,7 @@ if (!document) { return function() { - JSUS.log('onFocusChange: no document detected.'); + console.log('onFocusChange: no document detected.'); return; }; } @@ -1540,6 +1557,12 @@ } }; - JSUS.extend(DOM); + if ('undefined' !== typeof JSUS) { + JSUS.extend(DOM); + } + // Node.JS ESM or CJS + else { + module.exports = DOM; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); 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..566cb75 100644 --- a/lib/eval.js +++ b/lib/eval.js @@ -5,7 +5,7 @@ * * Evaluation of strings as JavaScript commands */ -(function(JSUS) { +(function() { "use strict"; @@ -50,6 +50,12 @@ return func.call(context, str); }; - JSUS.extend(EVAL); + if ('undefined' !== typeof JSUS) { + JSUS.extend(EVAL); + } + // Node.JS ESM or CJS + else { + module.exports = EVAL; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); diff --git a/lib/fs.js b/lib/fs.js index 1883596..93204ee 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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'); + const getQueue = require('./queue').getQueue; 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 = 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 = getQueue(); for (i in files) { if (ext && path.extname(files[i]) !== ext) { continue; @@ -279,6 +275,6 @@ return fdr.pipe(fdw); }; - JSUS.extend(FS); + module.exports = FS; -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); diff --git a/lib/obj.js b/lib/obj.js index c2e474b..ee58af4 100644 --- a/lib/obj.js +++ b/lib/obj.js @@ -5,17 +5,28 @@ * * Collection of static functions to manipulate JavaScript objects */ -(function(JSUS) { +(function() { "use strict"; function OBJ() {} + var isArray; + if ('undefined' !== typeof JSUS) { + isArray = JSUS.isArray; + } + else { + isArray = require('./array').isArray; + } + var compatibility = null; - if ('undefined' !== typeof JSUS.compatibility) { + if ('undefined' !== typeof JSUS && JSUS.compatibility) { compatibility = JSUS.compatibility(); } + else { + compatibility = require('./compatibility').compatibility(); + } /** * ## OBJ.createObj @@ -468,7 +479,7 @@ res.push(tmp); } // If array, expand it. - else if (JSUS.isArray(tmp) && tmp.length) { + else if (isArray(tmp) && tmp.length) { if (tmp.length < 4) { res.push(tmp[0]); if (tmp.length > 1) { @@ -635,12 +646,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 +661,7 @@ return clone; } else { - return JSUS.clone(obj); + return OBJ.clone(obj); } }; @@ -963,7 +974,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 +1138,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 +1158,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 +1173,7 @@ } else { - curPosAsKey = posAsKeys || !JSUS.isArray(value); + curPosAsKey = posAsKeys || !isArray(value); for (i in value) { if (value.hasOwnProperty(i)) { @@ -1197,7 +1208,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 +1275,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 +1484,12 @@ return res; }; - JSUS.extend(OBJ); + if ('undefined' !== typeof JSUS) { + JSUS.extend(OBJ); + } + // Node.JS ESM or CJS + else { + module.exports = OBJ; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); diff --git a/lib/parse.js b/lib/parse.js index 7ef3fad..3f5ff37 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -5,10 +5,20 @@ * * Collection of static functions related to parsing strings */ -(function(JSUS) { +(function() { "use strict"; + var isArray, jsusEval; + if ('undefined' !== typeof JSUS) { + isArray = JSUS.isArray; + jsusEval = JSUS.eval; + } + else { + isArray = require('./array').isArray; + jsusEval = require('./eval').eval; + } + function PARSE() {} /** @@ -114,7 +124,7 @@ pattern = '['; - JSUS.each(separators, function(s) { + separators.forEach(function(s) { if (s === ' ') s = '\\s'; pattern += s; @@ -248,7 +258,7 @@ return value; } else if (value.substring(0, len_func) === PARSE.marker_func) { - return JSUS.eval(value.substring(len_prefix)); + return jsusEval(value.substring(len_prefix)); } else if (value.substring(0, len_null) === PARSE.marker_null) { return null; @@ -470,7 +480,7 @@ if ('undefined' === typeof available) { available = expr; } - else if (JSUS.isArray(available)) { + else if (isArray(available)) { if (available.length === 0) return solution; begin = Math.min.apply(null, available); end = Math.max.apply(null, available); @@ -619,11 +629,11 @@ throw new Error('PARSE.range: invalid dot found: ' + expr); } - if (JSUS.isArray(available)) { + if (isArray(available)) { i = -1, len = available.length; for ( ; ++i < len ; ) { x = parseInt(available[i], 10); - if (JSUS.eval(expr.replace(/x/g, x))) { + if (jsusEval(expr.replace(/x/g, x))) { solution.push(x); } } @@ -631,7 +641,7 @@ else { while (!available.isFinished()) { x = parseInt(available.next(), 10); - if (JSUS.eval(expr.replace(/x/g, x))) { + if (jsusEval(expr.replace(/x/g, x))) { solution.push(x); } } @@ -709,6 +719,12 @@ }; } - JSUS.extend(PARSE); + if ('undefined' !== typeof JSUS) { + JSUS.extend(PARSE); + } + // Node.JS ESM or CJS + else { + module.exports = PARSE; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); diff --git a/lib/queue.js b/lib/queue.js index c41b44d..291df82 100644 --- a/lib/queue.js +++ b/lib/queue.js @@ -1,3 +1,5 @@ +const { uniqueKey } = require('./obj'); + /** * # QUEUE * Copyright(c) 2015 Stefano Balietti @@ -5,10 +7,19 @@ * * Handles a simple queue of operations */ -(function(JSUS) { +(function() { "use strict"; + var isEmpty, uniqueKey; + if ('undefined' !== typeof JSUS) { + isEmpty = JSUS.isEmpty; + uniqueKey = JSUS.uniqueKey; + } + else { + ({ uniqueKey, isEmpty } = require('./obj')); + } + var QUEUE = {}; QUEUE.getQueue = function() { @@ -43,7 +54,7 @@ * @return {boolean} TRUE, if no operation is in progress */ Queue.prototype.isReady = function() { - return JSUS.isEmpty(this.inProgress); + return isEmpty(this.inProgress); }; /** @@ -63,7 +74,7 @@ throw new TypeError('Queue.onReady: cb must be function. Found: ' + cb); } - if (JSUS.isEmpty(this.inProgress)) cb(); + if (isEmpty(this.inProgress)) cb(); else this.queue.push(cb); }; @@ -80,7 +91,7 @@ if (key && 'string' !== typeof key) { throw new Error('Queue.add: key must be string.'); } - key = JSUS.uniqueKey(this.inProgress, key); + key = uniqueKey(this.inProgress, key); if ('string' !== typeof key) { throw new Error('Queue.add: an error occurred ' + 'generating unique key.'); @@ -101,7 +112,7 @@ throw new Error('Queue.remove: key must be string.'); } delete this.inProgress[key]; - if (JSUS.isEmpty(this.inProgress)) { + if (isEmpty(this.inProgress)) { this.executeAndClear(); } }; @@ -141,6 +152,12 @@ } }; - JSUS.extend(QUEUE); + if ('undefined' !== typeof JSUS) { + JSUS.extend(QUEUE); + } + // Node.JS ESM or CJS + else { + module.exports = QUEUE; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); diff --git a/lib/random.js b/lib/random.js index cb3ed5c..ec4c9ad 100644 --- a/lib/random.js +++ b/lib/random.js @@ -5,10 +5,22 @@ * * Generates pseudo-random numbers */ -(function(JSUS) { +(function() { "use strict"; + + var seq, shuffle, isInt; + if ('undefined' !== typeof JSUS) { + seq = JSUS.seq; + shuffle = JSUS.shuffle; + isInt = JSUS.isInt; + } + else { + ({ seq, shuffle } = require('./array')); + ({ isInt } = require('./parse')); + } + function RANDOM() {} /** @@ -122,9 +134,9 @@ */ RANDOM.sample = function(a, b) { var out; - out = JSUS.seq(a,b); + out = seq(a,b); if (!out) return false; - return JSUS.shuffle(out); + return shuffle(out); }; /** @@ -419,7 +431,7 @@ if (nSpaces > -1) { nSpaces = chars.charAt(nSpaces + 1); // nSpaces is integer > 0 or 1. - nSpaces = JSUS.isInt(nSpaces, 0) || 1; + nSpaces = isInt(nSpaces, 0) || 1; if (nSpaces === 1) mask += ' '; else if (nSpaces === 2) mask += ' '; else if (nSpaces === 3) mask += ' '; @@ -457,6 +469,12 @@ RANDOM.randomString(RANDOM.randomInt(2,3)); }; - JSUS.extend(RANDOM); + if ('undefined' !== typeof JSUS) { + JSUS.extend(RANDOM); + } + // Node.JS ESM or CJS + else { + module.exports = RANDOM; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); diff --git a/lib/time.js b/lib/time.js index d1dc4f9..6f09c1c 100644 --- a/lib/time.js +++ b/lib/time.js @@ -6,7 +6,7 @@ * Collection of static functions related to the generation, * manipulation, and formatting of time strings in JavaScript */ -(function (JSUS) { +(function() { "use strict"; @@ -132,6 +132,12 @@ TIME.now = 'function' === typeof Date.now ? Date.now : function() { return new Date().getTime(); } - JSUS.extend(TIME); + if ('undefined' !== typeof JSUS) { + JSUS.extend(TIME); + } + // Node.JS ESM or CJS + else { + module.exports = TIME; + } -})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); +})(); From 3f8416bedef8ae6bf43e09275b2da631fa82a5e2 Mon Sep 17 00:00:00 2001 From: Stefano Balietti Date: Fri, 7 Mar 2025 11:49:13 +0100 Subject: [PATCH 2/4] lazy init in libs --- build/jsus.js | 377 +++++++++++++++++++++++++++---------------- build/jsus.min.js | 11 -- jsus.js | 44 ++--- lib/array.js | 23 +-- lib/compatibility.js | 8 +- lib/dom.js | 49 ++---- lib/eval.js | 9 +- lib/fs.js | 11 +- lib/obj.js | 32 ++-- lib/parse.js | 20 +-- lib/queue.js | 25 ++- lib/random.js | 24 +-- lib/time.js | 8 +- 13 files changed, 347 insertions(+), 294 deletions(-) delete mode 100644 build/jsus.min.js diff --git a/build/jsus.js b/build/jsus.js index 82ad1a9..f426c88 100644 --- a/build/jsus.js +++ b/build/jsus.js @@ -1,3 +1,5 @@ +const COMPATIBILITY = require('./lib/compatibility'); + /** * # JSUS: JavaScript UtilS. * Copyright(c) 2017 Stefano Balietti @@ -9,7 +11,9 @@ * --- */ (function(exports) { - + + var files, inits; + var JSUS = exports.JSUS = {}; // ## JSUS._classes @@ -143,17 +147,26 @@ // ## 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' + ]; + + // Load all files. + files.forEach(function(file) { + const [ FN, init ] = require('./lib/' + file); + JSUS.extend(FN); + inits.push(init); + }); + + // 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 +179,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 +278,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 +357,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 +410,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 +445,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 +574,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 +738,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 +785,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 +816,7 @@ */ ARRAY.arrayIntersect = function(a1, a2) { return a1.filter( function(i) { - return JSUS.inArray(i, a2); + return ARRAY.inArray(i, a2); }); }; @@ -819,7 +834,7 @@ */ ARRAY.arrayDiff = function(a1, a2) { return a1.filter( function(i) { - return !(JSUS.inArray(i, a2)); + return !(ARRAY.inArray(i, a2)); }); }; @@ -921,23 +936,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 +1010,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 +1199,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 +1214,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 +1323,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 +1341,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 +1461,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 +1474,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 +1506,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 +1616,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 +1649,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 +1810,7 @@ } } else { - prefix = JSUS.randomString(8, 'a'); + prefix = _J.randomString(8, 'a'); } id = prefix + '_'; @@ -1790,7 +1824,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 +2162,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 +2276,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 +2286,7 @@ // Option stopOnFocus. if (options.stopOnFocus) { - JSUS.onFocusIn(function() { + DOM.onFocusIn(function() { clearBlinkInterval(); onFocusChange(null, null); }); @@ -2275,8 +2309,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 +2319,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 +2485,7 @@ if (!document) { return function() { - JSUS.log('onFocusChange: no document detected.'); + console.log('onFocusChange: no document detected.'); return; }; } @@ -2531,9 +2565,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 +2605,7 @@ * Only the methods which do not follow the above-mentioned syntax * will receive further explanation. */ -(function(JSUS) { +(function() { "use strict"; @@ -4105,21 +4146,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 +4211,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 +4237,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'); + let _J, init; function FS() {} @@ -4300,7 +4355,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 +4374,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 +4386,7 @@ asq.add(file); mycb = asq.getRemoveCb(file); } - JSUS.deleteIfExists(dir + file, mycb); + FS.deleteIfExists(dir + file, mycb); }); if (cb) { @@ -4368,11 +4423,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 +4451,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 +4501,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 +4972,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 +5139,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 +5154,7 @@ return clone; } else { - return JSUS.clone(obj); + return OBJ.clone(obj); } }; @@ -5415,7 +5467,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 +5631,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 +5651,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 +5666,7 @@ } else { - curPosAsKey = posAsKeys || !JSUS.isArray(value); + curPosAsKey = posAsKeys || !_J.isArray(value); for (i in value) { if (value.hasOwnProperty(i)) { @@ -5649,7 +5701,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 +5768,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 +5977,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 +6110,7 @@ pattern = '['; - JSUS.each(separators, function(s) { + separators.forEach(function(s) { if (s === ' ') s = '\\s'; pattern += s; @@ -6179,7 +6244,7 @@ return value; } else if (value.substring(0, len_func) === PARSE.marker_func) { - return JSUS.eval(value.substring(len_prefix)); + return jsusEval(value.substring(len_prefix)); } else if (value.substring(0, len_null) === PARSE.marker_null) { return null; @@ -6401,7 +6466,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 +6615,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 (jsusEval(expr.replace(/x/g, x))) { solution.push(x); } } @@ -6562,7 +6627,7 @@ else { while (!available.isFinished()) { x = parseInt(available.next(), 10); - if (JSUS.eval(expr.replace(/x/g, x))) { + if (jsusEval(expr.replace(/x/g, x))) { solution.push(x); } } @@ -6640,21 +6705,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 +6764,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 +6784,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 +6801,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 +6822,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 +6862,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 +7000,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 +7297,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 +7335,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 +7483,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 deleted file mode 100644 index 60151e8..0000000 --- a/build/jsus.min.js +++ /dev/null @@ -1,11 +0,0 @@ -/** - * # JSUS: JavaScript UtilS. - * Copyright(c) 2017 Stefano Balietti - * MIT Licensed - * - * Collection of general purpose javascript functions. JSUS helps! - * - * 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 @@ -10,8 +12,8 @@ */ (function(exports) { - var tmpClass; - + var files, inits; + var JSUS = exports.JSUS = {}; // ## JSUS._classes @@ -145,26 +147,24 @@ // ## Node.JS includes if (JSUS.isNodeJS()) { - tmpClass = require('./lib/compatibility'); - JSUS.extend(tmpClass); - tmpClass = require('./lib/obj'); - JSUS.extend(tmpClass); - tmpClass = require('./lib/array'); - JSUS.extend(tmpClass); - tmpClass = require('./lib/time'); - JSUS.extend(tmpClass); - tmpClass = require('./lib/eval'); - JSUS.extend(tmpClass); - tmpClass = require('./lib/dom'); - JSUS.extend(tmpClass); - tmpClass = require('./lib/random'); - JSUS.extend(tmpClass); - tmpClass = require('./lib/parse'); - JSUS.extend(tmpClass); - tmpClass = require('./lib/queue'); - JSUS.extend(tmpClass); - tmpClass = require('./lib/fs'); - JSUS.extend(tmpClass); + inits = []; + files = [ + 'compatibility', 'obj', 'array', 'time', 'eval', 'dom', + 'random', 'parse', 'queue', 'fs' + ]; + + // Load all files. + files.forEach(function(file) { + const [ FN, init ] = require('./lib/' + file); + JSUS.extend(FN); + inits.push(init); + }); + + // 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 { diff --git a/lib/array.js b/lib/array.js index 031cb56..1fc8f52 100644 --- a/lib/array.js +++ b/lib/array.js @@ -1,6 +1,6 @@ /** * # ARRAY - * Copyright(c) 2017 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Collection of static functions to manipulate arrays @@ -9,15 +9,7 @@ "use strict"; - var randomInt, equals; - if ('undefined' !== typeof JSUS) { - randomInt = JSUS.randomInt; - equals = JSUS.equals; - } - else { - randomInt = require('./random').randomInt; - equals = require('./obj').equals; - } + var _J, init; function ARRAY() {} @@ -239,7 +231,7 @@ if ('undefined' === typeof needle || !haystack) return false; if ('object' === typeof needle) { - func = equals; + func = _J.equals; } else { func = function(a, b) { @@ -274,7 +266,7 @@ ARRAY.inArray = function(needle, haystack) { var func, i, len; if (!haystack) return false; - func = equals; + func = _J.equals; len = haystack.length; for (i = 0; i < len; i++) { if (func.call(this, needle, haystack[i])) { @@ -403,7 +395,7 @@ for (i=0; i < N; i++) { do { - idx = randomInt(start,limit); + idx = _J.randomInt(start,limit); } while (ARRAY.inArray(idx, extracted)); extracted.push(idx); @@ -765,13 +757,14 @@ return t; }; - if ('undefined' !== typeof JSUS) { + _J = JSUS; JSUS.extend(ARRAY); } // Node.JS ESM or CJS else { - module.exports = ARRAY; + init = function(J) { _J = J; }; + module.exports = [ ARRAY, init ]; } })(); diff --git a/lib/compatibility.js b/lib/compatibility.js index 69b625d..851d8d6 100644 --- a/lib/compatibility.js +++ b/lib/compatibility.js @@ -1,7 +1,7 @@ /** * # COMPATIBILITY * - * Copyright(c) 2015 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Tests browsers ECMAScript 5 compatibility @@ -11,6 +11,8 @@ (function() { "use strict"; + var _J, init; + function COMPATIBILITY() {} /** @@ -59,11 +61,13 @@ if ('undefined' !== typeof JSUS) { + _J = JSUS; JSUS.extend(COMPATIBILITY); } // Node.JS ESM or CJS else { - module.exports = COMPATIBILITY; + init = function(J) { _J = J; }; + module.exports = [ COMPATIBILITY, init ]; } })(); diff --git a/lib/dom.js b/lib/dom.js index ef0fd3d..319f769 100644 --- a/lib/dom.js +++ b/lib/dom.js @@ -1,6 +1,6 @@ /** * # DOM - * Copyright(c) 2019 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Helper library to perform generic operation with DOM elements. @@ -8,26 +8,10 @@ (function() { "use strict"; - - var isArray, randomInt, randomString, keys, size, sample, isInt, mixin; - if ('undefined' !== typeof JSUS) { - isArray = JSUS.isArray; - randomInt = JSUS.randomInt; - randomString = JSUS.randomString; - keys = JSUS.keys; - size = JSUS.size; - sample = JSUS.sample; - isInt = JSUS.isInt; - } - else { - isInt = require('./parse').isInt; - isArray = require('./array').isArray; - ({ keys, mixin, size } = require('./obj')); - ({ sample, randomString, randomInt } = require('./random')); - } - + + var _J, init; var onFocusChange, changeTitle; - + function DOM() {} // ## GET/ADD @@ -340,13 +324,13 @@ } // No span to create, return what we have. - if (!size(spans)) { + if (!_J.size(spans)) { return root.appendChild(document.createTextNode(string)); } // Re-assamble the string. - idxs = 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++) { @@ -466,7 +450,7 @@ cb = order; } else { - if (!isArray(order)) { + if (!_J.isArray(order)) { throw new TypeError('DOM.shuffleElements: order must be ' + 'array. Found: ' + order); } @@ -498,7 +482,7 @@ len = children.length; idOrder = new Array(len); if (cb) numOrder = new Array(len); - if (!order) order = 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 === "") { @@ -608,7 +592,7 @@ throw new Error('DOM.addCSS: root is undefined, and could not ' + 'detect a valid root for css: ' + cssPath); } - attributes = mixin({ + attributes = _J.mixin({ rel : 'stylesheet', type: 'text/css', href: cssPath @@ -641,7 +625,7 @@ throw new Error('DOM.addCSS: root is undefined, and could not ' + 'detect a valid root for css: ' + jsPath); } - attributes = mixin({ + attributes = _J.mixin({ charset : 'utf-8', type: 'text/javascript', src: jsPath @@ -802,7 +786,7 @@ } } else { - prefix = randomString(8, 'a'); + prefix = _J.randomString(8, 'a'); } id = prefix + '_'; @@ -816,7 +800,7 @@ found = true; counter = -1; while (found) { - id = prefix + '_' + randomInt(1000); + id = prefix + '_' + _J.randomInt(1000); found = scanDocuments(windows, id); if (++counter > limit) { throw new Error('DOM.generateUniqueId: could not ' + @@ -1268,7 +1252,7 @@ // Option repeatFor. if ('undefined' !== typeof options.repeatFor) { - nRepeats = 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: ' + @@ -1311,7 +1295,7 @@ if ('string' === typeof titles) { titles = [titles, '!!!']; } - else if (!isArray(titles)) { + else if (!_J.isArray(titles)) { throw new TypeError(where + 'titles must be string, ' + 'array of strings or undefined. Found: ' + titles); @@ -1558,11 +1542,12 @@ }; if ('undefined' !== typeof JSUS) { + _J = JSUS; JSUS.extend(DOM); } // Node.JS ESM or CJS else { - module.exports = DOM; + init = function(J) { _J = J; }; + module.exports = [ DOM, init ]; } - })(); diff --git a/lib/eval.js b/lib/eval.js index 566cb75..c0123df 100644 --- a/lib/eval.js +++ b/lib/eval.js @@ -1,6 +1,6 @@ /** * # EVAL - * Copyright(c) 2015 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Evaluation of strings as JavaScript commands @@ -9,6 +9,8 @@ "use strict"; + var _J, init; + function EVAL() {} /** @@ -51,11 +53,14 @@ }; if ('undefined' !== typeof JSUS) { + _J = JSUS; JSUS.extend(EVAL); } // Node.JS ESM or CJS else { - module.exports = EVAL; + init = function(J) { _J = J; }; + module.exports = [ EVAL, init ]; } + })(); diff --git a/lib/fs.js b/lib/fs.js index 93204ee..ce9cfa9 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. @@ -19,7 +19,7 @@ const path = require('path'); const fs = require('fs'); - const getQueue = require('./queue').getQueue; + let _J, init; function FS() {} @@ -152,7 +152,7 @@ return; } // Create async queue if a callback was specified. - if (cb) asq = getQueue(); + if (cb) asq = _J.getQueue(); // Create a nested callback for the async queue, if necessary. files.filter(filterFunc).forEach(function(file) { @@ -229,7 +229,7 @@ throw new Error(); } // Create async queue if a callback was specified. - if (cb) asq = getQueue(); + if (cb) asq = _J.getQueue(); for (i in files) { if (ext && path.extname(files[i]) !== ext) { continue; @@ -275,6 +275,7 @@ return fdr.pipe(fdw); }; - module.exports = FS; + init = function(J) { _J = J; }; + module.exports = [ FS, init ]; })(); diff --git a/lib/obj.js b/lib/obj.js index ee58af4..352f1f8 100644 --- a/lib/obj.js +++ b/lib/obj.js @@ -1,6 +1,6 @@ /** * # OBJ - * Copyright(c) 2019 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Collection of static functions to manipulate JavaScript objects @@ -9,24 +9,9 @@ "use strict"; - function OBJ() {} - - var isArray; - if ('undefined' !== typeof JSUS) { - isArray = JSUS.isArray; - } - else { - isArray = require('./array').isArray; - } + var _J, init, compatibility; - var compatibility = null; - - if ('undefined' !== typeof JSUS && JSUS.compatibility) { - compatibility = JSUS.compatibility(); - } - else { - compatibility = require('./compatibility').compatibility(); - } + function OBJ() {} /** * ## OBJ.createObj @@ -479,7 +464,7 @@ res.push(tmp); } // If array, expand it. - else if (isArray(tmp) && tmp.length) { + else if (_J.isArray(tmp) && tmp.length) { if (tmp.length < 4) { res.push(tmp[0]); if (tmp.length > 1) { @@ -1173,7 +1158,7 @@ } else { - curPosAsKey = posAsKeys || !isArray(value); + curPosAsKey = posAsKeys || !_J.isArray(value); for (i in value) { if (value.hasOwnProperty(i)) { @@ -1485,11 +1470,16 @@ }; if ('undefined' !== typeof JSUS) { + _J = JSUS; JSUS.extend(OBJ); } // Node.JS ESM or CJS else { - module.exports = OBJ; + init = function(J) { + _J = J; + compatibility = _J.compatibility(); + }; + module.exports = [ OBJ, init ]; } })(); diff --git a/lib/parse.js b/lib/parse.js index 3f5ff37..cdc84a2 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1,6 +1,6 @@ /** * # PARSE - * Copyright(c) 2017 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Collection of static functions related to parsing strings @@ -9,15 +9,7 @@ "use strict"; - var isArray, jsusEval; - if ('undefined' !== typeof JSUS) { - isArray = JSUS.isArray; - jsusEval = JSUS.eval; - } - else { - isArray = require('./array').isArray; - jsusEval = require('./eval').eval; - } + var _J, init; function PARSE() {} @@ -480,7 +472,7 @@ if ('undefined' === typeof available) { available = expr; } - else if (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); @@ -629,7 +621,7 @@ throw new Error('PARSE.range: invalid dot found: ' + expr); } - if (isArray(available)) { + if (_J.isArray(available)) { i = -1, len = available.length; for ( ; ++i < len ; ) { x = parseInt(available[i], 10); @@ -720,11 +712,13 @@ } if ('undefined' !== typeof JSUS) { + _J = JSUS; JSUS.extend(PARSE); } // Node.JS ESM or CJS else { - module.exports = PARSE; + init = function(J) { _J = J; }; + module.exports = [ PARSE, init ]; } })(); diff --git a/lib/queue.js b/lib/queue.js index 291df82..1eea708 100644 --- a/lib/queue.js +++ b/lib/queue.js @@ -1,8 +1,6 @@ -const { uniqueKey } = require('./obj'); - /** * # QUEUE - * Copyright(c) 2015 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Handles a simple queue of operations @@ -11,14 +9,7 @@ const { uniqueKey } = require('./obj'); "use strict"; - var isEmpty, uniqueKey; - if ('undefined' !== typeof JSUS) { - isEmpty = JSUS.isEmpty; - uniqueKey = JSUS.uniqueKey; - } - else { - ({ uniqueKey, isEmpty } = require('./obj')); - } + var _J, init; var QUEUE = {}; @@ -54,7 +45,7 @@ const { uniqueKey } = require('./obj'); * @return {boolean} TRUE, if no operation is in progress */ Queue.prototype.isReady = function() { - return isEmpty(this.inProgress); + return _J.isEmpty(this.inProgress); }; /** @@ -74,7 +65,7 @@ const { uniqueKey } = require('./obj'); throw new TypeError('Queue.onReady: cb must be function. Found: ' + cb); } - if (isEmpty(this.inProgress)) cb(); + if (_J.isEmpty(this.inProgress)) cb(); else this.queue.push(cb); }; @@ -91,7 +82,7 @@ const { uniqueKey } = require('./obj'); if (key && 'string' !== typeof key) { throw new Error('Queue.add: key must be string.'); } - key = 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.'); @@ -112,7 +103,7 @@ const { uniqueKey } = require('./obj'); throw new Error('Queue.remove: key must be string.'); } delete this.inProgress[key]; - if (isEmpty(this.inProgress)) { + if (_J.isEmpty(this.inProgress)) { this.executeAndClear(); } }; @@ -153,11 +144,13 @@ const { uniqueKey } = require('./obj'); }; if ('undefined' !== typeof JSUS) { + _J = JSUS; JSUS.extend(QUEUE); } // Node.JS ESM or CJS else { - module.exports = QUEUE; + init = function(J) { _J = J; }; + module.exports = [ QUEUE, init ]; } })(); diff --git a/lib/random.js b/lib/random.js index ec4c9ad..861ffd2 100644 --- a/lib/random.js +++ b/lib/random.js @@ -1,6 +1,6 @@ /** * # RANDOM - * Copyright(c) 2017 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Generates pseudo-random numbers @@ -9,17 +9,7 @@ "use strict"; - - var seq, shuffle, isInt; - if ('undefined' !== typeof JSUS) { - seq = JSUS.seq; - shuffle = JSUS.shuffle; - isInt = JSUS.isInt; - } - else { - ({ seq, shuffle } = require('./array')); - ({ isInt } = require('./parse')); - } + var _J, init; function RANDOM() {} @@ -134,9 +124,9 @@ */ RANDOM.sample = function(a, b) { var out; - out = seq(a,b); + out = _J.seq(a,b); if (!out) return false; - return shuffle(out); + return _J.shuffle(out); }; /** @@ -431,7 +421,7 @@ if (nSpaces > -1) { nSpaces = chars.charAt(nSpaces + 1); // nSpaces is integer > 0 or 1. - nSpaces = isInt(nSpaces, 0) || 1; + nSpaces = _J.isInt(nSpaces, 0) || 1; if (nSpaces === 1) mask += ' '; else if (nSpaces === 2) mask += ' '; else if (nSpaces === 3) mask += ' '; @@ -470,11 +460,13 @@ }; if ('undefined' !== typeof JSUS) { + _J = JSUS; JSUS.extend(RANDOM); } // Node.JS ESM or CJS else { - module.exports = RANDOM; + init = function(J) { _J = J; }; + module.exports = [ RANDOM, init ]; } })(); diff --git a/lib/time.js b/lib/time.js index 6f09c1c..b92e890 100644 --- a/lib/time.js +++ b/lib/time.js @@ -1,6 +1,6 @@ /** * # TIME - * Copyright(c) 2021 Stefano Balietti + * Copyright(c) 2025 Stefano Balietti * MIT Licensed * * Collection of static functions related to the generation, @@ -10,6 +10,8 @@ "use strict"; + var _J, init; + function TIME() {} function pad(number) { @@ -133,11 +135,13 @@ Date.now : function() { return new Date().getTime(); } if ('undefined' !== typeof JSUS) { + _J = JSUS; JSUS.extend(TIME); } // Node.JS ESM or CJS else { - module.exports = TIME; + init = function(J) { _J = J; }; + module.exports = [ TIME, init ]; } })(); From 24218763bf8a1c5c07aab889327f725a41c6ca19 Mon Sep 17 00:00:00 2001 From: Stefano Balietti Date: Fri, 7 Mar 2025 12:40:06 +0100 Subject: [PATCH 3/4] fixes --- bin/build.js | 8 ++++---- build/jsus.js | 24 ++++++++++++++++-------- build/jsus.min.js | 11 +++++++++++ jsus.js | 22 +++++++++++++++------- lib/fs.js | 2 +- 5 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 build/jsus.min.js 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 f426c88..ae830e5 100644 --- a/build/jsus.js +++ b/build/jsus.js @@ -1,5 +1,3 @@ -const COMPATIBILITY = require('./lib/compatibility'); - /** * # JSUS: JavaScript UtilS. * Copyright(c) 2017 Stefano Balietti @@ -149,15 +147,25 @@ const COMPATIBILITY = require('./lib/compatibility'); if (JSUS.isNodeJS()) { inits = []; files = [ - 'compatibility', 'obj', 'array', 'time', 'eval', 'dom', - 'random', 'parse', 'queue', 'fs' + 'compatibility', + 'obj', + 'array', + 'time', + 'eval', + 'dom', + 'random', + 'parse', + 'queue', + 'fs' ]; + // files = [ 'array' ] // Load all files. files.forEach(function(file) { - const [ FN, init ] = require('./lib/' + file); - JSUS.extend(FN); - inits.push(init); + 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 @@ -4245,7 +4253,7 @@ const COMPATIBILITY = require('./lib/compatibility'); const path = require('path'); const fs = require('fs'); - let _J, init; + var _J, init; function FS() {} diff --git a/build/jsus.min.js b/build/jsus.min.js new file mode 100644 index 0000000..2450ff4 --- /dev/null +++ b/build/jsus.min.js @@ -0,0 +1,11 @@ +/** + * # JSUS: JavaScript UtilS. + * Copyright(c) 2017 Stefano Balietti + * MIT Licensed + * + * Collection of general purpose javascript functions. JSUS helps! + * + * See README.md for extra help. + * --- + */ +(function(e){var t,n,r=e.JSUS={};r._classes={},"undefined"==typeof console&&(console={}),"undefined"==typeof console.log&&(console.log=function(){}),r.log=function(e){console.log(e)},r.extend=function(e,t){var n,i;if("object"!=typeof e&&"function"!=typeof e)return t;"undefined"==typeof t&&(t=t||this,"function"==typeof e?(n=e.toString(),n=n.substr("function ".length),n=n.substr(0,n.indexOf("("))):n=e.constructor||e.__proto__.constructor,n&&(this._classes[n]=e));for(i in e)e.hasOwnProperty(i)&&(typeof t[i]!="object"?t[i]=e[i]:r.extend(e[i],t[i]));return e.prototype&&r.extend(e.prototype,t.prototype||t),t},r.require=function(e,t){var n;t="undefined"==typeof t?!0:t;if(t&&"undefined"==typeof r.clone)return r.log("JSUS.require: JSUS.clone not found, but clone requested. Cannot continue."),!1;if("undefined"==typeof e)n=r._classes;else{n=r._classes[e];if("undefined"==typeof n)return r.log("JSUS.require: could not find component "+e),!1}return t?r.clone(n):n},r.isNodeJS=function(){return"undefined"!=typeof module&&"undefined"!=typeof module.exports&&"function"==typeof require},r.isNodeJS()?(n=[],t=["compatibility","obj","array","time","eval","dom","random","parse","queue","fs"],t.forEach(function(e){const t=require("./lib/"+e);r.extend(t[0]),n.push(t[1])}),n.forEach(function(e){e(r)})):e.J=e.JSUS})("undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports:window),function(){"use strict";function n(){}var e,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=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 @@ -149,15 +147,25 @@ const COMPATIBILITY = require('./lib/compatibility'); if (JSUS.isNodeJS()) { inits = []; files = [ - 'compatibility', 'obj', 'array', 'time', 'eval', 'dom', - 'random', 'parse', 'queue', 'fs' + 'compatibility', + 'obj', + 'array', + 'time', + 'eval', + 'dom', + 'random', + 'parse', + 'queue', + 'fs' ]; + // files = [ 'array' ] // Load all files. files.forEach(function(file) { - const [ FN, init ] = require('./lib/' + file); - JSUS.extend(FN); - inits.push(init); + 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 diff --git a/lib/fs.js b/lib/fs.js index ce9cfa9..decb17f 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -19,7 +19,7 @@ const path = require('path'); const fs = require('fs'); - let _J, init; + var _J, init; function FS() {} From e6fad459a59d8fff0911810260e7eb4c6562302a Mon Sep 17 00:00:00 2001 From: Stefano Balietti Date: Tue, 11 Mar 2025 14:07:17 +0100 Subject: [PATCH 4/4] fix --- build/jsus.js | 6 +++--- build/jsus.min.js | 2 +- lib/parse.js | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/build/jsus.js b/build/jsus.js index ae830e5..023dd90 100644 --- a/build/jsus.js +++ b/build/jsus.js @@ -6252,7 +6252,7 @@ return value; } else if (value.substring(0, len_func) === PARSE.marker_func) { - return jsusEval(value.substring(len_prefix)); + return _J.eval(value.substring(len_prefix)); } else if (value.substring(0, len_null) === PARSE.marker_null) { return null; @@ -6627,7 +6627,7 @@ i = -1, len = available.length; for ( ; ++i < len ; ) { x = parseInt(available[i], 10); - if (jsusEval(expr.replace(/x/g, x))) { + if (_J.eval(expr.replace(/x/g, x))) { solution.push(x); } } @@ -6635,7 +6635,7 @@ else { while (!available.isFinished()) { x = parseInt(available.next(), 10); - if (jsusEval(expr.replace(/x/g, x))) { + if (_J.eval(expr.replace(/x/g, x))) { solution.push(x); } } diff --git a/build/jsus.min.js b/build/jsus.min.js index 2450ff4..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,n,r=e.JSUS={};r._classes={},"undefined"==typeof console&&(console={}),"undefined"==typeof console.log&&(console.log=function(){}),r.log=function(e){console.log(e)},r.extend=function(e,t){var n,i;if("object"!=typeof e&&"function"!=typeof e)return t;"undefined"==typeof t&&(t=t||this,"function"==typeof e?(n=e.toString(),n=n.substr("function ".length),n=n.substr(0,n.indexOf("("))):n=e.constructor||e.__proto__.constructor,n&&(this._classes[n]=e));for(i in e)e.hasOwnProperty(i)&&(typeof t[i]!="object"?t[i]=e[i]:r.extend(e[i],t[i]));return e.prototype&&r.extend(e.prototype,t.prototype||t),t},r.require=function(e,t){var n;t="undefined"==typeof t?!0:t;if(t&&"undefined"==typeof r.clone)return r.log("JSUS.require: JSUS.clone not found, but clone requested. Cannot continue."),!1;if("undefined"==typeof e)n=r._classes;else{n=r._classes[e];if("undefined"==typeof n)return r.log("JSUS.require: could not find component "+e),!1}return t?r.clone(n):n},r.isNodeJS=function(){return"undefined"!=typeof module&&"undefined"!=typeof module.exports&&"function"==typeof require},r.isNodeJS()?(n=[],t=["compatibility","obj","array","time","eval","dom","random","parse","queue","fs"],t.forEach(function(e){const t=require("./lib/"+e);r.extend(t[0]),n.push(t[1])}),n.forEach(function(e){e(r)})):e.J=e.JSUS})("undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports:window),function(){"use strict";function n(){}var e,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=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>>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