|
|
AIWROKÈí¼þÔËËã·û·â×°¿âʾÀý
- /*
- 🍎AIworkÔËËã·ûµÄ¸ß¼¶·â×°ºÍÓ÷¨
- 🍎½»Á÷QQȺ711841924Ⱥһ£¬Æ»¹ûÄÚ²âȺ£¬528816639
- JavaScript ÔËËã·û·â×°¿âʾÀý
- */
- // ÈÕÖ¾´°¿ÚÅäÖã¨ÈçÐèʹÓÃÇëÈ¡Ïû×¢ÊÍ£©
- logWindow.show()
- logWindow.clear()
- logWindow.setHeight(2500)
- logWindow.setWidth(1500)
- logWindow.setNoClickModel()
- var OperatorUtils = {};
- OperatorUtils.version = '1.0.0';
- OperatorUtils.MathEngine = function() {
- this.history = [];
- this.variables = {};
- };
- OperatorUtils.MathEngine.prototype = {
- calculate: function(expression) {
- var keys = [];
- for (var key in this.variables) {
- if (this.variables.hasOwnProperty(key)) {
- keys.push(key);
- }
- }
- var values = [];
- for (var i = 0; i < keys.length; i++) {
- values.push(this.variables[keys[i]]);
- }
- var funcArgs = keys.join(',');
- var funcBody = 'return ' + expression;
- var func = new Function(funcArgs, funcBody);
- var result = func.apply(null, values);
- this.history.push({
- expression: expression,
- result: result,
- timestamp: new Date().getTime()
- });
- return result;
- },
- setVariable: function(name, value) {
- this.variables[name] = value;
- return this;
- },
- getVariable: function(name) {
- return this.variables[name];
- },
- getHistory: function() {
- return this.history;
- },
- clearHistory: function() {
- this.history = [];
- return this;
- }
- };
- OperatorUtils.Arithmetic = {
- add: function() {
- var result = 0;
- for (var i = 0; i < arguments.length; i++) {
- var num = Number(arguments[i]);
- if (isNaN(num)) {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×Ö');
- }
- result += num;
- }
- return result;
- },
- subtract: function(a, b) {
- a = Number(a);
- b = Number(b);
- if (isNaN(a) || isNaN(b)) {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×Ö');
- }
- return a - b;
- },
- multiply: function() {
- var result = 1;
- for (var i = 0; i < arguments.length; i++) {
- var num = Number(arguments[i]);
- if (isNaN(num)) {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×Ö');
- }
- result *= num;
- }
- return result;
- },
- divide: function(a, b) {
- a = Number(a);
- b = Number(b);
- if (isNaN(a) || isNaN(b)) {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×Ö');
- }
- if (b === 0) {
- throw new Error('³ýÊý²»ÄÜΪÁã');
- }
- return a / b;
- },
- modulo: function(a, b) {
- a = Number(a);
- b = Number(b);
- if (isNaN(a) || isNaN(b)) {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×Ö');
- }
- if (b === 0) {
- throw new Error('³ýÊý²»ÄÜΪÁã');
- }
- return a % b;
- },
- increment: function(value, prefix) {
- value = Number(value);
- if (isNaN(value)) {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×Ö');
- }
- if (prefix) {
- return ++value;
- }
- var original = value;
- value++;
- return original;
- },
- decrement: function(value, prefix) {
- value = Number(value);
- if (isNaN(value)) {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×Ö');
- }
- if (prefix) {
- return --value;
- }
- var original = value;
- value--;
- return original;
- },
- power: function(base, exponent) {
- base = Number(base);
- exponent = Number(exponent);
- if (isNaN(base) || isNaN(exponent)) {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×Ö');
- }
- var result = 1;
- for (var i = 0; i < exponent; i++) {
- result *= base;
- }
- return result;
- },
- sqrt: function(value) {
- value = Number(value);
- if (isNaN(value)) {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×Ö');
- }
- if (value < 0) {
- throw new Error('²»ÄܼÆË㸺ÊýµÄƽ·½¸ù');
- }
- return Math.sqrt(value);
- },
- abs: function(value) {
- value = Number(value);
- if (isNaN(value)) {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×Ö');
- }
- return value < 0 ? -value : value;
- },
- round: function(value, decimals) {
- value = Number(value);
- if (isNaN(value)) {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×Ö');
- }
- decimals = decimals || 0;
- var factor = Math.pow(10, decimals);
- return Math.round(value * factor) / factor;
- },
- sum: function(arr) {
- if (Object.prototype.toString.call(arr) !== '[object Array]') {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×é');
- }
- var total = 0;
- for (var i = 0; i < arr.length; i++) {
- total += Number(arr[i]) || 0;
- }
- return total;
- },
- average: function(arr) {
- if (Object.prototype.toString.call(arr) !== '[object Array]') {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×é');
- }
- if (arr.length === 0) {
- return 0;
- }
- return this.sum(arr) / arr.length;
- },
- max: function(arr) {
- if (Object.prototype.toString.call(arr) !== '[object Array]') {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×é');
- }
- if (arr.length === 0) {
- throw new Error('Êý×é²»ÄÜΪ¿Õ');
- }
- var maxVal = arr[0];
- for (var i = 1; i < arr.length; i++) {
- if (arr[i] > maxVal) {
- maxVal = arr[i];
- }
- }
- return maxVal;
- },
- min: function(arr) {
- if (Object.prototype.toString.call(arr) !== '[object Array]') {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×é');
- }
- if (arr.length === 0) {
- throw new Error('Êý×é²»ÄÜΪ¿Õ');
- }
- var minVal = arr[0];
- for (var i = 1; i < arr.length; i++) {
- if (arr[i] < minVal) {
- minVal = arr[i];
- }
- }
- return minVal;
- },
- range: function(start, end, step) {
- start = Number(start);
- end = Number(end);
- step = step || 1;
- step = Number(step);
-
- if (isNaN(start) || isNaN(end) || isNaN(step)) {
- throw new TypeError('²ÎÊý±ØÐëÊÇÊý×Ö');
- }
- if (step === 0) {
- throw new Error('²½³¤²»ÄÜΪÁã');
- }
-
- var result = [];
- var current = start;
- if (step > 0) {
- while (current <= end) {
- result.push(current);
- current += step;
- }
- } else {
- while (current >= end) {
- result.push(current);
- current += step;
- }
- }
- return result;
- }
- };
- OperatorUtils.Assignment = {
- assign: function(obj, key, value) {
- if (typeof obj !== 'object' || obj === null) {
- throw new TypeError('µÚÒ»¸ö²ÎÊý±ØÐëÊǶÔÏó');
- }
- obj[key] = value;
- return obj;
- },
- addAssign: function(obj, key, value) {
- if (typeof obj !== 'object' || obj === null) {
- throw new TypeError('µÚÒ»¸ö²ÎÊý±ØÐëÊǶÔÏó');
- }
- obj[key] = (obj[key] || 0) + value;
- return obj;
- },
- subtractAssign: function(obj, key, value) {
- if (typeof obj !== 'object' || obj === null) {
- throw new TypeError('µÚÒ»¸ö²ÎÊý±ØÐëÊǶÔÏó');
- }
- obj[key] = (obj[key] || 0) - value;
- return obj;
- },
- multiplyAssign: function(obj, key, value) {
- if (typeof obj !== 'object' || obj === null) {
- throw new TypeError('µÚÒ»¸ö²ÎÊý±ØÐëÊǶÔÏó');
- }
- obj[key] = (obj[key] || 0) * value;
- return obj;
- },
- divideAssign: function(obj, key, value) {
- if (typeof obj !== 'object' || obj === null) {
- throw new TypeError('µÚÒ»¸ö²ÎÊý±ØÐëÊǶÔÏó');
- }
- if (value === 0) {
- throw new Error('³ýÊý²»ÄÜΪÁã');
- }
- obj[key] = (obj[key] || 0) / value;
- return obj;
- },
- moduloAssign: function(obj, key, value) {
- if (typeof obj !== 'object' || obj === null) {
- throw new TypeError('µÚÒ»¸ö²ÎÊý±ØÐëÊǶÔÏó');
- }
- if (value === 0) {
- throw new Error('³ýÊý²»ÄÜΪÁã');
- }
- obj[key] = (obj[key] || 0) % value;
- return obj;
- },
- merge: function(target) {
- if (typeof target !== 'object' || target === null) {
- throw new TypeError('µÚÒ»¸ö²ÎÊý±ØÐëÊǶÔÏó');
- }
- for (var i = 1; i < arguments.length; i++) {
- var source = arguments[i];
- if (typeof source === 'object' && source !== null) {
- for (var key in source) {
- if (source.hasOwnProperty(key)) {
- target[key] = source[key];
- }
- }
- }
- }
- return target;
- },
- deepClone: function(obj) {
- if (obj === null || typeof obj !== 'object') {
- return obj;
- }
- if (Object.prototype.toString.call(obj) === '[object Array]') {
- var arrCopy = [];
- for (var i = 0; i < obj.length; i++) {
- arrCopy[i] = this.deepClone(obj[i]);
- }
- return arrCopy;
- }
- var objCopy = {};
- for (var key in obj) {
- if (obj.hasOwnProperty(key)) {
- objCopy[key] = this.deepClone(obj[key]);
- }
- }
- return objCopy;
- }
- };
- OperatorUtils.StringOps = {
- concat: function() {
- var result = '';
- for (var i = 0; i < arguments.length; i++) {
- result += String(arguments[i] || '');
- }
- return result;
- },
- join: function(sep) {
- sep = sep || '';
- var result = '';
- for (var i = 0; i < arguments.length; i++) {
- if (i > 0) {
- result += sep;
- }
- result += String(arguments[i] || '');
- }
- return result;
- },
- repeat: function(str, count) {
- str = String(str);
- count = Number(count) || 0;
- var result = '';
- for (var i = 0; i < count; i++) {
- result += str;
- }
- return result;
- },
- padLeft: function(str, length, char) {
- str = String(str);
- char = char || ' ';
- while (str.length < length) {
- str = char + str;
- }
- return str;
- },
- padRight: function(str, length, char) {
- str = String(str);
- char = char || ' ';
- while (str.length < length) {
- str = str + char;
- }
- return str;
- },
- truncate: function(str, length, suffix) {
- str = String(str);
- suffix = suffix || '...';
- if (str.length <= length) {
- return str;
- }
- return str.substring(0, length - suffix.length) + suffix;
- },
- capitalize: function(str) {
- str = String(str);
- if (str.length === 0) return str;
- return str.charAt(0).toUpperCase() + str.slice(1);
- },
- camelCase: function(str) {
- str = String(str);
- var words = str.split(/[\s_-]+/);
- var result = words[0].toLowerCase();
- for (var i = 1; i < words.length; i++) {
- result += this.capitalize(words[i].toLowerCase());
- }
- return result;
- },
- kebabCase: function(str) {
- str = String(str);
- return str.replace(/([a-z])([A-Z])/g, '$1-$2')
- .replace(/[\s_]+/g, '-')
- .toLowerCase();
- },
- snakeCase: function(str) {
- str = String(str);
- return str.replace(/([a-z])([A-Z])/g, '$1_$2')
- .replace(/[\s-]+/g, '_')
- .toLowerCase();
- },
- reverse: function(str) {
- str = String(str);
- var result = '';
- for (var i = str.length - 1; i >= 0; i--) {
- result += str.charAt(i);
- }
- return result;
- },
- contains: function(str, search) {
- str = String(str);
- search = String(search);
- return str.indexOf(search) !== -1;
- },
- startsWith: function(str, prefix) {
- str = String(str);
- prefix = String(prefix);
- return str.indexOf(prefix) === 0;
- },
- endsWith: function(str, suffix) {
- str = String(str);
- suffix = String(suffix);
- return str.slice(-suffix.length) === suffix;
- },
- stripTags: function(str) {
- str = String(str);
- return str.replace(/<[^>]*>/g, '');
- },
- escapeHtml: function(str) {
- str = String(str);
- var htmlEntities = {
- '&': '&',
- '<': '<',
- '>': '>',
- '"': '"',
- "'": '''
- };
- return str.replace(/[&<>"']/g, function(match) {
- return htmlEntities[match];
- });
- },
- unescapeHtml: function(str) {
- str = String(str);
- var htmlEntities = {
- '&': '&',
- '<': '<',
- '>': '>',
- '"': '"',
- ''': "'"
- };
- return str.replace(/&(amp|lt|gt|quot|#39);/g, function(match) {
- return htmlEntities[match];
- });
- },
- wordCount: function(str) {
- str = String(str);
- var trimmed = str.replace(/^\s+|\s+$/g, '');
- var words = trimmed.split(/\s+/);
- return trimmed === '' ? 0 : words.length;
- },
- countChars: function(str, char) {
- str = String(str);
- char = String(char);
- var count = 0;
- for (var i = 0; i < str.length; i++) {
- if (str.charAt(i) === char) {
- count++;
- }
- }
- return count;
- },
- format: function(str) {
- str = String(str);
- var args = arguments;
- return str.replace(/\{(\d+)\}/g, function(match, index) {
- var idx = parseInt(index, 10);
- return args[idx + 1] !== undefined ? String(args[idx + 1]) : match;
- });
- },
- template: function(str, data) {
- str = String(str);
- return str.replace(/\{\{(\w+)\}\}/g, function(match, key) {
- return data[key] !== undefined ? String(data[key]) : match;
- });
- }
- };
- OperatorUtils.TypeCoercion = {
- smartAdd: function(a, b) {
- var numA = Number(a);
- var numB = Number(b);
-
- if (!isNaN(numA) && !isNaN(numB) &&
- String(a).replace(/^\s+|\s+$/g, '') !== '' && String(b).replace(/^\s+|\s+$/g, '') !== '') {
- return numA + numB;
- }
- return String(a) + String(b);
- },
- smartConcat: function() {
- var hasNumber = false;
- var hasString = false;
- var numbers = [];
- var strings = [];
-
- for (var i = 0; i < arguments.length; i++) {
- var val = arguments[i];
- var num = Number(val);
-
- if (!isNaN(num) && String(val).replace(/^\s+|\s+$/g, '') !== '' &&
- !isNaN(parseFloat(val)) && isFinite(val)) {
- numbers.push(num);
- hasNumber = true;
- } else {
- strings.push(String(val));
- hasString = true;
- }
- }
-
- if (hasNumber && !hasString) {
- var sum = 0;
- for (var j = 0; j < numbers.length; j++) {
- sum += numbers[j];
- }
- return sum;
- }
-
- var result = '';
- for (var k = 0; k < arguments.length; k++) {
- result += String(arguments[k]);
- }
- return result;
- },
- toNumber: function(value, defaultValue) {
- var num = Number(value);
- return isNaN(num) ? (defaultValue || 0) : num;
- },
- toInt: function(value, defaultValue) {
- var num = parseInt(value, 10);
- return isNaN(num) ? (defaultValue || 0) : num;
- },
- toFloat: function(value, defaultValue) {
- var num = parseFloat(value);
- return isNaN(num) ? (defaultValue || 0) : num;
- },
- toString: function(value, defaultValue) {
- if (value === null || value === undefined) {
- return defaultValue || '';
- }
- return String(value);
- },
- toBoolean: function(value) {
- if (typeof value === 'boolean') {
- return value;
- }
- if (typeof value === 'string') {
- return value.toLowerCase() === 'true';
- }
- if (typeof value === 'number') {
- return value !== 0;
- }
- return Boolean(value);
- },
- isArray: function(value) {
- return Object.prototype.toString.call(value) === '[object Array]';
- },
- isObject: function(value) {
- return typeof value === 'object' && value !== null && !this.isArray(value);
- },
- isFunction: function(value) {
- return typeof value === 'function';
- },
- isString: function(value) {
- return typeof value === 'string';
- },
- isNumber: function(value) {
- return typeof value === 'number' && !isNaN(value);
- },
- isInteger: function(value) {
- return this.isNumber(value) && value % 1 === 0;
- },
- isFloat: function(value) {
- return this.isNumber(value) && value % 1 !== 0;
- }
- };
- OperatorUtils.Comparison = {
- equals: function(a, b) {
- if (a === b) return true;
- if (a === null || b === null) return false;
- if (typeof a !== typeof b) return false;
-
- if (typeof a === 'object') {
- var aKeys = [];
- for (var key in a) {
- if (a.hasOwnProperty(key)) {
- aKeys.push(key);
- }
- }
- var bKeys = [];
- for (var key in b) {
- if (b.hasOwnProperty(key)) {
- bKeys.push(key);
- }
- }
-
- if (aKeys.length !== bKeys.length) return false;
-
- for (var i = 0; i < aKeys.length; i++) {
- var key = aKeys[i];
- if (!b.hasOwnProperty(key) || !this.equals(a[key], b[key])) {
- return false;
- }
- }
- return true;
- }
-
- return false;
- },
- deepEquals: function(a, b) {
- return this.equals(a, b);
- },
- between: function(value, min, max, inclusive) {
- value = Number(value);
- min = Number(min);
- max = Number(max);
-
- if (inclusive) {
- return value >= min && value <= max;
- }
- return value > min && value < max;
- },
- clamp: function(value, min, max) {
- value = Number(value);
- min = Number(min);
- max = Number(max);
-
- if (value < min) return min;
- if (value > max) return max;
- return value;
- },
- inRange: function(value, range) {
- if (Object.prototype.toString.call(range) !== '[object Array]' || range.length !== 2) {
- throw new TypeError('·¶Î§²ÎÊý±ØÐëÊǰüº¬Á½¸öÔªËØµÄÊý×é');
- }
- return this.between(value, range[0], range[1], true);
- },
- compare: function(a, b) {
- if (a < b) return -1;
- if (a > b) return 1;
- return 0;
- }
- };
- OperatorUtils.Bitwise = {
- and: function(a, b) {
- return (a | 0) & (b | 0);
- },
- or: function(a, b) {
- return (a | 0) | (b | 0);
- },
- xor: function(a, b) {
- return (a | 0) ^ (b | 0);
- },
- not: function(a) {
- return ~(a | 0);
- },
- leftShift: function(a, b) {
- return (a | 0) << (b | 0);
- },
- rightShift: function(a, b) {
- return (a | 0) >> (b | 0);
- },
- unsignedRightShift: function(a, b) {
- return (a | 0) >>> (b | 0);
- }
- };
- OperatorUtils.Logical = {
- and: function() {
- for (var i = 0; i < arguments.length; i++) {
- if (!arguments[i]) {
- return arguments[i];
- }
- }
- return arguments[arguments.length - 1];
- },
- or: function() {
- for (var i = 0; i < arguments.length; i++) {
- if (arguments[i]) {
- return arguments[i];
- }
- }
- return arguments[arguments.length - 1];
- },
- not: function(value) {
- return !value;
- },
- ternary: function(condition, trueValue, falseValue) {
- return condition ? trueValue : falseValue;
- },
- nullish: function(value, defaultValue) {
- return value != null ? value : defaultValue;
- },
- coalesce: function() {
- for (var i = 0; i < arguments.length; i++) {
- if (arguments[i] != null) {
- return arguments[i];
- }
- }
- return null;
- }
- };
- OperatorUtils.Chainable = function(initialValue) {
- this.value = initialValue;
- };
- OperatorUtils.Chainable.prototype = {
- add: function(n) {
- this.value = OperatorUtils.Arithmetic.add(this.value, n);
- return this;
- },
- subtract: function(n) {
- this.value = OperatorUtils.Arithmetic.subtract(this.value, n);
- return this;
- },
- multiply: function(n) {
- this.value = OperatorUtils.Arithmetic.multiply(this.value, n);
- return this;
- },
- divide: function(n) {
- this.value = OperatorUtils.Arithmetic.divide(this.value, n);
- return this;
- },
- modulo: function(n) {
- this.value = OperatorUtils.Arithmetic.modulo(this.value, n);
- return this;
- },
- power: function(n) {
- this.value = OperatorUtils.Arithmetic.power(this.value, n);
- return this;
- },
- round: function(decimals) {
- this.value = OperatorUtils.Arithmetic.round(this.value, decimals);
- return this;
- },
- abs: function() {
- this.value = OperatorUtils.Arithmetic.abs(this.value);
- return this;
- },
- clamp: function(min, max) {
- this.value = OperatorUtils.Comparison.clamp(this.value, min, max);
- return this;
- },
- valueOf: function() {
- return this.value;
- },
- toString: function() {
- return String(this.value);
- }
- };
- OperatorUtils.chain = function(value) {
- return new OperatorUtils.Chainable(value);
- };
- OperatorUtils.Calculator = function() {
- this.stack = [];
- this.memory = 0;
- };
- OperatorUtils.Calculator.prototype = {
- push: function(value) {
- this.stack.push(Number(value));
- return this;
- },
- pop: function() {
- return this.stack.pop();
- },
- add: function() {
- if (this.stack.length < 2) throw new Error('Õ»ÖÐÖÁÉÙÐèÒªÁ½¸öÖµ');
- var b = this.stack.pop();
- var a = this.stack.pop();
- this.stack.push(a + b);
- return this;
- },
- subtract: function() {
- if (this.stack.length < 2) throw new Error('Õ»ÖÐÖÁÉÙÐèÒªÁ½¸öÖµ');
- var b = this.stack.pop();
- var a = this.stack.pop();
- this.stack.push(a - b);
- return this;
- },
- multiply: function() {
- if (this.stack.length < 2) throw new Error('Õ»ÖÐÖÁÉÙÐèÒªÁ½¸öÖµ');
- var b = this.stack.pop();
- var a = this.stack.pop();
- this.stack.push(a * b);
- return this;
- },
- divide: function() {
- if (this.stack.length < 2) throw new Error('Õ»ÖÐÖÁÉÙÐèÒªÁ½¸öÖµ');
- var b = this.stack.pop();
- var a = this.stack.pop();
- this.stack.push(a / b);
- return this;
- },
- modulo: function() {
- if (this.stack.length < 2) throw new Error('Õ»ÖÐÖÁÉÙÐèÒªÁ½¸öÖµ');
- var b = this.stack.pop();
- var a = this.stack.pop();
- this.stack.push(a % b);
- return this;
- },
- power: function() {
- if (this.stack.length < 2) throw new Error('Õ»ÖÐÖÁÉÙÐèÒªÁ½¸öÖµ');
- var b = this.stack.pop();
- var a = this.stack.pop();
- this.stack.push(Math.pow(a, b));
- return this;
- },
- memoryStore: function() {
- if (this.stack.length > 0) {
- this.memory = this.stack[this.stack.length - 1];
- }
- return this;
- },
- memoryRecall: function() {
- this.stack.push(this.memory);
- return this;
- },
- memoryClear: function() {
- this.memory = 0;
- return this;
- },
- memoryAdd: function() {
- if (this.stack.length > 0) {
- this.memory += this.stack[this.stack.length - 1];
- }
- return this;
- },
- memorySubtract: function() {
- if (this.stack.length > 0) {
- this.memory -= this.stack[this.stack.length - 1];
- }
- return this;
- },
- getResult: function() {
- return this.stack.length > 0 ? this.stack[this.stack.length - 1] : 0;
- },
- clear: function() {
- this.stack = [];
- return this;
- }
- };
- OperatorUtils.runAllExamples = function() {
- print.log("=== JavaScript ÔËËã·û¸ß¼¶·â×°¿â - ÍêÕû²âÊÔ ===\n");
- print.log("1. ËãÊõÔËËã·û²âÊÔ:");
- print.log("-------------------");
- print.log("¼Ó·¨(5+3+2): " + OperatorUtils.Arithmetic.add(5, 3, 2));
- print.log("¼õ·¨(10-4): " + OperatorUtils.Arithmetic.subtract(10, 4));
- print.log("³Ë·¨(3*4*2): " + OperatorUtils.Arithmetic.multiply(3, 4, 2));
- print.log("³ý·¨(15/3): " + OperatorUtils.Arithmetic.divide(15, 3));
- print.log("ȡģ(17%5): " + OperatorUtils.Arithmetic.modulo(17, 5));
- print.log("ÃÝÔËËã(2^10): " + OperatorUtils.Arithmetic.power(2, 10));
- print.log("ƽ·½¸ù(16): " + OperatorUtils.Arithmetic.sqrt(16));
- print.log("¾ø¶ÔÖµ(-42): " + OperatorUtils.Arithmetic.abs(-42));
- print.log("ËÄÉáÎåÈë(3.14159,2): " + OperatorUtils.Arithmetic.round(3.14159, 2));
- print.log("");
- print.log("2. Êý×éÔËËã²âÊÔ:");
- print.log("----------------");
- var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
- print.log("Êý×éÇóºÍ: " + OperatorUtils.Arithmetic.sum(numbers));
- print.log("Êý×鯽¾ù: " + OperatorUtils.Arithmetic.average(numbers));
- print.log("Êý×é×î´ó: " + OperatorUtils.Arithmetic.max(numbers));
- print.log("Êý×é×îС: " + OperatorUtils.Arithmetic.min(numbers));
- print.log("·¶Î§Éú³É(1-10,²½³¤2): " + JSON.stringify(OperatorUtils.Arithmetic.range(1, 10, 2)));
- print.log("");
- print.log("3. ¸³ÖµÔËËã·û²âÊÔ:");
- print.log("------------------");
- var obj = { x: 10, y: 5 };
- OperatorUtils.Assignment.addAssign(obj, 'x', 5);
- OperatorUtils.Assignment.multiplyAssign(obj, 'x', 2);
- print.log("¶ÔÏó²Ù×÷ºó: " + JSON.stringify(obj));
- var target = { a: 1 };
- OperatorUtils.Assignment.merge(target, { b: 2 }, { c: 3 });
- print.log("¶ÔÏóºÏ²¢: " + JSON.stringify(target));
- print.log("");
- print.log("4. ×Ö·û´®ÔËËã²âÊÔ:");
- print.log("--------------------");
- print.log("×Ö·û´®Á¬½Ó: " + OperatorUtils.StringOps.concat('Hello', ' ', 'World'));
- print.log("×Ö·û´®Öظ´: " + OperatorUtils.StringOps.repeat('Abc', 3));
- print.log("×óÌî³ä: " + OperatorUtils.StringOps.padLeft('42', 5, '0'));
- print.log("ÓÒÌî³ä: " + OperatorUtils.StringOps.padRight('Hi', 5, '*'));
- print.log("Ê××Öĸ´óд: " + OperatorUtils.StringOps.capitalize('hello'));
- print.log("ÍÕ·åÃüÃû: " + OperatorUtils.StringOps.camelCase('hello-world'));
- print.log("×Ö·û´®·´×ª: " + OperatorUtils.StringOps.reverse('Hello'));
- print.log("°üº¬¼ì²é: " + OperatorUtils.StringOps.contains('Hello World', 'World'));
- print.log("");
- print.log("5. ÀàÐÍת»»²âÊÔ:");
- print.log("----------------");
- print.log("ÖÇÄܼӷ¨(5+5): " + OperatorUtils.TypeCoercion.smartAdd(5, 5));
- print.log("ÖÇÄܼӷ¨(\"5\"+5): " + OperatorUtils.TypeCoercion.smartAdd('5', 5));
- print.log("תÊý×Ö(\"123.45\"): " + OperatorUtils.TypeCoercion.toNumber('123.45'));
- print.log("תÕûÊý(\"123.45\"): " + OperatorUtils.TypeCoercion.toInt('123.45'));
- print.log("ת¸¡µã(\"123.45\"): " + OperatorUtils.TypeCoercion.toFloat('123.45'));
- print.log("ת×Ö·û´®(12345): " + OperatorUtils.TypeCoercion.toString(12345));
- print.log("ת²¼¶û(\"true\"): " + OperatorUtils.TypeCoercion.toBoolean('true'));
- print.log("ÊÇÊý×é([1,2,3]): " + OperatorUtils.TypeCoercion.isArray([1, 2, 3]));
- print.log("ÊǶÔÏó({a:1}): " + OperatorUtils.TypeCoercion.isObject({ a: 1 }));
- print.log("");
- print.log("6. ±È½ÏÔËËã²âÊÔ:");
- print.log("----------------");
- print.log("Éî¶ÈÏàµÈ: " + OperatorUtils.Comparison.deepEquals({ a: 1 }, { a: 1 }));
- print.log("·¶Î§¼ì²é(5ÔÚ1-10): " + OperatorUtils.Comparison.between(5, 1, 10, true));
- print.log("ÏÞÖÆ·¶Î§(15ÔÚ1-10): " + OperatorUtils.Comparison.clamp(15, 1, 10));
- print.log("");
- print.log("7. λÔËËã²âÊÔ:");
- print.log("-------------");
- print.log("°´Î»Óë(5&3): " + OperatorUtils.Bitwise.and(5, 3));
- print.log("°´Î»»ò(5|3): " + OperatorUtils.Bitwise.or(5, 3));
- print.log("°´Î»Òì»ò(5^3): " + OperatorUtils.Bitwise.xor(5, 3));
- print.log("°´Î»·Ç(~5): " + OperatorUtils.Bitwise.not(5));
- print.log("×óÒÆ(5<<2): " + OperatorUtils.Bitwise.leftShift(5, 2));
- print.log("ÓÒÒÆ(16>>2): " + OperatorUtils.Bitwise.rightShift(16, 2));
- print.log("");
- print.log("8. Âß¼ÔËËã²âÊÔ:");
- print.log("----------------");
- print.log("Âß¼Óë: " + OperatorUtils.Logical.and(true, false, 'hello'));
- print.log("Âß¼»ò: " + OperatorUtils.Logical.or(false, null, 'world'));
- print.log("Âß¼·Ç(true): " + OperatorUtils.Logical.not(true));
- print.log("ÈýÔªÔËËã(true,\"yes\",\"no\"): " + OperatorUtils.Logical.ternary(true, 'yes', 'no'));
- print.log("¿ÕÖµºÏ²¢(null,\"default\"): " + OperatorUtils.Logical.nullish(null, 'default'));
- print.log("");
- print.log("9. Á´Ê½µ÷ÓòâÊÔ:");
- print.log("----------------");
- var chainResult = OperatorUtils.chain(10)
- .add(5)
- .multiply(2)
- .subtract(5)
- .divide(5)
- .round(2)
- .valueOf();
- print.log("Á´Ê½¼ÆËã: 10+5*2-5/5 = " + chainResult);
- print.log("");
- print.log("10. ¼ÆËãÆ÷Õ»²âÊÔ:");
- print.log("-----------------");
- var calc = new OperatorUtils.Calculator();
- calc.push(10).push(5).add().push(3).multiply();
- print.log("Õ»¼ÆËã (10+5)*3 = " + calc.getResult());
- print.log("");
- print.log("11. ÊýѧÒýÇæ²âÊÔ:");
- print.log("-----------------");
- var engine = new OperatorUtils.MathEngine();
- engine.setVariable('x', 5).setVariable('y', 10);
- var result = engine.calculate('x * y + 20');
- print.log("±äÁ¿¼ÆËã x=5,y=10, x*y+20 = " + result);
- print.log("");
- print.log("=== ²âÊÔÍê³É ===");
- };
- OperatorUtils.quickTest = function() {
- print.log("=== ¿ìËÙ²âÊÔ ===");
- print.log("¼Ó·¨: " + OperatorUtils.Arithmetic.add(1, 2, 3));
- print.log("³Ë·¨: " + OperatorUtils.Arithmetic.multiply(2, 3, 4));
- print.log("Á¬½Ó: " + OperatorUtils.StringOps.concat('A', 'B', 'C'));
- print.log("ÖÇÄܼӷ¨: " + OperatorUtils.TypeCoercion.smartAdd('5', 5));
- print.log("Á´Ê½: " + OperatorUtils.chain(10).add(5).multiply(2).valueOf());
- print.log("=== ¿ìËÙ²âÊÔÍê³É ===");
- };
- print.log("JavaScript ÔËËã·û·â×°¿â¼ÓÔØÍê³É£¡");
- print.log("");
- print.log("ʹÓ÷½·¨£º");
- print.log(" OperatorUtils.runAllExamples() - ÔËÐÐÍêÕû²âÊÔ");
- print.log(" OperatorUtils.quickTest() - ÔËÐпìËÙ²âÊÔ");
- print.log("");
- OperatorUtils.runAllExamples();
¸´ÖÆ´úÂë
|
|