diff --git a/README.en.md b/README.en.md index 5e87c70dece7264e7a3ff29da153ccb8b2ac5c75..407e6d753bb1e09fe655bdbbae219b4ba62766af 100644 --- a/README.en.md +++ b/README.en.md @@ -136,7 +136,7 @@ Interface introduction: Take printf and geterrorstring as examples: -1.printf() +1.printf() { var format = "%%%o%%%i%s"; @@ -150,7 +150,7 @@ Take printf and geterrorstring as examples: var result = util.printf(format,value,value1,value2); } -2.geterrorstring() +2.geterrorstring() { var errnum = 13; diff --git a/README.md b/README.md index 13750b365c80c9456e4044c8820a6e4ac47636c3..ed82bb6fad06de6da766d213626d2a5603ed52d6 100644 --- a/README.md +++ b/README.md @@ -89,11 +89,11 @@ helpfunction模块,涉及4个接口。 1.function printf(format: string, ...args: Object[]): string; - printf()方法使用第一个参数作为格式字符串(其可以包含零个或多个格式说明符)来返回格式化的字符串。 + printf()方法使用第一个参数作为格式字符串(其可以包含零个或多个格式说明符)来返回格式化的字符串。 每个说明符都替换为来自相应参数的转换后的值。 支持的说明符有: - %s: String 将用于转换除 BigInt、Object 和 -0 之外的所有值。 + %s: String 将用于转换除 BigInt、Object 和 -0 之外的所有值。 %d: Number 将用于转换除 BigInt 和 Symbol 之外的所有值。 @@ -103,13 +103,13 @@ helpfunction模块,涉及4个接口。 %j: JSON。 如果参数包含循环引用,则替换为字符串 '[Circular]'。 - %o: Object. 具有通用 JavaScript 对象格式的对象的字符串表示形式。 + %o: Object. 具有通用 JavaScript 对象格式的对象的字符串表示形式。 - 类似于具有选项 { showHidden: true, showProxy: true } 的 util.inspect()。 + 类似于具有选项 { showHidden: true, showProxy: true } 的 util.inspect()。 这将显示完整的对象,包括不可枚举的属性和代理。 - %O: Object. 具有通用 JavaScript 对象格式的对象的字符串表示形式。 + %O: Object. 具有通用 JavaScript 对象格式的对象的字符串表示形式。 类似于没有选项的 util.inspect()。 这将显示完整的对象,但不包括不可枚举的属性和代理。 @@ -133,13 +133,13 @@ helpfunction模块,涉及4个接口。 // 返回: 'foo:bar baz' - 如果第一个参数不包含有效的格式说明符,则 printf() 返回以空格分隔的所有参数的串联的字符串: + 如果第一个参数不包含有效的格式说明符,则printf()返回以空格分隔的所有参数的串联的字符串: printf(1, 2, 3); // 返回: '1 2 3' - 如果只有一个参数传给 printf(),则它会按原样返回,不进行任何格式化: + 如果只有一个参数传给printf(),则它会按原样返回,不进行任何格式化: util.format('%% %s'); @@ -167,9 +167,9 @@ helpfunction模块,涉及4个接口。 以printf、geterrorstring为例: -import util from '@ohos.util' +import util from '@ohos.util' -1.printf() +1.printf() { var format = "%%%o%%%i%s"; @@ -183,7 +183,7 @@ import util from '@ohos.util' var result = util.printf(format,value,value1,value2); } -2.geterrorstring() +2.geterrorstring() { var errnum = 13; diff --git a/README_EN.md b/README_EN.md new file mode 100644 index 0000000000000000000000000000000000000000..9d214e6bf63b3e3cb2c39fcc4045aa378163957f --- /dev/null +++ b/README_EN.md @@ -0,0 +1,86 @@ +# js_ util_ module + +####1、 Introduction to helpfunction + +It is mainly used to callback and promise functions, output error code information, and format a printf-like string. + +The helpfunction module involves four interfaces. + +Interface introduction: + +1.function printf(format: string, ...args: Object[]): string; + + The util.format() method returns a formatted string using the first argument as a printf-like format string which can contain zero or more format specifiers. Each specifier is replaced with the converted value from the corresponding argument. Supported specifiers are: + Each specifier is replaced with a converted value from the corresponding parameter. Supported specifiers are: + %s: String will be used to convert all values except BigInt, Object and -0. BigInt values will be represented with an n and Objects that have no user defined toString function are inspected using util.inspect() with options { depth: 0, colors: false, compact: 3 }. + %d: Number will be used to convert all values except BigInt and Symbol. + %i: parseInt(value, 10) is used for all values except BigInt and Symbol. + %f: parseFloat(value) is used for all values expect Symbol. + %j: JSON. Replaced with the string '[Circular]' if the argument contains circular references. + %o: Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() with options { showHidden: true, showProxy: true }. This will show the full object including non-enumerable properties and proxies. + %O: Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() without options. This will show the full object not including non-enumerable properties and proxies. + %c: CSS. This specifier is ignored and will skip any CSS passed in. + %%: single percent sign ('%'). This does not consume an argument. + Returns: The formatted string + If a specifier does not have a corresponding argument, it is not replaced: + util.format('%s:%s', 'foo'); + // Returns: 'foo:%s' + + Values that are not part of the format string are formatted using util.inspect() if their type is not string. + If there are more arguments passed to the util.format() method than the number of specifiers, the extra arguments are concatenated to the returned string, separated by spaces: + util.format('%s:%s', 'foo', 'bar', 'baz'); + // Returns: 'foo:bar baz' + + If the first argument does not contain a valid format specifier, util.format() returns a string that is the concatenation of all arguments separated by spaces: + util.format(1, 2, 3); + // Returns: '1 2 3' + + If only one argument is passed to util.format(), it is returned as it is without any formatting: + util.format('%% %s'); + // Returns: '%% %s' + +2.function getErrorString(errno: number): string; + + The geterrorstring () method uses a system error number as a parameter to return system error information. + +3.function callbackWrapper(original: Function): (err: Object, value: Object) => void; + + Takes an async function (or a function that returns a Promise) and returns a function following the error-first callback style, i.e. taking an (err, value) => ... callback as the last argument. In the callback, the first argument will be the rejection reason (or null if the Promise resolved), and the second argument will be the resolved value. + +4.function promiseWrapper(original: (err: Object, value: Object) => void): Object; + + Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises. + +####2、 Method of use + +Take printf and geterrorstring as examples: + +1.printf() + +{ + + var format = "%%%o%%%i%s"; + + var value = function aa(){}; + + var value1 = 1.5; + + var value2 = "qwer"; + + var result = util.printf(format,value,value1,value2); + + console.log("-----SK-----printf---result---+[" +result +"]"); + +} + +2.geterrorstring() + +{ + + var errnum = 13; + + var result = util.geterrorstring(errnum); + + console.log("-----SK------ = " + result); + +} \ No newline at end of file diff --git a/util/BUILD.gn b/util/BUILD.gn index 19f9f887c0694309cf43ba6bc40bfeafe001a029..c6044cf8c9b2b9412e9915011d0fb078b4356b6a 100644 --- a/util/BUILD.gn +++ b/util/BUILD.gn @@ -9,7 +9,7 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and -# limitations under the License. +# limitations under the License. import("//build/ohos.gni") import("//build/ohos/ace/ace.gni") diff --git a/util/js_textdecoder.cpp b/util/js_textdecoder.cpp index 25e56255d680cde156c4ebe7a21ad0dbef565066..912fc640c95881e5143c7150214060fb2ec82682 100644 --- a/util/js_textdecoder.cpp +++ b/util/js_textdecoder.cpp @@ -15,14 +15,16 @@ #include "js_textdecoder.h" #include -#include -#include -#include #include + #include -#include "utils/log.h" -#include "unicode/unistr.h" +#include +#include +#include + #include "securec.h" +#include "unicode/unistr.h" +#include "utils/log.h" TextDecoder::TextDecoder(napi_env env, std::string buff, std::vector optionVec) : env_(env), label_(0), encStr_(buff), tranTool_(nullptr, nullptr) @@ -109,7 +111,7 @@ napi_value TextDecoder::Decode(napi_value src, bool iflag) arrDat = &arr[2]; // 2: Obtains the 2 value of the array. } std::u16string tempStr16(arrDat); - std::string tepStr = std::wstring_convert< std::codecvt_utf8_utf16, char16_t > {}.to_bytes(tempStr16); + std::string tepStr = std::wstring_convert, char16_t> {}.to_bytes(tempStr16); const char* tempCh = tepStr.c_str(); char* rstCh = const_cast(tempCh); napi_value resultStr = nullptr; diff --git a/util/js_textdecoder.h b/util/js_textdecoder.h index b1786dd2a75083689cdfe6b9bbdd6f4f709ca7b6..2ff578c366f5cb3e28e7e084646b414cce30f555 100644 --- a/util/js_textdecoder.h +++ b/util/js_textdecoder.h @@ -16,9 +16,9 @@ #ifndef FOUNDATION_CCRUNTIME_TEXTCODER_JS_TEXTDECODER_H #define FOUNDATION_CCRUNTIME_TEXTCODER_JS_TEXTDECODER_H +#include #include #include -#include #include "napi/native_api.h" #include "napi/native_node_api.h" #include "unicode/ucnv.h" diff --git a/util/native_module_util.cpp b/util/native_module_util.cpp index 670eeea45e53e1a455c38430b29c0dc73b917f5d..36c412f58c0984d652be38e7264e6fd7beef6a39 100644 --- a/util/native_module_util.cpp +++ b/util/native_module_util.cpp @@ -19,9 +19,9 @@ #include "js_textdecoder.h" #include "js_textencoder.h" -#include "utils/log.h" #include "napi/native_api.h" #include "napi/native_node_api.h" +#include "utils/log.h" extern const char _binary_util_js_js_start[]; extern const char _binary_util_js_js_end[]; @@ -60,11 +60,10 @@ namespace OHOS::Util { } } } - if (j < valueSize) { - while (j < valueSize) { - str += " " + value[j++]; - } - } else if (i < formatSize) { + while (j < valueSize) { + str += " " + value[j++]; + } + if (i < formatSize) { str += format.substr(i); } return str; @@ -74,14 +73,23 @@ namespace OHOS::Util { { size_t argc = 0; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); - napi_value *argv = new napi_value[argc]; + napi_value *argv = nullptr; + if (argc > 0) { + argv = new napi_value[argc]; + } napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); char* format = nullptr; size_t formatsize = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &formatsize)); - format = new char[formatsize + 1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], format, formatsize + 1, &formatsize)); + napi_get_value_string_utf8(env, argv[0], nullptr, 0, &formatsize); + if (formatsize > 0) { + format = new char[formatsize + 1]; + napi_get_value_string_utf8(env, argv[0], format, formatsize + 1, &formatsize); + } std::string str = format; + delete []format; + delete []argv; + argv = nullptr; + format = nullptr; std::string res; size_t strSize = str.size(); for (size_t i = 0; i < strSize; ++i) { @@ -98,21 +106,19 @@ namespace OHOS::Util { res += "d "; } else if (str[i + 1] == 's') { res += "s "; + } else if (str[i + 1] == 'f') { + res += "f "; } else if (str[i + 1] == 'c') { res += "c "; } i++; - } else if(str[i] == '%' && (i + 1 < strSize && str[i + 1] == '%')) { + } else if (str[i] == '%' && (i + 1 < strSize && str[i + 1] == '%')) { i++; } } res = res.substr(0, res.size() - 1); napi_value result = nullptr; - NAPI_CALL(env, napi_create_string_utf8(env, res.c_str(), res.size(), &result)); - delete []format; - delete []argv; - argv = nullptr; - format = nullptr; + napi_create_string_utf8(env, res.c_str(), res.size(), &result); return result; } @@ -128,21 +134,28 @@ namespace OHOS::Util { napi_value result = nullptr; size_t argc = 0; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); - napi_value *argv = new napi_value[argc]; + napi_value *argv = nullptr; + if (argc > 0) { + argv = new napi_value[argc]; + } napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); char* format = nullptr; size_t formatsize = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &formatsize)); - format = new char[formatsize + 1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], format, formatsize + 1, &formatsize)); + napi_get_value_string_utf8(env, argv[0], nullptr, 0, &formatsize); + if (formatsize > 0) { + format = new char[formatsize + 1]; + } + napi_get_value_string_utf8(env, argv[0], format, formatsize + 1, &formatsize); std::string printInfo; std::vector value; - for (size_t i = 1; i < argc; i++) { + for (size_t i = 1; i < argc; i++) { char* valueString = nullptr; size_t valuesize = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[i], nullptr, 0, &valuesize)); - valueString = new char[valuesize + 1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[i], valueString, valuesize + 1, &valuesize)); + napi_get_value_string_utf8(env, argv[i], nullptr, 0, &valuesize); + if (valuesize > 0) { + valueString = new char[valuesize + 1]; + } + napi_get_value_string_utf8(env, argv[i], valueString, valuesize + 1, &valuesize); value.push_back(valueString); delete []valueString; valueString = nullptr; @@ -260,7 +273,7 @@ namespace OHOS::Util { } }, nullptr, nullptr)); - return thisVar; + return thisVar; } static napi_value TextdecoderDecode(napi_env env, napi_callback_info info) @@ -339,7 +352,6 @@ namespace OHOS::Util { // Encoder static napi_value TextEncoderConstructor(napi_env env, napi_callback_info info) { - HILOG_INFO("SK TextEncoderConstructor start"); napi_value thisVar = nullptr; void* data = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data)); @@ -354,7 +366,6 @@ namespace OHOS::Util { } }, nullptr, nullptr)); - HILOG_INFO("SK TextEncoderConstructor end"); return thisVar; } @@ -425,7 +436,6 @@ namespace OHOS::Util { static napi_value TextcoderInit(napi_env env, napi_value exports) { - HILOG_INFO("SK TextcoderInit start "); const char* textEncoderClassName = "TextEncoder"; napi_value textEncoderClass = nullptr; static napi_property_descriptor textEncoderDesc[] = { @@ -457,13 +467,11 @@ namespace OHOS::Util { }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); - HILOG_INFO("SK TextcoderInit end "); return exports; } static napi_value UtilInit(napi_env env, napi_value exports) { - HILOG_INFO("SK UtilInit start "); static napi_property_descriptor desc[] = { DECLARE_NAPI_FUNCTION("printf", Printf), DECLARE_NAPI_FUNCTION("geterrorstring", GetErrorString), @@ -471,7 +479,6 @@ namespace OHOS::Util { }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); TextcoderInit(env, exports); - HILOG_INFO("SK UtilInit end "); return exports; } diff --git a/util/util_js.js b/util/util_js.js index a0ce53eab9e8a9ae2f01e80d7233d69b9ab35563..4589a78731e22253a1cbfc4c3ef39a41ba813182 100644 --- a/util/util_js.js +++ b/util/util_js.js @@ -17,152 +17,318 @@ const helpUtil = requireInternal('util'); let TextEncoder = helpUtil.TextEncoder; let TextDecoder = helpUtil.TextDecoder; -function switchLittleObject(str, obj, count) +function switchLittleObject(enter, obj, count) { - if (obj instanceof Array) { - str += ' [ ' + arrayToString(obj, count) + '[length]: ' + obj.length + ' ],\n '; - str = str.substr(0, str.length - 4); - } - else if (typeof obj === 'object') { - count++; + var str = ''; + if (obj === null) { + str += obj; + } else if (obj instanceof Array) { + str += '[ ' + arrayToString(enter, obj, count) + '[length]: ' + obj.length + ' ]'; + } else if (typeof obj === 'function') { + str += '{ [Function: ' + obj.name + ']' + enter + + '[length]: ' + obj.length + ',' + enter + + '[name] :\'' + obj.name + '\',' + enter + + '[prototype]: ' + obj.name + ' { [constructor]: [Circular] } }'; + } else if (typeof obj === 'object') { str += '{ '; var i = 0; for (i in obj) { - str += switchLittleValue(i, obj[i], count); + str += switchLittleValue(enter, i, obj, count); } - str = str.substr(0, str.length - 4); - str = str + ' }'; - } else if (typeof obj === 'function') { - str += '{ '; - str += '[Function: ' + obj.name + ']\n ' + '[length]: ' - + obj.length + ',\n' + ' [name] :\'' + obj.name + '\',\n' + ' [prototype]: ' + obj.name - + ' { [constructor]: [Circular] },\n '; - str = str.substr(0, str.length - 4); - str = str + ' }'; + if (i === 0) { + return obj; + } + str = str.substr(0, str.length - enter.length - 1); + str += ' }'; } else if (typeof obj === 'string') { - str += '\'' + obj + '\'' + '\n '; - } else if (typeof obj === 'number') { - str += obj.toString(); + str += '\'' + obj + '\''; + } else { + str += obj; } return str; } -function switchLittleValue(protoName, obj, count) { +function switchLittleValue(enter, protoName, obj, count) +{ var str = ''; - if (obj === null) { - str += protoName + ': null,\n '; - } else if (obj instanceof Array) { - str += protoName + ': [ ' + arrayToString(obj, count) + '[length]: ' + obj.length + ' ],\n '; - } else if (typeof obj === 'object') { - str += protoName + ': \n '; - var temp = ''; - str += switchLittleObject(temp, obj, count) + ',\n '; - } else if (typeof obj === 'function') { - str += '{ [Function: ' + obj.name + ']\n ' + '[length]: ' + obj.length + ',\n' - + ' [name] :\'' + obj.name + '\''; - if (count === 1) { - str += ',\n [prototype]: ' + obj.name + '{ [constructor]: [Circular] } },\n '; + if (obj[protoName] === null) { + str += protoName + ': null,' + enter; + } else if (obj[protoName] instanceof Array) { + str += protoName + ':' + enter + + '[ ' + arrayToString(enter + ' ', obj[protoName], count) + '[length]: ' + + obj[protoName].length + ' ],' + enter; + } else if (typeof obj[protoName] === 'object') { + if (obj[protoName] === obj) { + str += protoName + ': [Circular]' + enter; } else { - str += ',\n [prototype]: ' + '[' + obj.name + '] },\n '; + str += protoName + ':' + enter; + str += switchLittleObject(enter + ' ', obj[protoName], count + 1) + ',' + enter; } + } else if (typeof obj[protoName] === 'function') { + var space = enter; + if (obj[protoName].name !== '') { + str += obj[protoName].name + ':' + space; + } + space += ' '; + str += '{ [Function: ' + obj[protoName].name + ']' + space + + '[length]: ' + obj[protoName].length + ',' + space + + '[name] :\'' + obj[protoName].name + '\',' + space + + '[prototype]: ' + obj[protoName].name + + ' { [constructor]: [Circular] } },' + enter; } else { - if (typeof obj === 'string') { - str += protoName + ': \'' + obj + '\',\n '; - } else if (typeof obj === 'number') { - str += protoName + ': ' + obj + ',\n '; + if (typeof obj[protoName] === 'string') { + str += protoName + ': \'' + obj[protoName] + '\',' + enter; + } else { + str += protoName + ': ' + obj[protoName] + ',' + enter; } } return str; } -function arrayToString(arr, count) +function arrayToString(enter, arr, count) { var str = ''; if (!arr.length) { return ''; } var i = 0; + var arrayEnter = ', '; + for (i in arr) { + if (arr[i] !== null && (typeof arr[i] === 'function' || typeof arr[i] === 'object') && count <= 2) { + arrayEnter += enter; + break; + } + } + i = 0; for (i in arr) { if (typeof arr[i] === 'string') { - str += '\'' + arr[i].toString() + '\', '; + str += '\'' + arr[i].toString() + '\'' + arrayEnter; } else if (typeof arr[i] === 'object') { - var temp = ''; - str += '\n ' + switchLittleObject(temp, arr[i], count); - str += ',\n '; + str += switchLittleObject(enter + ' ', arr[i], count + 1); + str += arrayEnter; } else if (typeof arr[i] === 'function') { + var space = enter; + space += ' '; var end = ''; if (arr[i].name !== '') { - str += '{ [Function: ' + arr[i].name + ']\n '; - end = ' [' + arr[i].name + '] },\n '; + str += '{ [Function: ' + arr[i].name + ']' + space; + end = arr[i].name + ' { [constructor]: [Circular] } }' + arrayEnter; } else { - str += '{ [Function]\n '; - end = '[Object] },\n '; + str += '{ [Function]' + space; + end = '{ [constructor]: [Circular] } }' + arrayEnter; } str += '[length]: ' - + arr[i].length + ',\n' + ' [name] :\'' + arr[i].name - + '\',\n' + ',\n [prototype]: ' + end; + + arr[i].length + ',' + space + + '[name] :\'' + arr[i].name + '\',' + space + + '[prototype]: ' + end; } else { - str += arr[i].toString() + ', '; + str += arr[i] + arrayEnter; } } return str; } -function switchBigObject(obj) +function switchBigObject(enter, obj, count) { var str = ''; - if (obj instanceof Array) { - str += '[ '; - var i = 0; - for (i in obj) { - if (typeof obj[i] === 'string') { - str += '\'' + obj[i] + '\', '; - } else if (typeof obj[i] === 'number') { - str += obj[i] + ', '; - } - } - str = str.substr(0, str.length - 2); - str += ' ] ,'; + if (obj === null) { + str += obj; + } else if (obj instanceof Array) { + str += '[ ' + arrayToBigString(enter, obj, count) + ' ]'; } else if (typeof obj === 'function') { - str += '[Function: ' + obj.name + '],\n'; - } else if (typeof obj === 'string') { - str += '\'' + obj + '\' ,'; - } else if (typeof obj === 'number') { - str += obj.toString() + ' ,'; + str += '{ [Function: ' + obj.name + '] }'; } else if (typeof obj === 'object') { str += '{ '; var i = 0; for (i in obj) { - if (typeof obj[i] === 'function') { - str += i + ': [Function: ' + obj[i].name + '] ,'; - } else if (typeof obj[i] === 'string') { - str += i + ': \'' + obj[i] + '\' ,'; - } else if (typeof obj[i] === 'number') { - str += i + ': ' + obj[i].toString() + ' ,'; - } else if (obj[i] instanceof Array) { - str += '[ '; - var i = 0; - var j = 0; - for (i in obj[i]) { - if (typeof obj[i][j] === 'string') { - str += '\'' + obj[i][j] + '\', '; - } else if (typeof obj[i][j] === 'number') { - str += obj[i][j] + ', '; - } - } - str = str.substr(0, str.length - 2); - str += ' ] ,'; - } else if (typeof obj[i] === 'boolean') { - str += obj[i].toString() + ','; + str += switchBigValue(enter, i, obj, count); + } + if (i === 0) { + return obj; + } + str = str.substr(0, str.length - enter.length - 1); + str += ' }'; + } else if (typeof obj === 'string') { + str += '\'' + obj + '\''; + } else { + str += obj; + } + return str; +} + +function switchBigValue(enter, protoName, obj, count) +{ + var str = ''; + if (obj[protoName] === null) { + str += protoName + ': null,' + enter; + } else if (obj[protoName] instanceof Array) { + str += protoName + ':' + enter + + '[ ' + arrayToBigString(enter + ' ', obj[protoName], count) + ' ],' + enter; + } else if (typeof obj[protoName] === 'object') { + if (obj[protoName] === obj) { + str += protoName + ': [Circular]' + enter; + } else { + str += protoName + ':' + enter; + str += switchBigObject(enter + ' ', obj[protoName], count + 1) + ',' + enter; + } + } else if (typeof obj[protoName] === 'function') { + if (obj[protoName].name !== '') { + str += obj[protoName].name + ': '; + } + str += '[Function: ' + obj[protoName].name + '],' + enter; + } else { + if (typeof obj[protoName] === 'string') { + str += protoName + ': \'' + obj[protoName] + '\',' + enter; + } else { + str += protoName + ': ' + obj[protoName] + ',' + enter; + } + } + return str; +} + +function arrayToBigString(enter, arr, count) +{ + var str = ''; + if (!arr.length) { + return ''; + } + var i = 0; + var arrayEnter = ', '; + for (i in arr) { + if (arr[i] !== null && (typeof arr[i] === 'object') && count <= 2) { + arrayEnter += enter; + break; + } + } + i = 0; + for (i in arr) { + if (typeof arr[i] === 'string') { + str += '\'' + arr[i] + '\'' + arrayEnter; + } else if (typeof arr[i] === 'object') { + str += switchBigObject(enter + ' ', arr[i], count + 1); + str += arrayEnter; + } else if (typeof arr[i] === 'function') { + var end = ''; + if (arr[i].name !== '') { + str += '[Function: ' + arr[i].name + ']' + arrayEnter; + } else { + str += '[Function]' + arrayEnter; + } + } else { + str += arr[i] + arrayEnter; + } + } + str = str.substr(0, str.length - arrayEnter.length); + return str; +} + +function switchIntValue(value) +{ + var str = ''; + if (value === '') { + str += 'NaN'; + } else if (typeof value === 'bigint') { + str += value + 'n'; + } else if (typeof value === 'symbol') { + str += 'NaN'; + } else if (typeof value === 'number') { + str += parseInt(value, 10); // 10:The function uses decimal. + } else if (value instanceof Array) { + if (typeof value[0] === 'number') { + str += parseInt(value[0], 10); // 10:The function uses decimal. + } else if (typeof value[0] === 'string') { + if (isNaN(value[0])) { + str += 'NaN'; + } else { + str += parseInt(value[0], 10); // 10:The function uses decimal. + } + } + } else if (typeof value === 'string') { + if (isNaN(value)) { + str += 'NaN'; + } else { + str += parseInt(value, 10); // 10:The function uses decimal. + } + } else { + str += 'NaN'; + } + return str; +} + +function switchFloatValue(value) +{ + var str = ''; + if (value === '') { + str += 'NaN'; + } else if (typeof value === 'symbol') { + str += 'NaN'; + } else if (typeof value === 'number') { + str += value; + } else if (value instanceof Array) { + if (typeof value[0] === 'number') { + str += parseFloat(value); + } else if (typeof value[0] === 'string') { + if (isNaN(value[0])) { + str += 'NaN'; + } else { + str += parseFloat(value[0]); } } - str = str.substr(0, str.length - 1); - str += '},'; - } else if (typeof obj === 'boolean') { - str += obj.toString() + ','; + } else if (typeof value === 'string') { + if (isNaN(value)) { + str += 'NaN'; + } else { + str += parseFloat(value); + } + } else if (typeof value === 'bigint') { + str += value; + } else { + str += 'NaN'; + } + return str; +} + +function switchNumberValue(value) +{ + var str = ''; + if (value === '') { + str += '0'; + } else if (typeof value === 'symbol') { + str += 'NaN'; + } else if (typeof value === 'number') { + str += value; + } else if (value instanceof Array) { + str += 'NaN'; + } else if (typeof value === 'string') { + if (isNaN(value)) { + str += 'NaN'; + } else { + str += Number(value); + } + } else if (typeof value === 'bigint') { + str += value.toString() + 'n'; + } else { + str += 'NaN'; + } + return str; +} + +function switchStringValue(value) +{ + var str = ''; + if (typeof value === 'undefined') { + str += 'undefined'; + } else if (typeof value === 'object') { + if (value === null) { + str += 'null'; + } else { + str += value; + } + } else if (typeof value === 'symbol') { + str += value.toString(); + } else { + str += value; } - str = str.substr(0, str.length - 1); - str = str + '\n'; return str; } @@ -176,63 +342,20 @@ function printf(formatString, ...valueString) var arrLength = arr.length; var i = 0; for (; i < valueLength && i < arrLength; i++) { - let inputType = typeof valueString[i]; if (arr[i] === 'o') { - var str = ''; - var count = 0; - switchString.push(switchLittleObject(str, valueString[i], count)); + switchString.push(switchLittleObject('\n ', valueString[i], 1)); } else if (arr[i] === 'O') { - switchString.push(switchBigObject(valueString[i])); + switchString.push(switchBigObject('\n ', valueString[i], 1)); } else if (arr[i] === 'i') { - if (inputType === 'number') { - switchString.push(parseInt(valueString[i], 10).toString()); // 10:The function uses decimal. - } else if (valueString[i] instanceof Array) { - if (typeof valueString[i][0] === 'number') { - switchString.push(parseInt(valueString[i][0], 10).toString()); // 10:The function uses decimal. - } else if (typeof valueString[i][0] === 'string') { - if (isNaN(valueString[i][0])) { - switchString.push('NaN'); - } else { - switchString.push(parseInt(valueString[i][0], 10).toString()); // 10:The function uses decimal. - } - } - } else if (typeof valueString[i] === 'string') { - if (isNaN(valueString[i])) { - switchString.push('NaN'); - } else { - switchString.push(parseInt(valueString[i], 10).toString()); // 10:The function uses decimal. - } - } else { - switchString.push('NaN'); - } + switchString.push(switchIntValue(valueString[i])); } else if (arr[i] === 'j') { switchString.push(JSON.stringify(valueString[i])); } else if (arr[i] === 'd') { - if (inputType === 'number') { - switchString.push(valueString[i].toString()); - } else if (inputType === 'boolean') { - if (valueString[i] === false) { - switchString.push('0'); - } else { - switchString.push('1'); - } - } else { - switchString.push('NaN'); - } + switchString.push(switchNumberValue(valueString[i])); } else if (arr[i] === 's') { - if (inputType === 'string') { - switchString.push(valueString[i]); - } else if (valueString[i] instanceof Array) { - var k = 0; - var strLength = valueString[i].length; - for (; k < strLength; k++) { - switchString.push(valueString[i][k].toString()); - } - } else if (inputType === 'object') { - switchString.push('[object Object]'); - } else { - switchString.push(valueString[i].toString()); - } + switchString.push(switchStringValue(valueString[i])); + } else if (arr[i] === 'f') { + switchString.push(switchFloatValue(valueString[i])); } else if (arr[i] === 'c') { switchString.push(valueString[i].toString()); }