From 7fe9aad7b492098da911aa533ad765e2344913f2 Mon Sep 17 00:00:00 2001 From: RoderickQiu Date: Sun, 23 Feb 2020 13:50:54 +0800 Subject: [PATCH] Features added - Allow to round the decimals for Arabic time parser. - Allow to output the current version. - Allow to use 'min' as the alternative of 'm'. --- package.json | 2 +- src/shi.js | 31 ++++++++++++++++++++++++------- test.js | 5 ++++- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 1dc3fe0..2fe9011 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-shi", - "version": "0.3.0", + "version": "0.4.0", "main": "index.js", "scripts": { "test": "node test.js" diff --git a/src/shi.js b/src/shi.js index e9df7c3..4a29bf6 100644 --- a/src/shi.js +++ b/src/shi.js @@ -106,7 +106,7 @@ function unitPreConverter(str, as) { return str * 86400; case "h": return str * 3600; - case "m": + case "m" || "min": return str * 60; case "s": return str; @@ -124,7 +124,7 @@ function unitConverterAfter(str, to) { return str / 86400; case "h": return str / 3600; - case "m": + case "m" || "min": return str / 60; case "s": return str; @@ -190,7 +190,7 @@ exports.humanTimeParser = humanTimeParser; *'ru', 'uk', 'ur', 'sk', 'sv', *'tr', 'th', 'vi', 'zh_CN', 'zh_TW' * @param {Number} number the time in Arabic number (If `as` is not defined in `option`, we'll parse the number as second in default.) - * @param {Object} option (optional) *{ lang: language, as: (d, h, m, s, ms), to: (d, h, m, s, ms) }* + * @param {Object} option (optional) *{ lang: language, round: boolean (round the decimal or not), as: (d, h, m, s, ms), to: (d, h, m, s, ms) }* */ function ArabicNumberTimeParser(number, option) { try { @@ -203,16 +203,19 @@ function ArabicNumberTimeParser(number, option) { if (option.to) result = humanizeDuration(result, { units: [option.to], language: option.lang, - fallbacks: ['en'] + fallbacks: ['en'], + round: option.round ? option.round : false }); else if (DEFAULT_TO) result = humanizeDuration(result, { units: [DEFAULT_TO], language: option.lang, - fallbacks: ['en'] + fallbacks: ['en'], + round: option.round ? option.round : false }); else result = humanizeDuration(result, { language: option.lang, - fallbacks: ['en'] + fallbacks: ['en'], + round: option.round ? option.round : false }); } else if (DEFAULT_TO) result = humanizeDuration(result, { units: [DEFAULT_TO], @@ -226,4 +229,18 @@ function ArabicNumberTimeParser(number, option) { } } -exports.ArabicNumberTimeParser = ArabicNumberTimeParser; \ No newline at end of file +exports.ArabicNumberTimeParser = ArabicNumberTimeParser; + +/*** + * Func 5: Output the version + */ + +/** + * output the version + * @returns {String} the current version + */ +function version() { + return require("../package.json").version; +} + +exports.version = version; \ No newline at end of file diff --git a/test.js b/test.js index fbb9091..f781dd0 100644 --- a/test.js +++ b/test.js @@ -16,4 +16,7 @@ console.log(shi.unitConverter(1, "d", "ms"));//86400000 // time in Arabic number -> human time console.log(shi.ArabicNumberTimeParser(1500));//25 minutes console.log(shi.ArabicNumberTimeParser(36, { lang: 'fr', to: 'h' }));//0,01 heure -console.log(shi.ArabicNumberTimeParser(1, { lang: 'zh_CN', as: 'h', to: 'ms' }));//3600000 毫秒 \ No newline at end of file +console.log(shi.ArabicNumberTimeParser(1, { lang: 'zh_CN', as: 'ms', to: 's', round: true }));//0 秒 + +// output the version +console.log(shi.version());//0.3.1 \ No newline at end of file