Skip to content

Commit

Permalink
Features added
Browse files Browse the repository at this point in the history
- Allow to round the decimals for Arabic time parser.
- Allow to output the current version.
- Allow to use 'min' as the alternative of 'm'.
  • Loading branch information
RoderickQiu committed Feb 23, 2020
1 parent db43aa7 commit 7fe9aad
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-shi",
"version": "0.3.0",
"version": "0.4.0",
"main": "index.js",
"scripts": {
"test": "node test.js"
Expand Down
31 changes: 24 additions & 7 deletions src/shi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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],
Expand All @@ -226,4 +229,18 @@ function ArabicNumberTimeParser(number, option) {
}
}

exports.ArabicNumberTimeParser = ArabicNumberTimeParser;
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;
5 changes: 4 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 毫秒
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

0 comments on commit 7fe9aad

Please sign in to comment.