Skip to content

Commit 2b659df

Browse files
committed
Error ignore & npmignore added
1 parent 50e0fcc commit 2b659df

5 files changed

Lines changed: 27 additions & 8 deletions

File tree

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
yarn.lock
2+
.gitattributes
3+
.vscode/*
4+
.yarnclean
5+
_config.yml

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ shi.setDefault({ as: 'm' });//set default
2525
console.log(shi.humanTimeParser("50", { to: 'h' }))//0.8333333333333334
2626
console.log(shi.humanTimeParser("50", { as: 'h', to: 'm' }))//3000
2727
shi.clearDefault();//clear defaults
28+
console.log(shi.humanTimeParser("badstring", { ignoreError: true }))//badstring (because errors ignored)
2829

2930
// unit convert
3031
console.log(shi.unitConverter(60, 'm', 'h'));//1
@@ -34,9 +35,10 @@ console.log(shi.unitConverter(1, "d", "ms"));//86400000
3435
console.log(shi.ArabicNumberTimeParser(1500));//25 minutes
3536
console.log(shi.ArabicNumberTimeParser(36, { lang: 'fr', to: 'h' }));//0,01 heure
3637
console.log(shi.ArabicNumberTimeParser(1, { lang: 'zh_CN', as: 'ms', to: 's', round: true }));//0 秒
38+
console.log(shi.ArabicNumberTimeParser(NaN, { ignoreError: true }))//0 seconds (because errors ignored)
3739

3840
// output the version
39-
console.log(shi.version());//0.3.1
41+
console.log(shi.version());//0.4.1
4042
```
4143

4244
## Features

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-shi",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"main": "index.js",
55
"scripts": {
66
"test": "node test.js"
@@ -43,6 +43,10 @@
4343
"English",
4444
"language",
4545
"timestamp",
46-
"generate"
46+
"generate",
47+
"chinese-numbers-converter",
48+
"english2number",
49+
"humanize-duration",
50+
"js-pinyin"
4751
]
4852
}

src/shi.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function unitConverterAfter(str, to) {
138138
/**
139139
* human time (中文/English) parsed to Arabic number in seconds.
140140
* @param {String|Number} str The string need to be parsed or the time in different unixes.
141-
* @param {Object} option (optional) *{ as (only used when no unix is in str): (d, h, m (min), s, ms), to: (d, h, m (min), s, ms) }*
141+
* @param {Object} option (optional) *{ ignoreError: boolean, as (only used when no unix is in str): (d, h, m (min), s, ms), to: (d, h, m (min), s, ms) }*
142142
* @returns {Number} The converted number
143143
*/
144144
function humanTimeParser(str, option) {
@@ -166,7 +166,10 @@ function humanTimeParser(str, option) {
166166
} else return timestring(str);
167167
}
168168
catch (e) {
169-
throw e;
169+
if (option) {
170+
if (option.ignoreError != true)
171+
throw e;
172+
} else throw e;
170173
}
171174
}
172175
}
@@ -190,7 +193,7 @@ exports.humanTimeParser = humanTimeParser;
190193
*'ru', 'uk', 'ur', 'sk', 'sv',
191194
*'tr', 'th', 'vi', 'zh_CN', 'zh_TW'
192195
* @param {Number} number the time in Arabic number (If `as` is not defined in `option`, we'll parse the number as second in default.)
193-
* @param {Object} option (optional) *{ lang: language, round: boolean (round the decimal or not), as: (d, h, m (min), s, ms), to: (d, h, m (min), s, ms) }*
196+
* @param {Object} option (optional) *{ ignoreError: boolean, lang: language, round: boolean (round the decimal or not), as: (d, h, m (min), s, ms), to: (d, h, m (min), s, ms) }*
194197
*/
195198
function ArabicNumberTimeParser(number, option) {
196199
try {
@@ -225,7 +228,10 @@ function ArabicNumberTimeParser(number, option) {
225228
return result;
226229
}
227230
catch (e) {
228-
throw e;
231+
if (option) {
232+
if (option.ignoreError != true)
233+
throw e;
234+
} else throw e;
229235
}
230236
}
231237

test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ shi.setDefault({ as: 'm' });//set default
88
console.log(shi.humanTimeParser("50", { to: 'h' }))//0.8333333333333334
99
console.log(shi.humanTimeParser("50", { as: 'h', to: 'm' }))//3000
1010
shi.clearDefault();//clear defaults
11+
console.log(shi.humanTimeParser("badstring", { ignoreError: true }))//badstring (because errors ignored)
1112

1213
// unit convert
1314
console.log(shi.unitConverter(60, 'm', 'h'));//1
@@ -17,6 +18,7 @@ console.log(shi.unitConverter(1, "d", "ms"));//86400000
1718
console.log(shi.ArabicNumberTimeParser(1500));//25 minutes
1819
console.log(shi.ArabicNumberTimeParser(36, { lang: 'fr', to: 'h' }));//0,01 heure
1920
console.log(shi.ArabicNumberTimeParser(1, { lang: 'zh_CN', as: 'ms', to: 's', round: true }));//0 秒
21+
console.log(shi.ArabicNumberTimeParser(NaN, { ignoreError: true }))//0 seconds (because errors ignored)
2022

2123
// output the version
22-
console.log(shi.version());//0.3.1
24+
console.log(shi.version());//0.4.1

0 commit comments

Comments
 (0)